cmdbabstract.class.inc.php 58 KB

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