cmdbabstract.class.inc.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. $oAppContext = new ApplicationContext();
  36. $sExtClassNameAtt = MetaModel::GetNameAttributeCode($sObjClass);
  37. $sPage = self::ComputeUIPage($sObjClass);
  38. // Use the "name" of the target class as the label of the hyperlink
  39. // unless it's not available in the external attributes...
  40. if (isset($aAvailableFields[$sExtClassNameAtt]))
  41. {
  42. $sLabel = $aAvailableFields[$sExtClassNameAtt];
  43. }
  44. else
  45. {
  46. $sLabel = implode(' / ', $aAvailableFields);
  47. }
  48. $sHint = htmlentities("$sObjClass::$sObjKey");
  49. return "<a href=\"$sPage?operation=details&class=$sObjClass&id=$sObjKey&".$oAppContext->GetForLink()."\" title=\"$sHint\">$sLabel</a>";
  50. }
  51. public function GetHyperlink()
  52. {
  53. $aAvailableFields[MetaModel::GetNameAttributeCode(get_class($this))] = $this->GetName();
  54. return $this->MakeHyperLink(get_class($this), $this->GetKey(), $aAvailableFields);
  55. }
  56. public function GetDisplayValue($sAttCode)
  57. {
  58. $sDisplayValue = "";
  59. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  60. if ($sStateAttCode == $sAttCode)
  61. {
  62. $aStates = MetaModel::EnumStates(get_class($this));
  63. $sDisplayValue = $aStates[$this->Get($sAttCode)]['label'];
  64. }
  65. else
  66. {
  67. $oAtt = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  68. if ($oAtt->IsExternalKey())
  69. {
  70. // retrieve the "external fields" linked to this external key
  71. $sTargetClass = $oAtt->GetTargetClass();
  72. $aAvailableFields = array();
  73. foreach (MetaModel::GetExternalFields(get_class($this), $sAttCode) as $oExtField)
  74. {
  75. $aAvailableFields[$oExtField->GetExtAttCode()] = $oExtField->GetAsHTML($this->Get($oExtField->GetCode()));
  76. }
  77. $sExtClassNameAtt = MetaModel::GetNameAttributeCode($sTargetClass);
  78. // Use the "name" of the target class as the label of the hyperlink
  79. // unless it's not available in the external fields...
  80. if (isset($aAvailableFields[$sExtClassNameAtt]))
  81. {
  82. $sDisplayValue = $aAvailableFields[$sExtClassNameAtt];
  83. }
  84. else
  85. {
  86. $sDisplayValue = implode(' / ', $aAvailableFields);
  87. }
  88. }
  89. else
  90. {
  91. $sDisplayValue = $this->GetAsHTML($sAttCode);
  92. }
  93. }
  94. return $sDisplayValue;
  95. }
  96. function DisplayBareDetails(web_page $oPage)
  97. {
  98. $oPage->add($this->GetBareDetails($oPage));
  99. }
  100. function GetDisplayName()
  101. {
  102. return $this->GetAsHTML(MetaModel::GetNameAttributeCode(get_class($this)));
  103. }
  104. function GetBareDetails(web_page $oPage)
  105. {
  106. $sHtml = '';
  107. $oAppContext = new ApplicationContext();
  108. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  109. $aDetails = array();
  110. $sClass = get_class($this);
  111. $aList = MetaModel::GetZListItems($sClass, 'details');
  112. foreach($aList as $sAttCode)
  113. {
  114. $iFlags = $this->GetAttributeFlags($sAttCode);
  115. if ( ($iFlags & OPT_ATT_HIDDEN) == 0)
  116. {
  117. // The field is visible in the current state of the object
  118. if ($sStateAttCode == $sAttCode)
  119. {
  120. // Special display for the 'state' attribute itself
  121. $sDisplayValue = $this->GetState();
  122. }
  123. else
  124. {
  125. $sDisplayValue = $this->GetAsHTML($sAttCode);
  126. }
  127. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' => $sDisplayValue);
  128. }
  129. }
  130. $sHtml .= $oPage->GetDetails($aDetails);
  131. return $sHtml;
  132. }
  133. function DisplayDetails(web_page $oPage)
  134. {
  135. $sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
  136. if (!empty($sTemplate))
  137. {
  138. $oTemplate = new DisplayTemplate($sTemplate);
  139. $oTemplate->Render($oPage, array('class_name'=> MetaModel::GetName(get_class($this)),'class'=> get_class($this),'pkey'=> $this->GetKey(), 'name' => $this->GetName()));
  140. }
  141. else
  142. {
  143. // Standard Header with name, actions menu and history block
  144. $oPage->add("<div class=\"page_header\">\n");
  145. $oSingletonFilter = new DBObjectSearch(get_class($this));
  146. $oSingletonFilter->AddCondition('pkey', array($this->GetKey()));
  147. $oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
  148. $oBlock->Display($oPage, -1);
  149. $oPage->add("<h1>".Metamodel::GetName(MetaModel::GetName(get_class($this))).": <span class=\"hilite\">".$this->GetDisplayName()."</span></h1>\n");
  150. $oHistoryFilter = new DBObjectSearch('CMDBChangeOpSetAttribute');
  151. $oHistoryFilter->AddCondition('objkey', $this->GetKey());
  152. $oBlock = new HistoryBlock($oHistoryFilter, 'toggle', false);
  153. $oBlock->Display($oPage, -1);
  154. $oPage->add("</div>\n");
  155. // Object's details
  156. // template not found display the object using the *old style*
  157. self::DisplayBareDetails($oPage);
  158. // Related objects
  159. $oPage->AddTabContainer('Related Objects');
  160. $oPage->SetCurrentTabContainer('Related Objects');
  161. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  162. {
  163. if ((get_class($oAttDef) == 'AttributeLinkedSetIndirect') || (get_class($oAttDef) == 'AttributeLinkedSet'))
  164. {
  165. $oPage->SetCurrentTab($oAttDef->GetLabel());
  166. $oPage->p($oAttDef->GetDescription());
  167. if (get_class($oAttDef) == 'AttributeLinkedSet')
  168. {
  169. $sTargetClass = $oAttDef->GetLinkedClass();
  170. $oFilter = new DBObjectSearch($sTargetClass);
  171. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey()); // @@@ condition has same name as field ??
  172. $oBlock = new DisplayBlock($oFilter, 'list', false);
  173. $oBlock->Display($oPage, 0);
  174. }
  175. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  176. {
  177. $sLinkClass = $oAttDef->GetLinkedClass();
  178. // Transform the DBObjectSet into a CMBDObjectSet !!!
  179. $aLinkedObjects = $this->Get($sAttCode)->ToArray(false);
  180. if (count($aLinkedObjects) > 0)
  181. {
  182. $oSet = CMDBObjectSet::FromArray($sLinkClass, $aLinkedObjects);
  183. $this->DisplaySet($oPage, $oSet, $oAttDef->GetExtKeyToMe());
  184. }
  185. }
  186. }
  187. }
  188. $oPage->SetCurrentTab('');
  189. }
  190. }
  191. function DisplayPreview(web_page $oPage)
  192. {
  193. $aDetails = array();
  194. $sClass = get_class($this);
  195. $aList = MetaModel::GetZListItems($sClass, 'preview');
  196. foreach($aList as $sAttCode)
  197. {
  198. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  199. }
  200. $oPage->details($aDetails);
  201. }
  202. // Comment by Rom: this helper may be used to display objects of class DBObject
  203. // -> I am using this to display the changes history
  204. public static function DisplaySet(web_page $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false, $iObjectId = 0)
  205. {
  206. $oPage->add(self::GetDisplaySet($oPage, $oSet, array( 'link_attr' => $sLinkageAttribute, 'object_id' => $iObjectId, 'menu' => $bDisplayMenu, 'selection_mode' => $bSelectMode)));
  207. }
  208. //public static function GetDisplaySet(web_page $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
  209. public static function GetDisplaySet(web_page $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  210. {
  211. static $iListId = 0;
  212. $iListId++;
  213. // Initialize and check the parameters
  214. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  215. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  216. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  217. if (!empty($sLinkageAttribute))
  218. {
  219. if($iLinkedObjectId == 0)
  220. {
  221. // if 'links' mode is requested the d of the object to link to must be specified
  222. throw new ApplicationException("Parameter object_id is mandatory when link_attr is specified. Check the definition of the display template.");
  223. }
  224. if($sTargetAttr == '')
  225. {
  226. // if 'links' mode is requested the d of the object to link to must be specified
  227. throw new ApplicationException("Parameter target_attr is mandatory when link_attr is specified. Check the definition of the display template.");
  228. }
  229. }
  230. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  231. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  232. $sHtml = '';
  233. $oAppContext = new ApplicationContext();
  234. $sClassName = $oSet->GetFilter()->GetClass();
  235. $aAttribs = array();
  236. $aList = MetaModel::GetZListItems($sClassName, 'list');
  237. if (!empty($sLinkageAttribute))
  238. {
  239. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  240. // and other objects...
  241. // The display will then group all the attributes related to the link itself:
  242. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  243. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  244. assert(isset($aAttDefs[$sLinkageAttribute]));
  245. $oAttDef = $aAttDefs[$sLinkageAttribute];
  246. assert($oAttDef->IsExternalKey());
  247. // First display all the attributes specific to the link record
  248. foreach($aList as $sLinkAttCode)
  249. {
  250. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  251. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  252. {
  253. $aDisplayList[] = $sLinkAttCode;
  254. }
  255. }
  256. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  257. foreach($aList as $sLinkAttCode)
  258. {
  259. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  260. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  261. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  262. {
  263. $aDisplayList[] = $sLinkAttCode;
  264. }
  265. }
  266. // First display all the attributes specific to the link
  267. // Then display all the attributes linked to the other end of the relationship
  268. $aList = $aDisplayList;
  269. }
  270. foreach($aList as $sAttCode)
  271. {
  272. if ($bSelectMode)
  273. {
  274. $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');
  275. }
  276. $aAttribs['key'] = array('label' => '', 'description' => 'Click to display');
  277. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  278. }
  279. $aValues = array();
  280. $oSet->Seek(0);
  281. while ($oObj = $oSet->Fetch())
  282. {
  283. $aRow['key'] = $oObj->GetKey();
  284. if ($bSelectMode)
  285. {
  286. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  287. }
  288. $aRow['key'] = $oObj->GetKey();
  289. foreach($aList as $sAttCode)
  290. {
  291. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  292. }
  293. $aValues[] = $aRow;
  294. }
  295. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  296. $sHtml .= '<table class="listContainer">';
  297. $sColspan = '';
  298. if ($bDisplayMenu)
  299. {
  300. $sColspan = 'colspan="2"';
  301. $aMenuExtraParams = array();
  302. if (!empty($sLinkageAttribute))
  303. {
  304. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  305. $aMenuExtraParams = $aExtraParams;
  306. }
  307. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.$oSet->Count().' object(s)</td><td>';
  308. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  309. $sHtml .= '</td></tr>';
  310. }
  311. $sHtml .= "<tr><td $sColspan>";
  312. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$sClassName, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  313. $sHtml .= '</td></tr>';
  314. $sHtml .= '</table>';
  315. return $sHtml;
  316. }
  317. static function DisplaySetAsCSV(web_page $oPage, CMDBObjectSet $oSet, $aParams = array())
  318. {
  319. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  320. }
  321. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  322. {
  323. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  324. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  325. $oAppContext = new ApplicationContext();
  326. $sClassName = $oSet->GetFilter()->GetClass();
  327. $aAttribs = array();
  328. $aList = MetaModel::GetZListItems($sClassName, 'details');
  329. $aHeader = array();
  330. $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  331. foreach($aList as $sAttCode)
  332. {
  333. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  334. }
  335. $sHtml = '#'.$oSet->GetFilter()->ToOQL()."\n";
  336. $sHtml .= implode($sSeparator, $aHeader)."\n";
  337. $oSet->Seek(0);
  338. while ($oObj = $oSet->Fetch())
  339. {
  340. $aRow = array();
  341. $aRow[] = $oObj->GetKey();
  342. foreach($aList as $sAttCode)
  343. {
  344. if (strstr($oObj->Get($sAttCode), $sSeparator)) // Escape the text only when it contains the separator
  345. {
  346. $aRow[] = $sTextQualifier.$oObj->Get($sAttCode).$sTextQualifier;
  347. }
  348. else
  349. {
  350. $aRow[] = $oObj->Get($sAttCode);
  351. }
  352. }
  353. $sHtml .= implode($sSeparator, $aRow)."\n";
  354. }
  355. return $sHtml;
  356. }
  357. static function DisplaySetAsXML(web_page $oPage, CMDBObjectSet $oSet, $aParams = array())
  358. {
  359. $oAppContext = new ApplicationContext();
  360. $sClassName = $oSet->GetFilter()->GetClass();
  361. $aAttribs = array();
  362. $aList = MetaModel::GetZListItems($sClassName, 'details');
  363. $oPage->add("<Set>\n");
  364. $oSet->Seek(0);
  365. while ($oObj = $oSet->Fetch())
  366. {
  367. $oPage->add("<$sClassName id=\"".$oObj->GetKey()."\">\n");
  368. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  369. {
  370. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()) && ($sAttCode != 'finalclass') )
  371. {
  372. $sValue = $oObj->GetAsXML($sAttCode);
  373. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  374. }
  375. }
  376. $oPage->add("</$sClassName>\n");
  377. }
  378. $oPage->add("</Set>\n");
  379. }
  380. // By rom
  381. function DisplayChangesLog(web_page $oPage)
  382. {
  383. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  384. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  385. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  386. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  387. $count = $oSet->Count();
  388. if ($count > 0)
  389. {
  390. $oPage->p("Changes log ($count):");
  391. self::DisplaySet($oPage, $oSet);
  392. }
  393. else
  394. {
  395. $oPage->p("Changes log is empty");
  396. }
  397. }
  398. public static function DisplaySearchForm(web_page $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  399. {
  400. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  401. }
  402. public static function GetSearchForm(web_page $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  403. {
  404. static $iSearchFormId = 0;
  405. $sHtml = '';
  406. $numCols=4;
  407. $iSearchFormId++;
  408. $sClassName = $oSet->GetFilter()->GetClass();
  409. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  410. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  411. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  412. </ul></div>\n";
  413. // Simple search form
  414. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  415. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  416. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  417. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  418. $index = 0;
  419. $sHtml .= "<table>\n";
  420. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  421. $aMapCriteria = array();
  422. foreach($aFilterCriteria as $aCriteria)
  423. {
  424. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  425. }
  426. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  427. foreach($aList as $sFilterCode)
  428. {
  429. if (($index % $numCols) == 0)
  430. {
  431. if ($index != 0)
  432. {
  433. $sHtml .= "</tr>\n";
  434. }
  435. $sHtml .= "<tr>\n";
  436. }
  437. $sFilterValue = '';
  438. $sFilterValue = utils::ReadParam($sFilterCode, '');
  439. $sFilterOpCode = null; // Use the default 'loose' OpCode
  440. if (empty($sFilterValue))
  441. {
  442. if (isset($aMapCriteria[$sFilterCode]))
  443. {
  444. if (count($aMapCriteria[$sFilterCode]) > 1)
  445. {
  446. $sFilterValue = '* mixed *';
  447. }
  448. else
  449. {
  450. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  451. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  452. }
  453. if ($sFilterCode != 'company')
  454. {
  455. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  456. }
  457. }
  458. }
  459. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, array(), '');
  460. if ($aAllowedValues != null)
  461. {
  462. //Enum field or external key, display a combo
  463. $sValue = "<select name=\"$sFilterCode\">\n";
  464. $sValue .= "<option value=\"\">* Any *</option>\n";
  465. foreach($aAllowedValues as $key => $value)
  466. {
  467. if ($sFilterValue == $key)
  468. {
  469. $sSelected = ' selected';
  470. }
  471. else
  472. {
  473. $sSelected = '';
  474. }
  475. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  476. }
  477. $sValue .= "</select>\n";
  478. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  479. }
  480. else
  481. {
  482. // Any value is possible, display an input box
  483. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  484. }
  485. $index++;
  486. }
  487. if (($index % $numCols) != 0)
  488. {
  489. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  490. }
  491. $sHtml .= "</tr>\n";
  492. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  493. $sHtml .= "</table>\n";
  494. foreach($aExtraParams as $sName => $sValue)
  495. {
  496. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\">\n";
  497. }
  498. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\">\n";
  499. $sHtml .= "</form>\n";
  500. $sHtml .= "</div><!-- Simple search form -->\n";
  501. // OQL query builder
  502. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  503. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  504. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  505. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  506. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  507. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  508. $sOQLClause = utils::ReadParam('oql_clause', '');
  509. asort($aClasses);
  510. foreach($aClasses as $sChildClass)
  511. {
  512. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  513. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  514. }
  515. $sHtml .= "</select>&nbsp;</td><td>\n";
  516. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  517. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  518. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\">\n";
  519. foreach($aExtraParams as $sName => $sValue)
  520. {
  521. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\">\n";
  522. }
  523. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\">\n";
  524. $sHtml .= "</table></form>\n";
  525. $sHtml .= "</div><!-- OQL query form -->\n";
  526. return $sHtml;
  527. }
  528. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '')
  529. {
  530. static $iInputId = 0;
  531. if (!empty($iId))
  532. {
  533. $iInputId = $iId;
  534. }
  535. else
  536. {
  537. $iInputId++;
  538. }
  539. if (!$oAttDef->IsExternalField())
  540. {
  541. switch($oAttDef->GetEditClass())
  542. {
  543. case 'Date':
  544. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\" class=\"date-pick\"/>";
  545. break;
  546. case 'Text':
  547. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\">$value</textarea>";
  548. break;
  549. case 'List':
  550. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  551. $sHTMLValue = $oWidget->Display($oPage, $value);
  552. break;
  553. case 'String':
  554. default:
  555. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array(), '');
  556. if ($aAllowedValues !== null)
  557. {
  558. //Enum field or external key, display a combo
  559. if (count($aAllowedValues) == 0)
  560. {
  561. $sHTMLValue = "<input type=\"text\" size=\"70\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"/>";
  562. }
  563. else if (count($aAllowedValues) > 50)
  564. {
  565. // too many choices, use an autocomplete
  566. // The input for the auto complete
  567. $sHTMLValue = "<input type=\"text\" id=\"label_$iInputId\" size=\"50\" name=\"\" value=\"$sDisplayValue\" />";
  568. // another hidden input to store & pass the object's Id
  569. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  570. $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."'}});");
  571. }
  572. else
  573. {
  574. // Few choices, use a normal 'select'
  575. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\">\n";
  576. foreach($aAllowedValues as $key => $display_value)
  577. {
  578. $sSelected = ($value == $key) ? ' selected' : '';
  579. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  580. }
  581. $sHTMLValue .= "</select>\n";
  582. }
  583. }
  584. else
  585. {
  586. $sHTMLValue = "<input type=\"text\" size=\"50\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\">";
  587. }
  588. }
  589. }
  590. return $sHTMLValue;
  591. }
  592. public function DisplayModifyForm(web_page $oPage)
  593. {
  594. $oAppContext = new ApplicationContext();
  595. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  596. $iKey = $this->GetKey();
  597. $aDetails = array();
  598. $oPage->add("<form method=\"post\">\n");
  599. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  600. {
  601. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  602. {
  603. // Do nothing, the class field is always hidden, it cannot be edited
  604. }
  605. else if ($sStateAttCode == $sAttCode)
  606. {
  607. // State attribute is always read-only from the UI
  608. $sHTMLValue = $this->GetState();
  609. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  610. }
  611. else if (!$oAttDef->IsExternalField())
  612. {
  613. $iFlags = $this->GetAttributeFlags($sAttCode);
  614. if ($iFlags & OPT_ATT_HIDDEN)
  615. {
  616. // Attribute is hidden, do nothing
  617. }
  618. else
  619. {
  620. if ($iFlags & OPT_ATT_READONLY)
  621. {
  622. // Attribute is read-only
  623. $sHTMLValue = $this->GetAsHTML($sAttCode);
  624. }
  625. else
  626. {
  627. $sValue = $this->Get($sAttCode);
  628. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  629. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue);
  630. }
  631. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  632. }
  633. }
  634. }
  635. $oPage->details($aDetails);
  636. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  637. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  638. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  639. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  640. $oPage->add($oAppContext->GetForForm());
  641. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  642. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  643. $oPage->add("</form>\n");
  644. }
  645. public static function DisplayCreationForm(web_page $oPage, $sClass, $oObjectToClone = null)
  646. {
  647. $oAppContext = new ApplicationContext();
  648. $aDetails = array();
  649. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  650. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObjectToClone));
  651. $oPage->add("<form method=\"post\">\n");
  652. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  653. {
  654. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  655. {
  656. // Do nothing, the class field is always hidden, it cannot be edited
  657. }
  658. else if ($sStateAttCode == $sAttCode)
  659. {
  660. // State attribute is always read-only from the UI
  661. $sHTMLValue = $oObjectToClone->GetState();
  662. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  663. }
  664. else if (!$oAttDef->IsExternalField())
  665. {
  666. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  667. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  668. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue);
  669. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  670. }
  671. }
  672. $oPage->details($aDetails);
  673. if ($oObjectToClone != null)
  674. {
  675. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  676. }
  677. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  678. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  679. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  680. $oPage->add($oAppContext->GetForForm());
  681. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  682. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  683. $oPage->add("</form>\n");
  684. }
  685. }
  686. ?>