cmdbabstract.class.inc.php 57 KB

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