cmdbabstract.class.inc.php 55 KB

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