cmdbabstract.class.inc.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Abstract class that implements some common and useful methods for displaying
  18. * the objects
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. define('OBJECT_PROPERTIES_TAB', 'ObjectProperties');
  26. require_once('../core/cmdbobject.class.inc.php');
  27. require_once('../application/utils.inc.php');
  28. require_once('../application/applicationcontext.class.inc.php');
  29. require_once('../application/ui.linkswidget.class.inc.php');
  30. abstract class cmdbAbstractObject extends CMDBObject
  31. {
  32. public static function GetUIPage()
  33. {
  34. return '../pages/UI.php';
  35. }
  36. public static function ComputeUIPage($sClass)
  37. {
  38. static $aUIPagesCache = array(); // Cache to store the php page used to display each class of object
  39. if (!isset($aUIPagesCache[$sClass]))
  40. {
  41. $UIPage = false;
  42. if (is_callable("$sClass::GetUIPage"))
  43. {
  44. $UIPage = eval("return $sClass::GetUIPage();"); // May return false in case of error
  45. }
  46. $aUIPagesCache[$sClass] = $UIPage === false ? './UI.php' : $UIPage;
  47. }
  48. $sPage = $aUIPagesCache[$sClass];
  49. return $sPage;
  50. }
  51. protected static function MakeHyperLink($sObjClass, $sObjKey, $aAvailableFields)
  52. {
  53. if ($sObjKey <= 0) return '<em>'.Dict::S('UI:UndefinedObject').'</em>'; // Objects built in memory have negative IDs
  54. $oAppContext = new ApplicationContext();
  55. $sExtClassNameAtt = MetaModel::GetNameAttributeCode($sObjClass);
  56. $sPage = self::ComputeUIPage($sObjClass);
  57. $sAbsoluteUrl = utils::GetAbsoluteUrl(false); // False => Don't get the query string
  58. $sAbsoluteUrl = substr($sAbsoluteUrl, 0, 1+strrpos($sAbsoluteUrl, '/')); // remove the current page, keep just the path, up to the last /
  59. // Use the "name" of the target class as the label of the hyperlink
  60. // unless it's not available in the external attributes...
  61. if (isset($aAvailableFields[$sExtClassNameAtt]))
  62. {
  63. $sLabel = $aAvailableFields[$sExtClassNameAtt];
  64. }
  65. else
  66. {
  67. $sLabel = implode(' / ', $aAvailableFields);
  68. }
  69. // Safety belt
  70. //
  71. if (empty($sLabel))
  72. {
  73. // Developer's note:
  74. // This is doing the job for you, but that is just there in case
  75. // the external fields associated to the external key are blanks
  76. // The ultimate solution will be to query the name automatically
  77. // and independantly from the data model (automatic external field)
  78. // AND make the name be a mandatory field
  79. //
  80. $sObject = MetaModel::GetObject($sObjClass, $sObjKey);
  81. $sLabel = $sObject->GetDisplayName();
  82. }
  83. // Safety net
  84. //
  85. if (empty($sLabel))
  86. {
  87. $sLabel = MetaModel::GetName($sObjClass)." #$sObjKey";
  88. }
  89. $sHint = MetaModel::GetName($sObjClass)."::$sObjKey";
  90. return "<a href=\"{$sAbsoluteUrl}{$sPage}?operation=details&class=$sObjClass&id=$sObjKey&".$oAppContext->GetForLink()."\" title=\"$sHint\">$sLabel</a>";
  91. }
  92. function DisplayBareHeader(WebPage $oPage)
  93. {
  94. // Standard Header with name, actions menu and history block
  95. //
  96. // action menu
  97. $oSingletonFilter = new DBObjectSearch(get_class($this));
  98. $oSingletonFilter->AddCondition('id', array($this->GetKey()));
  99. $oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
  100. $oBlock->Display($oPage, -1);
  101. $oPage->add("<div class=\"page_header\"><h1><img src=\"".$this->GetIcon()."\" style=\"margin-right:10px;margin-top: -16px;vertical-align:middle;\">\n");
  102. $oPage->add(MetaModel::GetName(get_class($this)).": <span class=\"hilite\">".$this->GetDisplayName()."</span></h1>\n");
  103. $oPage->add("</div>\n");
  104. }
  105. function DisplayBareHistory(WebPage $oPage)
  106. {
  107. // history block (with as a tab)
  108. $oHistoryFilter = new DBObjectSearch('CMDBChangeOp');
  109. $oHistoryFilter->AddCondition('objkey', $this->GetKey());
  110. $oHistoryFilter->AddCondition('objclass', get_class($this));
  111. $oBlock = new HistoryBlock($oHistoryFilter, 'tab', false);
  112. $oBlock->Display($oPage, -1);
  113. }
  114. function DisplayBareProperties(WebPage $oPage)
  115. {
  116. $oPage->add($this->GetBareProperties($oPage));
  117. }
  118. function DisplayBareRelations(WebPage $oPage)
  119. {
  120. // Related objects
  121. //$oPage->AddTabContainer('Related Objects');
  122. //$oPage->SetCurrentTabContainer('Related Objects');
  123. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  124. {
  125. if ($oAttDef->IsLinkset())
  126. {
  127. $oPage->SetCurrentTab($oAttDef->GetLabel());
  128. $oPage->p($oAttDef->GetDescription());
  129. if (get_class($oAttDef) == 'AttributeLinkedSet')
  130. {
  131. $sTargetClass = $oAttDef->GetLinkedClass();
  132. $oFilter = new DBObjectSearch($sTargetClass);
  133. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey());
  134. $oBlock = new DisplayBlock($oFilter, 'list', false);
  135. $aParams = array(
  136. 'target_attr' => $oAttDef->GetExtKeyToMe(),
  137. 'object_id' => $this->GetKey()
  138. );
  139. $oBlock->Display($oPage, 0, $aParams);
  140. }
  141. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  142. {
  143. $sLinkClass = $oAttDef->GetLinkedClass();
  144. // Transform the DBObjectSet into a CMBDObjectSet !!!
  145. $aLinkedObjects = $this->Get($sAttCode)->ToArray(false);
  146. if (count($aLinkedObjects) > 0)
  147. {
  148. $oSet = CMDBObjectSet::FromArray($sLinkClass, $aLinkedObjects);
  149. $aParams = array(
  150. 'link_attr' => $oAttDef->GetExtKeyToMe(),
  151. 'object_id' => $this->GetKey(),
  152. 'target_attr' => $oAttDef->GetExtKeyToRemote(),
  153. );
  154. self::DisplaySet($oPage, $oSet, $aParams);
  155. }
  156. }
  157. }
  158. }
  159. $oPage->SetCurrentTab('');
  160. }
  161. function GetDisplayName()
  162. {
  163. $sDisplayName = '';
  164. if (MetaModel::GetNameAttributeCode(get_class($this)) != '')
  165. {
  166. $sDisplayName = $this->GetAsHTML(MetaModel::GetNameAttributeCode(get_class($this)));
  167. }
  168. return $sDisplayName;
  169. }
  170. function GetBareProperties(WebPage $oPage)
  171. {
  172. $sHtml = '';
  173. $oAppContext = new ApplicationContext();
  174. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  175. $aDetails = array();
  176. $sClass = get_class($this);
  177. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  178. $aFullList = MetaModel::ListAttributeDefs($sClass);
  179. $aList = $aDetailsList;
  180. $aDetailsStruct = self::ProcessZlist($aDetailsList, array('UI:PropertiesTab' => array()), 'UI:PropertiesTab', 'col1', '');
  181. // Compute the list of properties to display, first the attributes in the 'details' list, then
  182. // all the remaining attributes that are not external fields
  183. $sHtml = '';
  184. $aDetails = array();
  185. foreach($aDetailsStruct as $sTab => $aCols )
  186. {
  187. $aDetails[$sTab] = array();
  188. ksort($aCols);
  189. $oPage->SetCurrentTab(Dict::S($sTab));
  190. $oPage->add('<table style="vertical-align:top"><tr>');
  191. foreach($aCols as $sColIndex => $aFieldsets)
  192. {
  193. $aDetails[$sTab][$sColIndex] = array();
  194. foreach($aFieldsets as $sFieldsetName => $aFields)
  195. {
  196. //if ($sFieldsetName == '')
  197. //{
  198. foreach($aFields as $sAttCode)
  199. {
  200. $val = $this->GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode);
  201. if ($val != null)
  202. {
  203. // The field is visible, add it to the current column
  204. $aDetails[$sTab][$sColIndex][] = $val;
  205. }
  206. }
  207. //}
  208. }
  209. $oPage->add('<td style="vertical-align:top">');
  210. $oPage->Details($aDetails[$sTab][$sColIndex]);
  211. $oPage->add('</td>');
  212. }
  213. $oPage->add('</tr></table>');
  214. }
  215. return $sHtml;
  216. }
  217. function DisplayDetails(WebPage $oPage)
  218. {
  219. $sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
  220. if (!empty($sTemplate))
  221. {
  222. $oTemplate = new DisplayTemplate($sTemplate);
  223. $sNameAttCode = MetaModel::GetNameAttributeCode(get_class($this));
  224. // Note: to preserve backward compatibility with home-made templates, the placeholder '$pkey$' has been preserved
  225. // but the preferred method is to use '$id$'
  226. $oTemplate->Render($oPage, array('class_name'=> MetaModel::GetName(get_class($this)),'class'=> get_class($this), 'pkey'=> $this->GetKey(), 'id'=> $this->GetKey(), 'name' => $this->Get($sNameAttCode)));
  227. }
  228. else
  229. {
  230. // Object's details
  231. // template not found display the object using the *old style*
  232. $this->DisplayBareHeader($oPage);
  233. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  234. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  235. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  236. $this->DisplayBareProperties($oPage);
  237. $this->DisplayBareRelations($oPage);
  238. $oPage->SetCurrentTab(Dict::S('UI:HistoryTab'));
  239. $this->DisplayBareHistory($oPage);
  240. }
  241. }
  242. function DisplayPreview(WebPage $oPage)
  243. {
  244. $aDetails = array();
  245. $sClass = get_class($this);
  246. $aList = MetaModel::GetZListItems($sClass, 'preview');
  247. foreach($aList as $sAttCode)
  248. {
  249. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  250. }
  251. $oPage->details($aDetails);
  252. }
  253. // Comment by Rom: this helper may be used to display objects of class DBObject
  254. // -> I am using this to display the changes history
  255. public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  256. {
  257. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  258. }
  259. //public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
  260. public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  261. {
  262. static $iListId = 0;
  263. $iListId++;
  264. // Initialize and check the parameters
  265. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  266. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  267. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  268. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  269. if (!empty($sLinkageAttribute))
  270. {
  271. if($iLinkedObjectId == 0)
  272. {
  273. // if 'links' mode is requested the id of the object to link to must be specified
  274. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
  275. }
  276. if($sTargetAttr == '')
  277. {
  278. // if 'links' mode is requested the d of the object to link to must be specified
  279. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
  280. }
  281. }
  282. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  283. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  284. $bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
  285. $sHtml = '';
  286. $oAppContext = new ApplicationContext();
  287. $sClassName = $oSet->GetFilter()->GetClass();
  288. $aAttribs = array();
  289. $aList = MetaModel::GetZListItems($sClassName, 'list');
  290. if (!empty($sLinkageAttribute))
  291. {
  292. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  293. // and other objects...
  294. // The display will then group all the attributes related to the link itself:
  295. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  296. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  297. assert(isset($aAttDefs[$sLinkageAttribute]));
  298. $oAttDef = $aAttDefs[$sLinkageAttribute];
  299. assert($oAttDef->IsExternalKey());
  300. // First display all the attributes specific to the link record
  301. foreach($aList as $sLinkAttCode)
  302. {
  303. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  304. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  305. {
  306. $aDisplayList[] = $sLinkAttCode;
  307. }
  308. }
  309. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  310. foreach($aList as $sLinkAttCode)
  311. {
  312. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  313. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  314. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  315. {
  316. $aDisplayList[] = $sLinkAttCode;
  317. }
  318. }
  319. // First display all the attributes specific to the link
  320. // Then display all the attributes linked to the other end of the relationship
  321. $aList = $aDisplayList;
  322. }
  323. if ($bSelectMode)
  324. {
  325. if (!$bSingleSelectMode)
  326. {
  327. $aAttribs['form::select'] = array('label' => "<input type=\"checkbox\" onChange=\"var value = this.checked; $('.selectList{$iListId}').each( function() { this.checked = value; } );\"></input>", 'description' => Dict::S('UI:SelectAllToggle+'));
  328. }
  329. else
  330. {
  331. $aAttribs['form::select'] = array('label' => "", 'description' => '');
  332. }
  333. }
  334. if ($bViewLink)
  335. {
  336. $aAttribs['key'] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  337. }
  338. foreach($aList as $sAttCode)
  339. {
  340. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  341. }
  342. $aValues = array();
  343. $oSet->Seek(0);
  344. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  345. $iMaxObjects = -1;
  346. if ($bDisplayLimit)
  347. {
  348. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  349. {
  350. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  351. }
  352. }
  353. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  354. {
  355. $aRow = array();
  356. if ($bViewLink)
  357. {
  358. $aRow['key'] = $oObj->GetHyperLink();
  359. }
  360. if ($bSelectMode)
  361. {
  362. if ($bSingleSelectMode)
  363. {
  364. $aRow['form::select'] = "<input type=\"radio\" class=\"selectList{$iListId}\" name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  365. }
  366. else
  367. {
  368. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  369. }
  370. }
  371. foreach($aList as $sAttCode)
  372. {
  373. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  374. }
  375. $aValues[] = $aRow;
  376. $iMaxObjects--;
  377. }
  378. $sHtml .= '<table class="listContainer">';
  379. $sColspan = '';
  380. if ($bDisplayMenu)
  381. {
  382. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  383. $sColspan = 'colspan="2"';
  384. $aMenuExtraParams = $aExtraParams;
  385. if (!empty($sLinkageAttribute))
  386. {
  387. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  388. $aMenuExtraParams = $aExtraParams;
  389. }
  390. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  391. {
  392. // list truncated
  393. $divId = $aExtraParams['block_id'];
  394. $sFilter = $oSet->GetFilter()->serialize();
  395. $aExtraParams['display_limit'] = false; // To expand the full list
  396. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  397. $sHtml .= '<tr class="containerHeader"><td>'.Dict::Format('UI:TruncatedResults', utils::GetConfig()->GetMinDisplayLimit(), $oSet->Count()).'&nbsp;&nbsp;<a href="#open_'.$divId.'" onClick="Javascript:ReloadTruncatedList(\''.$divId.'\', \''.$sFilter.'\', \''.$sExtraParams.'\');">'.Dict::S('UI:DisplayAll').'</a></td><td>';
  398. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  399. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  400. }
  401. else
  402. {
  403. // Full list
  404. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  405. }
  406. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  407. $sHtml .= '</td></tr>';
  408. }
  409. $sHtml .= "<tr><td $sColspan>";
  410. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  411. $sHtml .= '</td></tr>';
  412. $sHtml .= '</table>';
  413. return $sHtml;
  414. }
  415. public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  416. {
  417. static $iListId = 0;
  418. $iListId++;
  419. $aList = array();
  420. // Initialize and check the parameters
  421. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  422. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  423. // Check if there is a list of aliases to limit the display to...
  424. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  425. $sHtml = '';
  426. $oAppContext = new ApplicationContext();
  427. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  428. $aAuthorizedClasses = array();
  429. foreach($aClasses as $sAlias => $sClassName)
  430. {
  431. if ((UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES) &&
  432. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  433. {
  434. $aAuthorizedClasses[$sAlias] = $sClassName;
  435. }
  436. }
  437. $aAttribs = array();
  438. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  439. {
  440. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  441. if ($bViewLink)
  442. {
  443. $aAttribs['key_'.$sAlias] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  444. }
  445. foreach($aList[$sClassName] as $sAttCode)
  446. {
  447. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  448. }
  449. }
  450. $aValues = array();
  451. $oSet->Seek(0);
  452. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  453. $iMaxObjects = -1;
  454. if ($bDisplayLimit)
  455. {
  456. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  457. {
  458. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  459. }
  460. }
  461. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  462. {
  463. $aRow = array();
  464. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  465. {
  466. if ($bViewLink)
  467. {
  468. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetHyperLink();
  469. }
  470. foreach($aList[$sClassName] as $sAttCode)
  471. {
  472. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  473. }
  474. }
  475. $aValues[] = $aRow;
  476. $iMaxObjects--;
  477. }
  478. $sHtml .= '<table class="listContainer">';
  479. $sColspan = '';
  480. if ($bDisplayMenu)
  481. {
  482. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  483. $sColspan = 'colspan="2"';
  484. $aMenuExtraParams = $aExtraParams;
  485. if (!empty($sLinkageAttribute))
  486. {
  487. $aMenuExtraParams = $aExtraParams;
  488. }
  489. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  490. {
  491. // list truncated
  492. $divId = $aExtraParams['block_id'];
  493. $sFilter = $oSet->GetFilter()->serialize();
  494. $aExtraParams['display_limit'] = false; // To expand the full list
  495. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  496. $sHtml .= '<tr class="containerHeader"><td>'.Dict::Format('UI:TruncatedResults', utils::GetConfig()->GetMinDisplayLimit(), $oSet->Count()).'&nbsp;&nbsp;<a href="Javascript:ReloadTruncatedList(\''.$divId.'\', \''.$sFilter.'\', \''.$sExtraParams.'\');">'.Dict::S('UI:DisplayAll').'</a></td><td>';
  497. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  498. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  499. }
  500. else
  501. {
  502. // Full list
  503. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  504. }
  505. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  506. $sHtml .= '</td></tr>';
  507. }
  508. $sHtml .= "<tr><td $sColspan>";
  509. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  510. $sHtml .= '</td></tr>';
  511. $sHtml .= '</table>';
  512. return $sHtml;
  513. }
  514. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  515. {
  516. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  517. }
  518. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  519. {
  520. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  521. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  522. $aList = array();
  523. $oAppContext = new ApplicationContext();
  524. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  525. $aAuthorizedClasses = array();
  526. foreach($aClasses as $sAlias => $sClassName)
  527. {
  528. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  529. {
  530. $aAuthorizedClasses[$sAlias] = $sClassName;
  531. }
  532. }
  533. $aAttribs = array();
  534. $aHeader = array();
  535. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  536. {
  537. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode => $oAttDef)
  538. {
  539. if ((($oAttDef->IsExternalField()) || ($oAttDef->IsWritable())) && $oAttDef->IsScalar())
  540. {
  541. $aList[$sClassName][$sAttCode] = $oAttDef;
  542. }
  543. }
  544. $aHeader[] = 'id';
  545. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  546. {
  547. if ($oAttDef->IsExternalField())
  548. {
  549. $sExtKeyLabel = MetaModel::GetLabel($sClassName, $oAttDef->GetKeyAttCode());
  550. $sRemoteAttLabel = MetaModel::GetLabel($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  551. $aHeader[] = $sExtKeyLabel.'->'.$sRemoteAttLabel;
  552. }
  553. else
  554. {
  555. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  556. }
  557. }
  558. }
  559. $sHtml = implode($sSeparator, $aHeader)."\n";
  560. $oSet->Seek(0);
  561. while ($aObjects = $oSet->FetchAssoc())
  562. {
  563. $aRow = array();
  564. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  565. {
  566. $oObj = $aObjects[$sAlias];
  567. $aRow[] = $oObj->GetKey();
  568. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  569. {
  570. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, '\\');
  571. }
  572. }
  573. $sHtml .= implode($sSeparator, $aRow)."\n";
  574. }
  575. return $sHtml;
  576. }
  577. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  578. {
  579. $oAppContext = new ApplicationContext();
  580. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  581. $aAuthorizedClasses = array();
  582. foreach($aClasses as $sAlias => $sClassName)
  583. {
  584. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  585. {
  586. $aAuthorizedClasses[$sAlias] = $sClassName;
  587. }
  588. }
  589. $aAttribs = array();
  590. $aList = array();
  591. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  592. $oPage->add("<Set>\n");
  593. $oSet->Seek(0);
  594. while ($aObjects = $oSet->FetchAssoc())
  595. {
  596. if (count($aAuthorizedClasses) > 1)
  597. {
  598. $oPage->add("<Row>\n");
  599. }
  600. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  601. {
  602. $oObj = $aObjects[$sAlias];
  603. $sClassName = get_class($oObj);
  604. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  605. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  606. {
  607. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()))
  608. {
  609. $sValue = $oObj->GetAsXML($sAttCode);
  610. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  611. }
  612. }
  613. $oPage->add("</$sClassName>\n");
  614. }
  615. if (count($aAuthorizedClasses) > 1)
  616. {
  617. $oPage->add("</Row>\n");
  618. }
  619. }
  620. $oPage->add("</Set>\n");
  621. }
  622. // By rom
  623. function DisplayChangesLog(WebPage $oPage)
  624. {
  625. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  626. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  627. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  628. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  629. $count = $oSet->Count();
  630. if ($count > 0)
  631. {
  632. $oPage->p(Dict::Format('UI:ChangesLogTitle', $count));
  633. self::DisplaySet($oPage, $oSet);
  634. }
  635. else
  636. {
  637. $oPage->p(Dict::S('UI:EmptyChangesLogTitle'));
  638. }
  639. }
  640. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  641. {
  642. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  643. }
  644. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  645. {
  646. static $iSearchFormId = 0;
  647. $oAppContext = new ApplicationContext();
  648. $sHtml = '';
  649. $numCols=4;
  650. $sClassName = $oSet->GetFilter()->GetClass();
  651. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  652. // (especially when used to add a link)
  653. /*
  654. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  655. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:OQLQueryTab')."</a></li>
  656. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:SimpleSearchTab')."</a></li>
  657. </ul></div>\n";
  658. */
  659. // Simple search form
  660. if (isset($aExtraParams['currentId']))
  661. {
  662. $sSearchFormId = $aExtraParams['currentId'];
  663. $iSearchFormId++;
  664. }
  665. else
  666. {
  667. $iSearchFormId++;
  668. $sSearchFormId = 'SimpleSearchForm'.$iSearchFormId;
  669. $sHtml .= "<div id=\"$sSearchFormId\" class=\"mini_tab{$iSearchFormId}\">\n";
  670. }
  671. // Check if the current class has some sub-classes
  672. if (isset($aExtraParams['baseClass']))
  673. {
  674. $sRootClass = $aExtraParams['baseClass'];
  675. }
  676. else
  677. {
  678. $sRootClass = $sClassName;
  679. }
  680. $aSubClasses = MetaModel::GetSubclasses($sRootClass);
  681. if (count($aSubClasses) > 0)
  682. {
  683. $aOptions = array();
  684. $aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"$sRootClass\">".MetaModel::GetName($sRootClass)."</options>\n";
  685. foreach($aSubClasses as $sSubclassName)
  686. {
  687. $aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"$sSubclassName\">".MetaModel::GetName($sSubclassName)."</options>\n";
  688. }
  689. $aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"$sClassName\">".MetaModel::GetName($sClassName)."</options>\n";
  690. ksort($aOptions);
  691. $sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('$sSearchFormId', this.value, '$sRootClass')\">\n".implode('', $aOptions)."</select>\n";
  692. }
  693. else
  694. {
  695. $sClassesCombo = MetaModel::GetName($sClassName);
  696. }
  697. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  698. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  699. $sHtml .= "<h2>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</h2>\n";
  700. $index = 0;
  701. $sHtml .= "<table>\n";
  702. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  703. $aMapCriteria = array();
  704. foreach($aFilterCriteria as $aCriteria)
  705. {
  706. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  707. }
  708. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  709. foreach($aList as $sFilterCode)
  710. {
  711. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  712. if (($index % $numCols) == 0)
  713. {
  714. if ($index != 0)
  715. {
  716. $sHtml .= "</tr>\n";
  717. }
  718. $sHtml .= "<tr>\n";
  719. }
  720. $sFilterValue = '';
  721. $sFilterValue = utils::ReadParam($sFilterCode, '');
  722. $sFilterOpCode = null; // Use the default 'loose' OpCode
  723. if (empty($sFilterValue))
  724. {
  725. if (isset($aMapCriteria[$sFilterCode]))
  726. {
  727. if (count($aMapCriteria[$sFilterCode]) > 1)
  728. {
  729. $sFilterValue = Dict::S('UI:SearchValue:Mixed');
  730. }
  731. else
  732. {
  733. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  734. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  735. }
  736. if ($sFilterCode != 'company')
  737. {
  738. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  739. }
  740. }
  741. }
  742. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  743. if ($aAllowedValues != null)
  744. {
  745. //Enum field or external key, display a combo
  746. $sValue = "<select name=\"$sFilterCode\">\n";
  747. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  748. foreach($aAllowedValues as $key => $value)
  749. {
  750. if ($sFilterValue == $key)
  751. {
  752. $sSelected = ' selected';
  753. }
  754. else
  755. {
  756. $sSelected = '';
  757. }
  758. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  759. }
  760. $sValue .= "</select>\n";
  761. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  762. }
  763. else
  764. {
  765. // Any value is possible, display an input box
  766. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  767. }
  768. $index++;
  769. }
  770. if (($index % $numCols) != 0)
  771. {
  772. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  773. }
  774. $sHtml .= "</tr>\n";
  775. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></td></tr>\n";
  776. $sHtml .= "</table>\n";
  777. foreach($aExtraParams as $sName => $sValue)
  778. {
  779. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  780. }
  781. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  782. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  783. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  784. $sHtml .= $oAppContext->GetForForm();
  785. $sHtml .= "</form>\n";
  786. if (!isset($aExtraParams['currentId']))
  787. {
  788. $sHtml .= "</div><!-- Simple search form -->\n";
  789. }
  790. // OQL query builder
  791. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  792. $sHtml .= "<h1>".Dict::S('UI:OQLQueryBuilderTitle')."</h1>\n";
  793. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  794. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  795. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  796. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  797. $sOQLClause = utils::ReadParam('oql_clause', '');
  798. asort($aClasses);
  799. foreach($aClasses as $sChildClass)
  800. {
  801. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  802. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  803. }
  804. $sHtml .= "</select>&nbsp;</td><td>\n";
  805. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  806. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Query')."\"></td></tr>\n";
  807. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  808. foreach($aExtraParams as $sName => $sValue)
  809. {
  810. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  811. }
  812. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  813. $sHtml .= $oAppContext->GetForForm();
  814. $sHtml .= "</table></form>\n";
  815. $sHtml .= "</div><!-- OQL query form -->\n";
  816. return $sHtml;
  817. }
  818. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  819. {
  820. static $iInputId = 0;
  821. if (isset($aArgs[$sAttCode]) && empty($value))
  822. {
  823. // default value passed by the context (either the app context of the operation)
  824. $value = $aArgs[$sAttCode];
  825. }
  826. if (!empty($iId))
  827. {
  828. $iInputId = $iId;
  829. }
  830. else
  831. {
  832. $iInputId++;
  833. $iId = $iInputId;
  834. }
  835. if (!$oAttDef->IsExternalField())
  836. {
  837. $aCSSClasses = array();
  838. $bMandatory = 0;
  839. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  840. {
  841. $aCSSClasses[] = 'mandatory';
  842. $bMandatory = 1;
  843. }
  844. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  845. $sValidationField = "<span id=\"v_{$iId}\"></span>";
  846. $sHelpText = $oAttDef->GetHelpOnEdition();
  847. $aEventsList = array('validate');
  848. switch($oAttDef->GetEditClass())
  849. {
  850. case 'Date':
  851. case 'DateTime':
  852. $aEventsList[] ='keyup';
  853. $aEventsList[] ='change';
  854. $sHTMLValue = "<input title=\"$sHelpText\" class=\"date-pick\" type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  855. break;
  856. case 'Password':
  857. $aEventsList[] ='keyup';
  858. $aEventsList[] ='change';
  859. $sHTMLValue = "<input title=\"$sHelpText\" type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  860. break;
  861. case 'Text':
  862. $aEventsList[] ='keypress';
  863. $aEventsList[] ='change';
  864. $sHTMLValue = "<textarea class=\"resizable\" title=\"$sHelpText\" name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iId\">$value</textarea>&nbsp;{$sValidationField}";
  865. break;
  866. case 'LinkedSet':
  867. $aEventsList[] ='change';
  868. $oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix);
  869. $sHTMLValue = $oWidget->Display($oPage, $value);
  870. break;
  871. case 'Document':
  872. $aEventsList[] ='change';
  873. $oDocument = $value; // Value is an ormDocument object
  874. $sFileName = '';
  875. if (is_object($oDocument))
  876. {
  877. $sFileName = $oDocument->GetFileName();
  878. }
  879. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  880. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  881. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iId\" \" value=\"$sFileName\"/>\n";
  882. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  883. $sHTMLValue .= "<input title=\"$sHelpText\" name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iId\" onChange=\"UpdateFileName('$iId', this.value)\"/>&nbsp;{$sValidationField}\n";
  884. break;
  885. case 'List':
  886. // Not editable for now...
  887. break;
  888. case 'String':
  889. default:
  890. // #@# todo - add context information (depending on dimensions)
  891. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  892. if ($aAllowedValues !== null)
  893. {
  894. //Enum field or external key, display a combo
  895. //if (count($aAllowedValues) == 0)
  896. //{
  897. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  898. //}
  899. //else if (count($aAllowedValues) > 50)
  900. if (count($aAllowedValues) > 50)
  901. {
  902. // too many choices, use an autocomplete
  903. // The input for the auto complete
  904. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>&nbsp;{$sValidationField}";
  905. // another hidden input to store & pass the object's Id
  906. $sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  907. $oPage->add_ready_script("\$('#label_$iId').autocomplete('./ajax.render.php', { scroll:true, minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#$iId', extraParams:{operation:'autocomplete', sclass:'$sClass',attCode:'".$sAttCode."'}});");
  908. $oPage->add_ready_script("\$('#label_$iId').result( function(event, data, formatted) { if (data) { $('#{$iId}').val(data[1]); } } );");
  909. $aEventsList[] ='change';
  910. }
  911. else
  912. {
  913. // Few choices, use a normal 'select'
  914. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  915. $sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iId\">\n";
  916. $sHTMLValue .= "<option value=\"0\">".Dict::S('UI:SelectOne')."</option>\n";
  917. foreach($aAllowedValues as $key => $display_value)
  918. {
  919. $sSelected = ($value == $key) ? ' selected' : '';
  920. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  921. }
  922. $sHTMLValue .= "</select>&nbsp;{$sValidationField}\n";
  923. $aEventsList[] ='change';
  924. }
  925. }
  926. else
  927. {
  928. $sHTMLValue = "<input title=\"$sHelpText\" type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  929. $aEventsList[] ='keyup';
  930. $aEventsList[] ='change';
  931. }
  932. break;
  933. }
  934. $sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
  935. $oPage->add_ready_script("$('#$iId').bind('".implode(' ', $aEventsList)."', function(evt, sFormId) { return ValidateField('$iId', '$sPattern', $bMandatory, sFormId) } );"); // Bind to a custom event: validate
  936. $aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
  937. if (count($aDependencies) > 0)
  938. {
  939. $oPage->add_ready_script("$('#$iId').bind('change', function(evt, sFormId) { return UpdateDependentFields(['".implode("','", $aDependencies)."']) } );"); // Bind to a custom event: validate
  940. }
  941. }
  942. return "<div>{$sHTMLValue}</div>";
  943. }
  944. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  945. {
  946. static $iFormId = 0;
  947. $iFormId++;
  948. $sClass = get_class($this);
  949. $oAppContext = new ApplicationContext();
  950. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  951. $iKey = $this->GetKey();
  952. $aDetails = array();
  953. $aFieldsMap = array();
  954. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckFields('form_{$iFormId}', true)\">\n");
  955. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  956. $aFullList = MetaModel::ListAttributeDefs($sClass);
  957. $aList = $aDetailsList;
  958. // Compute the list of properties to display, first the attributes in the 'details' list, then
  959. // all the remaining attributes that are not external fields
  960. foreach($aFullList as $sAttCode => $void)
  961. {
  962. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  963. if (!in_array($sAttCode, $aDetailsList) && (!$oAttDef->IsExternalField()))
  964. {
  965. $aList[] = $sAttCode;
  966. }
  967. }
  968. foreach($aList as $sAttCode)
  969. {
  970. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  971. $iFlags = $this->GetAttributeFlags($sAttCode);
  972. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  973. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  974. {
  975. if ($oAttDef->IsWritable())
  976. {
  977. if ($sStateAttCode == $sAttCode)
  978. {
  979. // State attribute is always read-only from the UI
  980. $sHTMLValue = $this->GetStateLabel();
  981. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  982. }
  983. else
  984. {
  985. $iFlags = $this->GetAttributeFlags($sAttCode);
  986. if ($iFlags & OPT_ATT_HIDDEN)
  987. {
  988. // Attribute is hidden, do nothing
  989. }
  990. else
  991. {
  992. if ($iFlags & OPT_ATT_READONLY)
  993. {
  994. // Attribute is read-only
  995. $sHTMLValue = $this->GetAsHTML($sAttCode);
  996. }
  997. else
  998. {
  999. $sValue = $this->Get($sAttCode);
  1000. $sDisplayValue = $this->GetEditValue($sAttCode);
  1001. $aArgs = array('this' => $this);
  1002. $sInputId = $iFormId.'_'.$sAttCode;
  1003. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1004. $aFieldsMap[$sAttCode] = $sInputId;
  1005. }
  1006. $aDetails[] = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue);
  1007. }
  1008. }
  1009. }
  1010. else
  1011. {
  1012. $aDetails[] = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $this->GetAsHTML($sAttCode));
  1013. }
  1014. }
  1015. }
  1016. $oPage->details($aDetails);
  1017. // Now display the relations, one tab per relation
  1018. $oPage->AddTabContainer('Related Objects');
  1019. $oPage->SetCurrentTabContainer('Related Objects');
  1020. foreach($aList as $sAttCode)
  1021. {
  1022. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1023. if ($oAttDef->IsLinkset())
  1024. {
  1025. $oPage->SetCurrentTab($oAttDef->GetLabel());
  1026. $oPage->p($oAttDef->GetDescription());
  1027. if (get_class($oAttDef) == 'AttributeLinkedSet')
  1028. {
  1029. $sTargetClass = $oAttDef->GetLinkedClass();
  1030. $oFilter = new DBObjectSearch($sTargetClass);
  1031. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey());
  1032. $oBlock = new DisplayBlock($oFilter, 'list', false);
  1033. $oBlock->Display($oPage, 0);
  1034. }
  1035. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  1036. {
  1037. $sValue = $this->Get($sAttCode);
  1038. $sDisplayValue = $this->GetEditValue($sAttCode);
  1039. $aArgs = array('this' => $this);
  1040. $sInputId = $iFormId.'_'.$sAttCode;
  1041. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1042. $aFieldsMap[$sAttCode] = $sInputId;
  1043. $oPage->add($sHTMLValue);
  1044. }
  1045. }
  1046. }
  1047. $oPage->SetCurrentTab('');
  1048. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  1049. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1050. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  1051. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1052. foreach($aExtraParams as $sName => $value)
  1053. {
  1054. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1055. }
  1056. $oPage->add($oAppContext->GetForForm());
  1057. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"BackToDetails('$sClass', $iKey)\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1058. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  1059. $oPage->add("</form>\n");
  1060. $iFieldsCount = count($aFieldsMap);
  1061. $sJsonFieldsMap = json_encode($aFieldsMap);
  1062. $oPage->add_script(
  1063. <<<EOF
  1064. // Initializes the object once at the beginning of the page...
  1065. var oWizardHelper = new WizardHelper('$sClass');
  1066. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  1067. oWizardHelper.SetFieldsCount($iFieldsCount);
  1068. EOF
  1069. );
  1070. $oPage->add_ready_script(
  1071. <<<EOF
  1072. // Initializes the object once at the beginning of the page...
  1073. CheckFields('form_{$iFormId}', false);
  1074. EOF
  1075. );
  1076. }
  1077. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  1078. {
  1079. static $iCreationFormId = 0;
  1080. $iCreationFormId++;
  1081. $iFieldIndex = 0;
  1082. $oAppContext = new ApplicationContext();
  1083. $aDetails = array();
  1084. $aFieldsMap = array();
  1085. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  1086. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  1087. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1088. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" enctype=\"multipart/form-data\" onSubmit=\"return CheckFields('creation_form_{$iCreationFormId}', true)\">\n");
  1089. $aStates = MetaModel::EnumStates($sClass);
  1090. if ($oObjectToClone == null)
  1091. {
  1092. $sTargetState = MetaModel::GetDefaultState($sClass);
  1093. }
  1094. else
  1095. {
  1096. $sTargetState = $oObjectToClone->GetState();
  1097. }
  1098. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  1099. $aFullList = MetaModel::ListAttributeDefs($sClass);
  1100. $aList = $aDetailsList;
  1101. // Compute the list of properties to display, first the attributes in the 'details' list, then
  1102. // all the remaining attributes that are not external fields
  1103. foreach($aFullList as $sAttCode => $void)
  1104. {
  1105. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1106. if (!in_array($sAttCode, $aDetailsList) && (!$oAttDef->IsExternalField()))
  1107. {
  1108. $aList[] = $sAttCode;
  1109. }
  1110. }
  1111. foreach($aList as $sAttCode)
  1112. {
  1113. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1114. $iFlags = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1115. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  1116. {
  1117. if ($oAttDef->IsWritable())
  1118. {
  1119. if ($oObjectToClone != null)
  1120. {
  1121. $sValue = $oObjectToClone->GetEditValue($sAttCode);
  1122. $aArgs['this'] = $oObjectToClone;
  1123. }
  1124. else
  1125. {
  1126. if(isset($aArgs['default'][$sAttCode]))
  1127. {
  1128. $sValue = $aArgs['default'][$sAttCode];
  1129. }
  1130. else
  1131. {
  1132. $sValue = $oAttDef->GetDefaultValue();
  1133. }
  1134. }
  1135. // Prepopulate with a default value -- but no display value...
  1136. $sDisplayValue = '';
  1137. if (!empty($sValue))
  1138. {
  1139. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs, '');
  1140. switch (count($aAllowedValues))
  1141. {
  1142. case 1:
  1143. case 0:
  1144. $sDisplayValue = $sValue;
  1145. break;
  1146. default:
  1147. $sDisplayValue = $sValue;
  1148. foreach($aAllowedValues as $key => $display)
  1149. {
  1150. if ($key == $sValue)
  1151. {
  1152. $sDisplayValue = $display;
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. }
  1158. if ($sStateAttCode == $sAttCode)
  1159. {
  1160. // State attribute is always read-only from the UI
  1161. $sHTMLValue = MetaModel::GetStateLabel($sClass, $sTargetState);
  1162. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1163. }
  1164. else
  1165. {
  1166. if ($iFlags & OPT_ATT_HIDDEN)
  1167. {
  1168. // Attribute is hidden, do nothing
  1169. }
  1170. else
  1171. {
  1172. if ($iFlags & OPT_ATT_READONLY)
  1173. {
  1174. // Attribute is read-only
  1175. $sHTMLValue = ($oObjectToClone == null) ? $sDisplayValue : $oObjectToClone->GetAsHTML($sAttCode);
  1176. }
  1177. else
  1178. {
  1179. $sFieldId = 'att_'.$iFieldIndex;
  1180. $sHTMLValue = "<div id=\"field_{$sFieldId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sFieldId, '', $iFlags, $aArgs)."</div>";
  1181. $aFieldsMap[$sFieldId] = $sAttCode;
  1182. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1183. $iFieldIndex++;
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. $oPage->details($aDetails);
  1191. // Now display the relations, one tab per relation
  1192. $oPage->AddTabContainer('Related Objects');
  1193. $oPage->SetCurrentTabContainer('Related Objects');
  1194. foreach($aList as $sAttCode)
  1195. {
  1196. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1197. if ($oAttDef->IsLinkset())
  1198. {
  1199. $oPage->SetCurrentTab($oAttDef->GetLabel());
  1200. $oPage->p($oAttDef->GetDescription());
  1201. $iFlags = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1202. $sFieldId = 'att_'.$iFieldIndex;
  1203. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  1204. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetEditValue($sAttCode);
  1205. $iFlags = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1206. $sHTMLValue = "<div id=\"field_{$sFieldId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sFieldId, '', $iFlags, $aArgs)."</div>";
  1207. $aFieldsMap[$sFieldId] = $sAttCode;
  1208. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1209. $iFieldIndex++;
  1210. $oPage->add($sHTMLValue);
  1211. }
  1212. }
  1213. $oPage->SetCurrentTab('');
  1214. if ($oObjectToClone != null)
  1215. {
  1216. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  1217. }
  1218. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1219. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  1220. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1221. $oPage->add($oAppContext->GetForForm());
  1222. foreach($aExtraParams as $sName => $value)
  1223. {
  1224. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1225. }
  1226. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1227. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  1228. $oPage->add("</form>\n");
  1229. $aNewFieldsMap = array();
  1230. foreach($aFieldsMap as $id => $sFieldCode)
  1231. {
  1232. $aNewFieldsMap[$sFieldCode] = $id;
  1233. }
  1234. $iFieldsCount = count($aFieldsMap);
  1235. $sJsonFieldsMap = json_encode($aNewFieldsMap);
  1236. $oPage->add_script("
  1237. // Initializes the object once at the beginning of the page...
  1238. var oWizardHelper = new WizardHelper('$sClass');
  1239. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  1240. oWizardHelper.SetFieldsCount($iFieldsCount);");
  1241. $oPage->add_ready_script("CheckFields('creation_form_{$iCreationFormId}', false);");
  1242. }
  1243. protected static function GetCSSClasses($aCSSClasses)
  1244. {
  1245. $sCSSClasses = '';
  1246. if (!empty($aCSSClasses))
  1247. {
  1248. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  1249. }
  1250. return $sCSSClasses;
  1251. }
  1252. protected static function ProcessZlist($aList, $aDetails, $sCurrentTab, $sCurrentCol, $sCurrentSet)
  1253. {
  1254. //echo "<pre>ZList: ";
  1255. //print_r($aList);
  1256. //echo "</pre>\n";
  1257. foreach($aList as $sKey => $value)
  1258. {
  1259. if (is_array($value))
  1260. {
  1261. if (preg_match('/^(.*):(.*)$/U', $sKey, $aMatches))
  1262. {
  1263. $sCode = $aMatches[1];
  1264. $sName = $aMatches[2];
  1265. switch($sCode)
  1266. {
  1267. case 'tab':
  1268. //echo "<p>Found a tab: $sName ($sKey)</p>\n";
  1269. if(!isset($aDetails[$sName]))
  1270. {
  1271. $aDetails[$sName] = array('col1' => array('' => array()));
  1272. }
  1273. $aDetails = self::ProcessZlist($value, $aDetails, $sName, 'col1', '');
  1274. break;
  1275. case 'fieldset':
  1276. //echo "<p>Found a fieldset: $sName ($sKey)</p>\n";
  1277. if(!isset($aDetailsStruct[$sCurrentTab][$sCurrentCol][$sName]))
  1278. {
  1279. $aDetails[$sCurrentTab][$sCurrentCol][$sName] = array();
  1280. }
  1281. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sCurrentCol, $sName);
  1282. break;
  1283. default:
  1284. case 'col':
  1285. //echo "<p>Found a column: $sName ($sKey)</p>\n";
  1286. if(!isset($aDetails[$sCurrentTab][$sName]))
  1287. {
  1288. $aDetails[$sCurrentTab][$sName] = array('' => array());
  1289. }
  1290. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sName, '');
  1291. break;
  1292. }
  1293. }
  1294. }
  1295. else
  1296. {
  1297. //echo "<p>Scalar value: $value, in [$sCurrentTab][$sCurrentCol][$sCurrentSet][]</p>\n";
  1298. $aDetails[$sCurrentTab][$sCurrentCol][$sCurrentSet][] = $value;
  1299. }
  1300. }
  1301. return $aDetails;
  1302. }
  1303. protected function GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode)
  1304. {
  1305. $retVal = null;
  1306. $iFlags = $this->GetAttributeFlags($sAttCode);
  1307. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1308. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  1309. {
  1310. // The field is visible in the current state of the object
  1311. if ($sStateAttCode == $sAttCode)
  1312. {
  1313. // Special display for the 'state' attribute itself
  1314. $sDisplayValue = $this->GetStateLabel();
  1315. }
  1316. else
  1317. {
  1318. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1319. }
  1320. $retVal = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1321. }
  1322. return $retVal;
  1323. }
  1324. }
  1325. ?>