portalwebpage.class.inc.php 25 KB

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