cmdbabstract.class.inc.php 35 KB

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