cmdbabstract.class.inc.php 32 KB

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