cmdbabstract.class.inc.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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="#open_'.$divId.'" onClick="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. $iId = $iInputId;
  817. }
  818. if (!$oAttDef->IsExternalField())
  819. {
  820. $aCSSClasses = array();
  821. $bMandatory = 0;
  822. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  823. {
  824. $aCSSClasses[] = 'mandatory';
  825. $bMandatory = 1;
  826. }
  827. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  828. $sValidationField = "<span id=\"v_{$iId}\"></span>";
  829. $sHelpText = $oAttDef->GetHelpOnEdition();
  830. $aEventsList = array('validate');
  831. switch($oAttDef->GetEditClass())
  832. {
  833. case 'Date':
  834. case 'DateTime':
  835. $aEventsList[] ='keyup';
  836. $aEventsList[] ='change';
  837. $sHTMLValue = "<input title=\"$sHelpText\" class=\"date-pick\" type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  838. break;
  839. case 'Password':
  840. $aEventsList[] ='keyup';
  841. $aEventsList[] ='change';
  842. $sHTMLValue = "<input title=\"$sHelpText\" type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  843. break;
  844. case 'Text':
  845. $aEventsList[] ='keypress';
  846. $aEventsList[] ='change';
  847. $sHTMLValue = "<textarea title=\"$sHelpText\" name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iId\">$value</textarea>&nbsp;{$sValidationField}";
  848. break;
  849. case 'List':
  850. $aEventsList[] ='change';
  851. $oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix);
  852. $sHTMLValue = $oWidget->Display($oPage, $value);
  853. break;
  854. case 'Document':
  855. $aEventsList[] ='change';
  856. $oDocument = $value; // Value is an ormDocument object
  857. $sFileName = '';
  858. if (is_object($oDocument))
  859. {
  860. $sFileName = $oDocument->GetFileName();
  861. }
  862. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  863. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  864. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iId\" \" value=\"$sFileName\"/>\n";
  865. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  866. $sHTMLValue .= "<input title=\"$sHelpText\" name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iId\" onChange=\"UpdateFileName('$iId', this.value)\"/>&nbsp;{$sValidationField}\n";
  867. break;
  868. case 'String':
  869. default:
  870. // #@# todo - add context information (depending on dimensions)
  871. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  872. if ($aAllowedValues !== null)
  873. {
  874. //Enum field or external key, display a combo
  875. //if (count($aAllowedValues) == 0)
  876. //{
  877. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  878. //}
  879. //else if (count($aAllowedValues) > 50)
  880. if (count($aAllowedValues) > 50)
  881. {
  882. // too many choices, use an autocomplete
  883. // The input for the auto complete
  884. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>&nbsp;{$sValidationField}";
  885. // another hidden input to store & pass the object's Id
  886. $sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  887. $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."'}});");
  888. $oPage->add_ready_script("\$('#label_$iId').result( function(event, data, formatted) { if (data) { $('#{$iId}').val(data[1]); } } );");
  889. // Prepopulate with a default value -- but no display value...
  890. //if (!empty($value))
  891. //{
  892. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  893. //}
  894. $aEventsList[] ='change';
  895. }
  896. else
  897. {
  898. // Few choices, use a normal 'select'
  899. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  900. $sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iId\">\n";
  901. $sHTMLValue .= "<option value=\"0\">".Dict::S('UI:SelectOne')."</option>\n";
  902. foreach($aAllowedValues as $key => $display_value)
  903. {
  904. $sSelected = ($value == $key) ? ' selected' : '';
  905. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  906. }
  907. $sHTMLValue .= "</select>&nbsp;{$sValidationField}\n";
  908. $aEventsList[] ='change';
  909. }
  910. }
  911. else
  912. {
  913. $sHTMLValue = "<input title=\"$sHelpText\" type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  914. $aEventsList[] ='keyup';
  915. $aEventsList[] ='change';
  916. }
  917. break;
  918. }
  919. $sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
  920. $oPage->add_ready_script("$('#$iId').bind('".implode(' ', $aEventsList)."', function(evt, sFormId) { return ValidateField('$iId', '$sPattern', $bMandatory, sFormId) } );"); // Bind to a custom event: validate
  921. $aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
  922. if (count($aDependencies) > 0)
  923. {
  924. $oPage->add_ready_script("$('#$iId').bind('change', function(evt, sFormId) { return UpdateDependentFields(['".implode("','", $aDependencies)."']) } );"); // Bind to a custom event: validate
  925. }
  926. }
  927. return "<div>{$sHTMLValue}</div>";
  928. }
  929. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  930. {
  931. static $iFormId = 0;
  932. $iFormId++;
  933. $sClass = get_class($this);
  934. $oAppContext = new ApplicationContext();
  935. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  936. $iKey = $this->GetKey();
  937. $aDetails = array();
  938. $aFieldsMap = array();
  939. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckFields('form_{$iFormId}', true)\">\n");
  940. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  941. {
  942. if ($oAttDef->IsWritable())
  943. {
  944. if ($sStateAttCode == $sAttCode)
  945. {
  946. // State attribute is always read-only from the UI
  947. $sHTMLValue = $this->GetStateLabel();
  948. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  949. }
  950. else
  951. {
  952. $iFlags = $this->GetAttributeFlags($sAttCode);
  953. if ($iFlags & OPT_ATT_HIDDEN)
  954. {
  955. // Attribute is hidden, do nothing
  956. }
  957. else
  958. {
  959. if ($iFlags & OPT_ATT_READONLY)
  960. {
  961. // Attribute is read-only
  962. $sHTMLValue = $this->GetAsHTML($sAttCode);
  963. }
  964. else
  965. {
  966. $sValue = $this->Get($sAttCode);
  967. $sDisplayValue = $this->GetEditValue($sAttCode);
  968. $aArgs = array('this' => $this);
  969. $sInputId = $iFormId.'_'.$sAttCode;
  970. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs);
  971. $aFieldsMap[$sAttCode] = $sInputId;
  972. }
  973. $aDetails[] = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue);
  974. }
  975. }
  976. }
  977. }
  978. $oPage->details($aDetails);
  979. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  980. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  981. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  982. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  983. foreach($aExtraParams as $sName => $value)
  984. {
  985. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  986. }
  987. $oPage->add($oAppContext->GetForForm());
  988. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"BackToDetails('$sClass', $iKey)\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  989. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  990. $oPage->add("</form>\n");
  991. $iFieldsCount = count($aFieldsMap);
  992. $sJsonFieldsMap = json_encode($aFieldsMap);
  993. $oPage->add_script(
  994. <<<EOF
  995. // Initializes the object once at the beginning of the page...
  996. var oWizardHelper = new WizardHelper('$sClass');
  997. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  998. oWizardHelper.SetFieldsCount($iFieldsCount);
  999. EOF
  1000. );
  1001. $oPage->add_ready_script(
  1002. <<<EOF
  1003. // Initializes the object once at the beginning of the page...
  1004. CheckFields('form_{$iFormId}', false);
  1005. EOF
  1006. );
  1007. }
  1008. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  1009. {
  1010. static $iCreationFormId = 0;
  1011. $iCreationFormId++;
  1012. $iFieldIndex = 0;
  1013. $oAppContext = new ApplicationContext();
  1014. $aDetails = array();
  1015. $aFieldsMap = array();
  1016. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  1017. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  1018. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1019. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" enctype=\"multipart/form-data\" onSubmit=\"return CheckFields('creation_form_{$iCreationFormId}', true)\">\n");
  1020. $aStates = MetaModel::EnumStates($sClass);
  1021. if ($oObjectToClone == null)
  1022. {
  1023. $sTargetState = MetaModel::GetDefaultState($sClass);
  1024. }
  1025. else
  1026. {
  1027. $sTargetState = $oObjectToClone->GetState();
  1028. }
  1029. //foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  1030. foreach(MetaModel::GetZListItems($sClass, 'details') as $sAttCode)
  1031. {
  1032. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1033. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1034. if ($sStateAttCode == $sAttCode)
  1035. {
  1036. // State attribute is always read-only from the UI
  1037. $sHTMLValue = MetaModel::GetStateLabel($sClass, $sTargetState);
  1038. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1039. }
  1040. else if ((!$oAttDef->IsExternalField()) && ($oAttDef->IsWritable()) && ($iOptions != OPT_ATT_HIDDEN) && ($iOptions != OPT_ATT_READONLY) )
  1041. {
  1042. $sFieldId = 'att_'.$iFieldIndex;
  1043. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  1044. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetEditValue($sAttCode);
  1045. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1046. $sHTMLValue = "<div id=\"field_{$sFieldId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sFieldId, '', $iOptions, $aArgs)."</div>";
  1047. $aFieldsMap[$sFieldId] = $sAttCode;
  1048. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1049. $iFieldIndex++;
  1050. }
  1051. }
  1052. $oPage->details($aDetails);
  1053. if ($oObjectToClone != null)
  1054. {
  1055. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  1056. }
  1057. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1058. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  1059. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1060. $oPage->add($oAppContext->GetForForm());
  1061. foreach($aExtraParams as $sName => $value)
  1062. {
  1063. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1064. }
  1065. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1066. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  1067. $oPage->add("</form>\n");
  1068. $aNewFieldsMap = array();
  1069. foreach($aFieldsMap as $id => $sFieldCode)
  1070. {
  1071. $aNewFieldsMap[$sFieldCode] = $id;
  1072. }
  1073. $iFieldsCount = count($aFieldsMap);
  1074. $sJsonFieldsMap = json_encode($aNewFieldsMap);
  1075. $oPage->add_script("
  1076. // Initializes the object once at the beginning of the page...
  1077. var oWizardHelper = new WizardHelper('$sClass');
  1078. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  1079. oWizardHelper.SetFieldsCount($iFieldsCount);");
  1080. $oPage->add_ready_script("CheckFields('creation_form_{$iCreationFormId}', false);");
  1081. }
  1082. protected static function GetCSSClasses($aCSSClasses)
  1083. {
  1084. $sCSSClasses = '';
  1085. if (!empty($aCSSClasses))
  1086. {
  1087. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  1088. }
  1089. return $sCSSClasses;
  1090. }
  1091. }
  1092. ?>