cmdbabstract.class.inc.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Abstract class that implements some common and useful methods for displaying
  18. * the objects
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. define('OBJECT_PROPERTIES_TAB', 'ObjectProperties');
  26. require_once('../core/cmdbobject.class.inc.php');
  27. require_once('../application/utils.inc.php');
  28. require_once('../application/applicationcontext.class.inc.php');
  29. require_once('../application/ui.linkswidget.class.inc.php');
  30. abstract class cmdbAbstractObject extends CMDBObject
  31. {
  32. public static function GetUIPage()
  33. {
  34. return '../pages/UI.php';
  35. }
  36. public static function ComputeUIPage($sClass)
  37. {
  38. static $aUIPagesCache = array(); // Cache to store the php page used to display each class of object
  39. if (!isset($aUIPagesCache[$sClass]))
  40. {
  41. $UIPage = false;
  42. if (is_callable("$sClass::GetUIPage"))
  43. {
  44. $UIPage = eval("return $sClass::GetUIPage();"); // May return false in case of error
  45. }
  46. $aUIPagesCache[$sClass] = $UIPage === false ? './UI.php' : $UIPage;
  47. }
  48. $sPage = $aUIPagesCache[$sClass];
  49. return $sPage;
  50. }
  51. protected static function MakeHyperLink($sObjClass, $sObjKey, $aAvailableFields)
  52. {
  53. if ($sObjKey <= 0) return '<em>'.Dict::S('UI:UndefinedObject').'</em>'; // Objects built in memory have negative IDs
  54. $oAppContext = new ApplicationContext();
  55. $sExtClassNameAtt = MetaModel::GetNameAttributeCode($sObjClass);
  56. $sPage = self::ComputeUIPage($sObjClass);
  57. $sAbsoluteUrl = utils::GetAbsoluteUrl(false); // False => Don't get the query string
  58. $sAbsoluteUrl = substr($sAbsoluteUrl, 0, 1+strrpos($sAbsoluteUrl, '/')); // remove the current page, keep just the path, up to the last /
  59. // Use the "name" of the target class as the label of the hyperlink
  60. // unless it's not available in the external attributes...
  61. if (isset($aAvailableFields[$sExtClassNameAtt]))
  62. {
  63. $sLabel = $aAvailableFields[$sExtClassNameAtt];
  64. }
  65. else
  66. {
  67. $sLabel = implode(' / ', $aAvailableFields);
  68. }
  69. // Safety belt
  70. //
  71. if (empty($sLabel))
  72. {
  73. // Developer's note:
  74. // This is doing the job for you, but that is just there in case
  75. // the external fields associated to the external key are blanks
  76. // The ultimate solution will be to query the name automatically
  77. // and independantly from the data model (automatic external field)
  78. // AND make the name be a mandatory field
  79. //
  80. $sObject = MetaModel::GetObject($sObjClass, $sObjKey);
  81. $sLabel = $sObject->GetName();
  82. }
  83. // Safety net
  84. //
  85. if (empty($sLabel))
  86. {
  87. $sLabel = MetaModel::GetName($sObjClass)." #$sObjKey";
  88. }
  89. $sHint = MetaModel::GetName($sObjClass)."::$sObjKey";
  90. return "<a href=\"{$sAbsoluteUrl}{$sPage}?operation=details&class=$sObjClass&id=$sObjKey&".$oAppContext->GetForLink()."\" title=\"$sHint\">$sLabel</a>";
  91. }
  92. function DisplayBareHeader(WebPage $oPage)
  93. {
  94. // Standard Header with name, actions menu and history block
  95. //
  96. // action menu
  97. $oSingletonFilter = new DBObjectSearch(get_class($this));
  98. $oSingletonFilter->AddCondition('id', array($this->GetKey()));
  99. $oBlock = new MenuBlock($oSingletonFilter, 'popup', false);
  100. $oBlock->Display($oPage, -1);
  101. $oPage->add("<div class=\"page_header\"><h1><img src=\"".$this->GetIcon()."\" style=\"margin-right:10px;margin-top: -16px;vertical-align:middle;\">\n");
  102. $oPage->add(MetaModel::GetName(get_class($this)).": <span class=\"hilite\">".$this->GetName()."</span></h1>\n");
  103. $oPage->add("</div>\n");
  104. }
  105. function DisplayBareHistory(WebPage $oPage)
  106. {
  107. // history block (with as a tab)
  108. $oHistoryFilter = new DBObjectSearch('CMDBChangeOp');
  109. $oHistoryFilter->AddCondition('objkey', $this->GetKey());
  110. $oHistoryFilter->AddCondition('objclass', get_class($this));
  111. $oBlock = new HistoryBlock($oHistoryFilter, 'tab', false);
  112. $oBlock->Display($oPage, -1);
  113. }
  114. function DisplayBareProperties(WebPage $oPage)
  115. {
  116. $oPage->add($this->GetBareProperties($oPage));
  117. }
  118. function DisplayBareRelations(WebPage $oPage)
  119. {
  120. // Related objects: display all the linkset attributes, each as a separate tab
  121. // First in the order described by the 'display' ZList, then the remaining ones
  122. $aList = $this->FlattenZList(MetaModel::GetZListItems(get_class($this), 'details'));
  123. $aList = array_unique(array_merge($aList, array_keys(MetaModel::ListAttributeDefs(get_class($this)))));
  124. foreach($aList as $sAttCode)
  125. {
  126. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  127. if ($oAttDef->IsLinkset())
  128. {
  129. $oPage->SetCurrentTab($oAttDef->GetLabel());
  130. if (!$oAttDef->IsIndirect())
  131. {
  132. // 1:n links
  133. $sTargetClass = $oAttDef->GetLinkedClass();
  134. $aParams = array(
  135. 'target_attr' => $oAttDef->GetExtKeyToMe(),
  136. 'object_id' => $this->GetKey(),
  137. 'menu' => true,
  138. );
  139. }
  140. else
  141. {
  142. // n:n links
  143. $sLinkedClass = $oAttDef->GetLinkedClass();
  144. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  145. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  146. $bMenu = ($this->Get($sAttCode)->Count() > 0); // The menu is enabled only if there are already some elements...
  147. $aParams = array(
  148. 'link_attr' => $oAttDef->GetExtKeyToMe(),
  149. 'object_id' => $this->GetKey(),
  150. 'target_attr' => $oAttDef->GetExtKeyToRemote(),
  151. 'view_link' => false,
  152. 'menu' => $bMenu,
  153. );
  154. }
  155. $oPage->p("<img src=\"".MetaModel::GetClassIcon($sTargetClass)."\" style=\"vertical-align:middle;\">&nbsp;".$oAttDef->GetDescription());
  156. $oBlock = new DisplayBlock($this->Get($sAttCode)->GetFilter(), 'list', false);
  157. $oBlock->Display($oPage, 'rel_'.$sAttCode, $aParams);
  158. }
  159. }
  160. $oPage->SetCurrentTab('');
  161. }
  162. function GetBareProperties(WebPage $oPage)
  163. {
  164. $sHtml = '';
  165. $oAppContext = new ApplicationContext();
  166. $sStateAttCode = MetaModel::GetStateAttributeCode(get_class($this));
  167. $aDetails = array();
  168. $sClass = get_class($this);
  169. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  170. $aDetailsStruct = self::ProcessZlist($aDetailsList, array('UI:PropertiesTab' => array()), 'UI:PropertiesTab', 'col1', '');
  171. // Compute the list of properties to display, first the attributes in the 'details' list, then
  172. // all the remaining attributes that are not external fields
  173. $sHtml = '';
  174. $aDetails = array();
  175. foreach($aDetailsStruct as $sTab => $aCols )
  176. {
  177. $aDetails[$sTab] = array();
  178. ksort($aCols);
  179. $oPage->SetCurrentTab(Dict::S($sTab));
  180. $oPage->add('<table style="vertical-align:top"><tr>');
  181. foreach($aCols as $sColIndex => $aFieldsets)
  182. {
  183. $aDetails[$sTab][$sColIndex] = array();
  184. foreach($aFieldsets as $sFieldsetName => $aFields)
  185. {
  186. //if ($sFieldsetName == '')
  187. //{
  188. foreach($aFields as $sAttCode)
  189. {
  190. $val = $this->GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode);
  191. if ($val != null)
  192. {
  193. // The field is visible, add it to the current column
  194. $aDetails[$sTab][$sColIndex][] = $val;
  195. }
  196. }
  197. //}
  198. }
  199. $oPage->add('<td style="vertical-align:top">');
  200. $oPage->Details($aDetails[$sTab][$sColIndex]);
  201. $oPage->add('</td>');
  202. }
  203. $oPage->add('</tr></table>');
  204. }
  205. return $sHtml;
  206. }
  207. function DisplayDetails(WebPage $oPage)
  208. {
  209. $sTemplate = Utils::ReadFromFile(MetaModel::GetDisplayTemplate(get_class($this)));
  210. if (!empty($sTemplate))
  211. {
  212. $oTemplate = new DisplayTemplate($sTemplate);
  213. $sNameAttCode = MetaModel::GetNameAttributeCode(get_class($this));
  214. // Note: to preserve backward compatibility with home-made templates, the placeholder '$pkey$' has been preserved
  215. // but the preferred method is to use '$id$'
  216. $oTemplate->Render($oPage, array('class_name'=> MetaModel::GetName(get_class($this)),'class'=> get_class($this), 'pkey'=> $this->GetKey(), 'id'=> $this->GetKey(), 'name' => $this->Get($sNameAttCode)));
  217. }
  218. else
  219. {
  220. // Object's details
  221. // template not found display the object using the *old style*
  222. $this->DisplayBareHeader($oPage);
  223. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  224. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  225. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  226. $this->DisplayBareProperties($oPage);
  227. $this->DisplayBareRelations($oPage);
  228. $oPage->SetCurrentTab(Dict::S('UI:HistoryTab'));
  229. $this->DisplayBareHistory($oPage);
  230. }
  231. }
  232. function DisplayPreview(WebPage $oPage)
  233. {
  234. $aDetails = array();
  235. $sClass = get_class($this);
  236. $aList = MetaModel::GetZListItems($sClass, 'preview');
  237. foreach($aList as $sAttCode)
  238. {
  239. $aDetails[] = array('label' => MetaModel::GetLabel($sClass, $sAttCode), 'value' =>$this->GetAsHTML($sAttCode));
  240. }
  241. $oPage->details($aDetails);
  242. }
  243. // Comment by Rom: this helper may be used to display objects of class DBObject
  244. // -> I am using this to display the changes history
  245. public static function DisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  246. {
  247. $oPage->add(self::GetDisplaySet($oPage, $oSet, $aExtraParams));
  248. }
  249. //public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $sLinkageAttribute = '', $bDisplayMenu = true, $bSelectMode = false)
  250. public static function GetDisplaySet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  251. {
  252. static $iListId = 0;
  253. $iListId++;
  254. // Initialize and check the parameters
  255. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  256. $sLinkageAttribute = isset($aExtraParams['link_attr']) ? $aExtraParams['link_attr'] : '';
  257. $iLinkedObjectId = isset($aExtraParams['object_id']) ? $aExtraParams['object_id'] : 0;
  258. $sTargetAttr = isset($aExtraParams['target_attr']) ? $aExtraParams['target_attr'] : '';
  259. if (!empty($sLinkageAttribute))
  260. {
  261. if($iLinkedObjectId == 0)
  262. {
  263. // if 'links' mode is requested the id of the object to link to must be specified
  264. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
  265. }
  266. if($sTargetAttr == '')
  267. {
  268. // if 'links' mode is requested the d of the object to link to must be specified
  269. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
  270. }
  271. }
  272. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  273. $bSelectMode = isset($aExtraParams['selection_mode']) ? $aExtraParams['selection_mode'] == true : false;
  274. $bSingleSelectMode = isset($aExtraParams['selection_type']) ? ($aExtraParams['selection_type'] == 'single') : false;
  275. $sHtml = '';
  276. $oAppContext = new ApplicationContext();
  277. $sClassName = $oSet->GetFilter()->GetClass();
  278. $aAttribs = array();
  279. $aList = MetaModel::GetZListItems($sClassName, 'list');
  280. if (!empty($sLinkageAttribute))
  281. {
  282. // The set to display is in fact a set of links between the object specified in the $sLinkageAttribute
  283. // and other objects...
  284. // The display will then group all the attributes related to the link itself:
  285. // | Link_attr1 | link_attr2 | ... || Object_attr1 | Object_attr2 | Object_attr3 | .. | Object_attr_n |
  286. $aAttDefs = MetaModel::ListAttributeDefs($sClassName);
  287. assert(isset($aAttDefs[$sLinkageAttribute]));
  288. $oAttDef = $aAttDefs[$sLinkageAttribute];
  289. assert($oAttDef->IsExternalKey());
  290. // First display all the attributes specific to the link record
  291. foreach($aList as $sLinkAttCode)
  292. {
  293. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  294. if ( (!$oLinkAttDef->IsExternalKey()) && (!$oLinkAttDef->IsExternalField()) )
  295. {
  296. $aDisplayList[] = $sLinkAttCode;
  297. }
  298. }
  299. // Then display all the attributes neither specific to the link record nor to the 'linkage' object (because the latter are constant)
  300. foreach($aList as $sLinkAttCode)
  301. {
  302. $oLinkAttDef = $aAttDefs[$sLinkAttCode];
  303. if (($oLinkAttDef->IsExternalKey() && ($sLinkAttCode != $sLinkageAttribute))
  304. || ($oLinkAttDef->IsExternalField() && ($oLinkAttDef->GetKeyAttCode()!=$sLinkageAttribute)) )
  305. {
  306. $aDisplayList[] = $sLinkAttCode;
  307. }
  308. }
  309. // First display all the attributes specific to the link
  310. // Then display all the attributes linked to the other end of the relationship
  311. $aList = $aDisplayList;
  312. }
  313. if ($bSelectMode)
  314. {
  315. if (!$bSingleSelectMode)
  316. {
  317. $aAttribs['form::select'] = array('label' => "<input type=\"checkbox\" onChange=\"var value = this.checked; $('.selectList{$iListId}').each( function() { this.checked = value; } );\"></input>", 'description' => Dict::S('UI:SelectAllToggle+'));
  318. }
  319. else
  320. {
  321. $aAttribs['form::select'] = array('label' => "", 'description' => '');
  322. }
  323. }
  324. if ($bViewLink)
  325. {
  326. $aAttribs['key'] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  327. }
  328. foreach($aList as $sAttCode)
  329. {
  330. $aAttribs[$sAttCode] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  331. }
  332. $aValues = array();
  333. $oSet->Seek(0);
  334. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  335. $iMaxObjects = -1;
  336. if ($bDisplayLimit)
  337. {
  338. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  339. {
  340. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  341. }
  342. }
  343. while (($oObj = $oSet->Fetch()) && ($iMaxObjects != 0))
  344. {
  345. $aRow = array();
  346. if ($bViewLink)
  347. {
  348. $aRow['key'] = $oObj->GetHyperLink();
  349. }
  350. if ($bSelectMode)
  351. {
  352. if ($bSingleSelectMode)
  353. {
  354. $aRow['form::select'] = "<input type=\"radio\" class=\"selectList{$iListId}\" name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  355. }
  356. else
  357. {
  358. $aRow['form::select'] = "<input type=\"checkBox\" class=\"selectList{$iListId}\" name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  359. }
  360. }
  361. foreach($aList as $sAttCode)
  362. {
  363. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  364. }
  365. $aValues[] = $aRow;
  366. $iMaxObjects--;
  367. }
  368. $sHtml .= '<table class="listContainer">';
  369. $sColspan = '';
  370. if ($bDisplayMenu)
  371. {
  372. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  373. $sColspan = 'colspan="2"';
  374. $aMenuExtraParams = $aExtraParams;
  375. if (!empty($sLinkageAttribute))
  376. {
  377. //$aMenuExtraParams['linkage'] = $sLinkageAttribute;
  378. $aMenuExtraParams = $aExtraParams;
  379. }
  380. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  381. {
  382. // list truncated
  383. $divId = $aExtraParams['block_id'];
  384. $sFilter = $oSet->GetFilter()->serialize();
  385. $aExtraParams['display_limit'] = false; // To expand the full list
  386. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  387. $sHtml .= '<tr class="containerHeader"><td>'.Dict::Format('UI:TruncatedResults', utils::GetConfig()->GetMinDisplayLimit(), $oSet->Count()).'&nbsp;&nbsp;<a href="#open_'.$divId.'" onClick="Javascript:ReloadTruncatedList(\''.$divId.'\', \''.$sFilter.'\', \''.$sExtraParams.'\');">'.Dict::S('UI:DisplayAll').'</a></td><td>';
  388. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  389. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  390. }
  391. else
  392. {
  393. // Full list
  394. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  395. }
  396. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  397. $sHtml .= '</td></tr>';
  398. }
  399. $sHtml .= "<tr><td $sColspan>";
  400. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  401. $sHtml .= '</td></tr>';
  402. $sHtml .= '</table>';
  403. return $sHtml;
  404. }
  405. public static function GetDisplayExtendedSet(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  406. {
  407. static $iListId = 0;
  408. $iListId++;
  409. $aList = array();
  410. // Initialize and check the parameters
  411. $bViewLink = isset($aExtraParams['view_link']) ? $aExtraParams['view_link'] : true;
  412. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  413. // Check if there is a list of aliases to limit the display to...
  414. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']) : array();
  415. $sHtml = '';
  416. $oAppContext = new ApplicationContext();
  417. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  418. $aAuthorizedClasses = array();
  419. foreach($aClasses as $sAlias => $sClassName)
  420. {
  421. if ((UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES) &&
  422. ( (count($aDisplayAliases) == 0) || (in_array($sAlias, $aDisplayAliases))) )
  423. {
  424. $aAuthorizedClasses[$sAlias] = $sClassName;
  425. }
  426. }
  427. $aAttribs = array();
  428. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  429. {
  430. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'list');
  431. if ($bViewLink)
  432. {
  433. $aAttribs['key_'.$sAlias] = array('label' => MetaModel::GetName($sClassName), 'description' => '');
  434. }
  435. foreach($aList[$sClassName] as $sAttCode)
  436. {
  437. $aAttribs[$sAttCode.'_'.$sAlias] = array('label' => MetaModel::GetLabel($sClassName, $sAttCode), 'description' => MetaModel::GetDescription($sClassName, $sAttCode));
  438. }
  439. }
  440. $aValues = array();
  441. $oSet->Seek(0);
  442. $bDisplayLimit = isset($aExtraParams['display_limit']) ? $aExtraParams['display_limit'] : true;
  443. $iMaxObjects = -1;
  444. if ($bDisplayLimit)
  445. {
  446. if ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit())
  447. {
  448. $iMaxObjects = utils::GetConfig()->GetMinDisplayLimit();
  449. }
  450. }
  451. while (($aObjects = $oSet->FetchAssoc()) && ($iMaxObjects != 0))
  452. {
  453. $aRow = array();
  454. foreach($aAuthorizedClasses as $sAlias => $sClassName) // TO DO: check if the user has enough rights to view the classes of the list...
  455. {
  456. if ($bViewLink)
  457. {
  458. $aRow['key_'.$sAlias] = $aObjects[$sAlias]->GetHyperLink();
  459. }
  460. foreach($aList[$sClassName] as $sAttCode)
  461. {
  462. $aRow[$sAttCode.'_'.$sAlias] = $aObjects[$sAlias]->GetAsHTML($sAttCode);
  463. }
  464. }
  465. $aValues[] = $aRow;
  466. $iMaxObjects--;
  467. }
  468. $sHtml .= '<table class="listContainer">';
  469. $sColspan = '';
  470. if ($bDisplayMenu)
  471. {
  472. $oMenuBlock = new MenuBlock($oSet->GetFilter());
  473. $sColspan = 'colspan="2"';
  474. $aMenuExtraParams = $aExtraParams;
  475. if (!empty($sLinkageAttribute))
  476. {
  477. $aMenuExtraParams = $aExtraParams;
  478. }
  479. if ($bDisplayLimit && ($oSet->Count() > utils::GetConfig()->GetMaxDisplayLimit()))
  480. {
  481. // list truncated
  482. $divId = $aExtraParams['block_id'];
  483. $sFilter = $oSet->GetFilter()->serialize();
  484. $aExtraParams['display_limit'] = false; // To expand the full list
  485. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  486. $sHtml .= '<tr class="containerHeader"><td>'.Dict::Format('UI:TruncatedResults', utils::GetConfig()->GetMinDisplayLimit(), $oSet->Count()).'&nbsp;&nbsp;<a href="Javascript:ReloadTruncatedList(\''.$divId.'\', \''.$sFilter.'\', \''.$sExtraParams.'\');">'.Dict::S('UI:DisplayAll').'</a></td><td>';
  487. $oPage->add_ready_script("$('#{$divId} table.listResults').addClass('truncated');");
  488. $oPage->add_ready_script("$('#{$divId} table.listResults tr:last td').addClass('truncated');");
  489. }
  490. else
  491. {
  492. // Full list
  493. $sHtml .= '<tr class="containerHeader"><td>&nbsp;'.Dict::Format('UI:CountOfResults', $oSet->Count()).'</td><td>';
  494. }
  495. $sHtml .= $oMenuBlock->GetRenderContent($oPage, $aMenuExtraParams);
  496. $sHtml .= '</td></tr>';
  497. }
  498. $sHtml .= "<tr><td $sColspan>";
  499. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  500. $sHtml .= '</td></tr>';
  501. $sHtml .= '</table>';
  502. return $sHtml;
  503. }
  504. static function DisplaySetAsCSV(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  505. {
  506. $oPage->add(self::GetSetAsCSV($oSet, $aParams));
  507. }
  508. static function GetSetAsCSV(DBObjectSet $oSet, $aParams = array())
  509. {
  510. $sSeparator = isset($aParams['separator']) ? $aParams['separator'] : ','; // default separator is comma
  511. $sTextQualifier = isset($aParams['text_qualifier']) ? $aParams['text_qualifier'] : '"'; // default text qualifier is double quote
  512. $aList = array();
  513. $oAppContext = new ApplicationContext();
  514. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  515. $aAuthorizedClasses = array();
  516. foreach($aClasses as $sAlias => $sClassName)
  517. {
  518. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  519. {
  520. $aAuthorizedClasses[$sAlias] = $sClassName;
  521. }
  522. }
  523. $aAttribs = array();
  524. $aHeader = array();
  525. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  526. {
  527. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode => $oAttDef)
  528. {
  529. if ((($oAttDef->IsExternalField()) || ($oAttDef->IsWritable())) && $oAttDef->IsScalar())
  530. {
  531. $aList[$sClassName][$sAttCode] = $oAttDef;
  532. }
  533. }
  534. $aHeader[] = 'id';
  535. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  536. {
  537. if ($oAttDef->IsExternalField())
  538. {
  539. $sExtKeyLabel = MetaModel::GetLabel($sClassName, $oAttDef->GetKeyAttCode());
  540. $sRemoteAttLabel = MetaModel::GetLabel($oAttDef->GetTargetClass(), $oAttDef->GetExtAttCode());
  541. $aHeader[] = $sExtKeyLabel.'->'.$sRemoteAttLabel;
  542. }
  543. else
  544. {
  545. $aHeader[] = MetaModel::GetLabel($sClassName, $sAttCode);
  546. }
  547. }
  548. }
  549. $sHtml = implode($sSeparator, $aHeader)."\n";
  550. $oSet->Seek(0);
  551. while ($aObjects = $oSet->FetchAssoc())
  552. {
  553. $aRow = array();
  554. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  555. {
  556. $oObj = $aObjects[$sAlias];
  557. $aRow[] = $oObj->GetKey();
  558. foreach($aList[$sClassName] as $sAttCode => $oAttDef)
  559. {
  560. $aRow[] = $oObj->GetAsCSV($sAttCode, $sSeparator, '\\');
  561. }
  562. }
  563. $sHtml .= implode($sSeparator, $aRow)."\n";
  564. }
  565. return $sHtml;
  566. }
  567. static function DisplaySetAsXML(WebPage $oPage, CMDBObjectSet $oSet, $aParams = array())
  568. {
  569. $oAppContext = new ApplicationContext();
  570. $aClasses = $oSet->GetFilter()->GetSelectedClasses();
  571. $aAuthorizedClasses = array();
  572. foreach($aClasses as $sAlias => $sClassName)
  573. {
  574. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $oSet) == UR_ALLOWED_YES)
  575. {
  576. $aAuthorizedClasses[$sAlias] = $sClassName;
  577. }
  578. }
  579. $aAttribs = array();
  580. $aList = array();
  581. $aList[$sClassName] = MetaModel::GetZListItems($sClassName, 'details');
  582. $oPage->add("<Set>\n");
  583. $oSet->Seek(0);
  584. while ($aObjects = $oSet->FetchAssoc())
  585. {
  586. if (count($aAuthorizedClasses) > 1)
  587. {
  588. $oPage->add("<Row>\n");
  589. }
  590. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  591. {
  592. $oObj = $aObjects[$sAlias];
  593. $sClassName = get_class($oObj);
  594. $oPage->add("<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n");
  595. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  596. {
  597. if (($oAttDef->IsWritable()) && ($oAttDef->IsScalar()))
  598. {
  599. $sValue = $oObj->GetAsXML($sAttCode);
  600. $oPage->add("<$sAttCode>$sValue</$sAttCode>\n");
  601. }
  602. }
  603. $oPage->add("</$sClassName>\n");
  604. }
  605. if (count($aAuthorizedClasses) > 1)
  606. {
  607. $oPage->add("</Row>\n");
  608. }
  609. }
  610. $oPage->add("</Set>\n");
  611. }
  612. // By rom
  613. function DisplayChangesLog(WebPage $oPage)
  614. {
  615. $oFltChangeOps = new CMDBSearchFilter('CMDBChangeOpSetAttribute');
  616. $oFltChangeOps->AddCondition('objkey', $this->GetKey(), '=');
  617. $oFltChangeOps->AddCondition('objclass', get_class($this), '=');
  618. $oSet = new CMDBObjectSet($oFltChangeOps, array('date' => false)); // order by date descending (i.e. false)
  619. $count = $oSet->Count();
  620. if ($count > 0)
  621. {
  622. $oPage->p(Dict::Format('UI:ChangesLogTitle', $count));
  623. self::DisplaySet($oPage, $oSet);
  624. }
  625. else
  626. {
  627. $oPage->p(Dict::S('UI:EmptyChangesLogTitle'));
  628. }
  629. }
  630. public static function DisplaySearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  631. {
  632. $oPage->add(self::GetSearchForm($oPage, $oSet, $aExtraParams));
  633. }
  634. public static function GetSearchForm(WebPage $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
  635. {
  636. static $iSearchFormId = 0;
  637. $oAppContext = new ApplicationContext();
  638. $sHtml = '';
  639. $numCols=4;
  640. $sClassName = $oSet->GetFilter()->GetClass();
  641. // Romain: temporarily removed the tab "OQL query" because it was not finalized
  642. // (especially when used to add a link)
  643. /*
  644. $sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
  645. <li><a href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:OQLQueryTab')."</a></li>
  646. <li><a class=\"selected\" href=\"#\" onClick=\"$('div.mini_tab{$iSearchFormId}').toggle();$('#mini_tabs{$iSearchFormId} ul li a').toggleClass('selected');\">".Dict::S('UI:SimpleSearchTab')."</a></li>
  647. </ul></div>\n";
  648. */
  649. // Simple search form
  650. if (isset($aExtraParams['currentId']))
  651. {
  652. $sSearchFormId = $aExtraParams['currentId'];
  653. $iSearchFormId++;
  654. }
  655. else
  656. {
  657. $iSearchFormId++;
  658. $sSearchFormId = 'SimpleSearchForm'.$iSearchFormId;
  659. $sHtml .= "<div id=\"$sSearchFormId\" class=\"mini_tab{$iSearchFormId}\">\n";
  660. }
  661. // Check if the current class has some sub-classes
  662. if (isset($aExtraParams['baseClass']))
  663. {
  664. $sRootClass = $aExtraParams['baseClass'];
  665. }
  666. else
  667. {
  668. $sRootClass = $sClassName;
  669. }
  670. $aSubClasses = MetaModel::GetSubclasses($sRootClass);
  671. if (count($aSubClasses) > 0)
  672. {
  673. $aOptions = array();
  674. $aOptions[MetaModel::GetName($sRootClass)] = "<option value=\"$sRootClass\">".MetaModel::GetName($sRootClass)."</options>\n";
  675. foreach($aSubClasses as $sSubclassName)
  676. {
  677. $aOptions[MetaModel::GetName($sSubclassName)] = "<option value=\"$sSubclassName\">".MetaModel::GetName($sSubclassName)."</options>\n";
  678. }
  679. $aOptions[MetaModel::GetName($sClassName)] = "<option selected value=\"$sClassName\">".MetaModel::GetName($sClassName)."</options>\n";
  680. ksort($aOptions);
  681. $sClassesCombo = "<select name=\"class\" onChange=\"ReloadSearchForm('$sSearchFormId', this.value, '$sRootClass')\">\n".implode('', $aOptions)."</select>\n";
  682. }
  683. else
  684. {
  685. $sClassesCombo = MetaModel::GetName($sClassName);
  686. }
  687. $oUnlimitedFilter = new DBObjectSearch($sClassName);
  688. $sHtml .= "<form id=\"form{$iSearchFormId}\">\n";
  689. $sHtml .= "<h2>".Dict::Format('UI:SearchFor_Class_Objects', $sClassesCombo)."</h2>\n";
  690. $index = 0;
  691. $sHtml .= "<table>\n";
  692. $aFilterCriteria = $oSet->GetFilter()->GetCriteria();
  693. $aMapCriteria = array();
  694. foreach($aFilterCriteria as $aCriteria)
  695. {
  696. $aMapCriteria[$aCriteria['filtercode']][] = array('value' => $aCriteria['value'], 'opcode' => $aCriteria['opcode']);
  697. }
  698. $aList = MetaModel::GetZListItems($sClassName, 'standard_search');
  699. foreach($aList as $sFilterCode)
  700. {
  701. $oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
  702. if (($index % $numCols) == 0)
  703. {
  704. if ($index != 0)
  705. {
  706. $sHtml .= "</tr>\n";
  707. }
  708. $sHtml .= "<tr>\n";
  709. }
  710. $sFilterValue = '';
  711. $sFilterValue = utils::ReadParam($sFilterCode, '');
  712. $sFilterOpCode = null; // Use the default 'loose' OpCode
  713. if (empty($sFilterValue))
  714. {
  715. if (isset($aMapCriteria[$sFilterCode]))
  716. {
  717. if (count($aMapCriteria[$sFilterCode]) > 1)
  718. {
  719. $sFilterValue = Dict::S('UI:SearchValue:Mixed');
  720. }
  721. else
  722. {
  723. $sFilterValue = $aMapCriteria[$sFilterCode][0]['value'];
  724. $sFilterOpCode = $aMapCriteria[$sFilterCode][0]['opcode'];
  725. }
  726. if ($sFilterCode != 'company')
  727. {
  728. $oUnlimitedFilter->AddCondition($sFilterCode, $sFilterValue, $sFilterOpCode);
  729. }
  730. }
  731. }
  732. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClassName, $sFilterCode, $aExtraParams);
  733. if ($aAllowedValues != null)
  734. {
  735. //Enum field or external key, display a combo
  736. $sValue = "<select name=\"$sFilterCode\">\n";
  737. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  738. foreach($aAllowedValues as $key => $value)
  739. {
  740. if ($sFilterValue == $key)
  741. {
  742. $sSelected = ' selected';
  743. }
  744. else
  745. {
  746. $sSelected = '';
  747. }
  748. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  749. }
  750. $sValue .= "</select>\n";
  751. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td>$sValue</td>\n";
  752. }
  753. else
  754. {
  755. // Any value is possible, display an input box
  756. $sHtml .= "<td><label>".MetaModel::GetFilterLabel($sClassName, $sFilterCode).":</label></td><td><input class=\"textSearch\" name=\"$sFilterCode\" value=\"$sFilterValue\"/></td>\n";
  757. }
  758. $index++;
  759. }
  760. if (($index % $numCols) != 0)
  761. {
  762. $sHtml .= "<td colspan=\"".(2*($numCols - ($index % $numCols)))."\"></td>\n";
  763. }
  764. $sHtml .= "</tr>\n";
  765. $sHtml .= "<tr><td colspan=\"".(2*$numCols)."\" align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></td></tr>\n";
  766. $sHtml .= "</table>\n";
  767. foreach($aExtraParams as $sName => $sValue)
  768. {
  769. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  770. }
  771. $sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
  772. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  773. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
  774. $sHtml .= $oAppContext->GetForForm();
  775. $sHtml .= "</form>\n";
  776. if (!isset($aExtraParams['currentId']))
  777. {
  778. $sHtml .= "</div><!-- Simple search form -->\n";
  779. }
  780. // OQL query builder
  781. $sHtml .= "<div id=\"OQLQuery{$iSearchFormId}\" style=\"display:none\" class=\"mini_tab{$iSearchFormId}\">\n";
  782. $sHtml .= "<h1>".Dict::S('UI:OQLQueryBuilderTitle')."</h1>\n";
  783. $sHtml .= "<form id=\"formOQL{$iSearchFormId}\"><table style=\"width:80%;\"><tr style=\"vertical-align:top\">\n";
  784. $sHtml .= "<td style=\"text-align:right\"><label>SELECT&nbsp;</label><select name=\"oql_class\">";
  785. $aClasses = MetaModel::EnumChildClasses($sClassName, ENUM_CHILD_CLASSES_ALL);
  786. $sSelectedClass = utils::ReadParam('oql_class', $sClassName);
  787. $sOQLClause = utils::ReadParam('oql_clause', '');
  788. asort($aClasses);
  789. foreach($aClasses as $sChildClass)
  790. {
  791. $sSelected = ($sChildClass == $sSelectedClass) ? 'selected' : '';
  792. $sHtml.= "<option value=\"$sChildClass\" $sSelected>".MetaModel::GetName($sChildClass)."</option>\n";
  793. }
  794. $sHtml .= "</select>&nbsp;</td><td>\n";
  795. $sHtml .= "<textarea name=\"oql_clause\" style=\"width:100%\">$sOQLClause</textarea></td></tr>\n";
  796. $sHtml .= "<tr><td colspan=\"2\" style=\"text-align:right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Query')."\"></td></tr>\n";
  797. $sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
  798. foreach($aExtraParams as $sName => $sValue)
  799. {
  800. $sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
  801. }
  802. $sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
  803. $sHtml .= $oAppContext->GetForForm();
  804. $sHtml .= "</table></form>\n";
  805. $sHtml .= "</div><!-- OQL query form -->\n";
  806. return $sHtml;
  807. }
  808. public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
  809. {
  810. static $iInputId = 0;
  811. if (isset($aArgs[$sAttCode]) && empty($value))
  812. {
  813. // default value passed by the context (either the app context of the operation)
  814. $value = $aArgs[$sAttCode];
  815. }
  816. if (!empty($iId))
  817. {
  818. $iInputId = $iId;
  819. }
  820. else
  821. {
  822. $iInputId++;
  823. $iId = $iInputId;
  824. }
  825. if (!$oAttDef->IsExternalField())
  826. {
  827. $aCSSClasses = array();
  828. $bMandatory = 0;
  829. if ( (!$oAttDef->IsNullAllowed()) || ($iFlags & OPT_ATT_MANDATORY))
  830. {
  831. $aCSSClasses[] = 'mandatory';
  832. $bMandatory = 1;
  833. }
  834. $sCSSClasses = self::GetCSSClasses($aCSSClasses);
  835. $sValidationField = "<span id=\"v_{$iId}\"></span>";
  836. $sHelpText = $oAttDef->GetHelpOnEdition();
  837. $aEventsList = array('validate');
  838. switch($oAttDef->GetEditClass())
  839. {
  840. case 'Date':
  841. case 'DateTime':
  842. $aEventsList[] ='keyup';
  843. $aEventsList[] ='change';
  844. $sHTMLValue = "<input title=\"$sHelpText\" class=\"date-pick\" type=\"text\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  845. break;
  846. case 'Password':
  847. $aEventsList[] ='keyup';
  848. $aEventsList[] ='change';
  849. $sHTMLValue = "<input title=\"$sHelpText\" type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  850. break;
  851. case 'Text':
  852. $aEventsList[] ='keypress';
  853. $aEventsList[] ='change';
  854. $sHTMLValue = "<textarea class=\"resizable\" title=\"$sHelpText\" name=\"attr_{$sAttCode}{$sNameSuffix}\" rows=\"8\" cols=\"40\" id=\"$iId\">$value</textarea>&nbsp;{$sValidationField}";
  855. break;
  856. case 'LinkedSet':
  857. $aEventsList[] ='change';
  858. $oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix);
  859. $sHTMLValue = $oWidget->Display($oPage, $value);
  860. break;
  861. case 'Document':
  862. $aEventsList[] ='change';
  863. $oDocument = $value; // Value is an ormDocument object
  864. $sFileName = '';
  865. if (is_object($oDocument))
  866. {
  867. $sFileName = $oDocument->GetFileName();
  868. }
  869. $iMaxFileSize = utils::ConvertToBytes(ini_get('upload_max_filesize'));
  870. $sHTMLValue = "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$iMaxFileSize\" />\n";
  871. $sHTMLValue .= "<input name=\"attr_{$sAttCode}{$sNameSuffix}\" type=\"hidden\" id=\"$iId\" \" value=\"$sFileName\"/>\n";
  872. $sHTMLValue .= "<span id=\"name_$iInputId\">$sFileName</span><br/>\n";
  873. $sHTMLValue .= "<input title=\"$sHelpText\" name=\"file_{$sAttCode}{$sNameSuffix}\" type=\"file\" id=\"file_$iId\" onChange=\"UpdateFileName('$iId', this.value)\"/>&nbsp;{$sValidationField}\n";
  874. break;
  875. case 'List':
  876. // Not editable for now...
  877. $sHTMLValue = '';
  878. break;
  879. case 'String':
  880. default:
  881. // #@# todo - add context information (depending on dimensions)
  882. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
  883. if ($aAllowedValues !== null)
  884. {
  885. //Enum field or external key, display a combo
  886. //if (count($aAllowedValues) == 0)
  887. //{
  888. // $sHTMLValue = "<input count=\"0\" type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
  889. //}
  890. //else if (count($aAllowedValues) > 50)
  891. if (count($aAllowedValues) > 50)
  892. {
  893. // too many choices, use an autocomplete
  894. // The input for the auto complete
  895. $sHTMLValue = "<input count=\"".count($aAllowedValues)."\" type=\"text\" id=\"label_$iId\" size=\"30\" value=\"$sDisplayValue\"{$sCSSClasses}/>&nbsp;{$sValidationField}";
  896. // another hidden input to store & pass the object's Id
  897. $sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
  898. $oPage->add_ready_script("\$('#label_$iId').autocomplete('./ajax.render.php', { scroll:true, minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#$iId', extraParams:{operation:'autocomplete', sclass:'$sClass',attCode:'".$sAttCode."'}});");
  899. $oPage->add_ready_script("\$('#label_$iId').result( function(event, data, formatted) { if (data) { $('#{$iId}').val(data[1]); } } );");
  900. $aEventsList[] ='change';
  901. }
  902. else
  903. {
  904. // Few choices, use a normal 'select'
  905. // In case there are no valid values, the select will be empty, thus blocking the user from validating the form
  906. $sHTMLValue = "<select title=\"$sHelpText\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iId\">\n";
  907. $sHTMLValue .= "<option value=\"0\">".Dict::S('UI:SelectOne')."</option>\n";
  908. foreach($aAllowedValues as $key => $display_value)
  909. {
  910. if ((count($aAllowedValues) == 1) && $bMandatory )
  911. {
  912. // When there is only once choice, select it by default
  913. $sSelected = ' selected';
  914. }
  915. else
  916. {
  917. $sSelected = ($value == $key) ? ' selected' : '';
  918. }
  919. $sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
  920. }
  921. $sHTMLValue .= "</select>&nbsp;{$sValidationField}\n";
  922. $aEventsList[] ='change';
  923. }
  924. }
  925. else
  926. {
  927. $sHTMLValue = "<input title=\"$sHelpText\" type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iId\"/>&nbsp;{$sValidationField}";
  928. $aEventsList[] ='keyup';
  929. $aEventsList[] ='change';
  930. }
  931. break;
  932. }
  933. $sPattern = addslashes($oAttDef->GetValidationPattern()); //'^([0-9]+)$';
  934. $oPage->add_ready_script("$('#$iId').bind('".implode(' ', $aEventsList)."', function(evt, sFormId) { return ValidateField('$iId', '$sPattern', $bMandatory, sFormId) } );"); // Bind to a custom event: validate
  935. $aDependencies = MetaModel::GetDependentAttributes($sClass, $sAttCode); // List of attributes that depend on the current one
  936. if (count($aDependencies) > 0)
  937. {
  938. $oPage->add_ready_script("$('#$iId').bind('change', function(evt, sFormId) { return UpdateDependentFields(['".implode("','", $aDependencies)."']) } );"); // Bind to a custom event: validate
  939. }
  940. }
  941. return "<div>{$sHTMLValue}</div>";
  942. }
  943. public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
  944. {
  945. static $iFormId = 0;
  946. $iFormId++;
  947. $sClass = get_class($this);
  948. $oAppContext = new ApplicationContext();
  949. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  950. $iKey = $this->GetKey();
  951. $aDetails = array();
  952. $aFieldsMap = array();
  953. $oPage->add("<form id=\"form_{$iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckFields('form_{$iFormId}', true)\">\n");
  954. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  955. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  956. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  957. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  958. $aFullList = MetaModel::ListAttributeDefs($sClass);
  959. $aList = $aDetailsList;
  960. // Compute the list of properties to display, first the attributes in the 'details' list, then
  961. // all the remaining attributes that are not external fields
  962. foreach($aFullList as $sAttCode => $void)
  963. {
  964. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  965. if (!in_array($sAttCode, $aDetailsList) && (!$oAttDef->IsExternalField()))
  966. {
  967. $aList[] = $sAttCode;
  968. }
  969. }
  970. foreach($aList as $sAttCode)
  971. {
  972. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  973. $iFlags = $this->GetAttributeFlags($sAttCode);
  974. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  975. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  976. {
  977. if ($oAttDef->IsWritable())
  978. {
  979. if ($sStateAttCode == $sAttCode)
  980. {
  981. // State attribute is always read-only from the UI
  982. $sHTMLValue = $this->GetStateLabel();
  983. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  984. }
  985. else
  986. {
  987. $iFlags = $this->GetAttributeFlags($sAttCode);
  988. if ($iFlags & OPT_ATT_HIDDEN)
  989. {
  990. // Attribute is hidden, do nothing
  991. }
  992. else
  993. {
  994. if ($iFlags & OPT_ATT_READONLY)
  995. {
  996. // Attribute is read-only
  997. $sHTMLValue = $this->GetAsHTML($sAttCode);
  998. }
  999. else
  1000. {
  1001. $sValue = $this->Get($sAttCode);
  1002. $sDisplayValue = $this->GetEditValue($sAttCode);
  1003. $aArgs = array('this' => $this);
  1004. $sInputId = $iFormId.'_'.$sAttCode;
  1005. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1006. $aFieldsMap[$sAttCode] = $sInputId;
  1007. }
  1008. $aDetails[] = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $sHTMLValue);
  1009. }
  1010. }
  1011. }
  1012. else
  1013. {
  1014. $aDetails[] = array('label' => '<span title="'.$oAttDef->GetDescription().'">'.$oAttDef->GetLabel().'</span>', 'value' => $this->GetAsHTML($sAttCode));
  1015. }
  1016. }
  1017. }
  1018. $oPage->details($aDetails);
  1019. // Now display the relations, one tab per relation
  1020. foreach($aList as $sAttCode)
  1021. {
  1022. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1023. if ($oAttDef->IsLinkset())
  1024. {
  1025. $oPage->SetCurrentTab($oAttDef->GetLabel());
  1026. if (get_class($oAttDef) == 'AttributeLinkedSet')
  1027. {
  1028. // 1:n links
  1029. $sTargetClass = $oAttDef->GetLinkedClass();
  1030. $oPage->p("<img src=\"".MetaModel::GetClassIcon($sTargetClass)."\" style=\"vertical-align:middle;\">&nbsp;".$oAttDef->GetDescription());
  1031. $oFilter = new DBObjectSearch($sTargetClass);
  1032. $oFilter->AddCondition($oAttDef->GetExtKeyToMe(), $this->GetKey());
  1033. $oBlock = new DisplayBlock($oFilter, 'list', false);
  1034. $oBlock->Display($oPage, 0);
  1035. }
  1036. else // get_class($oAttDef) == 'AttributeLinkedSetIndirect'
  1037. {
  1038. // n:n links
  1039. $sLinkedClass = $oAttDef->GetLinkedClass();
  1040. $oLinkingAttDef = MetaModel::GetAttributeDef($sLinkedClass, $oAttDef->GetExtKeyToRemote());
  1041. $sTargetClass = $oLinkingAttDef->GetTargetClass();
  1042. $oPage->p("<img src=\"".MetaModel::GetClassIcon($sTargetClass)."\" style=\"vertical-align:middle;\">&nbsp;".$oAttDef->GetDescription());
  1043. $sValue = $this->Get($sAttCode);
  1044. $sDisplayValue = $this->GetEditValue($sAttCode);
  1045. $aArgs = array('this' => $this);
  1046. $sInputId = $iFormId.'_'.$sAttCode;
  1047. $sHTMLValue = "<span id=\"field_{$sInputId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sInputId, '', $iFlags, $aArgs).'</span>';
  1048. $aFieldsMap[$sAttCode] = $sInputId;
  1049. $oPage->add($sHTMLValue);
  1050. }
  1051. }
  1052. }
  1053. $oPage->SetCurrentTab('');
  1054. $oPage->add("<input type=\"hidden\" name=\"id\" value=\"$iKey\">\n");
  1055. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1056. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"apply_modify\">\n");
  1057. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1058. foreach($aExtraParams as $sName => $value)
  1059. {
  1060. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1061. }
  1062. $oPage->add($oAppContext->GetForForm());
  1063. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"BackToDetails('$sClass', $iKey)\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1064. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Apply')."</span></button>\n");
  1065. $oPage->add("</form>\n");
  1066. $iFieldsCount = count($aFieldsMap);
  1067. $sJsonFieldsMap = json_encode($aFieldsMap);
  1068. $oPage->add_script(
  1069. <<<EOF
  1070. // Initializes the object once at the beginning of the page...
  1071. var oWizardHelper = new WizardHelper('$sClass');
  1072. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  1073. oWizardHelper.SetFieldsCount($iFieldsCount);
  1074. EOF
  1075. );
  1076. $oPage->add_ready_script(
  1077. <<<EOF
  1078. // Initializes the object once at the beginning of the page...
  1079. CheckFields('form_{$iFormId}', false);
  1080. EOF
  1081. );
  1082. }
  1083. public static function DisplayCreationForm(WebPage $oPage, $sClass, $oObjectToClone = null, $aArgs = array(), $aExtraParams = array())
  1084. {
  1085. static $iCreationFormId = 0;
  1086. $iCreationFormId++;
  1087. $iFieldIndex = 0;
  1088. $oAppContext = new ApplicationContext();
  1089. $aDetails = array();
  1090. $aFieldsMap = array();
  1091. $sOperation = ($oObjectToClone == null) ? 'apply_new' : 'apply_clone';
  1092. $sClass = ($oObjectToClone == null) ? $sClass : get_class($oObjectToClone);
  1093. $sStateAttCode = MetaModel::GetStateAttributeCode($sClass);
  1094. $oPage->add("<form id=\"creation_form_{$iCreationFormId}\" method=\"post\" enctype=\"multipart/form-data\" onSubmit=\"return CheckFields('creation_form_{$iCreationFormId}', true)\">\n");
  1095. $aStates = MetaModel::EnumStates($sClass);
  1096. $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB);
  1097. $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB);
  1098. $oPage->SetCurrentTab(Dict::S('UI:PropertiesTab'));
  1099. if ($oObjectToClone == null)
  1100. {
  1101. $sTargetState = MetaModel::GetDefaultState($sClass);
  1102. }
  1103. else
  1104. {
  1105. $sTargetState = $oObjectToClone->GetState();
  1106. }
  1107. $aDetailsList = MetaModel::GetZListItems($sClass, 'details');
  1108. $aFullList = MetaModel::ListAttributeDefs($sClass);
  1109. $aList = $aDetailsList;
  1110. // Compute the list of properties to display, first the attributes in the 'details' list, then
  1111. // all the remaining attributes that are not external fields
  1112. foreach($aFullList as $sAttCode => $void)
  1113. {
  1114. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1115. if (!in_array($sAttCode, $aDetailsList) && (!$oAttDef->IsExternalField()))
  1116. {
  1117. $aList[] = $sAttCode;
  1118. }
  1119. }
  1120. foreach($aList as $sAttCode)
  1121. {
  1122. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1123. $iFlags = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1124. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  1125. {
  1126. if ($oAttDef->IsWritable())
  1127. {
  1128. if ($oObjectToClone != null)
  1129. {
  1130. $sValue = $oObjectToClone->GetEditValue($sAttCode);
  1131. $aArgs['this'] = $oObjectToClone;
  1132. }
  1133. else
  1134. {
  1135. if(isset($aArgs['default'][$sAttCode]))
  1136. {
  1137. $sValue = $aArgs['default'][$sAttCode];
  1138. }
  1139. else
  1140. {
  1141. $sValue = $oAttDef->GetDefaultValue();
  1142. }
  1143. }
  1144. // Prepopulate with a default value -- but no display value...
  1145. $sDisplayValue = '';
  1146. if (!empty($sValue))
  1147. {
  1148. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs, '');
  1149. switch (count($aAllowedValues))
  1150. {
  1151. case 1:
  1152. case 0:
  1153. $sDisplayValue = $sValue;
  1154. break;
  1155. default:
  1156. $sDisplayValue = $sValue;
  1157. foreach($aAllowedValues as $key => $display)
  1158. {
  1159. if ($key == $sValue)
  1160. {
  1161. $sDisplayValue = $display;
  1162. break;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. if ($sStateAttCode == $sAttCode)
  1168. {
  1169. // State attribute is always read-only from the UI
  1170. $sHTMLValue = MetaModel::GetStateLabel($sClass, $sTargetState);
  1171. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1172. }
  1173. else
  1174. {
  1175. if ($iFlags & OPT_ATT_HIDDEN)
  1176. {
  1177. // Attribute is hidden, do nothing
  1178. }
  1179. else
  1180. {
  1181. if ($iFlags & OPT_ATT_READONLY)
  1182. {
  1183. // Attribute is read-only
  1184. $sHTMLValue = ($oObjectToClone == null) ? $sDisplayValue : $oObjectToClone->GetAsHTML($sAttCode);
  1185. }
  1186. else
  1187. {
  1188. $sFieldId = 'att_'.$iFieldIndex;
  1189. $sHTMLValue = "<div id=\"field_{$sFieldId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sFieldId, '', $iFlags, $aArgs)."</div>";
  1190. $aFieldsMap[$sFieldId] = $sAttCode;
  1191. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1192. $iFieldIndex++;
  1193. }
  1194. }
  1195. }
  1196. }
  1197. }
  1198. }
  1199. $oPage->details($aDetails);
  1200. // Now display the relations, one tab per relation
  1201. foreach($aList as $sAttCode)
  1202. {
  1203. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1204. if ($oAttDef->IsLinkset())
  1205. {
  1206. $oPage->SetCurrentTab($oAttDef->GetLabel());
  1207. $oPage->p($oAttDef->GetDescription());
  1208. $iFlags = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1209. $sFieldId = 'att_'.$iFieldIndex;
  1210. $sValue = ($oObjectToClone == null) ? '' : $oObjectToClone->Get($sAttCode);
  1211. $sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetEditValue($sAttCode);
  1212. $iFlags = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
  1213. $sHTMLValue = "<div id=\"field_{$sFieldId}\">".self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, $sFieldId, '', $iFlags, $aArgs)."</div>";
  1214. $aFieldsMap[$sFieldId] = $sAttCode;
  1215. $aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
  1216. $iFieldIndex++;
  1217. $oPage->add($sHTMLValue);
  1218. }
  1219. }
  1220. $oPage->SetCurrentTab('');
  1221. if ($oObjectToClone != null)
  1222. {
  1223. $oPage->add("<input type=\"hidden\" name=\"clone_id\" value=\"".$oObjectToClone->GetKey()."\">\n");
  1224. }
  1225. $oPage->add("<input type=\"hidden\" name=\"class\" value=\"$sClass\">\n");
  1226. $oPage->add("<input type=\"hidden\" name=\"operation\" value=\"$sOperation\">\n");
  1227. $oPage->add("<input type=\"hidden\" name=\"transaction_id\" value=\"".utils::GetNewTransactionId()."\">\n");
  1228. $oPage->add($oAppContext->GetForForm());
  1229. foreach($aExtraParams as $sName => $value)
  1230. {
  1231. $oPage->add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">\n");
  1232. }
  1233. $oPage->add("<button type=\"button\" class=\"action\" onClick=\"goBack()\"><span>".Dict::S('UI:Button:Cancel')."</span></button>&nbsp;&nbsp;&nbsp;&nbsp;\n");
  1234. $oPage->add("<button type=\"submit\" class=\"action\"><span>".Dict::S('UI:Button:Create')."</span></button>\n");
  1235. $oPage->add("</form>\n");
  1236. $aNewFieldsMap = array();
  1237. foreach($aFieldsMap as $id => $sFieldCode)
  1238. {
  1239. $aNewFieldsMap[$sFieldCode] = $id;
  1240. }
  1241. $iFieldsCount = count($aFieldsMap);
  1242. $sJsonFieldsMap = json_encode($aNewFieldsMap);
  1243. $oPage->add_script("
  1244. // Initializes the object once at the beginning of the page...
  1245. var oWizardHelper = new WizardHelper('$sClass');
  1246. oWizardHelper.SetFieldsMap($sJsonFieldsMap);
  1247. oWizardHelper.SetFieldsCount($iFieldsCount);");
  1248. $oPage->add_ready_script("CheckFields('creation_form_{$iCreationFormId}', false);");
  1249. }
  1250. protected static function GetCSSClasses($aCSSClasses)
  1251. {
  1252. $sCSSClasses = '';
  1253. if (!empty($aCSSClasses))
  1254. {
  1255. $sCSSClasses = ' class="'.implode(' ', $aCSSClasses).'" ';
  1256. }
  1257. return $sCSSClasses;
  1258. }
  1259. protected static function ProcessZlist($aList, $aDetails, $sCurrentTab, $sCurrentCol, $sCurrentSet)
  1260. {
  1261. //echo "<pre>ZList: ";
  1262. //print_r($aList);
  1263. //echo "</pre>\n";
  1264. foreach($aList as $sKey => $value)
  1265. {
  1266. if (is_array($value))
  1267. {
  1268. if (preg_match('/^(.*):(.*)$/U', $sKey, $aMatches))
  1269. {
  1270. $sCode = $aMatches[1];
  1271. $sName = $aMatches[2];
  1272. switch($sCode)
  1273. {
  1274. case 'tab':
  1275. //echo "<p>Found a tab: $sName ($sKey)</p>\n";
  1276. if(!isset($aDetails[$sName]))
  1277. {
  1278. $aDetails[$sName] = array('col1' => array('' => array()));
  1279. }
  1280. $aDetails = self::ProcessZlist($value, $aDetails, $sName, 'col1', '');
  1281. break;
  1282. case 'fieldset':
  1283. //echo "<p>Found a fieldset: $sName ($sKey)</p>\n";
  1284. if(!isset($aDetailsStruct[$sCurrentTab][$sCurrentCol][$sName]))
  1285. {
  1286. $aDetails[$sCurrentTab][$sCurrentCol][$sName] = array();
  1287. }
  1288. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sCurrentCol, $sName);
  1289. break;
  1290. default:
  1291. case 'col':
  1292. //echo "<p>Found a column: $sName ($sKey)</p>\n";
  1293. if(!isset($aDetails[$sCurrentTab][$sName]))
  1294. {
  1295. $aDetails[$sCurrentTab][$sName] = array('' => array());
  1296. }
  1297. $aDetails = self::ProcessZlist($value, $aDetails, $sCurrentTab, $sName, '');
  1298. break;
  1299. }
  1300. }
  1301. }
  1302. else
  1303. {
  1304. //echo "<p>Scalar value: $value, in [$sCurrentTab][$sCurrentCol][$sCurrentSet][]</p>\n";
  1305. $aDetails[$sCurrentTab][$sCurrentCol][$sCurrentSet][] = $value;
  1306. }
  1307. }
  1308. return $aDetails;
  1309. }
  1310. protected function FlattenZList($aList)
  1311. {
  1312. $aResult = array();
  1313. foreach($aList as $value)
  1314. {
  1315. if (!is_array($value))
  1316. {
  1317. $aResult[] = $value;
  1318. }
  1319. else
  1320. {
  1321. $aResult = array_merge($aResult, $this->FlattenZList($value));
  1322. }
  1323. }
  1324. return $aResult;
  1325. }
  1326. protected function GetFieldAsHtml($sClass, $sAttCode, $sStateAttCode)
  1327. {
  1328. $retVal = null;
  1329. $iFlags = $this->GetAttributeFlags($sAttCode);
  1330. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1331. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  1332. {
  1333. // The field is visible in the current state of the object
  1334. if ($sStateAttCode == $sAttCode)
  1335. {
  1336. // Special display for the 'state' attribute itself
  1337. $sDisplayValue = $this->GetStateLabel();
  1338. }
  1339. else if ($oAttDef->GetEditClass() == 'Document')
  1340. {
  1341. $oDocument = $this->Get($sAttCode);
  1342. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1343. $sDisplayValue .= "<br/>".Dict::Format('UI:OpenDocumentInNewWindow_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n";
  1344. $sDisplayValue .= "<br/>".Dict::Format('UI:DownloadDocument_', $oDocument->GetDisplayLink(get_class($this), $this->GetKey(), $sAttCode)).", \n";
  1345. }
  1346. else
  1347. {
  1348. $sDisplayValue = $this->GetAsHTML($sAttCode);
  1349. }
  1350. $retVal = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  1351. }
  1352. return $retVal;
  1353. }
  1354. /**
  1355. * Displays a blob document *inline* (if possible, depending on the type of the document)
  1356. * @return string
  1357. */
  1358. public function DisplayDocumentInline(WebPage $oPage, $sAttCode)
  1359. {
  1360. $oDoc = $this->Get($sAttCode);
  1361. $sClass = get_class($this);
  1362. $Id = $this->GetKey();
  1363. switch ($oDoc->GetMainMimeType())
  1364. {
  1365. case 'text':
  1366. case 'html':
  1367. $data = $oDoc->GetData();
  1368. switch($oDoc->GetMimeType())
  1369. {
  1370. case 'text/html':
  1371. case 'text/xml':
  1372. $oPage->add("<iframe id='preview_$sAttCode' src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" width=\"100%\" height=\"400\">Loading...</iframe>\n");
  1373. break;
  1374. default:
  1375. $oPage->add("<pre>".htmlentities(MyHelpers::beautifulstr($data, 1000, true))."</pre>\n");
  1376. }
  1377. break;
  1378. case 'application':
  1379. switch($oDoc->GetMimeType())
  1380. {
  1381. case 'application/pdf':
  1382. $oPage->add("<iframe id='preview_$sAttCode' src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" width=\"100%\" height=\"400\">Loading...</iframe>\n");
  1383. break;
  1384. default:
  1385. $oPage->add(Dict::S('UI:Document:NoPreview'));
  1386. }
  1387. break;
  1388. case 'image':
  1389. $oPage->add("<img src=\"../pages/ajax.render.php?operation=display_document&class=$sClass&id=$Id&field=$sAttCode\" />\n");
  1390. break;
  1391. default:
  1392. $oPage->add(Dict::S('UI:Document:NoPreview'));
  1393. }
  1394. }
  1395. }
  1396. ?>