portalwebpage.class.inc.php 25 KB

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