cmdbabstract.class.inc.php 56 KB

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