cmdbabstract.class.inc.php 58 KB

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