cmdbabstract.class.inc.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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' => MetaModel::GetLabel($sClass, $sAttCode), '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. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  249. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  250. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  251. if (!empty($sLinkageAttribute))
  252. {
  253. if($iLinkedObjectId == 0)
  254. {
  255. // if 'links' mode is requested the id of the object to link to must be specified
  256. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
  257. }
  258. if($sTargetAttr == '')
  259. {
  260. // if 'links' mode is requested the d of the object to link to must be specified
  261. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
  262. }
  263. }
  264. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  265. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  266. $bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
  267. $sHtml = '';
  268. $oAppContext = new ApplicationContext();
  269. $sClassName = $oSet->GetFilter()->GetClass();
  270. $aAttribs = array();
  271. $aList = MetaModel::GetZListItems($sClassName, 'list');
  272. if (!empty($sLinkageAttribute))
  273. {
  274. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  275. // and other objects...
  276. // The display will then group all the attributes related to the link itself:
  277. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  278. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  279. assert(isset($aAttDefs[$sLinkageAttribute]));
  280. $oAttDef = $aAttDefs[$sLinkageAttribute];
  281. assert($oAttDef->IsExternalKey());
  282. // First display all the attributes specific to the link record
  283. foreach($aList as $sLinkAttCode)
  284. {
  285. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  286. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  287. {
  288. $aDisplayList[] = $sLinkAttCode;
  289. }
  290. }
  291. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  292. foreach($aList as $sLinkAttCode)
  293. {
  294. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  295. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  296. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  297. {
  298. $aDisplayList[] = $sLinkAttCode;
  299. }
  300. }
  301. // First display all the attributes specific to the link
  302. // Then display all the attributes linked to the other end of the relationship
  303. $aList = $aDisplayList;
  304. }
  305. if ($bSelectMode)
  306. {
  307. if (!$bSingleSelectMode)
  308. {
  309. $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+'));
  310. }
  311. else
  312. {
  313. $aAttribs['form::select'] = array('label' => "", 'description' => '');
  314. }
  315. }
  316. $aAttribs['key'] = array('label' => '', 'description' => Dict::S('UI:ClickToDisplay+'));
  317. foreach($aList as $sAttCode)
  318. {
  319. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  320. }
  321. $aValues = array();
  322. $oSet->Seek(0);
  323. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  324. $iMaxObjects = -1;
  325. if ($bDisplayLimit)
  326. {
  327. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  328. {
  329. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  330. }
  331. }
  332. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  333. {
  334. $aRow = array();
  335. $aRow['key'] = $oObj->GetKey();
  336. if ($bSelectMode)
  337. {
  338. if ($bSingleSelectMode)
  339. {
  340. $aRow['form::select'] = "<input type=\"radio\" class=\"selectList{$iListId}\" name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  341. }
  342. else
  343. {
  344. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  345. }
  346. }
  347. $aRow['key'] = $oObj->GetKey();
  348. foreach($aList as $sAttCode)
  349. {
  350. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  351. }
  352. $aValues[] = $aRow;
  353. $iMaxObjects--;
  354. }
  355. $sHtml .= '<table class="listContainer">';
  356. $sColspan = '';
  357. if ($bDisplayMenu)
  358. {
  359. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  360. $sColspan = 'colspan="2"';
  361. $aMenuExtraParams = $aExtraParams;
  362. if (!empty($sLinkageAttribute))
  363. {
  364. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  365. $aMenuExtraParams = $aExtraParams;
  366. }
  367. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  368. {
  369. // list truncated
  370. $divId = $aExtraParams['block_id'];
  371. $sFilter = $oSet->GetFilter()->serialize();
  372. $aExtraParams['display_limit'] = false; // To expand the full list
  373. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  374. $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>';
  375. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  376. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  377. }
  378. else
  379. {
  380. // Full list
  381. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  382. }
  383. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  384. $sHtml .= '</td></tr>';
  385. }
  386. $sHtml .= "<tr><td $sColspan>";
  387. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$sClassName, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  388. $sHtml .= '</td></tr>';
  389. $sHtml .= '</table>';
  390. return $sHtml;
  391. }
  392. public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  393. {
  394. static $iListId = 0;
  395. $iListId++;
  396. $aList = array();
  397. // Initialize and check the parameters
  398. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  399. // Check if there is a list of aliases to limit the display to...
  400. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  401. $sHtml = '';
  402. $oAppContext = new ApplicationContext();
  403. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  404. $aAuthorizedClasses = array();
  405. foreach($aClasses as $sAlias => $sClassName)
  406. {
  407. if ((UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES) &&
  408. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  409. {
  410. $aAuthorizedClasses[$sAlias] = $sClassName;
  411. }
  412. }
  413. $aAttribs = array();
  414. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  415. {
  416. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  417. $aAttribs['key_'.$sAlias] = array('label' => '', 'description' => Dict::S('UI:ClickToDisplay+'));
  418. foreach($aList[$sClassName] as $sAttCode)
  419. {
  420. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  421. }
  422. }
  423. $aValues = array();
  424. $oSet->Seek(0);
  425. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  426. $iMaxObjects = -1;
  427. if ($bDisplayLimit)
  428. {
  429. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  430. {
  431. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  432. }
  433. }
  434. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  435. {
  436. $aRow = array();
  437. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  438. {
  439. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetKey();
  440. foreach($aList[$sClassName] as $sAttCode)
  441. {
  442. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  443. }
  444. }
  445. $aValues[] = $aRow;
  446. $iMaxObjects--;
  447. }
  448. $sHtml .= '<table class="listContainer">';
  449. $sColspan = '';
  450. if ($bDisplayMenu)
  451. {
  452. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  453. $sColspan = 'colspan="2"';
  454. $aMenuExtraParams = $aExtraParams;
  455. if (!empty($sLinkageAttribute))
  456. {
  457. $aMenuExtraParams = $aExtraParams;
  458. }
  459. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  460. {
  461. // list truncated
  462. $divId = $aExtraParams['block_id'];
  463. $sFilter = $oSet->GetFilter()->serialize();
  464. $aExtraParams['display_limit'] = false; // To expand the full list
  465. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  466. $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>';
  467. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  468. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  469. }
  470. else
  471. {
  472. // Full list
  473. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  474. }
  475. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  476. $sHtml .= '</td></tr>';
  477. }
  478. $sHtml .= "<tr><td $sColspan>";
  479. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$aAuthorizedClasses, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  480. $sHtml .= '</td></tr>';
  481. $sHtml .= '</table>';
  482. return $sHtml;
  483. }
  484. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  485. {
  486. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  487. }
  488. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  489. {
  490. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  491. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  492. $aList = array();
  493. $oAppContext = new ApplicationContext();
  494. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  495. $aAuthorizedClasses = array();
  496. foreach($aClasses as $sAlias => $sClassName)
  497. {
  498. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  499. {
  500. $aAuthorizedClasses[$sAlias] = $sClassName;
  501. }
  502. }
  503. $aAttribs = array();
  504. $aHeader = array();
  505. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  506. {
  507. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode => $oAttDef)
  508. {
  509. if ((($oAttDef->IsExternalField()) || ($oAttDef->IsWritable())) && $oAttDef->IsScalar())
  510. {
  511. $aList[$sClassName][$sAttCode] = $oAttDef;
  512. }
  513. }
  514. $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  515. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  516. {
  517. if ($oAttDef->IsExternalField())
  518. {
  519. $sExtKeyLabel = MetaModel::GetLabel($sClassName, $oAttDef->GetKeyAttCode());
  520. $sRemoteAttLabel = MetaModel::GetLabel($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  521. $aHeader[] = $sExtKeyLabel.'->'.$sRemoteAttLabel;
  522. }
  523. else
  524. {
  525. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  526. }
  527. }
  528. }
  529. $sHtml = implode($sSeparator, $aHeader)."\n";
  530. $oSet->Seek(0);
  531. while ($aObjects = $oSet->FetchAssoc())
  532. {
  533. $aRow = array();
  534. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  535. {
  536. $oObj = $aObjects[$sAlias];
  537. $aRow[] = $oObj->GetKey();
  538. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  539. {
  540. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, '\\');
  541. }
  542. }
  543. $sHtml .= implode($sSeparator, $aRow)."\n";
  544. }
  545. return $sHtml;
  546. }
  547. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  548. {
  549. $oAppContext = new ApplicationContext();
  550. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  551. $aAuthorizedClasses = array();
  552. foreach($aClasses as $sAlias => $sClassName)
  553. {
  554. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  555. {
  556. $aAuthorizedClasses[$sAlias] = $sClassName;
  557. }
  558. }
  559. $aAttribs = array();
  560. $aList = array();
  561. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  562. $oPage->add("<Set>\n");
  563. $oSet->Seek(0);
  564. while ($aObjects = $oSet->FetchAssoc())
  565. {
  566. if (count($aAuthorizedClasses) > 1)
  567. {
  568. $oPage->add("<Row>\n");
  569. }
  570. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  571. {
  572. $oObj = $aObjects[$sAlias];
  573. $sClassName = get_class($oObj);
  574. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  575. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  576. {
  577. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()))
  578. {
  579. $sValue = $oObj->GetAsXML($sAttCode);
  580. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  581. }
  582. }
  583. $oPage->add("</$sClassName>\n");
  584. }
  585. if (count($aAuthorizedClasses) > 1)
  586. {
  587. $oPage->add("</Row>\n");
  588. }
  589. }
  590. $oPage->add("</Set>\n");
  591. }
  592. // By rom
  593. function DisplayChangesLog(WebPage $oPage)
  594. {
  595. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  596. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  597. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  598. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  599. $count = $oSet->Count();
  600. if ($count > 0)
  601. {
  602. $oPage->p(Dict::Format('UI:ChangesLogTitle', $count));
  603. self::DisplaySet($oPage, $oSet);
  604. }
  605. else
  606. {
  607. $oPage->p(Dict::S('UI:EmptyChangesLogTitle'));
  608. }
  609. }
  610. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  611. {
  612. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  613. }
  614. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  615. {
  616. static $iSearchFormId = 0;
  617. $oAppContext = new ApplicationContext();
  618. $sHtml = '';
  619. $numCols=4;
  620. $sClassName = $oSet->GetFilter()->GetClass();
  621. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  622. // (especially when used to add a link)
  623. /*
  624. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  625. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:OQLQueryTab')."</a></li>
  626. <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>
  627. </ul></div>\n";
  628. */
  629. // Simple search form
  630. if (isset($aExtraParams['currentId']))
  631. {
  632. $sSearchFormId = $aExtraParams['currentId'];
  633. $iSearchFormId++;
  634. }
  635. else
  636. {
  637. $iSearchFormId++;
  638. $sSearchFormId = 'SimpleSearchForm'.$iSearchFormId;
  639. $sHtml .= "<div id=\"$sSearchFormId\" class=\"mini_tab{$iSearchFormId}\">\n";
  640. }
  641. // Check if the current class has some sub-classes
  642. if (isset($aExtraParams['baseClass']))
  643. {
  644. $sRootClass = $aExtraParams['baseClass'];
  645. }
  646. else
  647. {
  648. $sRootClass = $sClassName;
  649. }
  650. $aSubClasses = MetaModel::GetSubclasses($sRootClass);
  651. if (count($aSubClasses) > 0)
  652. {
  653. $aOptions = array();
  654. $aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"$sRootClass\">".MetaModel::GetName($sRootClass)."</options>\n";
  655. foreach($aSubClasses as $sSubclassName)
  656. {
  657. $aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"$sSubclassName\">".MetaModel::GetName($sSubclassName)."</options>\n";
  658. }
  659. $aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"$sClassName\">".MetaModel::GetName($sClassName)."</options>\n";
  660. ksort($aOptions);
  661. $sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('$sSearchFormId', this.value, '$sRootClass')\">\n".implode('', $aOptions)."</select>\n";
  662. }
  663. else
  664. {
  665. $sClassesCombo = MetaModel::GetName($sClassName);
  666. }
  667. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  668. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  669. $sHtml .= "<h1>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</h1>\n";
  670. $index = 0;
  671. $sHtml .= "<table>\n";
  672. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  673. $aMapCriteria = array();
  674. foreach($aFilterCriteria as $aCriteria)
  675. {
  676. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  677. }
  678. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  679. foreach($aList as $sFilterCode)
  680. {
  681. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  682. if (($index % $numCols) == 0)
  683. {
  684. if ($index != 0)
  685. {
  686. $sHtml .= "</tr>\n";
  687. }
  688. $sHtml .= "<tr>\n";
  689. }
  690. $sFilterValue = '';
  691. $sFilterValue = utils::ReadParam($sFilterCode, '');
  692. $sFilterOpCode = null; // Use the default 'loose' OpCode
  693. if (empty($sFilterValue))
  694. {
  695. if (isset($aMapCriteria[$sFilterCode]))
  696. {
  697. if (count($aMapCriteria[$sFilterCode]) > 1)
  698. {
  699. $sFilterValue = Dict::S('UI:SearchValue:Mixed');
  700. }
  701. else
  702. {
  703. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  704. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  705. }
  706. if ($sFilterCode != 'company')
  707. {
  708. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  709. }
  710. }
  711. }
  712. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  713. if ($aAllowedValues != null)
  714. {
  715. //Enum field or external key, display a combo
  716. $sValue = "<select name=\"$sFilterCode\">\n";
  717. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  718. foreach($aAllowedValues as $key => $value)
  719. {
  720. if ($sFilterValue == $key)
  721. {
  722. $sSelected = ' selected';
  723. }
  724. else
  725. {
  726. $sSelected = '';
  727. }
  728. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  729. }
  730. $sValue .= "</select>\n";
  731. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  732. }
  733. else
  734. {
  735. // Any value is possible, display an input box
  736. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  737. }
  738. $index++;
  739. }
  740. if (($index % $numCols) != 0)
  741. {
  742. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  743. }
  744. $sHtml .= "</tr>\n";
  745. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></td></tr>\n";
  746. $sHtml .= "</table>\n";
  747. foreach($aExtraParams as $sName => $sValue)
  748. {
  749. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  750. }
  751. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  752. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  753. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  754. $sHtml .= $oAppContext->GetForForm();
  755. $sHtml .= "</form>\n";
  756. if (!isset($aExtraParams['currentId']))
  757. {
  758. $sHtml .= "</div><!-- Simple search form -->\n";
  759. }
  760. // OQL query builder
  761. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  762. $sHtml .= "<h1>".Dict::S('UI:OQLQueryBuilderTitle')."</h1>\n";
  763. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  764. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  765. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  766. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  767. $sOQLClause = utils::ReadParam('oql_clause', '');
  768. asort($aClasses);
  769. foreach($aClasses as $sChildClass)
  770. {
  771. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  772. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  773. }
  774. $sHtml .= "</select>&nbsp;</td><td>\n";
  775. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  776. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Query')."\"></td></tr>\n";
  777. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  778. foreach($aExtraParams as $sName => $sValue)
  779. {
  780. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  781. }
  782. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  783. $sHtml .= $oAppContext->GetForForm();
  784. $sHtml .= "</table></form>\n";
  785. $sHtml .= "</div><!-- OQL query form -->\n";
  786. return $sHtml;
  787. }
  788. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  789. {
  790. static $iInputId = 0;
  791. if (isset($aArgs[$sAttCode]) && empty($value))
  792. {
  793. // default value passed by the context (either the app context of the operation)
  794. $value = $aArgs[$sAttCode];
  795. }
  796. if (!empty($iId))
  797. {
  798. $iInputId = $iId;
  799. }
  800. else
  801. {
  802. $iInputId++;
  803. }
  804. if (!$oAttDef->IsExternalField())
  805. {
  806. $aCSSClasses = array();
  807. $bMandatory = 0;
  808. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  809. {
  810. $aCSSClasses[] = 'mandatory';
  811. $bMandatory = 1;
  812. }
  813. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  814. $sValidationField = "<span id=\"v_{$iInputId}\"></span>";
  815. switch($oAttDef->GetEditClass())
  816. {
  817. case 'Date':
  818. case 'DateTime':
  819. $aCSSClasses[] = 'date-pick';
  820. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  821. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>$sValidationField";
  822. break;
  823. case 'Password':
  824. $sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>$sValidationField";
  825. break;
  826. case 'Text':
  827. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>$sValidationField";
  828. break;
  829. case 'List':
  830. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  831. $sHTMLValue = $oWidget->Display($oPage, $value);
  832. break;
  833. case 'Document':
  834. $oDocument = $value; // Value is an ormDocument object
  835. $sFileName = '';
  836. if (is_object($oDocument))
  837. {
  838. $sFileName = $oDocument->GetFileName();
  839. }
  840. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  841. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  842. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iInputId\" \" value=\"$sFileName\"/>\n";
  843. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  844. $sHTMLValue .= "<input name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iInputId\" onChange=\"UpdateFileName('$iInputId', this.value);\"/>\n";
  845. break;
  846. case 'String':
  847. default:
  848. // #@# todo - add context information (depending on dimensions)
  849. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  850. if ($aAllowedValues !== null)
  851. {
  852. //Enum field or external key, display a combo
  853. //if (count($aAllowedValues) == 0)
  854. //{
  855. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  856. //}
  857. //else if (count($aAllowedValues) > 50)
  858. if (count($aAllowedValues) > 50)
  859. {
  860. // too many choices, use an autocomplete
  861. // The input for the auto complete
  862. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>$sValidationField";
  863. // another hidden input to store & pass the object's Id
  864. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  865. $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."'}});");
  866. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  867. // Prepopulate with a default value -- but no display value...
  868. //if (!empty($value))
  869. //{
  870. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  871. //}
  872. }
  873. else
  874. {
  875. // Few choices, use a normal 'select'
  876. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  877. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  878. $sHTMLValue .= "<option value=\"0\">".Dict::S('UI:SelectOne')."</option>\n";
  879. foreach($aAllowedValues as $key => $display_value)
  880. {
  881. $sSelected = ($value == $key) ? ' selected' : '';
  882. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  883. }
  884. $sHTMLValue .= "</select>$sValidationField\n";
  885. }
  886. }
  887. else
  888. {
  889. $sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses} />$sValidationField";
  890. }
  891. break;
  892. }
  893. $sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
  894. $oPage->add_ready_script("$('#$iInputId').bind('validate blur', function(evt, sFormId) { return ValidateField('$iInputId', '$sPattern', $bMandatory, sFormId) } );"); // Bind to a custom event: validate
  895. $aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
  896. if (count($aDependencies) > 0)
  897. {
  898. $oPage->add_ready_script("$('#$iInputId').bind('change', function(evt, sFormId) { return UpdateDependentFields(['".implode("','", $aDependencies)."']) } );"); // Bind to a custom event: validate
  899. }
  900. }
  901. return $sHTMLValue;
  902. }
  903. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  904. {
  905. static $iFormId = 0;
  906. $iFormId++;
  907. $sClass = get_class($this);
  908. $oAppContext = new ApplicationContext();
  909. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  910. $iKey = $this->GetKey();
  911. $aDetails = array();
  912. $aFieldsMap = array();
  913. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckFields('form_{$iFormId}')\">\n");
  914. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  915. {
  916. if ($oAttDef->IsWritable())
  917. {
  918. if ($sStateAttCode == $sAttCode)
  919. {
  920. // State attribute is always read-only from the UI
  921. $sHTMLValue = $this->GetStateLabel();
  922. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  923. }
  924. else
  925. {
  926. $iFlags = $this->GetAttributeFlags($sAttCode);
  927. if ($iFlags & OPT_ATT_HIDDEN)
  928. {
  929. // Attribute is hidden, do nothing
  930. }
  931. else
  932. {
  933. if ($iFlags & OPT_ATT_READONLY)
  934. {
  935. // Attribute is read-only
  936. $sHTMLValue = $this->GetAsHTML($sAttCode);
  937. }
  938. else
  939. {
  940. $sValue = $this->Get($sAttCode);
  941. $sDisplayValue = $this->GetEditValue($sAttCode);
  942. $aArgs = array('this' => $this);
  943. $sInputId = $iFormId.'_'.$sAttCode;
  944. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs);
  945. $aFieldsMap[$sAttCode] = $sInputId;
  946. }
  947. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  948. }
  949. }
  950. }
  951. }
  952. $oPage->details($aDetails);
  953. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  954. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  955. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  956. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  957. foreach($aExtraParams as $sName => $value)
  958. {
  959. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  960. }
  961. $oPage->add($oAppContext->GetForForm());
  962. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  963. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  964. $oPage->add("</form>\n");
  965. $iFieldsCount = count($aFieldsMap);
  966. $sJsonFieldsMap = json_encode($aFieldsMap);
  967. $oPage->add_script(
  968. <<<EOF
  969. // Initializes the object once at the beginning of the page...
  970. var oWizardHelper = new WizardHelper('$sClass');
  971. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  972. oWizardHelper.SetFieldsCount($iFieldsCount);
  973. EOF
  974. );
  975. }
  976. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  977. {
  978. static $iCreationFormId = 0;
  979. $iCreationFormId++;
  980. $oAppContext = new ApplicationContext();
  981. $aDetails = array();
  982. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  983. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  984. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  985. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" enctype=\"multipart/form-data\" onSubmit=\"return CheckFields('creation_form_{$iCreationFormId}')\">\n");
  986. $aStates = MetaModel::EnumStates($sClass);
  987. if ($oObjectToClone == null)
  988. {
  989. $sTargetState = MetaModel::GetDefaultState($sClass);
  990. }
  991. else
  992. {
  993. $sTargetState = $oObjectToClone->GetState();
  994. }
  995. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  996. {
  997. if ($sStateAttCode == $sAttCode)
  998. {
  999. // State attribute is always read-only from the UI
  1000. $sHTMLValue = $oObjectToClone->GetStateLabel();
  1001. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1002. }
  1003. else if ((!$oAttDef->IsExternalField()) && ($oAttDef->IsWritable()) )
  1004. {
  1005. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  1006. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetEditValue($sAttCode);
  1007. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1008. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
  1009. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1010. }
  1011. }
  1012. $oPage->details($aDetails);
  1013. if ($oObjectToClone != null)
  1014. {
  1015. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  1016. }
  1017. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1018. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  1019. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1020. $oPage->add($oAppContext->GetForForm());
  1021. foreach($aExtraParams as $sName => $value)
  1022. {
  1023. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1024. }
  1025. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1026. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  1027. $oPage->add("</form>\n");
  1028. }
  1029. protected static function GetCSSClasses($aCSSClasses)
  1030. {
  1031. $sCSSClasses = '';
  1032. if (!empty($aCSSClasses))
  1033. {
  1034. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  1035. }
  1036. return $sCSSClasses;
  1037. }
  1038. }
  1039. ?>