cmdbabstract.class.inc.php 23 KB

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