cmdbabstract.class.inc.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. $oTemplate->Render($oPage, array('class_name'=> MetaModel::GetName(get_class($this)),'class'=> get_class($this),'pkey'=> $this->GetKey(), 'name' => $this->GetName()));
  249. }
  250. else
  251. {
  252. // Object's details
  253. // template not found display the object using the *old style*
  254. $this->DisplayBareHeader($oPage);
  255. $this->DisplayBareDetails($oPage);
  256. $this->DisplayBareRelations($oPage);
  257. }
  258. }
  259. function DisplayPreview(WebPage $oPage)
  260. {
  261. $aDetails = array();
  262. $sClass = get_class($this);
  263. $aList = MetaModel::GetZListItems($sClass, 'preview');
  264. foreach($aList as $sAttCode)
  265. {
  266. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  267. }
  268. $oPage->details($aDetails);
  269. }
  270. // Comment by Rom: this helper may be used to display objects of class DBObject
  271. // -> I am using this to display the changes history
  272. public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  273. {
  274. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  275. }
  276. //public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
  277. public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  278. {
  279. static $iListId = 0;
  280. $iListId++;
  281. // Initialize and check the parameters
  282. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  283. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  284. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  285. if (!empty($sLinkageAttribute))
  286. {
  287. if($iLinkedObjectId == 0)
  288. {
  289. // if 'links' mode is requested the id of the object to link to must be specified
  290. throw new ApplicationException("Parameter object_id is mandatory when link_attr is specified. Check the definition of the display template.");
  291. }
  292. if($sTargetAttr == '')
  293. {
  294. // if 'links' mode is requested the d of the object to link to must be specified
  295. throw new ApplicationException("Parameter target_attr is mandatory when link_attr is specified. Check the definition of the display template.");
  296. }
  297. }
  298. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  299. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  300. $sHtml = '';
  301. $oAppContext = new ApplicationContext();
  302. $sClassName = $oSet->GetFilter()->GetClass();
  303. $aAttribs = array();
  304. $aList = MetaModel::GetZListItems($sClassName, 'list');
  305. if (!empty($sLinkageAttribute))
  306. {
  307. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  308. // and other objects...
  309. // The display will then group all the attributes related to the link itself:
  310. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  311. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  312. assert(isset($aAttDefs[$sLinkageAttribute]));
  313. $oAttDef = $aAttDefs[$sLinkageAttribute];
  314. assert($oAttDef->IsExternalKey());
  315. // First display all the attributes specific to the link record
  316. foreach($aList as $sLinkAttCode)
  317. {
  318. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  319. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  320. {
  321. $aDisplayList[] = $sLinkAttCode;
  322. }
  323. }
  324. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  325. foreach($aList as $sLinkAttCode)
  326. {
  327. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  328. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  329. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  330. {
  331. $aDisplayList[] = $sLinkAttCode;
  332. }
  333. }
  334. // First display all the attributes specific to the link
  335. // Then display all the attributes linked to the other end of the relationship
  336. $aList = $aDisplayList;
  337. }
  338. if ($bSelectMode)
  339. {
  340. $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');
  341. }
  342. $aAttribs['key'] = array('label' => '', 'description' => 'Click to display');
  343. foreach($aList as $sAttCode)
  344. {
  345. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  346. }
  347. $aValues = array();
  348. $oSet->Seek(0);
  349. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  350. $iMaxObjects = -1;
  351. if ($bDisplayLimit)
  352. {
  353. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  354. {
  355. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  356. }
  357. }
  358. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  359. {
  360. $aRow = array();
  361. $aRow['key'] = $oObj->GetKey();
  362. if ($bSelectMode)
  363. {
  364. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  365. }
  366. $aRow['key'] = $oObj->GetKey();
  367. foreach($aList as $sAttCode)
  368. {
  369. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  370. }
  371. $aValues[] = $aRow;
  372. $iMaxObjects--;
  373. }
  374. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  375. $sHtml .= '<table class="listContainer">';
  376. $sColspan = '';
  377. if ($bDisplayMenu)
  378. {
  379. $sColspan = 'colspan="2"';
  380. $aMenuExtraParams = $aExtraParams;
  381. if (!empty($sLinkageAttribute))
  382. {
  383. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  384. $aMenuExtraParams = $aExtraParams;
  385. }
  386. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  387. {
  388. // list truncated
  389. $divId = $aExtraParams['block_id'];
  390. $sFilter = $oSet->GetFilter()->serialize();
  391. $aExtraParams['display_limit'] = false; // To expand the full list
  392. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  393. $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>';
  394. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  395. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  396. }
  397. else
  398. {
  399. // Full list
  400. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.$oSet->Count().' object(s)</td><td>';
  401. }
  402. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  403. $sHtml .= '</td></tr>';
  404. }
  405. $sHtml .= "<tr><td $sColspan>";
  406. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$sClassName, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  407. $sHtml .= '</td></tr>';
  408. $sHtml .= '</table>';
  409. return $sHtml;
  410. }
  411. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  412. {
  413. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  414. }
  415. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  416. {
  417. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  418. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  419. $oAppContext = new ApplicationContext();
  420. $sClassName = $oSet->GetFilter()->GetClass();
  421. $aAttribs = array();
  422. $aList = MetaModel::GetZListItems($sClassName, 'details');
  423. $aHeader = array();
  424. $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  425. foreach($aList as $sAttCode)
  426. {
  427. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  428. }
  429. $sHtml = '#'.$oSet->GetFilter()->ToOQL()."\n";
  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, '\\');
  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. $oPage->add("<$sClassName id=\"".$oObj->GetKey()."\">\n");
  455. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  456. {
  457. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()) && ($sAttCode != 'finalclass') )
  458. {
  459. $sValue = $oObj->GetAsXML($sAttCode);
  460. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  461. }
  462. }
  463. $oPage->add("</$sClassName>\n");
  464. }
  465. $oPage->add("</Set>\n");
  466. }
  467. // By rom
  468. function DisplayChangesLog(WebPage $oPage)
  469. {
  470. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  471. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  472. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  473. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  474. $count = $oSet->Count();
  475. if ($count > 0)
  476. {
  477. $oPage->p("Changes log ($count):");
  478. self::DisplaySet($oPage, $oSet);
  479. }
  480. else
  481. {
  482. $oPage->p("Changes log is empty");
  483. }
  484. }
  485. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  486. {
  487. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  488. }
  489. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  490. {
  491. static $iSearchFormId = 0;
  492. $oAppContext = new ApplicationContext();
  493. $sHtml = '';
  494. $numCols=4;
  495. $iSearchFormId++;
  496. $sClassName = $oSet->GetFilter()->GetClass();
  497. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  498. // (especially when used to add a link)
  499. /*
  500. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  501. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  502. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  503. </ul></div>\n";
  504. */
  505. // Simple search form
  506. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  507. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  508. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  509. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  510. $index = 0;
  511. $sHtml .= "<table>\n";
  512. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  513. $aMapCriteria = array();
  514. foreach($aFilterCriteria as $aCriteria)
  515. {
  516. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  517. }
  518. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  519. foreach($aList as $sFilterCode)
  520. {
  521. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  522. if (($index % $numCols) == 0)
  523. {
  524. if ($index != 0)
  525. {
  526. $sHtml .= "</tr>\n";
  527. }
  528. $sHtml .= "<tr>\n";
  529. }
  530. $sFilterValue = '';
  531. $sFilterValue = utils::ReadParam($sFilterCode, '');
  532. $sFilterOpCode = null; // Use the default 'loose' OpCode
  533. if (empty($sFilterValue))
  534. {
  535. if (isset($aMapCriteria[$sFilterCode]))
  536. {
  537. if (count($aMapCriteria[$sFilterCode]) > 1)
  538. {
  539. $sFilterValue = '* mixed *';
  540. }
  541. else
  542. {
  543. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  544. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  545. }
  546. if ($sFilterCode != 'company')
  547. {
  548. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  549. }
  550. }
  551. }
  552. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  553. if ($aAllowedValues != null)
  554. {
  555. //Enum field or external key, display a combo
  556. $sValue = "<select name=\"$sFilterCode\">\n";
  557. $sValue .= "<option value=\"\">* Any *</option>\n";
  558. foreach($aAllowedValues as $key => $value)
  559. {
  560. if ($sFilterValue == $key)
  561. {
  562. $sSelected = ' selected';
  563. }
  564. else
  565. {
  566. $sSelected = '';
  567. }
  568. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  569. }
  570. $sValue .= "</select>\n";
  571. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  572. }
  573. else
  574. {
  575. // Any value is possible, display an input box
  576. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  577. }
  578. $index++;
  579. }
  580. if (($index % $numCols) != 0)
  581. {
  582. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  583. }
  584. $sHtml .= "</tr>\n";
  585. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  586. $sHtml .= "</table>\n";
  587. foreach($aExtraParams as $sName => $sValue)
  588. {
  589. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  590. }
  591. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  592. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  593. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  594. $sHtml .= $oAppContext->GetForForm();
  595. $sHtml .= "</form>\n";
  596. $sHtml .= "</div><!-- Simple search form -->\n";
  597. // OQL query builder
  598. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  599. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  600. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  601. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  602. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  603. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  604. $sOQLClause = utils::ReadParam('oql_clause', '');
  605. asort($aClasses);
  606. foreach($aClasses as $sChildClass)
  607. {
  608. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  609. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  610. }
  611. $sHtml .= "</select>&nbsp;</td><td>\n";
  612. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  613. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  614. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  615. foreach($aExtraParams as $sName => $sValue)
  616. {
  617. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  618. }
  619. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  620. $sHtml .= $oAppContext->GetForForm();
  621. $sHtml .= "</table></form>\n";
  622. $sHtml .= "</div><!-- OQL query form -->\n";
  623. return $sHtml;
  624. }
  625. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  626. {
  627. static $iInputId = 0;
  628. if (isset($aArgs[$sAttCode]) && empty($value))
  629. {
  630. // default value passed by the context (either the app context of the operation)
  631. $value = $aArgs[$sAttCode];
  632. }
  633. if (!empty($iId))
  634. {
  635. $iInputId = $iId;
  636. }
  637. else
  638. {
  639. $iInputId++;
  640. }
  641. if (!$oAttDef->IsExternalField())
  642. {
  643. $aCSSClasses = array();
  644. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  645. {
  646. $aCSSClasses[] = 'mandatory';
  647. }
  648. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  649. switch($oAttDef->GetEditClass())
  650. {
  651. case 'Date':
  652. $aCSSClasses[] = 'date-pick';
  653. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  654. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  655. break;
  656. case 'Password':
  657. $sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  658. break;
  659. case 'Text':
  660. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>";
  661. break;
  662. case 'List':
  663. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  664. $sHTMLValue = $oWidget->Display($oPage, $value);
  665. break;
  666. case 'Document':
  667. $oDocument = $value; // Value is an ormDocument object
  668. $sFileName = '';
  669. if (is_object($oDocument))
  670. {
  671. $sFileName = $oDocument->GetFileName();
  672. }
  673. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  674. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  675. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iInputId\" \" value=\"$sFileName\"/>\n";
  676. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  677. $sHTMLValue .= "<input name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iInputId\" onChange=\"UpdateFileName('$iInputId', this.value);\"/>\n";
  678. break;
  679. case 'String':
  680. default:
  681. // #@# todo - add context information (depending on dimensions)
  682. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  683. if ($aAllowedValues !== null)
  684. {
  685. //Enum field or external key, display a combo
  686. //if (count($aAllowedValues) == 0)
  687. //{
  688. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  689. //}
  690. //else if (count($aAllowedValues) > 50)
  691. if (count($aAllowedValues) > 50)
  692. {
  693. // too many choices, use an autocomplete
  694. // The input for the auto complete
  695. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
  696. // another hidden input to store & pass the object's Id
  697. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  698. $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."'}});");
  699. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  700. // Prepopulate with a default value -- but no display value...
  701. //if (!empty($value))
  702. //{
  703. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  704. //}
  705. }
  706. else
  707. {
  708. // Few choices, use a normal 'select'
  709. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  710. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  711. $sHTMLValue .= "<option value=\"0\">-- select one --</option>\n";
  712. foreach($aAllowedValues as $key => $display_value)
  713. {
  714. $sSelected = ($value == $key) ? ' selected' : '';
  715. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  716. }
  717. $sHTMLValue .= "</select>\n";
  718. }
  719. }
  720. else
  721. {
  722. $sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses} />";
  723. }
  724. break;
  725. }
  726. }
  727. return $sHTMLValue;
  728. }
  729. public function DisplayModifyForm(WebPage $oPage)
  730. {
  731. static $iFormId = 0;
  732. $iFormId++;
  733. $oAppContext = new ApplicationContext();
  734. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  735. $iKey = $this->GetKey();
  736. $aDetails = array();
  737. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckMandatoryFields('form_{$iFormId}')\">\n");
  738. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  739. {
  740. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  741. {
  742. // Do nothing, the class field is always hidden, it cannot be edited
  743. }
  744. else if ($sStateAttCode == $sAttCode)
  745. {
  746. // State attribute is always read-only from the UI
  747. $sHTMLValue = $this->GetState();
  748. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  749. }
  750. else if (!$oAttDef->IsExternalField())
  751. {
  752. $iFlags = $this->GetAttributeFlags($sAttCode);
  753. if ($iFlags & OPT_ATT_HIDDEN)
  754. {
  755. // Attribute is hidden, do nothing
  756. }
  757. else
  758. {
  759. if ($iFlags & OPT_ATT_READONLY)
  760. {
  761. // Attribute is read-only
  762. $sHTMLValue = $this->GetAsHTML($sAttCode);
  763. }
  764. else
  765. {
  766. $sValue = $this->Get($sAttCode);
  767. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  768. $aArgs = array('this' => $this);
  769. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iFlags, $aArgs);
  770. }
  771. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  772. }
  773. }
  774. }
  775. $oPage->details($aDetails);
  776. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  777. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  778. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  779. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  780. $oPage->add($oAppContext->GetForForm());
  781. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  782. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  783. $oPage->add("</form>\n");
  784. }
  785. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array())
  786. {
  787. static $iCreationFormId = 0;
  788. $iCreationFormId++;
  789. $oAppContext = new ApplicationContext();
  790. $aDetails = array();
  791. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  792. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObjectToClone));
  793. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" onSubmit=\"return CheckMandatoryFields('creation_form_{$iCreationFormId}')\">\n");
  794. $aStates = MetaModel::EnumStates($sClass);
  795. if ($oObjectToClone == null)
  796. {
  797. $sTargetState = MetaModel::GetDefaultState($sClass);
  798. }
  799. else
  800. {
  801. $sTargetState = $oObjectToClone->GetState();
  802. }
  803. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  804. {
  805. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  806. {
  807. // Do nothing, the class field is always hidden, it cannot be edited
  808. }
  809. else if ($sStateAttCode == $sAttCode)
  810. {
  811. // State attribute is always read-only from the UI
  812. $sHTMLValue = $oObjectToClone->GetState();
  813. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  814. }
  815. else if (!$oAttDef->IsExternalField())
  816. {
  817. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  818. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  819. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  820. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
  821. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  822. }
  823. }
  824. $oPage->details($aDetails);
  825. if ($oObjectToClone != null)
  826. {
  827. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  828. }
  829. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  830. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  831. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  832. $oPage->add($oAppContext->GetForForm());
  833. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  834. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  835. $oPage->add("</form>\n");
  836. }
  837. protected static function GetCSSClasses($aCSSClasses)
  838. {
  839. $sCSSClasses = '';
  840. if (!empty($aCSSClasses))
  841. {
  842. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  843. }
  844. return $sCSSClasses;
  845. }
  846. }
  847. ?>