cmdbabstract.class.inc.php 33 KB

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