cmdbabstract.class.inc.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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(WebPage $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(WebPage $oPage)
  132. {
  133. $oPage->add($this->GetBareDetails($oPage));
  134. }
  135. function DisplayBareRelations(WebPage $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(WebPage $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(WebPage $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(WebPage $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(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  258. {
  259. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  260. }
  261. //public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
  262. public static function GetDisplaySet(WebPage $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 = $aExtraParams;
  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(WebPage $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(WebPage $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(WebPage $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(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  470. {
  471. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  472. }
  473. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  474. {
  475. static $iSearchFormId = 0;
  476. $oAppContext = new ApplicationContext();
  477. $sHtml = '';
  478. $numCols=4;
  479. $iSearchFormId++;
  480. $sClassName = $oSet->GetFilter()->GetClass();
  481. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  482. // (especially when used to add a link)
  483. /*
  484. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  485. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  486. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  487. </ul></div>\n";
  488. */
  489. // Simple search form
  490. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  491. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  492. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  493. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  494. $index = 0;
  495. $sHtml .= "<table>\n";
  496. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  497. $aMapCriteria = array();
  498. foreach($aFilterCriteria as $aCriteria)
  499. {
  500. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  501. }
  502. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  503. foreach($aList as $sFilterCode)
  504. {
  505. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  506. if (($index % $numCols) == 0)
  507. {
  508. if ($index != 0)
  509. {
  510. $sHtml .= "</tr>\n";
  511. }
  512. $sHtml .= "<tr>\n";
  513. }
  514. $sFilterValue = '';
  515. $sFilterValue = utils::ReadParam($sFilterCode, '');
  516. $sFilterOpCode = null; // Use the default 'loose' OpCode
  517. if (empty($sFilterValue))
  518. {
  519. if (isset($aMapCriteria[$sFilterCode]))
  520. {
  521. if (count($aMapCriteria[$sFilterCode]) > 1)
  522. {
  523. $sFilterValue = '* mixed *';
  524. }
  525. else
  526. {
  527. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  528. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  529. }
  530. if ($sFilterCode != 'company')
  531. {
  532. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  533. }
  534. }
  535. }
  536. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  537. if ($aAllowedValues != null)
  538. {
  539. //Enum field or external key, display a combo
  540. $sValue = "<select name=\"$sFilterCode\">\n";
  541. $sValue .= "<option value=\"\">* Any *</option>\n";
  542. foreach($aAllowedValues as $key => $value)
  543. {
  544. if ($sFilterValue == $key)
  545. {
  546. $sSelected = ' selected';
  547. }
  548. else
  549. {
  550. $sSelected = '';
  551. }
  552. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  553. }
  554. $sValue .= "</select>\n";
  555. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  556. }
  557. else
  558. {
  559. // Any value is possible, display an input box
  560. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  561. }
  562. $index++;
  563. }
  564. if (($index % $numCols) != 0)
  565. {
  566. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  567. }
  568. $sHtml .= "</tr>\n";
  569. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  570. $sHtml .= "</table>\n";
  571. foreach($aExtraParams as $sName => $sValue)
  572. {
  573. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  574. }
  575. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  576. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  577. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  578. $sHtml .= $oAppContext->GetForForm();
  579. $sHtml .= "</form>\n";
  580. $sHtml .= "</div><!-- Simple search form -->\n";
  581. // OQL query builder
  582. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  583. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  584. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  585. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  586. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  587. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  588. $sOQLClause = utils::ReadParam('oql_clause', '');
  589. asort($aClasses);
  590. foreach($aClasses as $sChildClass)
  591. {
  592. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  593. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  594. }
  595. $sHtml .= "</select>&nbsp;</td><td>\n";
  596. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  597. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  598. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  599. foreach($aExtraParams as $sName => $sValue)
  600. {
  601. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  602. }
  603. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  604. $sHtml .= $oAppContext->GetForForm();
  605. $sHtml .= "</table></form>\n";
  606. $sHtml .= "</div><!-- OQL query form -->\n";
  607. return $sHtml;
  608. }
  609. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  610. {
  611. static $iInputId = 0;
  612. if (isset($aArgs[$sAttCode]) && empty($value))
  613. {
  614. // default value passed by the context (either the app context of the operation)
  615. $value = $aArgs[$sAttCode];
  616. }
  617. if (!empty($iId))
  618. {
  619. $iInputId = $iId;
  620. }
  621. else
  622. {
  623. $iInputId++;
  624. }
  625. if (!$oAttDef->IsExternalField())
  626. {
  627. $aCSSClasses = array();
  628. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  629. {
  630. $aCSSClasses[] = 'mandatory';
  631. }
  632. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  633. switch($oAttDef->GetEditClass())
  634. {
  635. case 'Date':
  636. $aCSSClasses[] = 'date-pick';
  637. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  638. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  639. break;
  640. case 'Password':
  641. $sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  642. break;
  643. case 'Text':
  644. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>";
  645. break;
  646. case 'List':
  647. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  648. $sHTMLValue = $oWidget->Display($oPage, $value);
  649. break;
  650. case 'Document':
  651. $oDocument = $value; // Value is an ormDocument object
  652. $sFileName = '';
  653. if (is_object($oDocument))
  654. {
  655. $sFileName = $oDocument->GetFileName();
  656. }
  657. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  658. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  659. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iInputId\" \" value=\"$sFileName\"/>\n";
  660. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  661. $sHTMLValue .= "<input name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iInputId\" onChange=\"UpdateFileName('$iInputId', this.value);\"/>\n";
  662. break;
  663. case 'String':
  664. default:
  665. // #@# todo - add context information (depending on dimensions)
  666. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  667. if ($aAllowedValues !== null)
  668. {
  669. //Enum field or external key, display a combo
  670. //if (count($aAllowedValues) == 0)
  671. //{
  672. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  673. //}
  674. //else if (count($aAllowedValues) > 50)
  675. if (count($aAllowedValues) > 50)
  676. {
  677. // too many choices, use an autocomplete
  678. // The input for the auto complete
  679. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
  680. // another hidden input to store & pass the object's Id
  681. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  682. $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."'}});");
  683. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  684. // Prepopulate with a default value -- but no display value...
  685. //if (!empty($value))
  686. //{
  687. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  688. //}
  689. }
  690. else
  691. {
  692. // Few choices, use a normal 'select'
  693. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  694. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  695. $sHTMLValue .= "<option value=\"0\">-- select one --</option>\n";
  696. foreach($aAllowedValues as $key => $display_value)
  697. {
  698. $sSelected = ($value == $key) ? ' selected' : '';
  699. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  700. }
  701. $sHTMLValue .= "</select>\n";
  702. }
  703. }
  704. else
  705. {
  706. $sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses} />";
  707. }
  708. break;
  709. }
  710. }
  711. return $sHTMLValue;
  712. }
  713. public function DisplayModifyForm(WebPage $oPage)
  714. {
  715. static $iFormId = 0;
  716. $iFormId++;
  717. $oAppContext = new ApplicationContext();
  718. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  719. $iKey = $this->GetKey();
  720. $aDetails = array();
  721. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckMandatoryFields('form_{$iFormId}')\">\n");
  722. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  723. {
  724. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  725. {
  726. // Do nothing, the class field is always hidden, it cannot be edited
  727. }
  728. else if ($sStateAttCode == $sAttCode)
  729. {
  730. // State attribute is always read-only from the UI
  731. $sHTMLValue = $this->GetState();
  732. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  733. }
  734. else if (!$oAttDef->IsExternalField())
  735. {
  736. $iFlags = $this->GetAttributeFlags($sAttCode);
  737. if ($iFlags & OPT_ATT_HIDDEN)
  738. {
  739. // Attribute is hidden, do nothing
  740. }
  741. else
  742. {
  743. if ($iFlags & OPT_ATT_READONLY)
  744. {
  745. // Attribute is read-only
  746. $sHTMLValue = $this->GetAsHTML($sAttCode);
  747. }
  748. else
  749. {
  750. $sValue = $this->Get($sAttCode);
  751. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  752. $aArgs = array('this' => $this);
  753. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iFlags, $aArgs);
  754. }
  755. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  756. }
  757. }
  758. }
  759. $oPage->details($aDetails);
  760. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  761. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  762. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  763. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  764. $oPage->add($oAppContext->GetForForm());
  765. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  766. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  767. $oPage->add("</form>\n");
  768. }
  769. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array())
  770. {
  771. static $iCreationFormId = 0;
  772. $iCreationFormId++;
  773. $oAppContext = new ApplicationContext();
  774. $aDetails = array();
  775. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  776. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObjectToClone));
  777. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" onSubmit=\"return CheckMandatoryFields('creation_form_{$iCreationFormId}')\">\n");
  778. $aStates = MetaModel::EnumStates($sClass);
  779. if ($oObjectToClone == null)
  780. {
  781. $sTargetState = MetaModel::GetDefaultState($sClass);
  782. }
  783. else
  784. {
  785. $sTargetState = $oObjectToClone->GetState();
  786. }
  787. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  788. {
  789. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  790. {
  791. // Do nothing, the class field is always hidden, it cannot be edited
  792. }
  793. else if ($sStateAttCode == $sAttCode)
  794. {
  795. // State attribute is always read-only from the UI
  796. $sHTMLValue = $oObjectToClone->GetState();
  797. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  798. }
  799. else if (!$oAttDef->IsExternalField())
  800. {
  801. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  802. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  803. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  804. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
  805. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  806. }
  807. }
  808. $oPage->details($aDetails);
  809. if ($oObjectToClone != null)
  810. {
  811. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  812. }
  813. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  814. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  815. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  816. $oPage->add($oAppContext->GetForForm());
  817. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  818. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  819. $oPage->add("</form>\n");
  820. }
  821. protected static function GetCSSClasses($aCSSClasses)
  822. {
  823. $sCSSClasses = '';
  824. if (!empty($aCSSClasses))
  825. {
  826. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  827. }
  828. return $sCSSClasses;
  829. }
  830. }
  831. ?>