portalwebpage.class.inc.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. * Class PortalWebPage
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once(APPROOT."/application/nicewebpage.class.inc.php");
  25. require_once(APPROOT."/application/applicationcontext.class.inc.php");
  26. require_once(APPROOT."/application/user.preferences.class.inc.php");
  27. define('BUTTON_CANCEL', 1);
  28. define('BUTTON_BACK', 2);
  29. define('BUTTON_NEXT', 4);
  30. define('BUTTON_FINISH', 8);
  31. define('PARAM_ARROW_SEP', '_x_');
  32. /**
  33. * Web page with some associated CSS and scripts (jquery) for a fancier display
  34. * of the Portal web page
  35. */
  36. class PortalWebPage extends NiceWebPage
  37. {
  38. /**
  39. * Portal menu
  40. */
  41. protected $m_sWelcomeMsg;
  42. protected $m_aMenuButtons;
  43. public function __construct($sTitle, $sAlternateStyleSheet = '')
  44. {
  45. $this->m_sWelcomeMsg = '';
  46. $this->m_aMenuButtons = array();
  47. parent::__construct($sTitle);
  48. $this->add_header("Content-type: text/html; charset=utf-8");
  49. $this->add_header("Cache-control: no-cache");
  50. $this->add_linked_stylesheet("../css/jquery.treeview.css");
  51. $this->add_linked_stylesheet("../css/jquery.autocomplete.css");
  52. $sAbsURLAppRoot = addslashes(utils::GetAbsoluteUrlAppRoot()); // Pass it to Javascript scripts
  53. if ($sAlternateStyleSheet != '')
  54. {
  55. $this->add_linked_stylesheet("../portal/$sAlternateStyleSheet/portal.css");
  56. }
  57. else
  58. {
  59. $this->add_linked_stylesheet("../portal/portal.css");
  60. }
  61. $this->add_linked_script('../js/jquery.layout.min.js');
  62. $this->add_linked_script('../js/jquery.ba-bbq.min.js');
  63. $this->add_linked_script("../js/jquery.tablehover.js");
  64. $this->add_linked_script("../js/jquery.treeview.js");
  65. $this->add_linked_script("../js/jquery.autocomplete.js");
  66. $this->add_linked_script("../js/jquery.positionBy.js");
  67. $this->add_linked_script("../js/jquery.popupmenu.js");
  68. $this->add_linked_script("../js/date.js");
  69. $this->add_linked_script("../js/jquery.tablesorter.min.js");
  70. $this->add_linked_script("../js/jquery.tablesorter.pager.js");
  71. $this->add_linked_script("../js/jquery.blockUI.js");
  72. $this->add_linked_script("../js/utils.js");
  73. $this->add_linked_script("../js/forms-json-utils.js");
  74. $this->add_linked_script("../js/swfobject.js");
  75. $this->add_linked_script("../js/jquery.qtip-1.0.min.js");
  76. $this->add_ready_script(
  77. <<<EOF
  78. try
  79. {
  80. //add new widget called TruncatedList to properly display truncated lists when they are sorted
  81. $.tablesorter.addWidget({
  82. // give the widget a id
  83. id: "truncatedList",
  84. // format is called when the on init and when a sorting has finished
  85. format: function(table)
  86. {
  87. // Check if there is a "truncated" line
  88. this.truncatedList = false;
  89. if ($("tr td.truncated",table).length > 0)
  90. {
  91. this.truncatedList = true;
  92. }
  93. if (this.truncatedList)
  94. {
  95. $("tr td",table).removeClass('truncated');
  96. $("tr:last td",table).addClass('truncated');
  97. }
  98. }
  99. });
  100. $.tablesorter.addWidget({
  101. // give the widget a id
  102. id: "myZebra",
  103. // format is called when the on init and when a sorting has finished
  104. format: function(table)
  105. {
  106. // Replace the 'red even' lines by 'red_even' since most browser do not support 2 classes selector in CSS, etc..
  107. $("tbody tr:even",table).addClass('even');
  108. $("tbody tr.red:even",table).removeClass('red').removeClass('even').addClass('red_even');
  109. $("tbody tr.orange:even",table).removeClass('orange').removeClass('even').addClass('orange_even');
  110. $("tbody tr.green:even",table).removeClass('green').removeClass('even').addClass('green_even');
  111. }
  112. });
  113. $(".date-pick").datepicker({
  114. showOn: 'button',
  115. buttonImage: '../images/calendar.png',
  116. buttonImageOnly: true,
  117. dateFormat: 'yy-mm-dd',
  118. constrainInput: false,
  119. changeMonth: true,
  120. changeYear: true
  121. });
  122. //$('.resizable').resizable(); // Make resizable everything that claims to be resizable !
  123. $('.caselog_header').click( function () { $(this).toggleClass('open').next('.caselog_entry').toggle(); });
  124. }
  125. catch(err)
  126. {
  127. // Do something with the error !
  128. alert(err);
  129. }
  130. EOF
  131. );
  132. $this->add_script(
  133. <<<EOF
  134. function CheckSelection(sMessage)
  135. {
  136. var bResult = ($('input:checked').length > 0);
  137. if (!bResult)
  138. {
  139. alert(sMessage);
  140. }
  141. return bResult;
  142. }
  143. function GetAbsoluteUrlAppRoot()
  144. {
  145. return '$sAbsURLAppRoot';
  146. }
  147. function GoBack(sFormId)
  148. {
  149. var form = $('#'+sFormId);
  150. var step_back = $('input[name=step_back]');
  151. form.unbind('submit'); // De-activate validation
  152. step_back.val(1);
  153. form.submit(); // Go
  154. }
  155. function GoHome()
  156. {
  157. var form = $('FORM');
  158. form.unbind('submit'); // De-activate validation
  159. window.location.href = '?operation=';
  160. return false;
  161. }
  162. function SetWizardNextStep(sStep)
  163. {
  164. var next_step = $('input[id=next_step]');
  165. next_step.val(sStep);
  166. }
  167. EOF
  168. );
  169. }
  170. public function SetCurrentTab($sTabLabel = '')
  171. {
  172. }
  173. /**
  174. * Specify a welcome message (optional)
  175. */
  176. public function SetWelcomeMessage($sMsg)
  177. {
  178. $this->m_sWelcomeMsg = $sMsg;
  179. }
  180. /**
  181. * Add a button to the portal's main menu
  182. */
  183. public function AddMenuButton($sId, $sLabel, $sHyperlink)
  184. {
  185. $this->m_aMenuButtons[] = array('id' => $sId, 'label' => $sLabel, 'hyperlink' => $sHyperlink);
  186. }
  187. var $m_bEnableDisconnectButton = true;
  188. public function EnableDisconnectButton($bEnable)
  189. {
  190. $this->m_bEnableDisconnectButton = $bEnable;
  191. }
  192. public function output()
  193. {
  194. $sMenu = '';
  195. if ($this->m_bEnableDisconnectButton)
  196. {
  197. $this->AddMenuButton('logoff', 'Portal:Disconnect', utils::GetAbsoluteUrlAppRoot().'pages/logoff.php'); // This menu is always present and is the last one
  198. }
  199. foreach($this->m_aMenuButtons as $aMenuItem)
  200. {
  201. $sMenu .= "<a class=\"button\" id=\"{$aMenuItem['id']}\" href=\"{$aMenuItem['hyperlink']}\"><span>".Dict::S($aMenuItem['label'])."</span></a>";
  202. }
  203. $this->s_content = '<div id="portal"><div id="welcome">'.$this->m_sWelcomeMsg.'</div><div id="banner"><div id="logo"></div><div id="menu">'.$sMenu.'</div></div><div id="content">'.$this->s_content.'</div></div>';
  204. parent::output();
  205. }
  206. /**
  207. * Displays a list of objects, without any hyperlink (except for the object's details)
  208. * @param DBObjectSet $oSet The set of objects to display
  209. * @param Array $aZList The ZList (list of field codes) to use for the tabular display
  210. * @param String $sEmptyListMessage Message displayed whenever the list is empty
  211. * @return string The HTML text representing the list
  212. */
  213. public function DisplaySet($oSet, $aZList, $sEmptyListMessage = '')
  214. {
  215. if ($oSet->Count() > 0)
  216. {
  217. $sClass = $oSet->GetClass();
  218. if (is_subclass_of($sClass, 'cmdbAbstractObject'))
  219. {
  220. // Home-made and very limited display of an object set
  221. //
  222. //$oSet->Seek(0);// juste pour que le warning soit moins crado
  223. //$oSet->Fetch();// juste pour que le warning soit moins crado
  224. //
  225. $this->add("<div id=\"listOf$sClass\">\n");
  226. cmdbAbstractObject::DisplaySet($this, $oSet, array('currentId' => "listOf$sClass", 'menu' => false, 'zlist' => false, 'extra_fields' => implode(',', $aZList)));
  227. $this->add("</div>\n");
  228. }
  229. else
  230. {
  231. // Home-made and very limited display of an object set
  232. $aAttribs = array();
  233. $aValues = array();
  234. $aAttribs['key'] = array('label' => MetaModel::GetName($sClass), 'description' => '');
  235. foreach($aZList as $sAttCode)
  236. {
  237. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  238. $aAttribs[$sAttCode] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  239. }
  240. while($oObj = $oSet->Fetch())
  241. {
  242. $aRow = array();
  243. $aRow['key'] = '<a href="./index.php?operation=details&class='.get_class($oObj).'&id='.$oObj->GetKey().'">'.$oObj->Get('friendlyname').'</a>';
  244. $sHilightClass = $oObj->GetHilightClass();
  245. if ($sHilightClass != '')
  246. {
  247. $aRow['@class'] = $sHilightClass;
  248. }
  249. foreach($aZList as $sAttCode)
  250. {
  251. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  252. }
  253. $aValues[$oObj->GetKey()] = $aRow;
  254. }
  255. $this->table($aAttribs, $aValues);
  256. }
  257. }
  258. elseif (strlen($sEmptyListMessage) > 0)
  259. {
  260. $this->add($sEmptyListMessage);
  261. }
  262. }
  263. /**
  264. * Display the attributes of an object (no title, no form)
  265. * @param Object $oObj Any kind of object
  266. * @param aAttList The list of attributes to display
  267. * @return void
  268. */
  269. public function DisplayObjectDetails($oObj, $aAttList)
  270. {
  271. $sClass = get_class($oObj);
  272. $aDetails = array();
  273. foreach($aAttList as $sAttCode)
  274. {
  275. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  276. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  277. if ( (!$oAttDef->IsLinkSet()) && (($iFlags & OPT_ATT_HIDDEN) == 0) )
  278. {
  279. // Don't display linked set and non-visible attributes (in this state)
  280. $sDisplayValue = $oObj->GetAsHTML($sAttCode);
  281. $aDetails[] = array('label' => '<span title="'.MetaModel::GetDescription($sClass, $sAttCode).'">'.MetaModel::GetLabel($sClass, $sAttCode).'</span>', 'value' => $sDisplayValue);
  282. }
  283. }
  284. $this->details($aDetails);
  285. }
  286. /**
  287. * DisplayObjectLinkset
  288. * @param Object $oObj Any kind of object
  289. * @param $sLinkSetAttCode The attribute code of the link set attribute to display
  290. * @param $sRemoteAttCode The external key on the linked class, pointing to the remote objects
  291. * @param $aZList The list of attribute of the remote object
  292. * @param $sEmptyListMessage The message to display if the list is empty
  293. * @return void
  294. */
  295. public function DisplayObjectLinkset($oObj, $sLinkSetAttCode, $sRemoteAttCode, $aZList, $sEmptyListMessage = '')
  296. {
  297. if (empty($sEmptyListMessage))
  298. {
  299. $sEmptyListMessage = Dict::S('UI:Search:NoObjectFound');
  300. }
  301. $oSet = $oObj->Get($sLinkSetAttCode);
  302. if ($oSet->Count() > 0)
  303. {
  304. $sClass = $oSet->GetClass();
  305. $oExtKeyToRemote = MetaModel::GetAttributeDef($sClass, $sRemoteAttCode);
  306. $sRemoteClass = $oExtKeyToRemote->GetTargetClass();
  307. $aAttribs = array();
  308. $aValues = array();
  309. $aAttribs['key'] = array('label' => MetaModel::GetName($sRemoteClass), 'description' => '');
  310. foreach($aZList as $sAttCode)
  311. {
  312. $oAttDef = MetaModel::GetAttributeDef($sRemoteClass, $sAttCode);
  313. $aAttribs[$sAttCode] = array('label' => $oAttDef->GetLabel(), 'description' => $oAttDef->GetDescription());
  314. }
  315. while($oLink = $oSet->Fetch())
  316. {
  317. $aRow = array();
  318. $oObj = MetaModel::GetObject($sRemoteClass, $oLink->Get($sRemoteAttCode));
  319. $aRow['key'] = '<a href="./index.php?operation=details&class='.get_class($oObj).'&id='.$oObj->GetKey().'">'.$oObj->Get('friendlyname').'</a>';
  320. $sHilightClass = $oObj->GetHilightClass();
  321. if ($sHilightClass != '')
  322. {
  323. $aRow['@class'] = $sHilightClass;
  324. }
  325. foreach($aZList as $sAttCode)
  326. {
  327. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  328. }
  329. $aValues[$oObj->GetKey()] = $aRow;
  330. }
  331. $this->Table($aAttribs, $aValues);
  332. }
  333. elseif (strlen($sEmptyListMessage) > 0)
  334. {
  335. $this->add($sEmptyListMessage);
  336. }
  337. }
  338. protected function DisplaySearchField($sClass, $sAttSpec, $aExtraParams, $sPrefix, $sFieldName = null)
  339. {
  340. if (is_null($sFieldName))
  341. {
  342. $sFieldName = str_replace('->', PARAM_ARROW_SEP, $sAttSpec);
  343. }
  344. $iPos = strpos($sAttSpec, '->');
  345. if ($iPos !== false)
  346. {
  347. $sAttCode = substr($sAttSpec, 0, $iPos);
  348. $sSubSpec = substr($sAttSpec, $iPos + 2);
  349. if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
  350. {
  351. throw new Exception("Invalid attribute code '$sClass/$sAttCode' in search specification '$sAttSpec'");
  352. }
  353. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  354. if ($oAttDef->IsLinkSet())
  355. {
  356. $sTargetClass = $oAttDef->GetLinkedClass();
  357. }
  358. elseif ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  359. {
  360. $sTargetClass = $oAttDef->GetTargetClass(EXTKEY_ABSOLUTE);
  361. }
  362. else
  363. {
  364. throw new Exception("Attribute specification '$sAttSpec', '$sAttCode' should be either a link set or an external key");
  365. }
  366. $this->DisplaySearchField($sTargetClass, $sSubSpec, $aExtraParams, $sPrefix, $sFieldName);
  367. }
  368. else
  369. {
  370. // $sAttSpec is an attribute code
  371. //
  372. $this->add('<span style="white-space: nowrap;padding:5px;display:inline-block;">');
  373. $sFilterValue = '';
  374. $sFilterValue = utils::ReadParam($sPrefix.$sFieldName, '');
  375. $sFilterOpCode = null; // Use the default 'loose' OpCode
  376. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttSpec);
  377. if ($oAttDef->IsExternalKey())
  378. {
  379. $sTargetClass = $oAttDef->GetTargetClass();
  380. $oAllowedValues = new DBObjectSet(new DBObjectSearch($sTargetClass));
  381. $iFieldSize = $oAttDef->GetMaxSize();
  382. $iMaxComboLength = $oAttDef->GetMaximumComboLength();
  383. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;");
  384. //$oWidget = UIExtKeyWidget::DIsplayFromAttCode($sAttSpec, $sClass, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sPrefix.$sFieldName, false, '', $sPrefix, '');
  385. //$this->add($oWidget->Display($this, $aExtraParams, true /* bSearchMode */));
  386. $aExtKeyParams = $aExtraParams;
  387. $aExtKeyParams['iFieldSize'] = $oAttDef->GetMaxSize();
  388. $aExtKeyParams['iMinChars'] = $oAttDef->GetMinAutoCompleteChars();
  389. // DisplayFromAttCode($this, $sAttCode, $sClass, $sTitle, $oAllowedValues, $value, $iInputId, $bMandatory, $sFieldName = '', $sFormPrefix = '', $aArgs, $bSearchMode = false)
  390. $sHtml = UIExtKeyWidget::DisplayFromAttCode($this, $sAttSpec, $sClass, $oAttDef->GetLabel(), $oAllowedValues, $sFilterValue, $sPrefix.$sFieldName, false, $sPrefix.$sFieldName, $sPrefix, $aExtKeyParams, true);
  391. $this->add($sHtml);
  392. }
  393. else
  394. {
  395. $aAllowedValues = MetaModel::GetAllowedValues_flt($sClass, $sAttSpec, $aExtraParams);
  396. if (is_null($aAllowedValues))
  397. {
  398. // Any value is possible, display an input box
  399. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;<input class=\"textSearch\" name=\"$sPrefix$sFieldName\" value=\"$sFilterValue\"/>\n");
  400. }
  401. else
  402. {
  403. //Enum field or external key, display a combo
  404. $sValue = "<select name=\"$sPrefix$sFieldName\">\n";
  405. $sValue .= "<option value=\"\">".Dict::S('UI:SearchValue:Any')."</option>\n";
  406. foreach($aAllowedValues as $key => $value)
  407. {
  408. if ($sFilterValue == $key)
  409. {
  410. $sSelected = ' selected';
  411. }
  412. else
  413. {
  414. $sSelected = '';
  415. }
  416. $sValue .= "<option value=\"$key\"$sSelected>$value</option>\n";
  417. }
  418. $sValue .= "</select>\n";
  419. $this->add("<label>".MetaModel::GetFilterLabel($sClass, $sAttSpec).":</label>&nbsp;$sValue\n");
  420. }
  421. }
  422. unset($aExtraParams[$sFieldName]);
  423. $this->add('</span> ');
  424. $sTip = $oAttDef->GetHelpOnSmartSearch();
  425. if (strlen($sTip) > 0)
  426. {
  427. $sTip = addslashes($sTip);
  428. $sTip = str_replace(array("\n", "\r"), " ", $sTip);
  429. // :input does represent in form visible input (INPUT, SELECT, TEXTAREA)
  430. $this->add_ready_script("$(':input[name={$sPrefix}$sFieldName]').qtip( { content: '$sTip', show: 'mouseover', hide: 'mouseout', style: { name: 'dark', tip: 'leftTop' }, position: { corner: { target: 'rightMiddle', tooltip: 'leftTop' }} } );");
  431. }
  432. }
  433. }
  434. public function DisplaySearchForm($sClass, $aAttList, $aExtraParams, $sPrefix, $bClosed = true)
  435. {
  436. $sCSSClass = ($bClosed) ? 'DrawerClosed' : '';
  437. $this->add("<div id=\"ds_$sPrefix\" class=\"SearchDrawer $sCSSClass\">\n");
  438. $this->add_ready_script(
  439. <<<EOF
  440. $("#dh_$sPrefix").click( function() {
  441. $("#ds_$sPrefix").slideToggle('normal', function() { $("#ds_$sPrefix").parent().resize(); } );
  442. $("#dh_$sPrefix").toggleClass('open');
  443. });
  444. EOF
  445. );
  446. $this->add("<form id=\"search_$sClass\" action=\"\" method=\"post\">\n"); // Don't use $_SERVER['SCRIPT_NAME'] since the form may be called asynchronously (from ajax.php)
  447. // $this->add("<h2>".Dict::Format('UI:SearchFor_Class_Objects', 'xxxxxx')."</h2>\n");
  448. $this->add("<p>\n");
  449. foreach($aAttList as $sAttSpec)
  450. {
  451. //$oAppContext->Reset($sAttSpec); // Make sure the same parameter will not be passed twice
  452. $this->DisplaySearchField($sClass, $sAttSpec, $aExtraParams, $sPrefix);
  453. }
  454. $this->add("</p>\n");
  455. $this->add("<p align=\"right\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Search')."\"></p>\n");
  456. foreach($aExtraParams as $sName => $sValue)
  457. {
  458. $this->add("<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n");
  459. }
  460. // $this->add($oAppContext->GetForForm());
  461. $this->add("</form>\n");
  462. $this->add("</div>\n");
  463. $this->add("<div class=\"HRDrawer\"></div>\n");
  464. $this->add("<div id=\"dh_$sPrefix\" class=\"DrawerHandle\">".Dict::S('UI:SearchToggle')."</div>\n");
  465. }
  466. /**
  467. * Read parameters from the page
  468. * Parameters that were absent from the page's parameters are not set in the resulting hash array
  469. * @input string $sMethod Either get or post
  470. * @return Hash Array of name => value corresponding to the parameters that were passed to the page
  471. */
  472. public function ReadAllParams($sParamList, $sPrefix = 'attr_', $sMethod = 'get')
  473. {
  474. $aParams = explode(',', $sParamList);
  475. $aValues = array();
  476. foreach($aParams as $sName)
  477. {
  478. $sName = trim($sName);
  479. $value = utils::ReadParam($sPrefix.$sName, null, $sMethod);
  480. if (!is_null($value))
  481. {
  482. $aValues[$sName] = $value;
  483. }
  484. }
  485. return $aValues;
  486. }
  487. /**
  488. * Outputs a list of parameters as hidden fields
  489. * Example: attr_dummy[-123][id] = "blah"
  490. * @param Hash $aParameters Array name => value for the parameters
  491. * @param Array $aExclude The list of parameters that must not be handled this way (probably already in the visible part of the form)
  492. * @return void
  493. */
  494. protected function DumpHiddenParamsInternal($sName, $value)
  495. {
  496. if (is_array($value))
  497. {
  498. foreach($value as $sKey => $item)
  499. {
  500. $this->DumpHiddenParamsInternal($sName.'['.$sKey.']', $item);
  501. }
  502. }
  503. else
  504. {
  505. $this->Add("<input type=\"hidden\" name=\"$sName\" value=\"$value\">");
  506. }
  507. }
  508. /**
  509. * Outputs a list of parameters as hidden field into the current page
  510. * (must be called when inside a form)
  511. * @param Hash $aParameters Array name => value for the parameters
  512. * @param Array $aExclude The list of parameters that must not be handled this way (probably already in the visible part of the form)
  513. * @return void
  514. */
  515. public function DumpHiddenParams($aParameters, $aExclude = null, $sPrefix = 'attr_')
  516. {
  517. foreach($aParameters as $sAttCode => $value)
  518. {
  519. if (is_null($aExclude) || !in_array($sAttCode, $aExclude))
  520. {
  521. $this->DumpHiddenParamsInternal($sPrefix.$sAttCode, $value);
  522. }
  523. }
  524. }
  525. public function PostedParamsToFilter($sClass, $aAttList, $sPrefix)
  526. {
  527. $oFilter = new DBObjectSearch($sClass);
  528. $iCountParams = 0;
  529. foreach($aAttList as $sAttSpec)
  530. {
  531. $sFieldName = str_replace('->', PARAM_ARROW_SEP, $sAttSpec);
  532. $value = utils::ReadPostedParam($sPrefix.$sFieldName, null);
  533. if (!is_null($value) && strlen($value) > 0)
  534. {
  535. $oFilter->AddConditionAdvanced($sAttSpec, $value);
  536. $iCountParams++;
  537. }
  538. }
  539. if ($iCountParams == 0)
  540. {
  541. return null;
  542. }
  543. else
  544. {
  545. return $oFilter;
  546. }
  547. }
  548. /**
  549. * Updates the object form POSTED arguments, and writes it into the DB (applies a stimuli if requested)
  550. * @param DBObject $oObj The object to update
  551. * $param array $aAttList If set, this will limit the list of updated attributes
  552. * @return void
  553. */
  554. public function DoUpdateObjectFromPostedForm(DBObject $oObj, $aAttList = null)
  555. {
  556. $sTransactionId = utils::ReadPostedParam('transaction_id', '');
  557. if (!utils::IsTransactionValid($sTransactionId))
  558. {
  559. throw new Exception(Dict::S('UI:Error:ObjectAlreadyUpdated'));
  560. }
  561. $sClass = get_class($oObj);
  562. $oObj->UpdateObject('' /* form prefix */, $aAttList);
  563. // Optional: apply a stimulus
  564. //
  565. $sStimulus = trim(utils::ReadPostedParam('apply_stimulus', ''));
  566. if (!empty($sStimulus))
  567. {
  568. if (!$oObj->ApplyStimulus($sStimulus))
  569. {
  570. throw new Exception("Cannot apply stimulus '$sStimulus' to {$oObj->GetName()}");
  571. }
  572. }
  573. // Record the change
  574. //
  575. $oMyChange = MetaModel::NewObject("CMDBChange");
  576. $oMyChange->Set("date", time());
  577. $sUserString = CMDBChange::GetCurrentUserName();
  578. $oMyChange->Set("userinfo", $sUserString);
  579. $iChangeId = $oMyChange->DBInsert();
  580. $oObj->DBUpdateTracked($oMyChange);
  581. $this->p("<h1>".Dict::Format('UI:Class_Object_Updated', MetaModel::GetName(get_class($oObj)), $oObj->GetName())."</h1>\n");
  582. }
  583. /**
  584. * Find the object of the specified Class/ID.
  585. * @param WebPage $oP The current page
  586. * @return DBObject The found object, or throws an exception in case of failure
  587. */
  588. public function FindObjectFromArgs($aAllowedClasses = null)
  589. {
  590. $sClass = utils::ReadParam('class', '');
  591. $iId = utils::ReadParam('id', 0);
  592. if (empty($sClass))
  593. {
  594. throw new Exception("Missing argument 'class'");
  595. }
  596. if (!MetaModel::IsValidClass($sClass))
  597. {
  598. throw new Exception("Wrong value for argument 'class': $sClass");
  599. }
  600. if ($iId == 0)
  601. {
  602. throw new Exception("Missing argument 'id'");
  603. }
  604. if(!is_null($aAllowedClasses))
  605. {
  606. $bAllowed = false;
  607. foreach($aAllowedClasses as $sParentClass)
  608. {
  609. if (MetaModel::IsParentClass($sParentClass, $sClass))
  610. {
  611. $bAllowed = true;
  612. }
  613. }
  614. if (!$bAllowed)
  615. {
  616. throw new Exception("Class '$sClass not allowed in this implementation'");
  617. }
  618. }
  619. $oObj = MetaModel::GetObject($sClass, $iId, false);
  620. if (!is_object($oObj))
  621. {
  622. throw new Exception("Could not find the object $sClass/$iId");
  623. }
  624. return $oObj;
  625. }
  626. var $m_sWizardId = null;
  627. public function WizardFormStart($sId = '', $sNextStep = null, $bAttachment = false, $sMethod = 'post')
  628. {
  629. $this->m_sWizardId = $sId;
  630. // multipart... needed for file upload
  631. $this->add("<form id=\"{$this->m_sWizardId}\" method=\"$sMethod\" enctype=\"multipart/form-data\">\n");
  632. $aPreviousSteps = $this->GetWizardStepHistory();
  633. if (utils::ReadParam('step_back', 0) == 1)
  634. {
  635. // Back into the past history
  636. array_pop($aPreviousSteps);
  637. }
  638. else
  639. {
  640. // Moving forward
  641. array_push($aPreviousSteps, utils::ReadParam('next_step'));
  642. }
  643. $sStepHistory = implode(',', $aPreviousSteps);
  644. $this->add("<input type=\"hidden\" id=\"step_history\" name=\"step_history\" value=\"$sStepHistory\">");
  645. if (!is_null($sNextStep))
  646. {
  647. $this->add("<input type=\"hidden\" id=\"next_step\" name=\"next_step\" value=\"$sNextStep\">");
  648. }
  649. $this->add("<input type=\"hidden\" id=\"step_back\" name=\"step_back\" value=\"0\">");
  650. $sTransactionId = utils::GetNewTransactionId();
  651. $this->SetTransactionId($sTransactionId);
  652. $this->add("<input type=\"hidden\" id=\"transaction_id\" name=\"transaction_id\" value=\"$sTransactionId\">\n");
  653. $this->add_ready_script("$(window).unload(function() { OnUnload('$sTransactionId') } );\n");
  654. }
  655. public function WizardFormButtons($iButtonFlags)
  656. {
  657. $aButtons = array();
  658. if ($iButtonFlags & BUTTON_CANCEL)
  659. {
  660. $aButtons[] = "<input id=\"btn_cancel\" type=\"button\" value=\"".Dict::S('UI:Button:Cancel')."\" onClick=\"GoHome();\">";
  661. }
  662. if ($iButtonFlags & BUTTON_BACK)
  663. {
  664. $aButtons[] = "<input id=\"btn_back\" type=\"submit\" value=\"".Dict::S('UI:Button:Back')."\" onClick=\"GoBack('{$this->m_sWizardId}');\">";
  665. }
  666. if ($iButtonFlags & BUTTON_NEXT)
  667. {
  668. $aButtons[] = "<input id=\"btn_next\" type=\"submit\" value=\"".Dict::S('UI:Button:Next')."\">";
  669. }
  670. if ($iButtonFlags & BUTTON_FINISH)
  671. {
  672. $aButtons[] = "<input id=\"btn_finish\" type=\"submit\" value=\"".Dict::S('UI:Button:Finish')."\">";
  673. }
  674. $this->add('<div id="buttons">');
  675. $this->add(implode('', $aButtons));
  676. $this->add('</div>');
  677. }
  678. public function WizardFormEnd()
  679. {
  680. $this->add("</form>\n");
  681. }
  682. public function GetWizardStep()
  683. {
  684. if (utils::ReadParam('step_back', 0) == 1)
  685. {
  686. // Take the value into the history - one level above
  687. $aPreviousSteps = $this->GetWizardStepHistory();
  688. array_pop($aPreviousSteps);
  689. return end($aPreviousSteps);
  690. }
  691. else
  692. {
  693. return utils::ReadParam('next_step');
  694. }
  695. }
  696. protected function GetWizardStepHistory()
  697. {
  698. $sRawHistory = trim(utils::ReadParam('step_history', ''));
  699. if (strlen($sRawHistory) == 0)
  700. {
  701. return array();
  702. }
  703. else
  704. {
  705. return explode(',', $sRawHistory);
  706. }
  707. }
  708. public function WizardCheckSelectionOnSubmit($sMessageIfNoSelection)
  709. {
  710. $this->add_ready_script(
  711. <<<EOF
  712. $('#{$this->m_sWizardId}').submit(function() {
  713. return CheckSelection('$sMessageIfNoSelection');
  714. });
  715. EOF
  716. );
  717. }
  718. }
  719. ?>