cmdbabstract.class.inc.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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>undefined</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. public function GetDisplayValue($sAttCode)
  75. {
  76. $sDisplayValue = "";
  77. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  78. if ($sStateAttCode == $sAttCode)
  79. {
  80. $sDisplayValue = MetaModel::GetStateLabel(get_class($this), $this->Get($sAttCode));
  81. }
  82. else
  83. {
  84. $oAtt = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  85. if ($oAtt->IsExternalKey())
  86. {
  87. $sTargetClass = $oAtt->GetTargetClass();
  88. if ($this->IsNew())
  89. {
  90. // The current object exists only in memory, don't try to query it in the DB !
  91. // instead let's query for the object pointed by the external key, and get its name
  92. $targetObjId = $this->Get($sAttCode);
  93. $oTargetObj = MetaModel::GetObject($sTargetClass, $targetObjId, false); // false => not sure it exists
  94. if (is_object($oTargetObj))
  95. {
  96. $sDisplayValue = $oTargetObj->GetName();
  97. }
  98. }
  99. else
  100. {
  101. // retrieve the "external fields" linked to this external key
  102. foreach (MetaModel::GetExternalFields(get_class($this), $sAttCode) as $oExtField)
  103. {
  104. $aAvailableFields[$oExtField->GetExtAttCode()] = $oExtField->GetAsHTML($this->Get($oExtField->GetCode()));
  105. }
  106. // Use the "name" of the target class as the label of the hyperlink
  107. // unless it's not available in the external fields...
  108. $sExtClassNameAtt = MetaModel::GetNameAttributeCode($sTargetClass);
  109. if (isset($aAvailableFields[$sExtClassNameAtt]))
  110. {
  111. $sDisplayValue = $aAvailableFields[$sExtClassNameAtt];
  112. }
  113. else
  114. {
  115. $sDisplayValue = implode(' / ', $aAvailableFields);
  116. }
  117. }
  118. }
  119. else
  120. {
  121. $sDisplayValue = $this->GetAsHTML($sAttCode);
  122. }
  123. }
  124. return $sDisplayValue;
  125. }
  126. function DisplayBareHeader(WebPage $oPage)
  127. {
  128. // Standard Header with name, actions menu and history block
  129. //
  130. $oPage->add("<div class=\"page_header\">\n");
  131. // action menu
  132. $oSingletonFilter = new DBObjectSearch(get_class($this));
  133. $oSingletonFilter->AddCondition('pkey', array($this->GetKey()));
  134. $oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
  135. $oBlock->Display($oPage, -1);
  136. $oPage->add("<h1>".MetaModel::GetName(get_class($this)).": <span class=\"hilite\">".$this->GetDisplayName()."</span></h1>\n");
  137. // history block (with toggle)
  138. $oHistoryFilter = new DBObjectSearch('CMDBChangeOp');
  139. $oHistoryFilter->AddCondition('objkey', $this->GetKey());
  140. $oHistoryFilter->AddCondition('objclass', get_class($this));
  141. $oBlock = new HistoryBlock($oHistoryFilter, 'toggle', false);
  142. $oBlock->Display($oPage, -1);
  143. $oPage->add("</div>\n");
  144. }
  145. function DisplayBareDetails(WebPage $oPage)
  146. {
  147. $oPage->add($this->GetBareDetails($oPage));
  148. }
  149. function DisplayBareRelations(WebPage $oPage)
  150. {
  151. // Related objects
  152. $oPage->AddTabContainer('Related Objects');
  153. $oPage->SetCurrentTabContainer('Related Objects');
  154. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  155. {
  156. if ((get_class($oAttDef) == 'AttributeLinkedSetIndirect') || (get_class($oAttDef) == 'AttributeLinkedSet'))
  157. {
  158. $oPage->SetCurrentTab($oAttDef->GetLabel());
  159. $oPage->p($oAttDef->GetDescription());
  160. if (get_class($oAttDef) == 'AttributeLinkedSet')
  161. {
  162. $sTargetClass = $oAttDef->GetLinkedClass();
  163. $oFilter = new DBObjectSearch($sTargetClass);
  164. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey()); // @@@ condition has same name as field ??
  165. $oBlock = new DisplayBlock($oFilter, 'list', false);
  166. $oBlock->Display($oPage, 0);
  167. }
  168. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  169. {
  170. $sLinkClass = $oAttDef->GetLinkedClass();
  171. // Transform the DBObjectSet into a CMBDObjectSet !!!
  172. $aLinkedObjects = $this->Get($sAttCode)->ToArray(false);
  173. if (count($aLinkedObjects) > 0)
  174. {
  175. $oSet = CMDBObjectSet::FromArray($sLinkClass, $aLinkedObjects);
  176. $aParams = array(
  177. 'link_attr' => $oAttDef->GetExtKeyToMe(),
  178. 'object_id' => $this->GetKey(),
  179. 'target_attr' => $oAttDef->GetExtKeyToRemote(),
  180. );
  181. self::DisplaySet($oPage, $oSet, $aParams);
  182. }
  183. }
  184. }
  185. }
  186. $oPage->SetCurrentTab('');
  187. }
  188. function GetDisplayName()
  189. {
  190. $sDisplayName = '';
  191. if (MetaModel::GetNameAttributeCode(get_class($this)) != '')
  192. {
  193. $sDisplayName = $this->GetAsHTML(MetaModel::GetNameAttributeCode(get_class($this)));
  194. }
  195. return $sDisplayName;
  196. }
  197. function GetBareDetails(WebPage $oPage)
  198. {
  199. $sHtml = '';
  200. $oAppContext = new ApplicationContext();
  201. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  202. $aDetails = array();
  203. $sClass = get_class($this);
  204. $aList = MetaModel::GetZListItems($sClass, 'details');
  205. foreach($aList as $sAttCode)
  206. {
  207. $iFlags = $this->GetAttributeFlags($sAttCode);
  208. if ( ($iFlags & OPT_ATT_HIDDEN) == 0)
  209. {
  210. // The field is visible in the current state of the object
  211. if ($sStateAttCode == $sAttCode)
  212. {
  213. // Special display for the 'state' attribute itself
  214. $sDisplayValue = $this->GetStateLabel();
  215. }
  216. else
  217. {
  218. $sDisplayValue = $this->GetAsHTML($sAttCode);
  219. }
  220. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' => $sDisplayValue);
  221. }
  222. }
  223. $sHtml .= $oPage->GetDetails($aDetails);
  224. // Documents displayed inline (when possible: images, html...)
  225. foreach($aList as $sAttCode)
  226. {
  227. $oAttDef = Metamodel::GetAttributeDef($sClass, $sAttCode);
  228. if ( $oAttDef->GetEditClass() == 'Document')
  229. {
  230. $oDoc = $this->Get($sAttCode);
  231. if (is_object($oDoc) && !$oDoc->IsEmpty())
  232. {
  233. $sHtml .= "<p>Open in New Window: ".$oDoc->GetDisplayLink($sClass, $this->GetKey(), $sAttCode).", \n";
  234. $sHtml .= "Download: ".$oDoc->GetDownloadLink($sClass, $this->GetKey(), $sAttCode)."</p>\n";
  235. $sHtml .= "<div>".$oDoc->GetDisplayInline($sClass, $this->GetKey(), $sAttCode)."</div>\n";
  236. }
  237. }
  238. }
  239. return $sHtml;
  240. }
  241. function DisplayDetails(WebPage $oPage)
  242. {
  243. $sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
  244. if (!empty($sTemplate))
  245. {
  246. $oTemplate = new DisplayTemplate($sTemplate);
  247. $sNameAttCode = MetaModel::GetNameAttributeCode(get_class($this));
  248. $oTemplate->Render($oPage, array('class_name'=> MetaModel::GetName(get_class($this)),'class'=> get_class($this),'pkey'=> $this->GetKey(), 'name' => $this->Get($sNameAttCode)));
  249. }
  250. else
  251. {
  252. // Object's details
  253. // template not found display the object using the *old style*
  254. $this->DisplayBareHeader($oPage);
  255. $this->DisplayBareDetails($oPage);
  256. $this->DisplayBareRelations($oPage);
  257. }
  258. }
  259. function DisplayPreview(WebPage $oPage)
  260. {
  261. $aDetails = array();
  262. $sClass = get_class($this);
  263. $aList = MetaModel::GetZListItems($sClass, 'preview');
  264. foreach($aList as $sAttCode)
  265. {
  266. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  267. }
  268. $oPage->details($aDetails);
  269. }
  270. // Comment by Rom: this helper may be used to display objects of class DBObject
  271. // -> I am using this to display the changes history
  272. public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  273. {
  274. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  275. }
  276. //public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
  277. public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  278. {
  279. static $iListId = 0;
  280. $iListId++;
  281. // Initialize and check the parameters
  282. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  283. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  284. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  285. if (!empty($sLinkageAttribute))
  286. {
  287. if($iLinkedObjectId == 0)
  288. {
  289. // if 'links' mode is requested the id of the object to link to must be specified
  290. throw new ApplicationException("Parameter object_id is mandatory when link_attr is specified. Check the definition of the display template.");
  291. }
  292. if($sTargetAttr == '')
  293. {
  294. // if 'links' mode is requested the d of the object to link to must be specified
  295. throw new ApplicationException("Parameter target_attr is mandatory when link_attr is specified. Check the definition of the display template.");
  296. }
  297. }
  298. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  299. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  300. $bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
  301. $sHtml = '';
  302. $oAppContext = new ApplicationContext();
  303. $sClassName = $oSet->GetFilter()->GetClass();
  304. $aAttribs = array();
  305. $aList = MetaModel::GetZListItems($sClassName, 'list');
  306. if (!empty($sLinkageAttribute))
  307. {
  308. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  309. // and other objects...
  310. // The display will then group all the attributes related to the link itself:
  311. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  312. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  313. assert(isset($aAttDefs[$sLinkageAttribute]));
  314. $oAttDef = $aAttDefs[$sLinkageAttribute];
  315. assert($oAttDef->IsExternalKey());
  316. // First display all the attributes specific to the link record
  317. foreach($aList as $sLinkAttCode)
  318. {
  319. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  320. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  321. {
  322. $aDisplayList[] = $sLinkAttCode;
  323. }
  324. }
  325. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  326. foreach($aList as $sLinkAttCode)
  327. {
  328. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  329. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  330. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  331. {
  332. $aDisplayList[] = $sLinkAttCode;
  333. }
  334. }
  335. // First display all the attributes specific to the link
  336. // Then display all the attributes linked to the other end of the relationship
  337. $aList = $aDisplayList;
  338. }
  339. if ($bSelectMode)
  340. {
  341. if (!$bSingleSelectMode)
  342. {
  343. $aAttribs['form::select'] = array('label' => "<input type=\"checkbox\" onChange=\"var value = this.checked; $('.selectList{$iListId}').each( function() { this.checked = value; } );\"></input>", 'description' => 'Select / Deselect All');
  344. }
  345. else
  346. {
  347. $aAttribs['form::select'] = array('label' => "", 'description' => '');
  348. }
  349. }
  350. $aAttribs['key'] = array('label' => '', 'description' => 'Click to display');
  351. foreach($aList as $sAttCode)
  352. {
  353. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  354. }
  355. $aValues = array();
  356. $oSet->Seek(0);
  357. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  358. $iMaxObjects = -1;
  359. if ($bDisplayLimit)
  360. {
  361. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  362. {
  363. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  364. }
  365. }
  366. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  367. {
  368. $aRow = array();
  369. $aRow['key'] = $oObj->GetKey();
  370. if ($bSelectMode)
  371. {
  372. if ($bSingleSelectMode)
  373. {
  374. $aRow['form::select'] = "<input type=\"radio\" class=\"selectList{$iListId}\" name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  375. }
  376. else
  377. {
  378. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  379. }
  380. }
  381. $aRow['key'] = $oObj->GetKey();
  382. foreach($aList as $sAttCode)
  383. {
  384. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  385. }
  386. $aValues[] = $aRow;
  387. $iMaxObjects--;
  388. }
  389. $sHtml .= '<table class="listContainer">';
  390. $sColspan = '';
  391. if ($bDisplayMenu)
  392. {
  393. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  394. $sColspan = 'colspan="2"';
  395. $aMenuExtraParams = $aExtraParams;
  396. if (!empty($sLinkageAttribute))
  397. {
  398. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  399. $aMenuExtraParams = $aExtraParams;
  400. }
  401. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  402. {
  403. // list truncated
  404. $divId = $aExtraParams['block_id'];
  405. $sFilter = $oSet->GetFilter()->serialize();
  406. $aExtraParams['display_limit'] = false; // To expand the full list
  407. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  408. $sHtml .= '<tr class="containerHeader"><td>'.utils::GetConfig()->GetMinDisplayLimit().' object(s) displayed out of '.$oSet->Count().'&nbsp;&nbsp;<a href="Javascript:ReloadTruncatedList(\''.$divId.'\', \''.$sFilter.'\', \''.$sExtraParams.'\');">Display All</a></td><td>';
  409. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  410. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  411. }
  412. else
  413. {
  414. // Full list
  415. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.$oSet->Count().' object(s)</td><td>';
  416. }
  417. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  418. $sHtml .= '</td></tr>';
  419. }
  420. $sHtml .= "<tr><td $sColspan>";
  421. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$sClassName, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  422. $sHtml .= '</td></tr>';
  423. $sHtml .= '</table>';
  424. return $sHtml;
  425. }
  426. public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  427. {
  428. static $iListId = 0;
  429. $iListId++;
  430. $aList = array();
  431. // Initialize and check the parameters
  432. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  433. // Check if there is a list of aliases to limit the display to...
  434. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  435. $sHtml = '';
  436. $oAppContext = new ApplicationContext();
  437. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  438. $aAuthorizedClasses = array();
  439. foreach($aClasses as $sAlias => $sClassName)
  440. {
  441. if ((UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES) &&
  442. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  443. {
  444. $aAuthorizedClasses[$sAlias] = $sClassName;
  445. }
  446. }
  447. $aAttribs = array();
  448. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  449. {
  450. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  451. $aAttribs['key_'.$sAlias] = array('label' => '', 'description' => 'Click to display');
  452. foreach($aList[$sClassName] as $sAttCode)
  453. {
  454. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  455. }
  456. }
  457. $aValues = array();
  458. $oSet->Seek(0);
  459. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  460. $iMaxObjects = -1;
  461. if ($bDisplayLimit)
  462. {
  463. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  464. {
  465. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  466. }
  467. }
  468. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  469. {
  470. $aRow = array();
  471. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  472. {
  473. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetKey();
  474. foreach($aList[$sClassName] as $sAttCode)
  475. {
  476. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  477. }
  478. }
  479. $aValues[] = $aRow;
  480. $iMaxObjects--;
  481. }
  482. $sHtml .= '<table class="listContainer">';
  483. $sColspan = '';
  484. if ($bDisplayMenu)
  485. {
  486. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  487. $sColspan = 'colspan="2"';
  488. $aMenuExtraParams = $aExtraParams;
  489. if (!empty($sLinkageAttribute))
  490. {
  491. $aMenuExtraParams = $aExtraParams;
  492. }
  493. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  494. {
  495. // list truncated
  496. $divId = $aExtraParams['block_id'];
  497. $sFilter = $oSet->GetFilter()->serialize();
  498. $aExtraParams['display_limit'] = false; // To expand the full list
  499. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  500. $sHtml .= '<tr class="containerHeader"><td>'.utils::GetConfig()->GetMinDisplayLimit().' object(s) displayed out of '.$oSet->Count().'&nbsp;&nbsp;<a href="Javascript:ReloadTruncatedList(\''.$divId.'\', \''.$sFilter.'\', \''.$sExtraParams.'\');">Display All</a></td><td>';
  501. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  502. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  503. }
  504. else
  505. {
  506. // Full list
  507. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.$oSet->Count().' object(s)</td><td>';
  508. }
  509. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  510. $sHtml .= '</td></tr>';
  511. }
  512. $sHtml .= "<tr><td $sColspan>";
  513. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$aAuthorizedClasses, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  514. $sHtml .= '</td></tr>';
  515. $sHtml .= '</table>';
  516. return $sHtml;
  517. }
  518. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  519. {
  520. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  521. }
  522. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  523. {
  524. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  525. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  526. $aList = array();
  527. $oAppContext = new ApplicationContext();
  528. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  529. $aAuthorizedClasses = array();
  530. foreach($aClasses as $sAlias => $sClassName)
  531. {
  532. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  533. {
  534. $aAuthorizedClasses[$sAlias] = $sClassName;
  535. }
  536. }
  537. $aAttribs = array();
  538. $aHeader = array();
  539. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  540. {
  541. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode => $oAttDef)
  542. {
  543. if ($oAttDef->IsScalar())
  544. {
  545. $aList[$sClassName][$sAttCode] = $oAttDef;
  546. }
  547. }
  548. $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  549. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  550. {
  551. if ($oAttDef->IsExternalField())
  552. {
  553. $sExtKeyLabel = MetaModel::GetLabel($sClassName, $oAttDef->GetKeyAttCode());
  554. $sRemoteAttLabel = MetaModel::GetLabel($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  555. $aHeader[] = $sExtKeyLabel.'->'.$sRemoteAttLabel;
  556. }
  557. else
  558. {
  559. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  560. }
  561. }
  562. }
  563. $sHtml = '#'.$oSet->GetFilter()->ToOQL()."\n";
  564. $sHtml .= implode($sSeparator, $aHeader)."\n";
  565. $oSet->Seek(0);
  566. while ($aObjects = $oSet->FetchAssoc())
  567. {
  568. $aRow = array();
  569. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  570. {
  571. $oObj = $aObjects[$sAlias];
  572. $aRow[] = $oObj->GetKey();
  573. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  574. {
  575. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, '\\');
  576. }
  577. }
  578. $sHtml .= implode($sSeparator, $aRow)."\n";
  579. }
  580. return $sHtml;
  581. }
  582. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  583. {
  584. $oAppContext = new ApplicationContext();
  585. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  586. $aAuthorizedClasses = array();
  587. foreach($aClasses as $sAlias => $sClassName)
  588. {
  589. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  590. {
  591. $aAuthorizedClasses[$sAlias] = $sClassName;
  592. }
  593. }
  594. $aAttribs = array();
  595. $aList = array();
  596. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  597. $oPage->add("<Set>\n");
  598. $oSet->Seek(0);
  599. while ($aObjects = $oSet->FetchAssoc())
  600. {
  601. if (count($aAuthorizedClasses) > 1)
  602. {
  603. $oPage->add("<Row>\n");
  604. }
  605. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  606. {
  607. $oObj = $aObjects[$sAlias];
  608. $sClassName = get_class($oObj);
  609. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  610. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  611. {
  612. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()) && ($sAttCode != 'finalclass') )
  613. {
  614. $sValue = $oObj->GetAsXML($sAttCode);
  615. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  616. }
  617. }
  618. $oPage->add("</$sClassName>\n");
  619. }
  620. if (count($aAuthorizedClasses) > 1)
  621. {
  622. $oPage->add("</Row>\n");
  623. }
  624. }
  625. $oPage->add("</Set>\n");
  626. }
  627. // By rom
  628. function DisplayChangesLog(WebPage $oPage)
  629. {
  630. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  631. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  632. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  633. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  634. $count = $oSet->Count();
  635. if ($count > 0)
  636. {
  637. $oPage->p("Changes log ($count):");
  638. self::DisplaySet($oPage, $oSet);
  639. }
  640. else
  641. {
  642. $oPage->p("Changes log is empty");
  643. }
  644. }
  645. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  646. {
  647. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  648. }
  649. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  650. {
  651. static $iSearchFormId = 0;
  652. $oAppContext = new ApplicationContext();
  653. $sHtml = '';
  654. $numCols=4;
  655. $iSearchFormId++;
  656. $sClassName = $oSet->GetFilter()->GetClass();
  657. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  658. // (especially when used to add a link)
  659. /*
  660. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  661. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  662. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  663. </ul></div>\n";
  664. */
  665. // Simple search form
  666. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  667. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  668. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  669. $sHtml .= "<form id=\"form{$iSearchFormId}\">\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 = '* 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=\"\">* 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=\" 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. $sHtml .= "</div><!-- Simple search form -->\n";
  757. // OQL query builder
  758. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  759. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  760. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  761. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  762. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  763. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  764. $sOQLClause = utils::ReadParam('oql_clause', '');
  765. asort($aClasses);
  766. foreach($aClasses as $sChildClass)
  767. {
  768. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  769. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  770. }
  771. $sHtml .= "</select>&nbsp;</td><td>\n";
  772. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  773. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  774. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  775. foreach($aExtraParams as $sName => $sValue)
  776. {
  777. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  778. }
  779. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  780. $sHtml .= $oAppContext->GetForForm();
  781. $sHtml .= "</table></form>\n";
  782. $sHtml .= "</div><!-- OQL query form -->\n";
  783. return $sHtml;
  784. }
  785. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  786. {
  787. static $iInputId = 0;
  788. if (isset($aArgs[$sAttCode]) && empty($value))
  789. {
  790. // default value passed by the context (either the app context of the operation)
  791. $value = $aArgs[$sAttCode];
  792. }
  793. if (!empty($iId))
  794. {
  795. $iInputId = $iId;
  796. }
  797. else
  798. {
  799. $iInputId++;
  800. }
  801. if (!$oAttDef->IsExternalField())
  802. {
  803. $aCSSClasses = array();
  804. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  805. {
  806. $aCSSClasses[] = 'mandatory';
  807. }
  808. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  809. switch($oAttDef->GetEditClass())
  810. {
  811. case 'Date':
  812. $aCSSClasses[] = 'date-pick';
  813. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  814. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  815. break;
  816. case 'Password':
  817. $sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  818. break;
  819. case 'Text':
  820. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>";
  821. break;
  822. case 'List':
  823. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  824. $sHTMLValue = $oWidget->Display($oPage, $value);
  825. break;
  826. case 'Document':
  827. $oDocument = $value; // Value is an ormDocument object
  828. $sFileName = '';
  829. if (is_object($oDocument))
  830. {
  831. $sFileName = $oDocument->GetFileName();
  832. }
  833. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  834. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  835. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iInputId\" \" value=\"$sFileName\"/>\n";
  836. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  837. $sHTMLValue .= "<input name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iInputId\" onChange=\"UpdateFileName('$iInputId', this.value);\"/>\n";
  838. break;
  839. case 'String':
  840. default:
  841. // #@# todo - add context information (depending on dimensions)
  842. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  843. if ($aAllowedValues !== null)
  844. {
  845. //Enum field or external key, display a combo
  846. //if (count($aAllowedValues) == 0)
  847. //{
  848. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  849. //}
  850. //else if (count($aAllowedValues) > 50)
  851. if (count($aAllowedValues) > 50)
  852. {
  853. // too many choices, use an autocomplete
  854. // The input for the auto complete
  855. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
  856. // another hidden input to store & pass the object's Id
  857. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  858. $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."'}});");
  859. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  860. // Prepopulate with a default value -- but no display value...
  861. //if (!empty($value))
  862. //{
  863. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  864. //}
  865. }
  866. else
  867. {
  868. // Few choices, use a normal 'select'
  869. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  870. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  871. $sHTMLValue .= "<option value=\"0\">-- select one --</option>\n";
  872. foreach($aAllowedValues as $key => $display_value)
  873. {
  874. $sSelected = ($value == $key) ? ' selected' : '';
  875. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  876. }
  877. $sHTMLValue .= "</select>\n";
  878. }
  879. }
  880. else
  881. {
  882. $sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses} />";
  883. }
  884. break;
  885. }
  886. }
  887. return $sHTMLValue;
  888. }
  889. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  890. {
  891. static $iFormId = 0;
  892. $iFormId++;
  893. $oAppContext = new ApplicationContext();
  894. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  895. $iKey = $this->GetKey();
  896. $aDetails = array();
  897. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckMandatoryFields('form_{$iFormId}')\">\n");
  898. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  899. {
  900. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  901. {
  902. // Do nothing, the class field is always hidden, it cannot be edited
  903. }
  904. else if ($sStateAttCode == $sAttCode)
  905. {
  906. // State attribute is always read-only from the UI
  907. $sHTMLValue = $this->GetStateLabel();
  908. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  909. }
  910. else if (!$oAttDef->IsExternalField())
  911. {
  912. $iFlags = $this->GetAttributeFlags($sAttCode);
  913. if ($iFlags & OPT_ATT_HIDDEN)
  914. {
  915. // Attribute is hidden, do nothing
  916. }
  917. else
  918. {
  919. if ($iFlags & OPT_ATT_READONLY)
  920. {
  921. // Attribute is read-only
  922. $sHTMLValue = $this->GetAsHTML($sAttCode);
  923. }
  924. else
  925. {
  926. $sValue = $this->Get($sAttCode);
  927. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  928. $aArgs = array('this' => $this);
  929. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iFlags, $aArgs);
  930. }
  931. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  932. }
  933. }
  934. }
  935. $oPage->details($aDetails);
  936. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  937. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  938. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  939. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  940. foreach($aExtraParams as $sName => $value)
  941. {
  942. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  943. }
  944. $oPage->add($oAppContext->GetForForm());
  945. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  946. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  947. $oPage->add("</form>\n");
  948. }
  949. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  950. {
  951. static $iCreationFormId = 0;
  952. $iCreationFormId++;
  953. $oAppContext = new ApplicationContext();
  954. $aDetails = array();
  955. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  956. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  957. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  958. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" onSubmit=\"return CheckMandatoryFields('creation_form_{$iCreationFormId}')\">\n");
  959. $aStates = MetaModel::EnumStates($sClass);
  960. if ($oObjectToClone == null)
  961. {
  962. $sTargetState = MetaModel::GetDefaultState($sClass);
  963. }
  964. else
  965. {
  966. $sTargetState = $oObjectToClone->GetState();
  967. }
  968. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  969. {
  970. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  971. {
  972. // Do nothing, the class field is always hidden, it cannot be edited
  973. }
  974. else if ($sStateAttCode == $sAttCode)
  975. {
  976. // State attribute is always read-only from the UI
  977. $sHTMLValue = $oObjectToClone->GetStateLabel();
  978. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  979. }
  980. else if (!$oAttDef->IsExternalField())
  981. {
  982. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  983. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  984. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  985. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
  986. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  987. }
  988. }
  989. $oPage->details($aDetails);
  990. if ($oObjectToClone != null)
  991. {
  992. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  993. }
  994. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  995. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  996. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  997. $oPage->add($oAppContext->GetForForm());
  998. foreach($aExtraParams as $sName => $value)
  999. {
  1000. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1001. }
  1002. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1003. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  1004. $oPage->add("</form>\n");
  1005. }
  1006. protected static function GetCSSClasses($aCSSClasses)
  1007. {
  1008. $sCSSClasses = '';
  1009. if (!empty($aCSSClasses))
  1010. {
  1011. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  1012. }
  1013. return $sCSSClasses;
  1014. }
  1015. }
  1016. ?>