cmdbabstract.class.inc.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  442. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  443. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  444. </ul></div>\n";
  445. // Simple search form
  446. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  447. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  448. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  449. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  450. $index = 0;
  451. $sHtml .= "<table>\n";
  452. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  453. $aMapCriteria = array();
  454. foreach($aFilterCriteria as $aCriteria)
  455. {
  456. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  457. }
  458. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  459. foreach($aList as $sFilterCode)
  460. {
  461. if (($index % $numCols) == 0)
  462. {
  463. if ($index != 0)
  464. {
  465. $sHtml .= "</tr>\n";
  466. }
  467. $sHtml .= "<tr>\n";
  468. }
  469. $sFilterValue = '';
  470. $sFilterValue = utils::ReadParam($sFilterCode, '');
  471. $sFilterOpCode = null; // Use the default 'loose' OpCode
  472. if (empty($sFilterValue))
  473. {
  474. if (isset($aMapCriteria[$sFilterCode]))
  475. {
  476. if (count($aMapCriteria[$sFilterCode]) > 1)
  477. {
  478. $sFilterValue = '* mixed *';
  479. }
  480. else
  481. {
  482. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  483. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  484. }
  485. if ($sFilterCode != 'company')
  486. {
  487. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  488. }
  489. }
  490. }
  491. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  492. if ($aAllowedValues != null)
  493. {
  494. //Enum field or external key, display a combo
  495. $sValue = "<select name=\"$sFilterCode\">\n";
  496. $sValue .= "<option value=\"\">* Any *</option>\n";
  497. foreach($aAllowedValues as $key => $value)
  498. {
  499. if ($sFilterValue == $key)
  500. {
  501. $sSelected = ' selected';
  502. }
  503. else
  504. {
  505. $sSelected = '';
  506. }
  507. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  508. }
  509. $sValue .= "</select>\n";
  510. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  511. }
  512. else
  513. {
  514. // Any value is possible, display an input box
  515. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  516. }
  517. $index++;
  518. }
  519. if (($index % $numCols) != 0)
  520. {
  521. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  522. }
  523. $sHtml .= "</tr>\n";
  524. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  525. $sHtml .= "</table>\n";
  526. foreach($aExtraParams as $sName => $sValue)
  527. {
  528. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\">\n";
  529. }
  530. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\">\n";
  531. $sHtml .= "</form>\n";
  532. $sHtml .= "</div><!-- Simple search form -->\n";
  533. // OQL query builder
  534. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  535. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  536. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  537. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  538. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  539. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  540. $sOQLClause = utils::ReadParam('oql_clause', '');
  541. asort($aClasses);
  542. foreach($aClasses as $sChildClass)
  543. {
  544. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  545. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  546. }
  547. $sHtml .= "</select>&nbsp;</td><td>\n";
  548. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  549. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  550. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\">\n";
  551. foreach($aExtraParams as $sName => $sValue)
  552. {
  553. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\">\n";
  554. }
  555. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\">\n";
  556. $sHtml .= "</table></form>\n";
  557. $sHtml .= "</div><!-- OQL query form -->\n";
  558. return $sHtml;
  559. }
  560. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  561. {
  562. static $iInputId = 0;
  563. if (!empty($iId))
  564. {
  565. $iInputId = $iId;
  566. }
  567. else
  568. {
  569. $iInputId++;
  570. }
  571. if (!$oAttDef->IsExternalField())
  572. {
  573. $aCSSClasses = array();
  574. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  575. {
  576. $aCSSClasses[] = 'mandatory';
  577. }
  578. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  579. switch($oAttDef->GetEditClass())
  580. {
  581. case 'Date':
  582. $aCSSClasses[] = 'date-pick';
  583. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  584. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  585. break;
  586. case 'Password':
  587. $sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  588. break;
  589. case 'Text':
  590. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>";
  591. break;
  592. case 'List':
  593. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  594. $sHTMLValue = $oWidget->Display($oPage, $value);
  595. break;
  596. case 'String':
  597. default:
  598. // #@# todo - add context information (depending on dimensions)
  599. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  600. if ($aAllowedValues !== null)
  601. {
  602. //Enum field or external key, display a combo
  603. if (count($aAllowedValues) == 0)
  604. {
  605. $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  606. }
  607. else if (count($aAllowedValues) > 50)
  608. {
  609. // too many choices, use an autocomplete
  610. // The input for the auto complete
  611. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" name=\"\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
  612. // another hidden input to store & pass the object's Id
  613. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  614. $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."'}});");
  615. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  616. }
  617. else
  618. {
  619. // Few choices, use a normal 'select'
  620. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  621. $sHTMLValue .= "<option value=\"0\">-- select one --</option>\n";
  622. foreach($aAllowedValues as $key => $display_value)
  623. {
  624. $sSelected = ($value == $key) ? ' selected' : '';
  625. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  626. }
  627. $sHTMLValue .= "</select>\n";
  628. }
  629. }
  630. else
  631. {
  632. $sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}>";
  633. }
  634. break;
  635. }
  636. }
  637. return $sHTMLValue;
  638. }
  639. public function DisplayModifyForm(web_page $oPage)
  640. {
  641. static $iFormId = 0;
  642. $iFormId++;
  643. $oAppContext = new ApplicationContext();
  644. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  645. $iKey = $this->GetKey();
  646. $aDetails = array();
  647. $oPage->add("<form id=\"form_{$iFormId}\" method=\"post\" onSubmit=\"return CheckMandatoryFields('form_{$iFormId}')\">\n");
  648. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  649. {
  650. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  651. {
  652. // Do nothing, the class field is always hidden, it cannot be edited
  653. }
  654. else if ($sStateAttCode == $sAttCode)
  655. {
  656. // State attribute is always read-only from the UI
  657. $sHTMLValue = $this->GetState();
  658. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  659. }
  660. else if (!$oAttDef->IsExternalField())
  661. {
  662. $iFlags = $this->GetAttributeFlags($sAttCode);
  663. if ($iFlags & OPT_ATT_HIDDEN)
  664. {
  665. // Attribute is hidden, do nothing
  666. }
  667. else
  668. {
  669. if ($iFlags & OPT_ATT_READONLY)
  670. {
  671. // Attribute is read-only
  672. $sHTMLValue = $this->GetAsHTML($sAttCode);
  673. }
  674. else
  675. {
  676. $sValue = $this->Get($sAttCode);
  677. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  678. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iFlags);
  679. }
  680. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  681. }
  682. }
  683. }
  684. $oPage->details($aDetails);
  685. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  686. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  687. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  688. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  689. $oPage->add($oAppContext->GetForForm());
  690. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  691. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  692. $oPage->add("</form>\n");
  693. }
  694. public static function DisplayCreationForm(web_page $oPage, $sClass, $oObjectToClone = null)
  695. {
  696. static $iCreationFormId = 0;
  697. $iCreationFormId++;
  698. $oAppContext = new ApplicationContext();
  699. $aDetails = array();
  700. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  701. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($oObjectToClone));
  702. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" onSubmit=\"return CheckMandatoryFields('creation_form_{$iCreationFormId}')\">\n");
  703. $aStates = MetaModel::EnumStates($sClass);
  704. if ($oObjectToClone == null)
  705. {
  706. $sTargetState = MetaModel::GetDefaultState($sClass);
  707. }
  708. else
  709. {
  710. $sTargetState = $oObjectToClone->GetState();
  711. }
  712. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  713. {
  714. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  715. {
  716. // Do nothing, the class field is always hidden, it cannot be edited
  717. }
  718. else if ($sStateAttCode == $sAttCode)
  719. {
  720. // State attribute is always read-only from the UI
  721. $sHTMLValue = $oObjectToClone->GetState();
  722. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  723. }
  724. else if (!$oAttDef->IsExternalField())
  725. {
  726. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  727. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  728. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  729. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions);
  730. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  731. }
  732. }
  733. $oPage->details($aDetails);
  734. if ($oObjectToClone != null)
  735. {
  736. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  737. }
  738. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  739. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  740. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  741. $oPage->add($oAppContext->GetForForm());
  742. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  743. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  744. $oPage->add("</form>\n");
  745. }
  746. protected static function GetCSSClasses($aCSSClasses)
  747. {
  748. $sCSSClasses = '';
  749. if (!empty($aCSSClasses))
  750. {
  751. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  752. }
  753. return $sCSSClasses;
  754. }
  755. }
  756. ?>