portalwebpage.class.inc.php 23 KB

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