portalwebpage.class.inc.php 27 KB

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