cmdbabstract.class.inc.php 55 KB

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