cmdbabstract.class.inc.php 28 KB

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