cmdbabstract.class.inc.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. $sClassName = $oSet->GetFilter()->GetClass();
  569. $aAttribs = array();
  570. $aList = MetaModel::GetZListItems($sClassName, 'details');
  571. $oPage->add("<Set>\n");
  572. $oSet->Seek(0);
  573. while ($oObj = $oSet->Fetch())
  574. {
  575. $sClassName = get_class($oObj);
  576. $oPage->add("<$sClassName id=\"".$oObj->GetKey()."\">\n");
  577. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  578. {
  579. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()) && ($sAttCode != 'finalclass') )
  580. {
  581. $sValue = $oObj->GetAsXML($sAttCode);
  582. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  583. }
  584. }
  585. $oPage->add("</$sClassName>\n");
  586. }
  587. $oPage->add("</Set>\n");
  588. }
  589. // By rom
  590. function DisplayChangesLog(WebPage $oPage)
  591. {
  592. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  593. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  594. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  595. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  596. $count = $oSet->Count();
  597. if ($count > 0)
  598. {
  599. $oPage->p("Changes log ($count):");
  600. self::DisplaySet($oPage, $oSet);
  601. }
  602. else
  603. {
  604. $oPage->p("Changes log is empty");
  605. }
  606. }
  607. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  608. {
  609. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  610. }
  611. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  612. {
  613. static $iSearchFormId = 0;
  614. $oAppContext = new ApplicationContext();
  615. $sHtml = '';
  616. $numCols=4;
  617. $iSearchFormId++;
  618. $sClassName = $oSet->GetFilter()->GetClass();
  619. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  620. // (especially when used to add a link)
  621. /*
  622. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  623. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">OQL Query</a></li>
  624. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">Simple Search</a></li>
  625. </ul></div>\n";
  626. */
  627. // Simple search form
  628. $sHtml .= "<div id=\"SimpleSearchForm{$iSearchFormId}\" class=\"mini_tab{$iSearchFormId}\">\n";
  629. $sHtml .= "<h1>Search for ".MetaModel::GetName($sClassName)." Objects</h1>\n";
  630. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  631. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  632. $index = 0;
  633. $sHtml .= "<table>\n";
  634. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  635. $aMapCriteria = array();
  636. foreach($aFilterCriteria as $aCriteria)
  637. {
  638. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  639. }
  640. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  641. foreach($aList as $sFilterCode)
  642. {
  643. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  644. if (($index % $numCols) == 0)
  645. {
  646. if ($index != 0)
  647. {
  648. $sHtml .= "</tr>\n";
  649. }
  650. $sHtml .= "<tr>\n";
  651. }
  652. $sFilterValue = '';
  653. $sFilterValue = utils::ReadParam($sFilterCode, '');
  654. $sFilterOpCode = null; // Use the default 'loose' OpCode
  655. if (empty($sFilterValue))
  656. {
  657. if (isset($aMapCriteria[$sFilterCode]))
  658. {
  659. if (count($aMapCriteria[$sFilterCode]) > 1)
  660. {
  661. $sFilterValue = '* mixed *';
  662. }
  663. else
  664. {
  665. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  666. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  667. }
  668. if ($sFilterCode != 'company')
  669. {
  670. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  671. }
  672. }
  673. }
  674. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  675. if ($aAllowedValues != null)
  676. {
  677. //Enum field or external key, display a combo
  678. $sValue = "<select name=\"$sFilterCode\">\n";
  679. $sValue .= "<option value=\"\">* Any *</option>\n";
  680. foreach($aAllowedValues as $key => $value)
  681. {
  682. if ($sFilterValue == $key)
  683. {
  684. $sSelected = ' selected';
  685. }
  686. else
  687. {
  688. $sSelected = '';
  689. }
  690. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  691. }
  692. $sValue .= "</select>\n";
  693. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  694. }
  695. else
  696. {
  697. // Any value is possible, display an input box
  698. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  699. }
  700. $index++;
  701. }
  702. if (($index % $numCols) != 0)
  703. {
  704. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  705. }
  706. $sHtml .= "</tr>\n";
  707. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\" Search \"></td></tr>\n";
  708. $sHtml .= "</table>\n";
  709. foreach($aExtraParams as $sName => $sValue)
  710. {
  711. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  712. }
  713. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  714. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  715. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  716. $sHtml .= $oAppContext->GetForForm();
  717. $sHtml .= "</form>\n";
  718. $sHtml .= "</div><!-- Simple search form -->\n";
  719. // OQL query builder
  720. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  721. $sHtml .= "<h1>OQL Query Builder</h1>\n";
  722. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  723. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  724. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  725. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  726. $sOQLClause = utils::ReadParam('oql_clause', '');
  727. asort($aClasses);
  728. foreach($aClasses as $sChildClass)
  729. {
  730. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  731. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  732. }
  733. $sHtml .= "</select>&nbsp;</td><td>\n";
  734. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  735. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\" Query \"></td></tr>\n";
  736. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  737. foreach($aExtraParams as $sName => $sValue)
  738. {
  739. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  740. }
  741. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  742. $sHtml .= $oAppContext->GetForForm();
  743. $sHtml .= "</table></form>\n";
  744. $sHtml .= "</div><!-- OQL query form -->\n";
  745. return $sHtml;
  746. }
  747. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  748. {
  749. static $iInputId = 0;
  750. if (isset($aArgs[$sAttCode]) && empty($value))
  751. {
  752. // default value passed by the context (either the app context of the operation)
  753. $value = $aArgs[$sAttCode];
  754. }
  755. if (!empty($iId))
  756. {
  757. $iInputId = $iId;
  758. }
  759. else
  760. {
  761. $iInputId++;
  762. }
  763. if (!$oAttDef->IsExternalField())
  764. {
  765. $aCSSClasses = array();
  766. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  767. {
  768. $aCSSClasses[] = 'mandatory';
  769. }
  770. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  771. switch($oAttDef->GetEditClass())
  772. {
  773. case 'Date':
  774. $aCSSClasses[] = 'date-pick';
  775. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  776. $sHTMLValue = "<input type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  777. break;
  778. case 'Password':
  779. $sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
  780. break;
  781. case 'Text':
  782. $sHTMLValue = "<textarea name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iInputId\"{$sCSSClasses}>$value</textarea>";
  783. break;
  784. case 'List':
  785. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sNameSuffix);
  786. $sHTMLValue = $oWidget->Display($oPage, $value);
  787. break;
  788. case 'Document':
  789. $oDocument = $value; // Value is an ormDocument object
  790. $sFileName = '';
  791. if (is_object($oDocument))
  792. {
  793. $sFileName = $oDocument->GetFileName();
  794. }
  795. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  796. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  797. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iInputId\" \" value=\"$sFileName\"/>\n";
  798. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  799. $sHTMLValue .= "<input name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iInputId\" onChange=\"UpdateFileName('$iInputId', this.value);\"/>\n";
  800. break;
  801. case 'String':
  802. default:
  803. // #@# todo - add context information (depending on dimensions)
  804. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  805. if ($aAllowedValues !== null)
  806. {
  807. //Enum field or external key, display a combo
  808. //if (count($aAllowedValues) == 0)
  809. //{
  810. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  811. //}
  812. //else if (count($aAllowedValues) > 50)
  813. if (count($aAllowedValues) > 50)
  814. {
  815. // too many choices, use an autocomplete
  816. // The input for the auto complete
  817. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iInputId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
  818. // another hidden input to store & pass the object's Id
  819. $sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  820. $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."'}});");
  821. $oPage->add_ready_script("\$('#label_$iInputId').result( function(event, data, formatted) { if (data) { $('#{$iInputId}').val(data[1]); } } );");
  822. // Prepopulate with a default value -- but no display value...
  823. //if (!empty($value))
  824. //{
  825. // $oPage->add_ready_script("\$('#label_$iInputId').search( 'domino.combodo.com' );");
  826. //}
  827. }
  828. else
  829. {
  830. // Few choices, use a normal 'select'
  831. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  832. $sHTMLValue = "<select name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}>\n";
  833. $sHTMLValue .= "<option value=\"0\">-- select one --</option>\n";
  834. foreach($aAllowedValues as $key => $display_value)
  835. {
  836. $sSelected = ($value == $key) ? ' selected' : '';
  837. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  838. }
  839. $sHTMLValue .= "</select>\n";
  840. }
  841. }
  842. else
  843. {
  844. $sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses} />";
  845. }
  846. break;
  847. }
  848. }
  849. return $sHTMLValue;
  850. }
  851. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  852. {
  853. static $iFormId = 0;
  854. $iFormId++;
  855. $oAppContext = new ApplicationContext();
  856. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  857. $iKey = $this->GetKey();
  858. $aDetails = array();
  859. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckMandatoryFields('form_{$iFormId}')\">\n");
  860. foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode=>$oAttDef)
  861. {
  862. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  863. {
  864. // Do nothing, the class field is always hidden, it cannot be edited
  865. }
  866. else if ($sStateAttCode == $sAttCode)
  867. {
  868. // State attribute is always read-only from the UI
  869. $sHTMLValue = $this->GetState();
  870. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  871. }
  872. else if (!$oAttDef->IsExternalField())
  873. {
  874. $iFlags = $this->GetAttributeFlags($sAttCode);
  875. if ($iFlags & OPT_ATT_HIDDEN)
  876. {
  877. // Attribute is hidden, do nothing
  878. }
  879. else
  880. {
  881. if ($iFlags & OPT_ATT_READONLY)
  882. {
  883. // Attribute is read-only
  884. $sHTMLValue = $this->GetAsHTML($sAttCode);
  885. }
  886. else
  887. {
  888. $sValue = $this->Get($sAttCode);
  889. $sDisplayValue = $this->GetDisplayValue($sAttCode);
  890. $aArgs = array('this' => $this);
  891. $sHTMLValue = self::GetFormElementForField($oPage, get_class($this), $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iFlags, $aArgs);
  892. }
  893. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  894. }
  895. }
  896. }
  897. $oPage->details($aDetails);
  898. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  899. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"".get_class($this)."\">\n");
  900. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  901. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  902. foreach($aExtraParams as $sName => $value)
  903. {
  904. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  905. }
  906. $oPage->add($oAppContext->GetForForm());
  907. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  908. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  909. $oPage->add("</form>\n");
  910. }
  911. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  912. {
  913. static $iCreationFormId = 0;
  914. $iCreationFormId++;
  915. $oAppContext = new ApplicationContext();
  916. $aDetails = array();
  917. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  918. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  919. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  920. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" onSubmit=\"return CheckMandatoryFields('creation_form_{$iCreationFormId}')\">\n");
  921. $aStates = MetaModel::EnumStates($sClass);
  922. if ($oObjectToClone == null)
  923. {
  924. $sTargetState = MetaModel::GetDefaultState($sClass);
  925. }
  926. else
  927. {
  928. $sTargetState = $oObjectToClone->GetState();
  929. }
  930. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode=>$oAttDef)
  931. {
  932. if ('finalclass' == $sAttCode) // finalclass is a reserved word, hardcoded !
  933. {
  934. // Do nothing, the class field is always hidden, it cannot be edited
  935. }
  936. else if ($sStateAttCode == $sAttCode)
  937. {
  938. // State attribute is always read-only from the UI
  939. $sHTMLValue = $oObjectToClone->GetState();
  940. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  941. }
  942. else if (!$oAttDef->IsExternalField())
  943. {
  944. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  945. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
  946. $iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  947. $sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
  948. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  949. }
  950. }
  951. $oPage->details($aDetails);
  952. if ($oObjectToClone != null)
  953. {
  954. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  955. }
  956. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  957. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  958. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  959. $oPage->add($oAppContext->GetForForm());
  960. foreach($aExtraParams as $sName => $value)
  961. {
  962. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  963. }
  964. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>Cancel</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  965. $oPage->add("<button type=\"submit\" class=\"action\"><span>Apply</span></button>\n");
  966. $oPage->add("</form>\n");
  967. }
  968. protected static function GetCSSClasses($aCSSClasses)
  969. {
  970. $sCSSClasses = '';
  971. if (!empty($aCSSClasses))
  972. {
  973. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  974. }
  975. return $sCSSClasses;
  976. }
  977. }
  978. ?>