cmdbabstract.class.inc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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_name'=> MetaModel::GetName(get_class($this)),'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(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 = '', $bDisplayMenu = true)
  200. {
  201. $oPage->add(self::GetDisplaySet($oPage, $oSet, $sLinkageAttribute, $bDisplayMenu));
  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. $sColspan = '';
  262. if ($bDisplayMenu)
  263. {
  264. $sColspan = 'colspan="2"';
  265. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.$oSet->Count().' object(s)</td><td>';
  266. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $sLinkageAttribute);
  267. $sHtml .= '</td></tr>';
  268. }
  269. $sHtml .= "<tr><td $sColspan>";
  270. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$sClassName, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  271. $sHtml .= '</td></tr>';
  272. $sHtml .= '</table>';
  273. return $sHtml;
  274. }
  275. static function DisplaySetAsCSV(web_page $oPage, CMDBObjectSet $oSet, $aParams = array())
  276. {
  277. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  278. }
  279. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  280. {
  281. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  282. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  283. $oAppContext = new ApplicationContext();
  284. $sClassName = $oSet->GetFilter()->GetClass();
  285. $aAttribs = array();
  286. $aList = MetaModel::GetZListItems($sClassName, 'details');
  287. $aHeader = array();
  288. $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  289. foreach($aList as $sAttCode)
  290. {
  291. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  292. }
  293. $sHtml = '#'.$oSet->GetFilter()->ToOQL()."\n";
  294. $sHtml .= implode($sSeparator, $aHeader)."\n";
  295. $oSet->Seek(0);
  296. while ($oObj = $oSet->Fetch())
  297. {
  298. $aRow = array();
  299. $aRow[] = $oObj->GetKey();
  300. foreach($aList as $sAttCode)
  301. {
  302. if (strstr($oObj->Get($sAttCode), $sSeparator)) // Escape the text only when it contains the separator
  303. {
  304. $aRow[] = $sTextQualifier.$oObj->Get($sAttCode).$sTextQualifier;
  305. }
  306. else
  307. {
  308. $aRow[] = $oObj->Get($sAttCode);
  309. }
  310. }
  311. $sHtml .= implode($sSeparator, $aRow)."\n";
  312. }
  313. return $sHtml;
  314. }
  315. static function DisplaySetAsXML(web_page $oPage, CMDBObjectSet $oSet, $aParams = array())
  316. {
  317. $oAppContext = new ApplicationContext();
  318. $sClassName = $oSet->GetFilter()->GetClass();
  319. $aAttribs = array();
  320. $aList = MetaModel::GetZListItems($sClassName, 'details');
  321. $oPage->add("<Set>\n");
  322. $oSet->Seek(0);
  323. while ($oObj = $oSet->Fetch())
  324. {
  325. $oPage->add("<$sClassName id=\"".$oObj->GetKey()."\">\n");
  326. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  327. {
  328. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()) && ($sAttCode != 'finalclass') )
  329. {
  330. $sValue = $oObj->GetAsXML($sAttCode);
  331. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  332. }
  333. }
  334. $oPage->add("</$sClassName>\n");
  335. }
  336. $oPage->add("</Set>\n");
  337. }
  338. // By rom
  339. function DisplayChangesLog(web_page $oPage)
  340. {
  341. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  342. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  343. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  344. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  345. $count = $oSet->Count();
  346. if ($count > 0)
  347. {
  348. $oPage->p("Changes log ($count):");
  349. self::DisplaySet($oPage, $oSet);
  350. }
  351. else
  352. {
  353. $oPage->p("Changes log is empty");
  354. }
  355. }
  356. public static function DisplaySearchForm(web_page $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  357. {
  358. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  359. }
  360. public static function GetSearchForm(web_page $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  361. {
  362. $sHtml = '';
  363. $numCols=4;
  364. $sClassName = $oSet->GetFilter()->GetClass();
  365. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  366. $sHtml .= "<form>\n";
  367. $index = 0;
  368. $sHtml .= "<table>\n";
  369. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  370. $aMapCriteria = array();
  371. foreach($aFilterCriteria as $aCriteria)
  372. {
  373. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  374. }
  375. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  376. foreach($aList as $sFilterCode)
  377. {
  378. if (($index % $numCols) == 0)
  379. {
  380. if ($index != 0)
  381. {
  382. $sHtml .= "</tr>\n";
  383. }
  384. $sHtml .= "<tr>\n";
  385. }
  386. $sFilterValue = '';
  387. $sFilterValue = utils::ReadParam($sFilterCode, '');
  388. $sFilterOpCode = null; // Use the default 'loose' OpCode
  389. if (empty($sFilterValue))
  390. {
  391. if (isset($aMapCriteria[$sFilterCode]))
  392. {
  393. if (count($aMapCriteria[$sFilterCode]) > 1)
  394. {
  395. $sFilterValue = '* mixed *';
  396. }
  397. else
  398. {
  399. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  400. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  401. }
  402. if ($sFilterCode != 'company')
  403. {
  404. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  405. }
  406. }
  407. }
  408. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, array(), '');
  409. if ($aAllowedValues != null)
  410. {
  411. //Enum field or external key, display a combo
  412. $sValue = "<select name=\"$sFilterCode\">\n";
  413. $sValue .= "<option value=\"\">* Any *</option>\n";
  414. foreach($aAllowedValues as $key => $value)
  415. {
  416. if ($sFilterValue == $key)
  417. {
  418. $sSelected = ' selected';
  419. }
  420. else
  421. {
  422. $sSelected = '';
  423. }
  424. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  425. }
  426. $sValue .= "</select>\n";
  427. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  428. }
  429. else
  430. {
  431. // Any value is possible, display an input box
  432. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  433. }
  434. $index++;
  435. }
  436. if (($index % $numCols) != 0)
  437. {
  438. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  439. }
  440. $sHtml .= "</tr>\n";
  441. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  442. $sHtml .= "</table>\n";
  443. foreach($aExtraParams as $sName => $sValue)
  444. {
  445. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\">\n";
  446. }
  447. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\">\n";
  448. $sHtml .= "</form>\n";
  449. // Soem Debug dumps...
  450. //$sHtml .= "<tt>".$oSet->GetFilter()->__DescribeHTML()."</tt><br/>\n";
  451. //$sHtml .= "<tt>encoding=\"text/serialize\" : ".$oSet->GetFilter()->serialize()."</tt><br/>\n";
  452. //$sHtml .= "<tt>encoding=\"text/sibusql\" : ".$oSet->GetFilter()->ToSibusQL()."</tt><br/>\n";
  453. //$sHtml .= "<tt>(Unlimited) ".$oUnlimitedFilter->__DescribeHTML()."</tt><br/>\n";
  454. //$sHtml .= "<tt>encoding=\"text/serialize\" : ".$oUnlimitedFilter->serialize()."</tt><br/>\n";
  455. //$sHtml .= "<tt>encoding=\"text/sibusql\" : ".$oUnlimitedFilter->ToSibusQL()."</tt>\n";
  456. return $sHtml;
  457. }
  458. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '')
  459. {
  460. static $iInputId = 0;
  461. if (!empty($iId))
  462. {
  463. $iInputId = $iId;
  464. }
  465. else
  466. {
  467. $iInputId++;
  468. }
  469. if (!$oAttDef->IsExternalField())
  470. {
  471. switch($oAttDef->GetEditClass())
  472. {
  473. case 'Date':
  474. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_$sAttCode\" value=\"$value\" id=\"$iInputId\" class=\"date-pick\"/>";
  475. break;
  476. case 'Text':
  477. $sHTMLValue = "<textarea name=\"attr_$sAttCode\" rows=\"8\" cols=\"40\" id=\"$iInputId\">$value</textarea>";
  478. break;
  479. case 'List':
  480. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId);
  481. $sHTMLValue = $oWidget->Display($oPage, $value);
  482. break;
  483. case 'String':
  484. default:
  485. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array(), '');
  486. if ($aAllowedValues !== null)
  487. {
  488. //Enum field or external key, display a combo
  489. if (count($aAllowedValues) == 0)
  490. {
  491. $sHTMLValue = "<input type=\"text\" size=\"70\" value=\"\" name=\"attr_$sAttCode\" id=\"$iInputId\"/>";
  492. }
  493. else if (count($aAllowedValues) > 50)
  494. {
  495. // too many choices, use an autocomplete
  496. // The input for the auto complete
  497. $sHTMLValue = "<input type=\"text\" id=\"label_$iInputId\" size=\"50\" name=\"\" value=\"$sDisplayValue\" />";
  498. // another hidden input to store & pass the object's Id
  499. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_$sAttCode\" value=\"$value\" />\n";
  500. $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."'}});");
  501. }
  502. else
  503. {
  504. // Few choices, use a normal 'select'
  505. $sHTMLValue = "<select name=\"attr_$sAttCode\" id=\"$iInputId\">\n";
  506. foreach($aAllowedValues as $key => $display_value)
  507. {
  508. $sSelected = ($value == $key) ? ' selected' : '';
  509. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  510. }
  511. $sHTMLValue .= "</select>\n";
  512. }
  513. }
  514. else
  515. {
  516. $sHTMLValue = "<input type=\"text\" size=\"50\" name=\"attr_$sAttCode\" value=\"$value\" id=\"$iInputId\">";
  517. }
  518. }
  519. }
  520. return $sHTMLValue;
  521. }
  522. public function DisplayModifyForm(web_page $oPage)
  523. {
  524. $oAppContext = new ApplicationContext();
  525. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  526. $iKey = $this->GetKey();
  527. $aDetails = array();
  528. $oPage->add("<form method=\"post\">\n");
  529. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  530. {
  531. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  532. {
  533. // Do nothing, the class field is always hidden, it cannot be edited
  534. }
  535. else if ($sStateAttCode == $sAttCode)
  536. {
  537. // State attribute is always read-only from the UI
  538. $sHTMLValue = $this->GetState();
  539. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  540. }
  541. else if (!$oAttDef->IsExternalField())
  542. {
  543. $iFlags = $this->GetAttributeFlags($sAttCode);
  544. if ($iFlags & OPT_ATT_HIDDEN)
  545. {
  546. // Attribute is hidden, do nothing
  547. }
  548. else
  549. {
  550. if ($iFlags & OPT_ATT_READONLY)
  551. {
  552. // Attribute is read-only
  553. $sHTMLValue = $this->GetAsHTML($sAttCode);
  554. }
  555. else
  556. {
  557. $sValue = $this->Get($sAttCode);
  558. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  559. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue);
  560. }
  561. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  562. }
  563. }
  564. }
  565. $oPage->details($aDetails);
  566. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  567. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  568. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  569. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  570. $oPage->add($oAppContext->GetForForm());
  571. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  572. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  573. $oPage->add("</form>\n");
  574. }
  575. public static function DisplayCreationForm(web_page $oPage, $sClass, $oObjectToClone = null)
  576. {
  577. $oAppContext = new ApplicationContext();
  578. $aDetails = array();
  579. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  580. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObjectToClone));
  581. $oPage->add("<form method=\"post\">\n");
  582. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  583. {
  584. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  585. {
  586. // Do nothing, the class field is always hidden, it cannot be edited
  587. }
  588. else if ($sStateAttCode == $sAttCode)
  589. {
  590. // State attribute is always read-only from the UI
  591. $sHTMLValue = $oObjectToClone->GetState();
  592. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  593. }
  594. else if (!$oAttDef->IsExternalField())
  595. {
  596. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  597. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  598. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue);
  599. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  600. }
  601. }
  602. $oPage->details($aDetails);
  603. if ($oObjectToClone != null)
  604. {
  605. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  606. }
  607. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  608. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  609. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  610. $oPage->add($oAppContext->GetForForm());
  611. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  612. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  613. $oPage->add("</form>\n");
  614. }
  615. }
  616. ?>