cmdbabstract.class.inc.php 55 KB

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