cmdbabstract.class.inc.php 40 KB

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