cmdbabstract.class.inc.php 34 KB

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