cmdbabstract.class.inc.php 52 KB

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