portalwebpage.class.inc.php 26 KB

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