cmdbabstract.class.inc.php 40 KB

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