cmdbabstract.class.inc.php 30 KB

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