cmdbabstract.class.inc.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. require_once('../core/cmdbobject.class.inc.php');
  26. require_once('../application/utils.inc.php');
  27. require_once('../application/applicationcontext.class.inc.php');
  28. require_once('../application/ui.linkswidget.class.inc.php');
  29. abstract class cmdbAbstractObject extends CMDBObject
  30. {
  31. public static function GetUIPage()
  32. {
  33. return '../pages/UI.php';
  34. }
  35. public static function ComputeUIPage($sClass)
  36. {
  37. static $aUIPagesCache = array(); // Cache to store the php page used to display each class of object
  38. if (!isset($aUIPagesCache[$sClass]))
  39. {
  40. $UIPage = false;
  41. if (is_callable("$sClass::GetUIPage"))
  42. {
  43. $UIPage = eval("return $sClass::GetUIPage();"); // May return false in case of error
  44. }
  45. $aUIPagesCache[$sClass] = $UIPage === false ? './UI.php' : $UIPage;
  46. }
  47. $sPage = $aUIPagesCache[$sClass];
  48. return $sPage;
  49. }
  50. protected static function MakeHyperLink($sObjClass, $sObjKey, $aAvailableFields)
  51. {
  52. if ($sObjKey <= 0) return '<em>'.Dict::S('UI:UndefinedObject').'</em>'; // Objects built in memory have negative IDs
  53. $oAppContext = new ApplicationContext();
  54. $sExtClassNameAtt = MetaModel::GetNameAttributeCode($sObjClass);
  55. $sPage = self::ComputeUIPage($sObjClass);
  56. $sAbsoluteUrl = utils::GetAbsoluteUrl(false); // False => Don't get the query string
  57. $sAbsoluteUrl = substr($sAbsoluteUrl, 0, 1+strrpos($sAbsoluteUrl, '/')); // remove the current page, keep just the path, up to the last /
  58. // Use the "name" of the target class as the label of the hyperlink
  59. // unless it's not available in the external attributes...
  60. if (isset($aAvailableFields[$sExtClassNameAtt]))
  61. {
  62. $sLabel = $aAvailableFields[$sExtClassNameAtt];
  63. }
  64. else
  65. {
  66. $sLabel = implode(' / ', $aAvailableFields);
  67. }
  68. // Safety belt
  69. //
  70. if (empty($sLabel))
  71. {
  72. // Developer's note:
  73. // This is doing the job for you, but that is just there in case
  74. // the external fields associated to the external key are blanks
  75. // The ultimate solution will be to query the name automatically
  76. // and independantly from the data model (automatic external field)
  77. // AND make the name be a mandatory field
  78. //
  79. $sObject = MetaModel::GetObject($sObjClass, $sObjKey);
  80. $sLabel = $sObject->GetDisplayName();
  81. }
  82. // Safety net
  83. //
  84. if (empty($sLabel))
  85. {
  86. $sLabel = MetaModel::GetName($sObjClass)." #$sObjKey";
  87. }
  88. $sHint = MetaModel::GetName($sObjClass)."::$sObjKey";
  89. return "<a href=\"{$sAbsoluteUrl}{$sPage}?operation=details&class=$sObjClass&id=$sObjKey&".$oAppContext->GetForLink()."\" title=\"$sHint\">$sLabel</a>";
  90. }
  91. function DisplayBareHeader(WebPage $oPage)
  92. {
  93. // Standard Header with name, actions menu and history block
  94. //
  95. $oPage->add("<div class=\"page_header\">\n");
  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("<h1>".MetaModel::GetName(get_class($this)).": <span class=\"hilite\">".$this->GetDisplayName()."</span></h1>\n");
  102. // history block (with toggle)
  103. $oHistoryFilter = new DBObjectSearch('CMDBChangeOp');
  104. $oHistoryFilter->AddCondition('objkey', $this->GetKey());
  105. $oHistoryFilter->AddCondition('objclass', get_class($this));
  106. $oBlock = new HistoryBlock($oHistoryFilter, 'toggle', false);
  107. $oBlock->Display($oPage, -1);
  108. $oPage->add("</div>\n");
  109. }
  110. function DisplayBareDetails(WebPage $oPage)
  111. {
  112. $oPage->add($this->GetBareDetails($oPage));
  113. }
  114. function DisplayBareRelations(WebPage $oPage)
  115. {
  116. // Related objects
  117. $oPage->AddTabContainer('Related Objects');
  118. $oPage->SetCurrentTabContainer('Related Objects');
  119. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  120. {
  121. if ((get_class($oAttDef) == 'AttributeLinkedSetIndirect') || (get_class($oAttDef) == 'AttributeLinkedSet'))
  122. {
  123. $oPage->SetCurrentTab($oAttDef->GetLabel());
  124. $oPage->p($oAttDef->GetDescription());
  125. if (get_class($oAttDef) == 'AttributeLinkedSet')
  126. {
  127. $sTargetClass = $oAttDef->GetLinkedClass();
  128. $oFilter = new DBObjectSearch($sTargetClass);
  129. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey()); // @@@ condition has same name as field ??
  130. $oBlock = new DisplayBlock($oFilter, 'list', false);
  131. $oBlock->Display($oPage, 0);
  132. }
  133. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  134. {
  135. $sLinkClass = $oAttDef->GetLinkedClass();
  136. // Transform the DBObjectSet into a CMBDObjectSet !!!
  137. $aLinkedObjects = $this->Get($sAttCode)->ToArray(false);
  138. if (count($aLinkedObjects) > 0)
  139. {
  140. $oSet = CMDBObjectSet::FromArray($sLinkClass, $aLinkedObjects);
  141. $aParams = array(
  142. 'link_attr' => $oAttDef->GetExtKeyToMe(),
  143. 'object_id' => $this->GetKey(),
  144. 'target_attr' => $oAttDef->GetExtKeyToRemote(),
  145. );
  146. self::DisplaySet($oPage, $oSet, $aParams);
  147. }
  148. }
  149. }
  150. }
  151. $oPage->SetCurrentTab('');
  152. }
  153. function GetDisplayName()
  154. {
  155. $sDisplayName = '';
  156. if (MetaModel::GetNameAttributeCode(get_class($this)) != '')
  157. {
  158. $sDisplayName = $this->GetAsHTML(MetaModel::GetNameAttributeCode(get_class($this)));
  159. }
  160. return $sDisplayName;
  161. }
  162. function GetBareDetails(WebPage $oPage)
  163. {
  164. $sHtml = '';
  165. $oAppContext = new ApplicationContext();
  166. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  167. $aDetails = array();
  168. $sClass = get_class($this);
  169. $aList = MetaModel::GetZListItems($sClass, 'details');
  170. foreach($aList as $sAttCode)
  171. {
  172. $iFlags = $this->GetAttributeFlags($sAttCode);
  173. if ( ($iFlags & OPT_ATT_HIDDEN) == 0)
  174. {
  175. // The field is visible in the current state of the object
  176. if ($sStateAttCode == $sAttCode)
  177. {
  178. // Special display for the 'state' attribute itself
  179. $sDisplayValue = $this->GetStateLabel();
  180. }
  181. else
  182. {
  183. $sDisplayValue = $this->GetAsHTML($sAttCode);
  184. }
  185. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  186. }
  187. }
  188. $sHtml .= $oPage->GetDetails($aDetails);
  189. // Documents displayed inline (when possible: images, html...)
  190. foreach($aList as $sAttCode)
  191. {
  192. $oAttDef = Metamodel::GetAttributeDef($sClass, $sAttCode);
  193. if ( $oAttDef->GetEditClass() == 'Document')
  194. {
  195. $oDoc = $this->Get($sAttCode);
  196. if (is_object($oDoc) && !$oDoc->IsEmpty())
  197. {
  198. $sHtml .= "<p>".Dict::Format('UI:Document:OpenInNewWindow:Download', $oDoc->GetDisplayLink($sClass, $this->GetKey(), $sAttCode), $oDoc->GetDownloadLink($sClass, $this->GetKey(), $sAttCode))."</p>\n";
  199. $sHtml .= "<div>".$oDoc->GetDisplayInline($sClass, $this->GetKey(), $sAttCode)."</div>\n";
  200. }
  201. }
  202. }
  203. return $sHtml;
  204. }
  205. function DisplayDetails(WebPage $oPage)
  206. {
  207. $sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
  208. if (!empty($sTemplate))
  209. {
  210. $oTemplate = new DisplayTemplate($sTemplate);
  211. $sNameAttCode = MetaModel::GetNameAttributeCode(get_class($this));
  212. // Note: to preserve backward compatibility with home-made templates, the placeholder '$pkey$' has been preserved
  213. // but the preferred method is to use '$id$'
  214. $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)));
  215. }
  216. else
  217. {
  218. // Object's details
  219. // template not found display the object using the *old style*
  220. $this->DisplayBareHeader($oPage);
  221. $this->DisplayBareDetails($oPage);
  222. $this->DisplayBareRelations($oPage);
  223. }
  224. }
  225. function DisplayPreview(WebPage $oPage)
  226. {
  227. $aDetails = array();
  228. $sClass = get_class($this);
  229. $aList = MetaModel::GetZListItems($sClass, 'preview');
  230. foreach($aList as $sAttCode)
  231. {
  232. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  233. }
  234. $oPage->details($aDetails);
  235. }
  236. // Comment by Rom: this helper may be used to display objects of class DBObject
  237. // -> I am using this to display the changes history
  238. public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  239. {
  240. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  241. }
  242. //public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
  243. public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  244. {
  245. static $iListId = 0;
  246. $iListId++;
  247. // Initialize and check the parameters
  248. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  249. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  250. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  251. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  252. if (!empty($sLinkageAttribute))
  253. {
  254. if($iLinkedObjectId == 0)
  255. {
  256. // if 'links' mode is requested the id of the object to link to must be specified
  257. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
  258. }
  259. if($sTargetAttr == '')
  260. {
  261. // if 'links' mode is requested the d of the object to link to must be specified
  262. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
  263. }
  264. }
  265. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  266. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  267. $bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
  268. $sHtml = '';
  269. $oAppContext = new ApplicationContext();
  270. $sClassName = $oSet->GetFilter()->GetClass();
  271. $aAttribs = array();
  272. $aList = MetaModel::GetZListItems($sClassName, 'list');
  273. if (!empty($sLinkageAttribute))
  274. {
  275. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  276. // and other objects...
  277. // The display will then group all the attributes related to the link itself:
  278. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  279. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  280. assert(isset($aAttDefs[$sLinkageAttribute]));
  281. $oAttDef = $aAttDefs[$sLinkageAttribute];
  282. assert($oAttDef->IsExternalKey());
  283. // First display all the attributes specific to the link record
  284. foreach($aList as $sLinkAttCode)
  285. {
  286. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  287. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  288. {
  289. $aDisplayList[] = $sLinkAttCode;
  290. }
  291. }
  292. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  293. foreach($aList as $sLinkAttCode)
  294. {
  295. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  296. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  297. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  298. {
  299. $aDisplayList[] = $sLinkAttCode;
  300. }
  301. }
  302. // First display all the attributes specific to the link
  303. // Then display all the attributes linked to the other end of the relationship
  304. $aList = $aDisplayList;
  305. }
  306. if ($bSelectMode)
  307. {
  308. if (!$bSingleSelectMode)
  309. {
  310. $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+'));
  311. }
  312. else
  313. {
  314. $aAttribs['form::select'] = array('label' => "", 'description' => '');
  315. }
  316. }
  317. if ($bViewLink)
  318. {
  319. $aAttribs['key'] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  320. }
  321. foreach($aList as $sAttCode)
  322. {
  323. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  324. }
  325. $aValues = array();
  326. $oSet->Seek(0);
  327. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  328. $iMaxObjects = -1;
  329. if ($bDisplayLimit)
  330. {
  331. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  332. {
  333. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  334. }
  335. }
  336. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  337. {
  338. $aRow = array();
  339. if ($bViewLink)
  340. {
  341. $aRow['key'] = $oObj->GetHyperLink();
  342. }
  343. if ($bSelectMode)
  344. {
  345. if ($bSingleSelectMode)
  346. {
  347. $aRow['form::select'] = "<input type=\"radio\" class=\"selectList{$iListId}\" name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  348. }
  349. else
  350. {
  351. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  352. }
  353. }
  354. foreach($aList as $sAttCode)
  355. {
  356. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  357. }
  358. $aValues[] = $aRow;
  359. $iMaxObjects--;
  360. }
  361. $sHtml .= '<table class="listContainer">';
  362. $sColspan = '';
  363. if ($bDisplayMenu)
  364. {
  365. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  366. $sColspan = 'colspan="2"';
  367. $aMenuExtraParams = $aExtraParams;
  368. if (!empty($sLinkageAttribute))
  369. {
  370. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  371. $aMenuExtraParams = $aExtraParams;
  372. }
  373. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  374. {
  375. // list truncated
  376. $divId = $aExtraParams['block_id'];
  377. $sFilter = $oSet->GetFilter()->serialize();
  378. $aExtraParams['display_limit'] = false; // To expand the full list
  379. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  380. $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>';
  381. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  382. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  383. }
  384. else
  385. {
  386. // Full list
  387. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  388. }
  389. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  390. $sHtml .= '</td></tr>';
  391. }
  392. $sHtml .= "<tr><td $sColspan>";
  393. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  394. $sHtml .= '</td></tr>';
  395. $sHtml .= '</table>';
  396. return $sHtml;
  397. }
  398. public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  399. {
  400. static $iListId = 0;
  401. $iListId++;
  402. $aList = array();
  403. // Initialize and check the parameters
  404. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  405. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  406. // Check if there is a list of aliases to limit the display to...
  407. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  408. $sHtml = '';
  409. $oAppContext = new ApplicationContext();
  410. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  411. $aAuthorizedClasses = array();
  412. foreach($aClasses as $sAlias => $sClassName)
  413. {
  414. if ((UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES) &&
  415. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  416. {
  417. $aAuthorizedClasses[$sAlias] = $sClassName;
  418. }
  419. }
  420. $aAttribs = array();
  421. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  422. {
  423. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  424. if ($bViewLink)
  425. {
  426. $aAttribs['key_'.$sAlias] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  427. }
  428. foreach($aList[$sClassName] as $sAttCode)
  429. {
  430. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  431. }
  432. }
  433. $aValues = array();
  434. $oSet->Seek(0);
  435. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  436. $iMaxObjects = -1;
  437. if ($bDisplayLimit)
  438. {
  439. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  440. {
  441. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  442. }
  443. }
  444. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  445. {
  446. $aRow = array();
  447. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  448. {
  449. if ($bViewLink)
  450. {
  451. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetHyperLink();
  452. }
  453. foreach($aList[$sClassName] as $sAttCode)
  454. {
  455. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  456. }
  457. }
  458. $aValues[] = $aRow;
  459. $iMaxObjects--;
  460. }
  461. $sHtml .= '<table class="listContainer">';
  462. $sColspan = '';
  463. if ($bDisplayMenu)
  464. {
  465. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  466. $sColspan = 'colspan="2"';
  467. $aMenuExtraParams = $aExtraParams;
  468. if (!empty($sLinkageAttribute))
  469. {
  470. $aMenuExtraParams = $aExtraParams;
  471. }
  472. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  473. {
  474. // list truncated
  475. $divId = $aExtraParams['block_id'];
  476. $sFilter = $oSet->GetFilter()->serialize();
  477. $aExtraParams['display_limit'] = false; // To expand the full list
  478. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  479. $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>';
  480. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  481. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  482. }
  483. else
  484. {
  485. // Full list
  486. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  487. }
  488. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  489. $sHtml .= '</td></tr>';
  490. }
  491. $sHtml .= "<tr><td $sColspan>";
  492. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  493. $sHtml .= '</td></tr>';
  494. $sHtml .= '</table>';
  495. return $sHtml;
  496. }
  497. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  498. {
  499. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  500. }
  501. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  502. {
  503. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  504. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  505. $aList = array();
  506. $oAppContext = new ApplicationContext();
  507. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  508. $aAuthorizedClasses = array();
  509. foreach($aClasses as $sAlias => $sClassName)
  510. {
  511. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  512. {
  513. $aAuthorizedClasses[$sAlias] = $sClassName;
  514. }
  515. }
  516. $aAttribs = array();
  517. $aHeader = array();
  518. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  519. {
  520. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode => $oAttDef)
  521. {
  522. if ((($oAttDef->IsExternalField()) || ($oAttDef->IsWritable())) && $oAttDef->IsScalar())
  523. {
  524. $aList[$sClassName][$sAttCode] = $oAttDef;
  525. }
  526. }
  527. $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  528. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  529. {
  530. if ($oAttDef->IsExternalField())
  531. {
  532. $sExtKeyLabel = MetaModel::GetLabel($sClassName, $oAttDef->GetKeyAttCode());
  533. $sRemoteAttLabel = MetaModel::GetLabel($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  534. $aHeader[] = $sExtKeyLabel.'->'.$sRemoteAttLabel;
  535. }
  536. else
  537. {
  538. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  539. }
  540. }
  541. }
  542. $sHtml = implode($sSeparator, $aHeader)."\n";
  543. $oSet->Seek(0);
  544. while ($aObjects = $oSet->FetchAssoc())
  545. {
  546. $aRow = array();
  547. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  548. {
  549. $oObj = $aObjects[$sAlias];
  550. $aRow[] = $oObj->GetKey();
  551. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  552. {
  553. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, '\\');
  554. }
  555. }
  556. $sHtml .= implode($sSeparator, $aRow)."\n";
  557. }
  558. return $sHtml;
  559. }
  560. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  561. {
  562. $oAppContext = new ApplicationContext();
  563. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  564. $aAuthorizedClasses = array();
  565. foreach($aClasses as $sAlias => $sClassName)
  566. {
  567. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  568. {
  569. $aAuthorizedClasses[$sAlias] = $sClassName;
  570. }
  571. }
  572. $aAttribs = array();
  573. $aList = array();
  574. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  575. $oPage->add("<Set>\n");
  576. $oSet->Seek(0);
  577. while ($aObjects = $oSet->FetchAssoc())
  578. {
  579. if (count($aAuthorizedClasses) > 1)
  580. {
  581. $oPage->add("<Row>\n");
  582. }
  583. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  584. {
  585. $oObj = $aObjects[$sAlias];
  586. $sClassName = get_class($oObj);
  587. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  588. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  589. {
  590. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()))
  591. {
  592. $sValue = $oObj->GetAsXML($sAttCode);
  593. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  594. }
  595. }
  596. $oPage->add("</$sClassName>\n");
  597. }
  598. if (count($aAuthorizedClasses) > 1)
  599. {
  600. $oPage->add("</Row>\n");
  601. }
  602. }
  603. $oPage->add("</Set>\n");
  604. }
  605. // By rom
  606. function DisplayChangesLog(WebPage $oPage)
  607. {
  608. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  609. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  610. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  611. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  612. $count = $oSet->Count();
  613. if ($count > 0)
  614. {
  615. $oPage->p(Dict::Format('UI:ChangesLogTitle', $count));
  616. self::DisplaySet($oPage, $oSet);
  617. }
  618. else
  619. {
  620. $oPage->p(Dict::S('UI:EmptyChangesLogTitle'));
  621. }
  622. }
  623. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  624. {
  625. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  626. }
  627. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  628. {
  629. static $iSearchFormId = 0;
  630. $oAppContext = new ApplicationContext();
  631. $sHtml = '';
  632. $numCols=4;
  633. $sClassName = $oSet->GetFilter()->GetClass();
  634. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  635. // (especially when used to add a link)
  636. /*
  637. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  638. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:OQLQueryTab')."</a></li>
  639. <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>
  640. </ul></div>\n";
  641. */
  642. // Simple search form
  643. if (isset($aExtraParams['currentId']))
  644. {
  645. $sSearchFormId = $aExtraParams['currentId'];
  646. $iSearchFormId++;
  647. }
  648. else
  649. {
  650. $iSearchFormId++;
  651. $sSearchFormId = 'SimpleSearchForm'.$iSearchFormId;
  652. $sHtml .= "<div id=\"$sSearchFormId\" class=\"mini_tab{$iSearchFormId}\">\n";
  653. }
  654. // Check if the current class has some sub-classes
  655. if (isset($aExtraParams['baseClass']))
  656. {
  657. $sRootClass = $aExtraParams['baseClass'];
  658. }
  659. else
  660. {
  661. $sRootClass = $sClassName;
  662. }
  663. $aSubClasses = MetaModel::GetSubclasses($sRootClass);
  664. if (count($aSubClasses) > 0)
  665. {
  666. $aOptions = array();
  667. $aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"$sRootClass\">".MetaModel::GetName($sRootClass)."</options>\n";
  668. foreach($aSubClasses as $sSubclassName)
  669. {
  670. $aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"$sSubclassName\">".MetaModel::GetName($sSubclassName)."</options>\n";
  671. }
  672. $aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"$sClassName\">".MetaModel::GetName($sClassName)."</options>\n";
  673. ksort($aOptions);
  674. $sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('$sSearchFormId', this.value, '$sRootClass')\">\n".implode('', $aOptions)."</select>\n";
  675. }
  676. else
  677. {
  678. $sClassesCombo = MetaModel::GetName($sClassName);
  679. }
  680. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  681. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  682. $sHtml .= "<h1>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</h1>\n";
  683. $index = 0;
  684. $sHtml .= "<table>\n";
  685. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  686. $aMapCriteria = array();
  687. foreach($aFilterCriteria as $aCriteria)
  688. {
  689. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  690. }
  691. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  692. foreach($aList as $sFilterCode)
  693. {
  694. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  695. if (($index % $numCols) == 0)
  696. {
  697. if ($index != 0)
  698. {
  699. $sHtml .= "</tr>\n";
  700. }
  701. $sHtml .= "<tr>\n";
  702. }
  703. $sFilterValue = '';
  704. $sFilterValue = utils::ReadParam($sFilterCode, '');
  705. $sFilterOpCode = null; // Use the default 'loose' OpCode
  706. if (empty($sFilterValue))
  707. {
  708. if (isset($aMapCriteria[$sFilterCode]))
  709. {
  710. if (count($aMapCriteria[$sFilterCode]) > 1)
  711. {
  712. $sFilterValue = Dict::S('UI:SearchValue:Mixed');
  713. }
  714. else
  715. {
  716. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  717. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  718. }
  719. if ($sFilterCode != 'company')
  720. {
  721. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  722. }
  723. }
  724. }
  725. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  726. if ($aAllowedValues != null)
  727. {
  728. //Enum field or external key, display a combo
  729. $sValue = "<select name=\"$sFilterCode\">\n";
  730. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  731. foreach($aAllowedValues as $key => $value)
  732. {
  733. if ($sFilterValue == $key)
  734. {
  735. $sSelected = ' selected';
  736. }
  737. else
  738. {
  739. $sSelected = '';
  740. }
  741. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  742. }
  743. $sValue .= "</select>\n";
  744. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  745. }
  746. else
  747. {
  748. // Any value is possible, display an input box
  749. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  750. }
  751. $index++;
  752. }
  753. if (($index % $numCols) != 0)
  754. {
  755. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  756. }
  757. $sHtml .= "</tr>\n";
  758. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></td></tr>\n";
  759. $sHtml .= "</table>\n";
  760. foreach($aExtraParams as $sName => $sValue)
  761. {
  762. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  763. }
  764. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  765. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  766. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  767. $sHtml .= $oAppContext->GetForForm();
  768. $sHtml .= "</form>\n";
  769. if (!isset($aExtraParams['currentId']))
  770. {
  771. $sHtml .= "</div><!-- Simple search form -->\n";
  772. }
  773. // OQL query builder
  774. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  775. $sHtml .= "<h1>".Dict::S('UI:OQLQueryBuilderTitle')."</h1>\n";
  776. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  777. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  778. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  779. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  780. $sOQLClause = utils::ReadParam('oql_clause', '');
  781. asort($aClasses);
  782. foreach($aClasses as $sChildClass)
  783. {
  784. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  785. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  786. }
  787. $sHtml .= "</select>&nbsp;</td><td>\n";
  788. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  789. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Query')."\"></td></tr>\n";
  790. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  791. foreach($aExtraParams as $sName => $sValue)
  792. {
  793. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  794. }
  795. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  796. $sHtml .= $oAppContext->GetForForm();
  797. $sHtml .= "</table></form>\n";
  798. $sHtml .= "</div><!-- OQL query form -->\n";
  799. return $sHtml;
  800. }
  801. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  802. {
  803. static $iInputId = 0;
  804. if (isset($aArgs[$sAttCode]) && empty($value))
  805. {
  806. // default value passed by the context (either the app context of the operation)
  807. $value = $aArgs[$sAttCode];
  808. }
  809. if (!empty($iId))
  810. {
  811. $iInputId = $iId;
  812. }
  813. else
  814. {
  815. $iInputId++;
  816. }
  817. if (!$oAttDef->IsExternalField())
  818. {
  819. $aCSSClasses = array();
  820. $bMandatory = 0;
  821. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  822. {
  823. $aCSSClasses[] = 'mandatory';
  824. $bMandatory = 1;
  825. }
  826. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  827. $sValidationField = "<span id=\"v_{$iInputId}\"></span>";
  828. $sHelpText = $oAttDef->GetHelpOnEdition();
  829. switch($oAttDef->GetEditClass())
  830. {
  831. case 'Date':
  832. case 'DateTime':
  833. $aCSSClasses[] = 'date-pick';
  834. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  835. $sHTMLValue = "<input title=\"$sHelpText\" type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>$sValidationField";
  836. break;
  837. case 'Password':
  838. $sHTMLValue = "<input title=\"$sHelpText\" type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>$sValidationField";
  839. break;
  840. case 'Text':
  841. $sHTMLValue = "<textarea title=\"$sHelpText\" name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>$sValidationField";
  842. break;
  843. case 'List':
  844. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  845. $sHTMLValue = $oWidget->Display($oPage, $value);
  846. break;
  847. case 'Document':
  848. $oDocument = $value; // Value is an ormDocument object
  849. $sFileName = '';
  850. if (is_object($oDocument))
  851. {
  852. $sFileName = $oDocument->GetFileName();
  853. }
  854. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  855. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  856. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iInputId\" \" value=\"$sFileName\"/>\n";
  857. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  858. $sHTMLValue .= "<input title=\"$sHelpText\" name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iInputId\" onChange=\"UpdateFileName('$iInputId', this.value);\"/>\n";
  859. break;
  860. case 'String':
  861. default:
  862. // #@# todo - add context information (depending on dimensions)
  863. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  864. if ($aAllowedValues !== null)
  865. {
  866. //Enum field or external key, display a combo
  867. //if (count($aAllowedValues) == 0)
  868. //{
  869. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  870. //}
  871. //else if (count($aAllowedValues) > 50)
  872. if (count($aAllowedValues) > 50)
  873. {
  874. // too many choices, use an autocomplete
  875. // The input for the auto complete
  876. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>$sValidationField";
  877. // another hidden input to store & pass the object's Id
  878. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  879. $oPage->add_ready_script("\$('#label_$iInputId').autocomplete('./ajax.render.php', { scroll:true, minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#$iInputId', extraParams:{operation:'autocomplete', sclass:'$sClass',attCode:'".$sAttCode."'}});");
  880. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  881. // Prepopulate with a default value -- but no display value...
  882. //if (!empty($value))
  883. //{
  884. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  885. //}
  886. }
  887. else
  888. {
  889. // Few choices, use a normal 'select'
  890. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  891. $sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  892. $sHTMLValue .= "<option value=\"0\">".Dict::S('UI:SelectOne')."</option>\n";
  893. foreach($aAllowedValues as $key => $display_value)
  894. {
  895. $sSelected = ($value == $key) ? ' selected' : '';
  896. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  897. }
  898. $sHTMLValue .= "</select>$sValidationField\n";
  899. }
  900. }
  901. else
  902. {
  903. $sHTMLValue = "<input title=\"$sHelpText\" type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses} />$sValidationField";
  904. }
  905. break;
  906. }
  907. $sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
  908. $oPage->add_ready_script("$('#$iInputId').bind('validate blur', function(evt, sFormId) { return ValidateField('$iInputId', '$sPattern', $bMandatory, sFormId) } );"); // Bind to a custom event: validate
  909. $aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
  910. if (count($aDependencies) > 0)
  911. {
  912. $oPage->add_ready_script("$('#$iInputId').bind('change', function(evt, sFormId) { return UpdateDependentFields(['".implode("','", $aDependencies)."']) } );"); // Bind to a custom event: validate
  913. }
  914. }
  915. return $sHTMLValue;
  916. }
  917. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  918. {
  919. static $iFormId = 0;
  920. $iFormId++;
  921. $sClass = get_class($this);
  922. $oAppContext = new ApplicationContext();
  923. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  924. $iKey = $this->GetKey();
  925. $aDetails = array();
  926. $aFieldsMap = array();
  927. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckFields('form_{$iFormId}')\">\n");
  928. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  929. {
  930. if ($oAttDef->IsWritable())
  931. {
  932. if ($sStateAttCode == $sAttCode)
  933. {
  934. // State attribute is always read-only from the UI
  935. $sHTMLValue = $this->GetStateLabel();
  936. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  937. }
  938. else
  939. {
  940. $iFlags = $this->GetAttributeFlags($sAttCode);
  941. if ($iFlags & OPT_ATT_HIDDEN)
  942. {
  943. // Attribute is hidden, do nothing
  944. }
  945. else
  946. {
  947. if ($iFlags & OPT_ATT_READONLY)
  948. {
  949. // Attribute is read-only
  950. $sHTMLValue = $this->GetAsHTML($sAttCode);
  951. }
  952. else
  953. {
  954. $sValue = $this->Get($sAttCode);
  955. $sDisplayValue = $this->GetEditValue($sAttCode);
  956. $aArgs = array('this' => $this);
  957. $sInputId = $iFormId.'_'.$sAttCode;
  958. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs);
  959. $aFieldsMap[$sAttCode] = $sInputId;
  960. }
  961. $aDetails[] = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue);
  962. }
  963. }
  964. }
  965. }
  966. $oPage->details($aDetails);
  967. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  968. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  969. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  970. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  971. foreach($aExtraParams as $sName => $value)
  972. {
  973. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  974. }
  975. $oPage->add($oAppContext->GetForForm());
  976. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  977. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  978. $oPage->add("</form>\n");
  979. $iFieldsCount = count($aFieldsMap);
  980. $sJsonFieldsMap = json_encode($aFieldsMap);
  981. $oPage->add_script(
  982. <<<EOF
  983. // Initializes the object once at the beginning of the page...
  984. var oWizardHelper = new WizardHelper('$sClass');
  985. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  986. oWizardHelper.SetFieldsCount($iFieldsCount);
  987. EOF
  988. );
  989. }
  990. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  991. {
  992. static $iCreationFormId = 0;
  993. $iCreationFormId++;
  994. $oAppContext = new ApplicationContext();
  995. $aDetails = array();
  996. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  997. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  998. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  999. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" enctype=\"multipart/form-data\" onSubmit=\"return CheckFields('creation_form_{$iCreationFormId}')\">\n");
  1000. $aStates = MetaModel::EnumStates($sClass);
  1001. if ($oObjectToClone == null)
  1002. {
  1003. $sTargetState = MetaModel::GetDefaultState($sClass);
  1004. }
  1005. else
  1006. {
  1007. $sTargetState = $oObjectToClone->GetState();
  1008. }
  1009. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  1010. {
  1011. if ($sStateAttCode == $sAttCode)
  1012. {
  1013. // State attribute is always read-only from the UI
  1014. $sHTMLValue = $oObjectToClone->GetStateLabel();
  1015. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1016. }
  1017. else if ((!$oAttDef->IsExternalField()) && ($oAttDef->IsWritable()) )
  1018. {
  1019. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  1020. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetEditValue($sAttCode);
  1021. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1022. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
  1023. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1024. }
  1025. }
  1026. $oPage->details($aDetails);
  1027. if ($oObjectToClone != null)
  1028. {
  1029. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  1030. }
  1031. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1032. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  1033. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1034. $oPage->add($oAppContext->GetForForm());
  1035. foreach($aExtraParams as $sName => $value)
  1036. {
  1037. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1038. }
  1039. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1040. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  1041. $oPage->add("</form>\n");
  1042. }
  1043. protected static function GetCSSClasses($aCSSClasses)
  1044. {
  1045. $sCSSClasses = '';
  1046. if (!empty($aCSSClasses))
  1047. {
  1048. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  1049. }
  1050. return $sCSSClasses;
  1051. }
  1052. }
  1053. ?>