cmdbabstract.class.inc.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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. // Check if there is a list of aliases to limit the display to...
  435. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  436. $sHtml = '';
  437. $oAppContext = new ApplicationContext();
  438. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  439. $aAuthorizedClasses = array();
  440. foreach($aClasses as $sAlias => $sClassName)
  441. {
  442. if ((UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES) &&
  443. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  444. {
  445. $aAuthorizedClasses[$sAlias] = $sClassName;
  446. }
  447. }
  448. $aAttribs = array();
  449. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  450. {
  451. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  452. $aAttribs['key_'.$sAlias] = array('label' => '', 'description' => 'Click to display');
  453. foreach($aList[$sClassName] as $sAttCode)
  454. {
  455. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  456. }
  457. }
  458. $aValues = array();
  459. $oSet->Seek(0);
  460. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  461. $iMaxObjects = -1;
  462. if ($bDisplayLimit)
  463. {
  464. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  465. {
  466. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  467. }
  468. }
  469. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  470. {
  471. $aRow = array();
  472. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  473. {
  474. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetKey();
  475. foreach($aList[$sClassName] as $sAttCode)
  476. {
  477. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  478. }
  479. }
  480. $aValues[] = $aRow;
  481. $iMaxObjects--;
  482. }
  483. $sHtml .= '<table class="listContainer">';
  484. $sColspan = '';
  485. if ($bDisplayMenu)
  486. {
  487. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  488. $sColspan = 'colspan="2"';
  489. $aMenuExtraParams = $aExtraParams;
  490. if (!empty($sLinkageAttribute))
  491. {
  492. $aMenuExtraParams = $aExtraParams;
  493. }
  494. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  495. {
  496. // list truncated
  497. $divId = $aExtraParams['block_id'];
  498. $sFilter = $oSet->GetFilter()->serialize();
  499. $aExtraParams['display_limit'] = false; // To expand the full list
  500. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  501. $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>';
  502. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  503. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  504. }
  505. else
  506. {
  507. // Full list
  508. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.$oSet->Count().' object(s)</td><td>';
  509. }
  510. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  511. $sHtml .= '</td></tr>';
  512. }
  513. $sHtml .= "<tr><td $sColspan>";
  514. $sHtml .= $oPage->GetTable($aAttribs, $aValues, array('class'=>$aAuthorizedClasses, 'filter'=>$oSet->GetFilter()->serialize(), 'preview' => true));
  515. $sHtml .= '</td></tr>';
  516. $sHtml .= '</table>';
  517. return $sHtml;
  518. }
  519. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  520. {
  521. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  522. }
  523. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  524. {
  525. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  526. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  527. $aList = array();
  528. $oAppContext = new ApplicationContext();
  529. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  530. $aAuthorizedClasses = array();
  531. foreach($aClasses as $sAlias => $sClassName)
  532. {
  533. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  534. {
  535. $aAuthorizedClasses[$sAlias] = $sClassName;
  536. }
  537. }
  538. $aAttribs = array();
  539. $aHeader = array();
  540. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  541. {
  542. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  543. $aHeader[] = MetaModel::GetKeyLabel($sClassName);
  544. foreach($aList[$sClassName] as $sAttCode)
  545. {
  546. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  547. }
  548. }
  549. $sHtml = '#'.$oSet->GetFilter()->ToOQL()."\n";
  550. $sHtml .= implode($sSeparator, $aHeader)."\n";
  551. $oSet->Seek(0);
  552. while ($aObjects = $oSet->FetchAssoc())
  553. {
  554. $aRow = array();
  555. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  556. {
  557. $oObj = $aObjects[$sAlias];
  558. $aRow[] = $oObj->GetKey();
  559. foreach($aList[$sClassName] as $sAttCode)
  560. {
  561. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, '\\');
  562. }
  563. }
  564. $sHtml .= implode($sSeparator, $aRow)."\n";
  565. }
  566. return $sHtml;
  567. }
  568. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  569. {
  570. $oAppContext = new ApplicationContext();
  571. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  572. $aAuthorizedClasses = array();
  573. foreach($aClasses as $sAlias => $sClassName)
  574. {
  575. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  576. {
  577. $aAuthorizedClasses[$sAlias] = $sClassName;
  578. }
  579. }
  580. $aAttribs = array();
  581. $aList = array();
  582. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  583. $oPage->add("<Set>\n");
  584. $oSet->Seek(0);
  585. while ($aObjects = $oSet->FetchAssoc())
  586. {
  587. if (count($aAuthorizedClasses) > 1)
  588. {
  589. $oPage->add("<Row>\n");
  590. }
  591. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  592. {
  593. $oObj = $aObjects[$sAlias];
  594. $sClassName = get_class($oObj);
  595. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  596. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  597. {
  598. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()) && ($sAttCode != 'finalclass') )
  599. {
  600. $sValue = $oObj->GetAsXML($sAttCode);
  601. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  602. }
  603. }
  604. $oPage->add("</$sClassName>\n");
  605. }
  606. if (count($aAuthorizedClasses) > 1)
  607. {
  608. $oPage->add("</Row>\n");
  609. }
  610. }
  611. $oPage->add("</Set>\n");
  612. }
  613. // By rom
  614. function DisplayChangesLog(WebPage $oPage)
  615. {
  616. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  617. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  618. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  619. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  620. $count = $oSet->Count();
  621. if ($count > 0)
  622. {
  623. $oPage->p("Changes log ($count):");
  624. self::DisplaySet($oPage, $oSet);
  625. }
  626. else
  627. {
  628. $oPage->p("Changes log is empty");
  629. }
  630. }
  631. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  632. {
  633. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  634. }
  635. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  636. {
  637. static $iSearchFormId = 0;
  638. $oAppContext = new ApplicationContext();
  639. $sHtml = '';
  640. $numCols=4;
  641. $iSearchFormId++;
  642. $sClassName = $oSet->GetFilter()->GetClass();
  643. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  644. // (especially when used to add a link)
  645. /*
  646. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  647. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  648. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  649. </ul></div>\n";
  650. */
  651. // Simple search form
  652. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  653. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  654. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  655. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  656. $index = 0;
  657. $sHtml .= "<table>\n";
  658. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  659. $aMapCriteria = array();
  660. foreach($aFilterCriteria as $aCriteria)
  661. {
  662. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  663. }
  664. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  665. foreach($aList as $sFilterCode)
  666. {
  667. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  668. if (($index % $numCols) == 0)
  669. {
  670. if ($index != 0)
  671. {
  672. $sHtml .= "</tr>\n";
  673. }
  674. $sHtml .= "<tr>\n";
  675. }
  676. $sFilterValue = '';
  677. $sFilterValue = utils::ReadParam($sFilterCode, '');
  678. $sFilterOpCode = null; // Use the default 'loose' OpCode
  679. if (empty($sFilterValue))
  680. {
  681. if (isset($aMapCriteria[$sFilterCode]))
  682. {
  683. if (count($aMapCriteria[$sFilterCode]) > 1)
  684. {
  685. $sFilterValue = '* mixed *';
  686. }
  687. else
  688. {
  689. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  690. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  691. }
  692. if ($sFilterCode != 'company')
  693. {
  694. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  695. }
  696. }
  697. }
  698. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  699. if ($aAllowedValues != null)
  700. {
  701. //Enum field or external key, display a combo
  702. $sValue = "<select name=\"$sFilterCode\">\n";
  703. $sValue .= "<option value=\"\">* Any *</option>\n";
  704. foreach($aAllowedValues as $key => $value)
  705. {
  706. if ($sFilterValue == $key)
  707. {
  708. $sSelected = ' selected';
  709. }
  710. else
  711. {
  712. $sSelected = '';
  713. }
  714. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  715. }
  716. $sValue .= "</select>\n";
  717. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  718. }
  719. else
  720. {
  721. // Any value is possible, display an input box
  722. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  723. }
  724. $index++;
  725. }
  726. if (($index % $numCols) != 0)
  727. {
  728. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  729. }
  730. $sHtml .= "</tr>\n";
  731. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  732. $sHtml .= "</table>\n";
  733. foreach($aExtraParams as $sName => $sValue)
  734. {
  735. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  736. }
  737. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  738. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  739. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  740. $sHtml .= $oAppContext->GetForForm();
  741. $sHtml .= "</form>\n";
  742. $sHtml .= "</div><!-- Simple search form -->\n";
  743. // OQL query builder
  744. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  745. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  746. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  747. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  748. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  749. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  750. $sOQLClause = utils::ReadParam('oql_clause', '');
  751. asort($aClasses);
  752. foreach($aClasses as $sChildClass)
  753. {
  754. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  755. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  756. }
  757. $sHtml .= "</select>&nbsp;</td><td>\n";
  758. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  759. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  760. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  761. foreach($aExtraParams as $sName => $sValue)
  762. {
  763. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  764. }
  765. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  766. $sHtml .= $oAppContext->GetForForm();
  767. $sHtml .= "</table></form>\n";
  768. $sHtml .= "</div><!-- OQL query form -->\n";
  769. return $sHtml;
  770. }
  771. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  772. {
  773. static $iInputId = 0;
  774. if (isset($aArgs[$sAttCode]) && empty($value))
  775. {
  776. // default value passed by the context (either the app context of the operation)
  777. $value = $aArgs[$sAttCode];
  778. }
  779. if (!empty($iId))
  780. {
  781. $iInputId = $iId;
  782. }
  783. else
  784. {
  785. $iInputId++;
  786. }
  787. if (!$oAttDef->IsExternalField())
  788. {
  789. $aCSSClasses = array();
  790. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  791. {
  792. $aCSSClasses[] = 'mandatory';
  793. }
  794. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  795. switch($oAttDef->GetEditClass())
  796. {
  797. case 'Date':
  798. $aCSSClasses[] = 'date-pick';
  799. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  800. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  801. break;
  802. case 'Password':
  803. $sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  804. break;
  805. case 'Text':
  806. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>";
  807. break;
  808. case 'List':
  809. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  810. $sHTMLValue = $oWidget->Display($oPage, $value);
  811. break;
  812. case 'Document':
  813. $oDocument = $value; // Value is an ormDocument object
  814. $sFileName = '';
  815. if (is_object($oDocument))
  816. {
  817. $sFileName = $oDocument->GetFileName();
  818. }
  819. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  820. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  821. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iInputId\" \" value=\"$sFileName\"/>\n";
  822. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  823. $sHTMLValue .= "<input name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iInputId\" onChange=\"UpdateFileName('$iInputId', this.value);\"/>\n";
  824. break;
  825. case 'String':
  826. default:
  827. // #@# todo - add context information (depending on dimensions)
  828. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  829. if ($aAllowedValues !== null)
  830. {
  831. //Enum field or external key, display a combo
  832. //if (count($aAllowedValues) == 0)
  833. //{
  834. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  835. //}
  836. //else if (count($aAllowedValues) > 50)
  837. if (count($aAllowedValues) > 50)
  838. {
  839. // too many choices, use an autocomplete
  840. // The input for the auto complete
  841. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
  842. // another hidden input to store & pass the object's Id
  843. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  844. $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."'}});");
  845. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  846. // Prepopulate with a default value -- but no display value...
  847. //if (!empty($value))
  848. //{
  849. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  850. //}
  851. }
  852. else
  853. {
  854. // Few choices, use a normal 'select'
  855. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  856. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  857. $sHTMLValue .= "<option value=\"0\">-- select one --</option>\n";
  858. foreach($aAllowedValues as $key => $display_value)
  859. {
  860. $sSelected = ($value == $key) ? ' selected' : '';
  861. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  862. }
  863. $sHTMLValue .= "</select>\n";
  864. }
  865. }
  866. else
  867. {
  868. $sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses} />";
  869. }
  870. break;
  871. }
  872. }
  873. return $sHTMLValue;
  874. }
  875. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  876. {
  877. static $iFormId = 0;
  878. $iFormId++;
  879. $oAppContext = new ApplicationContext();
  880. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  881. $iKey = $this->GetKey();
  882. $aDetails = array();
  883. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckMandatoryFields('form_{$iFormId}')\">\n");
  884. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  885. {
  886. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  887. {
  888. // Do nothing, the class field is always hidden, it cannot be edited
  889. }
  890. else if ($sStateAttCode == $sAttCode)
  891. {
  892. // State attribute is always read-only from the UI
  893. $sHTMLValue = $this->GetState();
  894. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  895. }
  896. else if (!$oAttDef->IsExternalField())
  897. {
  898. $iFlags = $this->GetAttributeFlags($sAttCode);
  899. if ($iFlags & OPT_ATT_HIDDEN)
  900. {
  901. // Attribute is hidden, do nothing
  902. }
  903. else
  904. {
  905. if ($iFlags & OPT_ATT_READONLY)
  906. {
  907. // Attribute is read-only
  908. $sHTMLValue = $this->GetAsHTML($sAttCode);
  909. }
  910. else
  911. {
  912. $sValue = $this->Get($sAttCode);
  913. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  914. $aArgs = array('this' => $this);
  915. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iFlags, $aArgs);
  916. }
  917. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  918. }
  919. }
  920. }
  921. $oPage->details($aDetails);
  922. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  923. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  924. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  925. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  926. foreach($aExtraParams as $sName => $value)
  927. {
  928. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  929. }
  930. $oPage->add($oAppContext->GetForForm());
  931. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  932. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  933. $oPage->add("</form>\n");
  934. }
  935. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  936. {
  937. static $iCreationFormId = 0;
  938. $iCreationFormId++;
  939. $oAppContext = new ApplicationContext();
  940. $aDetails = array();
  941. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  942. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  943. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  944. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" onSubmit=\"return CheckMandatoryFields('creation_form_{$iCreationFormId}')\">\n");
  945. $aStates = MetaModel::EnumStates($sClass);
  946. if ($oObjectToClone == null)
  947. {
  948. $sTargetState = MetaModel::GetDefaultState($sClass);
  949. }
  950. else
  951. {
  952. $sTargetState = $oObjectToClone->GetState();
  953. }
  954. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  955. {
  956. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  957. {
  958. // Do nothing, the class field is always hidden, it cannot be edited
  959. }
  960. else if ($sStateAttCode == $sAttCode)
  961. {
  962. // State attribute is always read-only from the UI
  963. $sHTMLValue = $oObjectToClone->GetState();
  964. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  965. }
  966. else if (!$oAttDef->IsExternalField())
  967. {
  968. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  969. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  970. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  971. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
  972. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  973. }
  974. }
  975. $oPage->details($aDetails);
  976. if ($oObjectToClone != null)
  977. {
  978. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  979. }
  980. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  981. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  982. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  983. $oPage->add($oAppContext->GetForForm());
  984. foreach($aExtraParams as $sName => $value)
  985. {
  986. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  987. }
  988. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  989. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  990. $oPage->add("</form>\n");
  991. }
  992. protected static function GetCSSClasses($aCSSClasses)
  993. {
  994. $sCSSClasses = '';
  995. if (!empty($aCSSClasses))
  996. {
  997. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  998. }
  999. return $sCSSClasses;
  1000. }
  1001. }
  1002. ?>