cmdbabstract.class.inc.php 57 KB

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