cmdbabstract.class.inc.php 50 KB

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