cmdbabstract.class.inc.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. static $iSearchFormId = 0;
  363. $sHtml = '';
  364. $numCols=4;
  365. $iSearchFormId++;
  366. $sClassName = $oSet->GetFilter()->GetClass();
  367. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  368. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  369. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  370. </ul></div>\n";
  371. // Simple search form
  372. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  373. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  374. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  375. $sHtml .= "<form>\n";
  376. $index = 0;
  377. $sHtml .= "<table>\n";
  378. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  379. $aMapCriteria = array();
  380. foreach($aFilterCriteria as $aCriteria)
  381. {
  382. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  383. }
  384. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  385. foreach($aList as $sFilterCode)
  386. {
  387. if (($index % $numCols) == 0)
  388. {
  389. if ($index != 0)
  390. {
  391. $sHtml .= "</tr>\n";
  392. }
  393. $sHtml .= "<tr>\n";
  394. }
  395. $sFilterValue = '';
  396. $sFilterValue = utils::ReadParam($sFilterCode, '');
  397. $sFilterOpCode = null; // Use the default 'loose' OpCode
  398. if (empty($sFilterValue))
  399. {
  400. if (isset($aMapCriteria[$sFilterCode]))
  401. {
  402. if (count($aMapCriteria[$sFilterCode]) > 1)
  403. {
  404. $sFilterValue = '* mixed *';
  405. }
  406. else
  407. {
  408. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  409. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  410. }
  411. if ($sFilterCode != 'company')
  412. {
  413. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  414. }
  415. }
  416. }
  417. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, array(), '');
  418. if ($aAllowedValues != null)
  419. {
  420. //Enum field or external key, display a combo
  421. $sValue = "<select name=\"$sFilterCode\">\n";
  422. $sValue .= "<option value=\"\">* Any *</option>\n";
  423. foreach($aAllowedValues as $key => $value)
  424. {
  425. if ($sFilterValue == $key)
  426. {
  427. $sSelected = ' selected';
  428. }
  429. else
  430. {
  431. $sSelected = '';
  432. }
  433. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  434. }
  435. $sValue .= "</select>\n";
  436. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  437. }
  438. else
  439. {
  440. // Any value is possible, display an input box
  441. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  442. }
  443. $index++;
  444. }
  445. if (($index % $numCols) != 0)
  446. {
  447. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  448. }
  449. $sHtml .= "</tr>\n";
  450. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  451. $sHtml .= "</table>\n";
  452. foreach($aExtraParams as $sName => $sValue)
  453. {
  454. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\">\n";
  455. }
  456. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\">\n";
  457. $sHtml .= "</form>\n";
  458. $sHtml .= "</div><!-- Simple search form -->\n";
  459. // OQL query builder
  460. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  461. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  462. $sHtml .= "<form><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  463. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  464. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  465. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  466. $sOQLClause = utils::ReadParam('oql_clause', '');
  467. asort($aClasses);
  468. foreach($aClasses as $sChildClass)
  469. {
  470. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  471. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  472. }
  473. $sHtml .= "</select>&nbsp;</td><td>\n";
  474. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  475. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  476. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\">\n";
  477. foreach($aExtraParams as $sName => $sValue)
  478. {
  479. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\">\n";
  480. }
  481. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\">\n";
  482. $sHtml .= "</table></form>\n";
  483. $sHtml .= "</div><!-- OQL query form -->\n";
  484. return $sHtml;
  485. }
  486. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '')
  487. {
  488. static $iInputId = 0;
  489. if (!empty($iId))
  490. {
  491. $iInputId = $iId;
  492. }
  493. else
  494. {
  495. $iInputId++;
  496. }
  497. if (!$oAttDef->IsExternalField())
  498. {
  499. switch($oAttDef->GetEditClass())
  500. {
  501. case 'Date':
  502. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_$sAttCode\" value=\"$value\" id=\"$iInputId\" class=\"date-pick\"/>";
  503. break;
  504. case 'Text':
  505. $sHTMLValue = "<textarea name=\"attr_$sAttCode\" rows=\"8\" cols=\"40\" id=\"$iInputId\">$value</textarea>";
  506. break;
  507. case 'List':
  508. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId);
  509. $sHTMLValue = $oWidget->Display($oPage, $value);
  510. break;
  511. case 'String':
  512. default:
  513. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array(), '');
  514. if ($aAllowedValues !== null)
  515. {
  516. //Enum field or external key, display a combo
  517. if (count($aAllowedValues) == 0)
  518. {
  519. $sHTMLValue = "<input type=\"text\" size=\"70\" value=\"\" name=\"attr_$sAttCode\" id=\"$iInputId\"/>";
  520. }
  521. else if (count($aAllowedValues) > 50)
  522. {
  523. // too many choices, use an autocomplete
  524. // The input for the auto complete
  525. $sHTMLValue = "<input type=\"text\" id=\"label_$iInputId\" size=\"50\" name=\"\" value=\"$sDisplayValue\" />";
  526. // another hidden input to store & pass the object's Id
  527. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_$sAttCode\" value=\"$value\" />\n";
  528. $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."'}});");
  529. }
  530. else
  531. {
  532. // Few choices, use a normal 'select'
  533. $sHTMLValue = "<select name=\"attr_$sAttCode\" id=\"$iInputId\">\n";
  534. foreach($aAllowedValues as $key => $display_value)
  535. {
  536. $sSelected = ($value == $key) ? ' selected' : '';
  537. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  538. }
  539. $sHTMLValue .= "</select>\n";
  540. }
  541. }
  542. else
  543. {
  544. $sHTMLValue = "<input type=\"text\" size=\"50\" name=\"attr_$sAttCode\" value=\"$value\" id=\"$iInputId\">";
  545. }
  546. }
  547. }
  548. return $sHTMLValue;
  549. }
  550. public function DisplayModifyForm(web_page $oPage)
  551. {
  552. $oAppContext = new ApplicationContext();
  553. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  554. $iKey = $this->GetKey();
  555. $aDetails = array();
  556. $oPage->add("<form method=\"post\">\n");
  557. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  558. {
  559. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  560. {
  561. // Do nothing, the class field is always hidden, it cannot be edited
  562. }
  563. else if ($sStateAttCode == $sAttCode)
  564. {
  565. // State attribute is always read-only from the UI
  566. $sHTMLValue = $this->GetState();
  567. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  568. }
  569. else if (!$oAttDef->IsExternalField())
  570. {
  571. $iFlags = $this->GetAttributeFlags($sAttCode);
  572. if ($iFlags & OPT_ATT_HIDDEN)
  573. {
  574. // Attribute is hidden, do nothing
  575. }
  576. else
  577. {
  578. if ($iFlags & OPT_ATT_READONLY)
  579. {
  580. // Attribute is read-only
  581. $sHTMLValue = $this->GetAsHTML($sAttCode);
  582. }
  583. else
  584. {
  585. $sValue = $this->Get($sAttCode);
  586. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  587. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue);
  588. }
  589. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  590. }
  591. }
  592. }
  593. $oPage->details($aDetails);
  594. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  595. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  596. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  597. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  598. $oPage->add($oAppContext->GetForForm());
  599. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  600. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  601. $oPage->add("</form>\n");
  602. }
  603. public static function DisplayCreationForm(web_page $oPage, $sClass, $oObjectToClone = null)
  604. {
  605. $oAppContext = new ApplicationContext();
  606. $aDetails = array();
  607. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  608. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObjectToClone));
  609. $oPage->add("<form method=\"post\">\n");
  610. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  611. {
  612. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  613. {
  614. // Do nothing, the class field is always hidden, it cannot be edited
  615. }
  616. else if ($sStateAttCode == $sAttCode)
  617. {
  618. // State attribute is always read-only from the UI
  619. $sHTMLValue = $oObjectToClone->GetState();
  620. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  621. }
  622. else if (!$oAttDef->IsExternalField())
  623. {
  624. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  625. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  626. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue);
  627. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  628. }
  629. }
  630. $oPage->details($aDetails);
  631. if ($oObjectToClone != null)
  632. {
  633. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  634. }
  635. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  636. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  637. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  638. $oPage->add($oAppContext->GetForForm());
  639. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  640. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  641. $oPage->add("</form>\n");
  642. }
  643. }
  644. ?>