cmdbabstract.class.inc.php 30 KB

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