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