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