cmdbabstract.class.inc.php 39 KB

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