cmdbabstract.class.inc.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. <?php
  2. require_once('../core/cmdbobject.class.inc.php');
  3. require_once('../application/utils.inc.php');
  4. require_once('../application/applicationcontext.class.inc.php');
  5. require_once('../application/ui.linkswidget.class.inc.php');
  6. ////////////////////////////////////////////////////////////////////////////////////
  7. /**
  8. * Abstract class that implements some common and useful methods for displaying
  9. * the objects
  10. */
  11. ////////////////////////////////////////////////////////////////////////////////////
  12. abstract class cmdbAbstractObject extends CMDBObject
  13. {
  14. public static function GetUIPage()
  15. {
  16. return './UI.php';
  17. }
  18. public static function ComputeUIPage($sClass)
  19. {
  20. static $aUIPagesCache = array(); // Cache to store the php page used to display each class of object
  21. if (!isset($aUIPagesCache[$sClass]))
  22. {
  23. $UIPage = false;
  24. if (is_callable("$sClass::GetUIPage"))
  25. {
  26. $UIPage = eval("return $sClass::GetUIPage();"); // May return false in case of error
  27. }
  28. $aUIPagesCache[$sClass] = $UIPage === false ? './UI.php' : $UIPage;
  29. }
  30. $sPage = $aUIPagesCache[$sClass];
  31. return $sPage;
  32. }
  33. protected static function MakeHyperLink($sObjClass, $sObjKey, $aAvailableFields)
  34. {
  35. if ($sObjKey <= 0) return '<em>undefined</em>'; // Objects built in memory have negative IDs
  36. $oAppContext = new ApplicationContext();
  37. $sExtClassNameAtt = MetaModel::GetNameAttributeCode($sObjClass);
  38. $sPage = self::ComputeUIPage($sObjClass);
  39. // Use the "name" of the target class as the label of the hyperlink
  40. // unless it's not available in the external attributes...
  41. if (isset($aAvailableFields[$sExtClassNameAtt]))
  42. {
  43. $sLabel = $aAvailableFields[$sExtClassNameAtt];
  44. }
  45. else
  46. {
  47. $sLabel = implode(' / ', $aAvailableFields);
  48. }
  49. // Safety belt
  50. //
  51. if (empty($sLabel))
  52. {
  53. // Developer's note:
  54. // This is doing the job for you, but that is just there in case
  55. // the external fields associated to the external key are blanks
  56. // The ultimate solution will be to query the name automatically
  57. // and independantly from the data model (automatic external field)
  58. // AND make the name be a mandatory field
  59. //
  60. $sObject = MetaModel::GetObject($sObjClass, $sObjKey);
  61. $sLabel = $sObject->GetDisplayName();
  62. }
  63. // Safety net
  64. //
  65. if (empty($sLabel))
  66. {
  67. $sLabel = MetaModel::GetName($sObjClass)." #$sObjKey";
  68. }
  69. $sHint = MetaModel::GetName($sObjClass)."::$sObjKey";
  70. return "<a href=\"$sPage?operation=details&class=$sObjClass&id=$sObjKey&".$oAppContext->GetForLink()."\" title=\"$sHint\">$sLabel</a>";
  71. }
  72. public function GetDisplayValue($sAttCode)
  73. {
  74. $sDisplayValue = "";
  75. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  76. if ($sStateAttCode == $sAttCode)
  77. {
  78. $aStates = MetaModel::EnumStates(get_class($this));
  79. $sDisplayValue = $aStates[$this->Get($sAttCode)]['label'];
  80. }
  81. else
  82. {
  83. $oAtt = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  84. if ($oAtt->IsExternalKey())
  85. {
  86. $sTargetClass = $oAtt->GetTargetClass();
  87. if ($this->IsNew())
  88. {
  89. // The current object exists only in memory, don't try to query it in the DB !
  90. // instead let's query for the object pointed by the external key, and get its name
  91. $targetObjId = $this->Get($sAttCode);
  92. $oTargetObj = MetaModel::GetObject($sTargetClass, $targetObjId, false); // false => not sure it exists
  93. if (is_object($oTargetObj))
  94. {
  95. $sDisplayValue = $oTargetObj->GetName();
  96. }
  97. }
  98. else
  99. {
  100. // retrieve the "external fields" linked to this external key
  101. foreach (MetaModel::GetExternalFields(get_class($this), $sAttCode) as $oExtField)
  102. {
  103. $aAvailableFields[$oExtField->GetExtAttCode()] = $oExtField->GetAsHTML($this->Get($oExtField->GetCode()));
  104. }
  105. // Use the "name" of the target class as the label of the hyperlink
  106. // unless it's not available in the external fields...
  107. if (isset($aAvailableFields[$sExtClassNameAtt]))
  108. {
  109. $sDisplayValue = $aAvailableFields[$sExtClassNameAtt];
  110. }
  111. else
  112. {
  113. $sDisplayValue = implode(' / ', $aAvailableFields);
  114. }
  115. }
  116. }
  117. else
  118. {
  119. $sDisplayValue = $this->GetAsHTML($sAttCode);
  120. }
  121. }
  122. return $sDisplayValue;
  123. }
  124. function DisplayBareHeader(WebPage $oPage)
  125. {
  126. // Standard Header with name, actions menu and history block
  127. //
  128. $oPage->add("<div class=\"page_header\">\n");
  129. // action menu
  130. $oSingletonFilter = new DBObjectSearch(get_class($this));
  131. $oSingletonFilter->AddCondition('pkey', array($this->GetKey()));
  132. $oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
  133. $oBlock->Display($oPage, -1);
  134. $oPage->add("<h1>".MetaModel::GetName(get_class($this)).": <span class=\"hilite\">".$this->GetDisplayName()."</span></h1>\n");
  135. // history block (with toggle)
  136. $oHistoryFilter = new DBObjectSearch('CMDBChangeOp');
  137. $oHistoryFilter->AddCondition('objkey', $this->GetKey());
  138. $oHistoryFilter->AddCondition('objclass', get_class($this));
  139. $oBlock = new HistoryBlock($oHistoryFilter, 'toggle', false);
  140. $oBlock->Display($oPage, -1);
  141. $oPage->add("</div>\n");
  142. }
  143. function DisplayBareDetails(WebPage $oPage)
  144. {
  145. $oPage->add($this->GetBareDetails($oPage));
  146. }
  147. function DisplayBareRelations(WebPage $oPage)
  148. {
  149. // Related objects
  150. $oPage->AddTabContainer('Related Objects');
  151. $oPage->SetCurrentTabContainer('Related Objects');
  152. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  153. {
  154. if ((get_class($oAttDef) == 'AttributeLinkedSetIndirect') || (get_class($oAttDef) == 'AttributeLinkedSet'))
  155. {
  156. $oPage->SetCurrentTab($oAttDef->GetLabel());
  157. $oPage->p($oAttDef->GetDescription());
  158. if (get_class($oAttDef) == 'AttributeLinkedSet')
  159. {
  160. $sTargetClass = $oAttDef->GetLinkedClass();
  161. $oFilter = new DBObjectSearch($sTargetClass);
  162. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey()); // @@@ condition has same name as field ??
  163. $oBlock = new DisplayBlock($oFilter, 'list', false);
  164. $oBlock->Display($oPage, 0);
  165. }
  166. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  167. {
  168. $sLinkClass = $oAttDef->GetLinkedClass();
  169. // Transform the DBObjectSet into a CMBDObjectSet !!!
  170. $aLinkedObjects = $this->Get($sAttCode)->ToArray(false);
  171. if (count($aLinkedObjects) > 0)
  172. {
  173. $oSet = CMDBObjectSet::FromArray($sLinkClass, $aLinkedObjects);
  174. $aParams = array(
  175. 'link_attr' => $oAttDef->GetExtKeyToMe(),
  176. 'object_id' => $this->GetKey(),
  177. 'target_attr' => $oAttDef->GetExtKeyToRemote(),
  178. );
  179. self::DisplaySet($oPage, $oSet, $aParams);
  180. }
  181. }
  182. }
  183. }
  184. $oPage->SetCurrentTab('');
  185. }
  186. function GetDisplayName()
  187. {
  188. $sDisplayName = '';
  189. if (MetaModel::GetNameAttributeCode(get_class($this)) != '')
  190. {
  191. $sDisplayName = $this->GetAsHTML(MetaModel::GetNameAttributeCode(get_class($this)));
  192. }
  193. return $sDisplayName;
  194. }
  195. function GetBareDetails(WebPage $oPage)
  196. {
  197. $sHtml = '';
  198. $oAppContext = new ApplicationContext();
  199. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  200. $aDetails = array();
  201. $sClass = get_class($this);
  202. $aList = MetaModel::GetZListItems($sClass, 'details');
  203. foreach($aList as $sAttCode)
  204. {
  205. $iFlags = $this->GetAttributeFlags($sAttCode);
  206. if ( ($iFlags & OPT_ATT_HIDDEN) == 0)
  207. {
  208. // The field is visible in the current state of the object
  209. if ($sStateAttCode == $sAttCode)
  210. {
  211. // Special display for the 'state' attribute itself
  212. $sDisplayValue = $this->GetState();
  213. }
  214. else
  215. {
  216. $sDisplayValue = $this->GetAsHTML($sAttCode);
  217. }
  218. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' => $sDisplayValue);
  219. }
  220. }
  221. $sHtml .= $oPage->GetDetails($aDetails);
  222. // Documents displayed inline (when possible: images, html...)
  223. foreach($aList as $sAttCode)
  224. {
  225. $oAttDef = Metamodel::GetAttributeDef($sClass, $sAttCode);
  226. if ( $oAttDef->GetEditClass() == 'Document')
  227. {
  228. $oDoc = $this->Get($sAttCode);
  229. if (is_object($oDoc) && !$oDoc->IsEmpty())
  230. {
  231. $sHtml .= "<p>Open in New Window: ".$oDoc->GetDisplayLink($sClass, $this->GetKey(), $sAttCode).", \n";
  232. $sHtml .= "Download: ".$oDoc->GetDownloadLink($sClass, $this->GetKey(), $sAttCode)."</p>\n";
  233. $sHtml .= "<div>".$oDoc->GetDisplayInline($sClass, $this->GetKey(), $sAttCode)."</div>\n";
  234. }
  235. }
  236. }
  237. return $sHtml;
  238. }
  239. function DisplayDetails(WebPage $oPage)
  240. {
  241. $sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
  242. if (!empty($sTemplate))
  243. {
  244. $oTemplate = new DisplayTemplate($sTemplate);
  245. $oTemplate->Render($oPage, array('class_name'=> MetaModel::GetName(get_class($this)),'class'=> get_class($this),'pkey'=> $this->GetKey(), 'name' => $this->GetName()));
  246. }
  247. else
  248. {
  249. // Object's details
  250. // template not found display the object using the *old style*
  251. $this->DisplayBareHeader($oPage);
  252. $this->DisplayBareDetails($oPage);
  253. $this->DisplayBareRelations($oPage);
  254. }
  255. }
  256. function DisplayPreview(WebPage $oPage)
  257. {
  258. $aDetails = array();
  259. $sClass = get_class($this);
  260. $aList = MetaModel::GetZListItems($sClass, 'preview');
  261. foreach($aList as $sAttCode)
  262. {
  263. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  264. }
  265. $oPage->details($aDetails);
  266. }
  267. // Comment by Rom: this helper may be used to display objects of class DBObject
  268. // -> I am using this to display the changes history
  269. public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  270. {
  271. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  272. }
  273. //public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
  274. public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  275. {
  276. static $iListId = 0;
  277. $iListId++;
  278. // Initialize and check the parameters
  279. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  280. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  281. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  282. if (!empty($sLinkageAttribute))
  283. {
  284. if($iLinkedObjectId == 0)
  285. {
  286. // if 'links' mode is requested the id of the object to link to must be specified
  287. throw new ApplicationException("Parameter object_id is mandatory when link_attr is specified. Check the definition of the display template.");
  288. }
  289. if($sTargetAttr == '')
  290. {
  291. // if 'links' mode is requested the d of the object to link to must be specified
  292. throw new ApplicationException("Parameter target_attr is mandatory when link_attr is specified. Check the definition of the display template.");
  293. }
  294. }
  295. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  296. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  297. $sHtml = '';
  298. $oAppContext = new ApplicationContext();
  299. $sClassName = $oSet->GetFilter()->GetClass();
  300. $aAttribs = array();
  301. $aList = MetaModel::GetZListItems($sClassName, 'list');
  302. if (!empty($sLinkageAttribute))
  303. {
  304. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  305. // and other objects...
  306. // The display will then group all the attributes related to the link itself:
  307. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  308. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  309. assert(isset($aAttDefs[$sLinkageAttribute]));
  310. $oAttDef = $aAttDefs[$sLinkageAttribute];
  311. assert($oAttDef->IsExternalKey());
  312. // First display all the attributes specific to the link record
  313. foreach($aList as $sLinkAttCode)
  314. {
  315. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  316. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  317. {
  318. $aDisplayList[] = $sLinkAttCode;
  319. }
  320. }
  321. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  322. foreach($aList as $sLinkAttCode)
  323. {
  324. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  325. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  326. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  327. {
  328. $aDisplayList[] = $sLinkAttCode;
  329. }
  330. }
  331. // First display all the attributes specific to the link
  332. // Then display all the attributes linked to the other end of the relationship
  333. $aList = $aDisplayList;
  334. }
  335. foreach($aList as $sAttCode)
  336. {
  337. if ($bSelectMode)
  338. {
  339. $aAttribs['form::select'] = array('label' => "<input type=\"checkbox\" onChange=\"var value = this.checked; $('.selectList{$iListId}').each( function() { this.checked = value; } );\"></input>", 'description' => 'Select / Deselect All');
  340. }
  341. $aAttribs['key'] = array('label' => '', 'description' => 'Click to display');
  342. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  343. }
  344. $aValues = array();
  345. $oSet->Seek(0);
  346. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  347. $iMaxObjects = -1;
  348. if ($bDisplayLimit)
  349. {
  350. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  351. {
  352. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  353. }
  354. }
  355. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  356. {
  357. $aRow['key'] = $oObj->GetKey();
  358. if ($bSelectMode)
  359. {
  360. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  361. }
  362. $aRow['key'] = $oObj->GetKey();
  363. foreach($aList as $sAttCode)
  364. {
  365. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  366. }
  367. $aValues[] = $aRow;
  368. $iMaxObjects--;
  369. }
  370. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  371. $sHtml .= '<table class="listContainer">';
  372. $sColspan = '';
  373. if ($bDisplayMenu)
  374. {
  375. $sColspan = 'colspan="2"';
  376. $aMenuExtraParams = $aExtraParams;
  377. if (!empty($sLinkageAttribute))
  378. {
  379. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  380. $aMenuExtraParams = $aExtraParams;
  381. }
  382. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  383. {
  384. // list truncated
  385. $divId = $aExtraParams['block_id'];
  386. $sFilter = $oSet->GetFilter()->serialize();
  387. $aExtraParams['display_limit'] = false; // To expand the full list
  388. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  389. $sHtml .= '<tr class="containerHeader"><td>'.utils::GetConfig()->GetMinDisplayLimit().' object(s) displayed out of '.$oSet->Count().'&nbsp;&nbsp;<a href="Javascript:ReloadTruncatedList(\''.$divId.'\', \''.$sFilter.'\', \''.$sExtraParams.'\');">Display All</a></td><td>';
  390. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  391. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  392. }
  393. else
  394. {
  395. // Full list
  396. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.$oSet->Count().' object(s)</td><td>';
  397. }
  398. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  399. $sHtml .= '</td></tr>';
  400. }
  401. $sHtml .= "<tr><td $sColspan>";
  402. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$sClassName, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  403. $sHtml .= '</td></tr>';
  404. $sHtml .= '</table>';
  405. return $sHtml;
  406. }
  407. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  408. {
  409. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  410. }
  411. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  412. {
  413. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  414. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  415. $oAppContext = new ApplicationContext();
  416. $sClassName = $oSet->GetFilter()->GetClass();
  417. $aAttribs = array();
  418. $aList = MetaModel::GetZListItems($sClassName, 'details');
  419. $aHeader = array();
  420. $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  421. foreach($aList as $sAttCode)
  422. {
  423. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  424. }
  425. $sHtml = '#'.$oSet->GetFilter()->ToOQL()."\n";
  426. $sHtml .= implode($sSeparator, $aHeader)."\n";
  427. $oSet->Seek(0);
  428. while ($oObj = $oSet->Fetch())
  429. {
  430. $aRow = array();
  431. $aRow[] = $oObj->GetKey();
  432. foreach($aList as $sAttCode)
  433. {
  434. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, '\\');
  435. }
  436. $sHtml .= implode($sSeparator, $aRow)."\n";
  437. }
  438. return $sHtml;
  439. }
  440. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  441. {
  442. $oAppContext = new ApplicationContext();
  443. $sClassName = $oSet->GetFilter()->GetClass();
  444. $aAttribs = array();
  445. $aList = MetaModel::GetZListItems($sClassName, 'details');
  446. $oPage->add("<Set>\n");
  447. $oSet->Seek(0);
  448. while ($oObj = $oSet->Fetch())
  449. {
  450. $oPage->add("<$sClassName id=\"".$oObj->GetKey()."\">\n");
  451. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  452. {
  453. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()) && ($sAttCode != 'finalclass') )
  454. {
  455. $sValue = $oObj->GetAsXML($sAttCode);
  456. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  457. }
  458. }
  459. $oPage->add("</$sClassName>\n");
  460. }
  461. $oPage->add("</Set>\n");
  462. }
  463. // By rom
  464. function DisplayChangesLog(WebPage $oPage)
  465. {
  466. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  467. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  468. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  469. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  470. $count = $oSet->Count();
  471. if ($count > 0)
  472. {
  473. $oPage->p("Changes log ($count):");
  474. self::DisplaySet($oPage, $oSet);
  475. }
  476. else
  477. {
  478. $oPage->p("Changes log is empty");
  479. }
  480. }
  481. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  482. {
  483. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  484. }
  485. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  486. {
  487. static $iSearchFormId = 0;
  488. $oAppContext = new ApplicationContext();
  489. $sHtml = '';
  490. $numCols=4;
  491. $iSearchFormId++;
  492. $sClassName = $oSet->GetFilter()->GetClass();
  493. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  494. // (especially when used to add a link)
  495. /*
  496. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  497. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  498. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  499. </ul></div>\n";
  500. */
  501. // Simple search form
  502. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  503. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  504. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  505. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  506. $index = 0;
  507. $sHtml .= "<table>\n";
  508. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  509. $aMapCriteria = array();
  510. foreach($aFilterCriteria as $aCriteria)
  511. {
  512. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  513. }
  514. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  515. foreach($aList as $sFilterCode)
  516. {
  517. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  518. if (($index % $numCols) == 0)
  519. {
  520. if ($index != 0)
  521. {
  522. $sHtml .= "</tr>\n";
  523. }
  524. $sHtml .= "<tr>\n";
  525. }
  526. $sFilterValue = '';
  527. $sFilterValue = utils::ReadParam($sFilterCode, '');
  528. $sFilterOpCode = null; // Use the default 'loose' OpCode
  529. if (empty($sFilterValue))
  530. {
  531. if (isset($aMapCriteria[$sFilterCode]))
  532. {
  533. if (count($aMapCriteria[$sFilterCode]) > 1)
  534. {
  535. $sFilterValue = '* mixed *';
  536. }
  537. else
  538. {
  539. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  540. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  541. }
  542. if ($sFilterCode != 'company')
  543. {
  544. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  545. }
  546. }
  547. }
  548. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  549. if ($aAllowedValues != null)
  550. {
  551. //Enum field or external key, display a combo
  552. $sValue = "<select name=\"$sFilterCode\">\n";
  553. $sValue .= "<option value=\"\">* Any *</option>\n";
  554. foreach($aAllowedValues as $key => $value)
  555. {
  556. if ($sFilterValue == $key)
  557. {
  558. $sSelected = ' selected';
  559. }
  560. else
  561. {
  562. $sSelected = '';
  563. }
  564. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  565. }
  566. $sValue .= "</select>\n";
  567. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  568. }
  569. else
  570. {
  571. // Any value is possible, display an input box
  572. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  573. }
  574. $index++;
  575. }
  576. if (($index % $numCols) != 0)
  577. {
  578. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  579. }
  580. $sHtml .= "</tr>\n";
  581. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  582. $sHtml .= "</table>\n";
  583. foreach($aExtraParams as $sName => $sValue)
  584. {
  585. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  586. }
  587. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  588. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  589. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  590. $sHtml .= $oAppContext->GetForForm();
  591. $sHtml .= "</form>\n";
  592. $sHtml .= "</div><!-- Simple search form -->\n";
  593. // OQL query builder
  594. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  595. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  596. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  597. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  598. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  599. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  600. $sOQLClause = utils::ReadParam('oql_clause', '');
  601. asort($aClasses);
  602. foreach($aClasses as $sChildClass)
  603. {
  604. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  605. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  606. }
  607. $sHtml .= "</select>&nbsp;</td><td>\n";
  608. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  609. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  610. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  611. foreach($aExtraParams as $sName => $sValue)
  612. {
  613. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  614. }
  615. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  616. $sHtml .= $oAppContext->GetForForm();
  617. $sHtml .= "</table></form>\n";
  618. $sHtml .= "</div><!-- OQL query form -->\n";
  619. return $sHtml;
  620. }
  621. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  622. {
  623. static $iInputId = 0;
  624. if (isset($aArgs[$sAttCode]) && empty($value))
  625. {
  626. // default value passed by the context (either the app context of the operation)
  627. $value = $aArgs[$sAttCode];
  628. }
  629. if (!empty($iId))
  630. {
  631. $iInputId = $iId;
  632. }
  633. else
  634. {
  635. $iInputId++;
  636. }
  637. if (!$oAttDef->IsExternalField())
  638. {
  639. $aCSSClasses = array();
  640. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  641. {
  642. $aCSSClasses[] = 'mandatory';
  643. }
  644. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  645. switch($oAttDef->GetEditClass())
  646. {
  647. case 'Date':
  648. $aCSSClasses[] = 'date-pick';
  649. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  650. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  651. break;
  652. case 'Password':
  653. $sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  654. break;
  655. case 'Text':
  656. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>";
  657. break;
  658. case 'List':
  659. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  660. $sHTMLValue = $oWidget->Display($oPage, $value);
  661. break;
  662. case 'Document':
  663. $oDocument = $value; // Value is an ormDocument object
  664. $sFileName = '';
  665. if (is_object($oDocument))
  666. {
  667. $sFileName = $oDocument->GetFileName();
  668. }
  669. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  670. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  671. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iInputId\" \" value=\"$sFileName\"/>\n";
  672. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  673. $sHTMLValue .= "<input name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iInputId\" onChange=\"UpdateFileName('$iInputId', this.value);\"/>\n";
  674. break;
  675. case 'String':
  676. default:
  677. // #@# todo - add context information (depending on dimensions)
  678. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  679. if ($aAllowedValues !== null)
  680. {
  681. //Enum field or external key, display a combo
  682. //if (count($aAllowedValues) == 0)
  683. //{
  684. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  685. //}
  686. //else if (count($aAllowedValues) > 50)
  687. if (count($aAllowedValues) > 50)
  688. {
  689. // too many choices, use an autocomplete
  690. // The input for the auto complete
  691. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
  692. // another hidden input to store & pass the object's Id
  693. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  694. $oPage->add_ready_script("\$('#label_$iInputId').autocomplete('./ajax.render.php', { minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#$iInputId', extraParams:{operation:'autocomplete', sclass:'$sClass',attCode:'".$sAttCode."'}});");
  695. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  696. // Prepopulate with a default value -- but no display value...
  697. //if (!empty($value))
  698. //{
  699. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  700. //}
  701. }
  702. else
  703. {
  704. // Few choices, use a normal 'select'
  705. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  706. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  707. $sHTMLValue .= "<option value=\"0\">-- select one --</option>\n";
  708. foreach($aAllowedValues as $key => $display_value)
  709. {
  710. $sSelected = ($value == $key) ? ' selected' : '';
  711. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  712. }
  713. $sHTMLValue .= "</select>\n";
  714. }
  715. }
  716. else
  717. {
  718. $sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses} />";
  719. }
  720. break;
  721. }
  722. }
  723. return $sHTMLValue;
  724. }
  725. public function DisplayModifyForm(WebPage $oPage)
  726. {
  727. static $iFormId = 0;
  728. $iFormId++;
  729. $oAppContext = new ApplicationContext();
  730. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  731. $iKey = $this->GetKey();
  732. $aDetails = array();
  733. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckMandatoryFields('form_{$iFormId}')\">\n");
  734. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  735. {
  736. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  737. {
  738. // Do nothing, the class field is always hidden, it cannot be edited
  739. }
  740. else if ($sStateAttCode == $sAttCode)
  741. {
  742. // State attribute is always read-only from the UI
  743. $sHTMLValue = $this->GetState();
  744. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  745. }
  746. else if (!$oAttDef->IsExternalField())
  747. {
  748. $iFlags = $this->GetAttributeFlags($sAttCode);
  749. if ($iFlags & OPT_ATT_HIDDEN)
  750. {
  751. // Attribute is hidden, do nothing
  752. }
  753. else
  754. {
  755. if ($iFlags & OPT_ATT_READONLY)
  756. {
  757. // Attribute is read-only
  758. $sHTMLValue = $this->GetAsHTML($sAttCode);
  759. }
  760. else
  761. {
  762. $sValue = $this->Get($sAttCode);
  763. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  764. $aArgs = array('this' => $this);
  765. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iFlags, $aArgs);
  766. }
  767. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  768. }
  769. }
  770. }
  771. $oPage->details($aDetails);
  772. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  773. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  774. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  775. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  776. $oPage->add($oAppContext->GetForForm());
  777. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  778. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  779. $oPage->add("</form>\n");
  780. }
  781. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array())
  782. {
  783. static $iCreationFormId = 0;
  784. $iCreationFormId++;
  785. $oAppContext = new ApplicationContext();
  786. $aDetails = array();
  787. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  788. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObjectToClone));
  789. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" onSubmit=\"return CheckMandatoryFields('creation_form_{$iCreationFormId}')\">\n");
  790. $aStates = MetaModel::EnumStates($sClass);
  791. if ($oObjectToClone == null)
  792. {
  793. $sTargetState = MetaModel::GetDefaultState($sClass);
  794. }
  795. else
  796. {
  797. $sTargetState = $oObjectToClone->GetState();
  798. }
  799. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  800. {
  801. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  802. {
  803. // Do nothing, the class field is always hidden, it cannot be edited
  804. }
  805. else if ($sStateAttCode == $sAttCode)
  806. {
  807. // State attribute is always read-only from the UI
  808. $sHTMLValue = $oObjectToClone->GetState();
  809. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  810. }
  811. else if (!$oAttDef->IsExternalField())
  812. {
  813. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  814. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  815. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  816. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
  817. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  818. }
  819. }
  820. $oPage->details($aDetails);
  821. if ($oObjectToClone != null)
  822. {
  823. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  824. }
  825. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  826. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  827. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  828. $oPage->add($oAppContext->GetForForm());
  829. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  830. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  831. $oPage->add("</form>\n");
  832. }
  833. protected static function GetCSSClasses($aCSSClasses)
  834. {
  835. $sCSSClasses = '';
  836. if (!empty($aCSSClasses))
  837. {
  838. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  839. }
  840. return $sCSSClasses;
  841. }
  842. }
  843. ?>