cmdbabstract.class.inc.php 57 KB

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