portalwebpage.class.inc.php 22 KB

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