cmdbabstract.class.inc.php 58 KB

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