webpage.class.inc.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. <?php
  2. // Copyright (C) 2010-2014 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 WebPage
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Generic interface common to CLI and Web pages
  26. */
  27. Interface Page
  28. {
  29. public function output();
  30. public function add($sText);
  31. public function p($sText);
  32. public function pre($sText);
  33. public function add_comment($sText);
  34. public function table($aConfig, $aData, $aParams = array());
  35. }
  36. /**
  37. * Simple helper class to ease the production of HTML pages
  38. *
  39. * This class provide methods to add content, scripts, includes... to a web page
  40. * and renders the full web page by putting the elements in the proper place & order
  41. * when the output() method is called.
  42. * Usage:
  43. * $oPage = new WebPage("Title of my page");
  44. * $oPage->p("Hello World !");
  45. * $oPage->output();
  46. */
  47. class WebPage implements Page
  48. {
  49. protected $s_title;
  50. protected $s_content;
  51. protected $s_deferred_content;
  52. protected $a_scripts;
  53. protected $a_dict_entries;
  54. protected $a_styles;
  55. protected $a_include_scripts;
  56. protected $a_include_stylesheets;
  57. protected $a_headers;
  58. protected $a_base;
  59. protected $iNextId;
  60. protected $iTransactionId;
  61. protected $sContentType;
  62. protected $sContentDisposition;
  63. protected $sContentFileName;
  64. protected $bTrashUnexpectedOutput;
  65. protected $s_sOutputFormat;
  66. protected $a_OutputOptions;
  67. public function __construct($s_title)
  68. {
  69. $this->s_title = $s_title;
  70. $this->s_content = "";
  71. $this->s_deferred_content = '';
  72. $this->a_scripts = array();
  73. $this->a_dict_entries = array();
  74. $this->a_styles = array();
  75. $this->a_linked_scripts = array();
  76. $this->a_linked_stylesheets = array();
  77. $this->a_headers = array();
  78. $this->a_base = array( 'href' => '', 'target' => '');
  79. $this->iNextId = 0;
  80. $this->iTransactionId = 0;
  81. $this->sContentType = '';
  82. $this->sContentDisposition = '';
  83. $this->sContentFileName = '';
  84. $this->bTrashUnexpectedOutput = false;
  85. $this->s_OutputFormat = utils::ReadParam('output_format', 'html');
  86. $this->a_OutputOptions = array();
  87. ob_start(); // Start capturing the output
  88. }
  89. /**
  90. * Change the title of the page after its creation
  91. */
  92. public function set_title($s_title)
  93. {
  94. $this->s_title = $s_title;
  95. }
  96. /**
  97. * Specify a default URL and a default target for all links on a page
  98. */
  99. public function set_base($s_href = '', $s_target = '')
  100. {
  101. $this->a_base['href'] = $s_href;
  102. $this->a_base['target'] = $s_target;
  103. }
  104. /**
  105. * Add any text or HTML fragment to the body of the page
  106. */
  107. public function add($s_html)
  108. {
  109. $this->s_content .= $s_html;
  110. }
  111. /**
  112. * Add any text or HTML fragment (identified by an ID) at the end of the body of the page
  113. * This is useful to add hidden content, DIVs or FORMs that should not
  114. * be embedded into each other.
  115. */
  116. public function add_at_the_end($s_html, $sId = '')
  117. {
  118. $this->s_deferred_content .= $s_html;
  119. }
  120. /**
  121. * Add a paragraph to the body of the page
  122. */
  123. public function p($s_html)
  124. {
  125. $this->add($this->GetP($s_html));
  126. }
  127. /**
  128. * Add a pre-formatted text to the body of the page
  129. */
  130. public function pre($s_html)
  131. {
  132. $this->add('<pre>'.$s_html.'</pre>');
  133. }
  134. /**
  135. * Add a comment
  136. */
  137. public function add_comment($sText)
  138. {
  139. $this->add('<!--'.$sText.'-->');
  140. }
  141. /**
  142. * Add a paragraph to the body of the page
  143. */
  144. public function GetP($s_html)
  145. {
  146. return "<p>$s_html</p>\n";
  147. }
  148. /**
  149. * Adds a tabular content to the web page
  150. * @param Hash $aConfig Configuration of the table: hash array of 'column_id' => 'Column Label'
  151. * @param Hash $aData Hash array. Data to display in the table: each row is made of 'column_id' => Data. A column 'pkey' is expected for each row
  152. * @param Hash $aParams Hash array. Extra parameters for the table.
  153. * @return void
  154. */
  155. public function table($aConfig, $aData, $aParams = array())
  156. {
  157. $this->add($this->GetTable($aConfig, $aData, $aParams));
  158. }
  159. public function GetTable($aConfig, $aData, $aParams = array())
  160. {
  161. $oAppContext = new ApplicationContext();
  162. static $iNbTables = 0;
  163. $iNbTables++;
  164. $sHtml = "";
  165. $sHtml .= "<table class=\"listResults\">\n";
  166. $sHtml .= "<thead>\n";
  167. $sHtml .= "<tr>\n";
  168. foreach($aConfig as $sName=>$aDef)
  169. {
  170. $sHtml .= "<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n";
  171. }
  172. $sHtml .= "</tr>\n";
  173. $sHtml .= "</thead>\n";
  174. $sHtml .= "<tbody>\n";
  175. foreach($aData as $aRow)
  176. {
  177. $sHtml .= $this->GetTableRow($aRow, $aConfig);
  178. }
  179. $sHtml .= "</tbody>\n";
  180. $sHtml .= "</table>\n";
  181. return $sHtml;
  182. }
  183. public function GetTableRow($aRow, $aConfig)
  184. {
  185. $sHtml = '';
  186. if (isset($aRow['@class'])) // Row specific class, for hilighting certain rows
  187. {
  188. $sHtml .= "<tr class=\"{$aRow['@class']}\">";
  189. }
  190. else
  191. {
  192. $sHtml .= "<tr>";
  193. }
  194. foreach($aConfig as $sName=>$aAttribs)
  195. {
  196. $sClass = isset($aAttribs['class']) ? 'class="'.$aAttribs['class'].'"' : '';
  197. $sValue = ($aRow[$sName] === '') ? '&nbsp;' : $aRow[$sName];
  198. $sHtml .= "<td $sClass>$sValue</td>";
  199. }
  200. $sHtml .= "</tr>";
  201. return $sHtml;
  202. }
  203. /**
  204. * Add some Javascript to the header of the page
  205. */
  206. public function add_script($s_script)
  207. {
  208. $this->a_scripts[] = $s_script;
  209. }
  210. /**
  211. * Add some Javascript to the header of the page
  212. */
  213. public function add_ready_script($s_script)
  214. {
  215. // Do nothing silently... this is not supported by this type of page...
  216. }
  217. /**
  218. * Add a dictionary entry for the Javascript side
  219. */
  220. public function add_dict_entry($s_entryId)
  221. {
  222. $this->a_dict_entries[$s_entryId] = Dict::S($s_entryId);
  223. }
  224. /**
  225. * Add some CSS definitions to the header of the page
  226. */
  227. public function add_style($s_style)
  228. {
  229. $this->a_styles[] = $s_style;
  230. }
  231. /**
  232. * Add a script (as an include, i.e. link) to the header of the page
  233. */
  234. public function add_linked_script($s_linked_script)
  235. {
  236. $this->a_linked_scripts[$s_linked_script] = $s_linked_script;
  237. }
  238. /**
  239. * Add a CSS stylesheet (as an include, i.e. link) to the header of the page
  240. */
  241. public function add_linked_stylesheet($s_linked_stylesheet, $s_condition = "")
  242. {
  243. $this->a_linked_stylesheets[] = array( 'link' => $s_linked_stylesheet, 'condition' => $s_condition);
  244. }
  245. public function add_saas($sSaasRelPath)
  246. {
  247. $sSaasPath = APPROOT.$sSaasRelPath;
  248. $sCssRelPath = preg_replace('/\.scss$/', '.css', $sSaasRelPath);
  249. $sCssPath = APPROOT.$sCssRelPath;
  250. clearstatcache();
  251. if (!file_exists($sCssPath) || (is_writable($sCssPath) && (filemtime($sCssPath) < filemtime($sSaasPath))))
  252. {
  253. // Rebuild the CSS file from the Saas file
  254. if (file_exists(APPROOT.'lib/sass/sass/SassParser.php'))
  255. {
  256. require_once(APPROOT.'lib/sass/sass/SassParser.php'); //including Sass libary (Syntactically Awesome Stylesheets)
  257. $oParser = new SassParser(array('style'=>'expanded'));
  258. $sCss = $oParser->toCss($sSaasPath);
  259. file_put_contents($sCssPath, $sCss);
  260. }
  261. }
  262. $sCSSUrl = utils::GetAbsoluteUrlAppRoot().$sCssRelPath;
  263. $this->add_linked_stylesheet($sCSSUrl);
  264. }
  265. /**
  266. * Add some custom header to the page
  267. */
  268. public function add_header($s_header)
  269. {
  270. $this->a_headers[] = $s_header;
  271. }
  272. /**
  273. * Add needed headers to the page so that it will no be cached
  274. */
  275. public function no_cache()
  276. {
  277. $this->add_header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  278. $this->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  279. }
  280. /**
  281. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  282. */
  283. public function details($aFields)
  284. {
  285. $this->add($this->GetDetails($aFields));
  286. }
  287. /**
  288. * Whether or not the page is a PDF page
  289. * @return boolean
  290. */
  291. public function is_pdf()
  292. {
  293. return false;
  294. }
  295. /**
  296. * Records the current state of the 'html' part of the page output
  297. * @return mixed The current state of the 'html' output
  298. */
  299. public function start_capture()
  300. {
  301. return strlen($this->s_content);
  302. }
  303. /**
  304. * Returns the part of the html output that occurred since the call to start_capture
  305. * and removes this part from the current html output
  306. * @param $offset mixed The value returned by start_capture
  307. * @return string The part of the html output that was added since the call to start_capture
  308. */
  309. public function end_capture($offset)
  310. {
  311. $sCaptured = substr($this->s_content, $offset);
  312. $this->s_content = substr($this->s_content, 0, $offset);
  313. return $sCaptured;
  314. }
  315. /**
  316. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  317. */
  318. public function GetDetails($aFields)
  319. {
  320. $sHtml = "<table class=\"details\">\n";
  321. $sHtml .= "<tbody>\n";
  322. foreach($aFields as $aAttrib)
  323. {
  324. $sHtml .= "<tr>\n";
  325. // By Rom, for csv import, proposed to show several values for column selection
  326. if (is_array($aAttrib['value']))
  327. {
  328. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".implode("</td><td>", $aAttrib['value'])."</td>\n";
  329. }
  330. else
  331. {
  332. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".$aAttrib['value']."</td>\n";
  333. }
  334. $sComment = (isset($aAttrib['comments'])) ? $aAttrib['comments'] : '&nbsp;';
  335. $sInfo = (isset($aAttrib['infos'])) ? $aAttrib['infos'] : '&nbsp;';
  336. $sHtml .= "<td>$sComment</td><td>$sInfo</td>\n";
  337. $sHtml .= "</tr>\n";
  338. }
  339. $sHtml .= "</tbody>\n";
  340. $sHtml .= "</table>\n";
  341. return $sHtml;
  342. }
  343. /**
  344. * Build a set of radio buttons suitable for editing a field/attribute of an object (including its validation)
  345. * @param $aAllowedValues hash Array of value => display_value
  346. * @param $value mixed Current value for the field/attribute
  347. * @param $iId mixed Unique Id for the input control in the page
  348. * @param $sFieldName string The name of the field, attr_<$sFieldName> will hold the value for the field
  349. * @param $bMandatory bool Whether or not the field is mandatory
  350. * @param $bVertical bool Disposition of the radio buttons vertical or horizontal
  351. * @param $sValidationField string HTML fragment holding the validation field (exclamation icon...)
  352. * @return string The HTML fragment corresponding to the radio buttons
  353. */
  354. public function GetRadioButtons($aAllowedValues, $value, $iId, $sFieldName, $bMandatory, $bVertical, $sValidationField)
  355. {
  356. $idx = 0;
  357. $sHTMLValue = '';
  358. foreach($aAllowedValues as $key => $display_value)
  359. {
  360. if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
  361. {
  362. // When there is only once choice, select it by default
  363. $sSelected = ' checked';
  364. }
  365. else
  366. {
  367. $sSelected = ($value == $key) ? ' checked' : '';
  368. }
  369. $sHTMLValue .= "<input type=\"radio\" id=\"{$iId}_{$key}\" name=\"radio_$sFieldName\" onChange=\"$('#{$iId}').val(this.value).trigger('change');\" value=\"$key\"$sSelected><label class=\"radio\" for=\"{$iId}_{$key}\">&nbsp;$display_value</label>&nbsp;";
  370. if ($bVertical)
  371. {
  372. if ($idx == 0)
  373. {
  374. // Validation icon at the end of the first line
  375. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  376. }
  377. $sHTMLValue .= "<br>\n";
  378. }
  379. $idx++;
  380. }
  381. $sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"$sFieldName\" value=\"$value\"/>";
  382. if (!$bVertical)
  383. {
  384. // Validation icon at the end of the line
  385. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  386. }
  387. return $sHTMLValue;
  388. }
  389. /**
  390. * Discard unexpected output data (such as PHP warnings)
  391. * This is a MUST when the Page output is DATA (download of a document, download CSV export, download ...)
  392. */
  393. public function TrashUnexpectedOutput()
  394. {
  395. $this->bTrashUnexpectedOutput = true;
  396. }
  397. /**
  398. * Read the output buffer and deal with its contents:
  399. * - trash unexpected output if the flag has been set
  400. * - report unexpected behaviors such as the output buffering being stopped
  401. *
  402. * Possible improvement: I've noticed that several output buffers are stacked,
  403. * if they are not empty, the output will be corrupted. The solution would
  404. * consist in unstacking all of them (and concatenate the contents).
  405. */
  406. protected function ob_get_clean_safe()
  407. {
  408. $sOutput = ob_get_contents();
  409. if ($sOutput === false)
  410. {
  411. $sMsg = "Design/integration issue: No output buffer. Some piece of code has called ob_get_clean() or ob_end_clean() without calling ob_start()";
  412. if ($this->bTrashUnexpectedOutput)
  413. {
  414. IssueLog::Error($sMsg);
  415. $sOutput = '';
  416. }
  417. else
  418. {
  419. $sOutput = $sMsg;
  420. }
  421. }
  422. else
  423. {
  424. ob_end_clean(); // on some versions of PHP doing so when the output buffering is stopped can cause a notice
  425. if ($this->bTrashUnexpectedOutput)
  426. {
  427. if (trim($sOutput) != '')
  428. {
  429. if (Utils::GetConfig() && Utils::GetConfig()->Get('debug_report_spurious_chars'))
  430. {
  431. IssueLog::Error("Trashing unexpected output:'$s_captured_output'\n");
  432. }
  433. }
  434. $sOutput = '';
  435. }
  436. }
  437. return $sOutput;
  438. }
  439. /**
  440. * Outputs (via some echo) the complete HTML page by assembling all its elements
  441. */
  442. public function output()
  443. {
  444. foreach($this->a_headers as $s_header)
  445. {
  446. header($s_header);
  447. }
  448. $s_captured_output = $this->ob_get_clean_safe();
  449. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  450. echo "<html>\n";
  451. echo "<head>\n";
  452. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
  453. echo "<title>".htmlentities($this->s_title, ENT_QUOTES, 'UTF-8')."</title>\n";
  454. echo $this->get_base_tag();
  455. foreach($this->a_linked_scripts as $s_script)
  456. {
  457. // Make sure that the URL to the script contains the application's version number
  458. // so that the new script do NOT get reloaded from the cache when the application is upgraded
  459. if (strpos($s_script, '?') === false)
  460. {
  461. $s_script .= "?itopversion=".ITOP_VERSION;
  462. }
  463. else
  464. {
  465. $s_script .= "&itopversion=".ITOP_VERSION;
  466. }
  467. echo "<script type=\"text/javascript\" src=\"$s_script\"></script>\n";
  468. }
  469. if (count($this->a_scripts)>0)
  470. {
  471. echo "<script type=\"text/javascript\">\n";
  472. foreach($this->a_scripts as $s_script)
  473. {
  474. echo "$s_script\n";
  475. }
  476. echo "</script>\n";
  477. }
  478. $this->output_dict_entries();
  479. foreach($this->a_linked_stylesheets as $a_stylesheet)
  480. {
  481. if ($a_stylesheet['condition'] != "")
  482. {
  483. echo "<!--[if {$a_stylesheet['condition']}]>\n";
  484. }
  485. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$a_stylesheet['link']}\" />\n";
  486. if ($a_stylesheet['condition'] != "")
  487. {
  488. echo "<![endif]-->\n";
  489. }
  490. }
  491. if (count($this->a_styles)>0)
  492. {
  493. echo "<style>\n";
  494. foreach($this->a_styles as $s_style)
  495. {
  496. echo "$s_style\n";
  497. }
  498. echo "</style>\n";
  499. }
  500. if (class_exists('MetaModel') && MetaModel::GetConfig())
  501. {
  502. echo "<link rel=\"shortcut icon\" href=\"".utils::GetAbsoluteUrlAppRoot()."images/favicon.ico\" />\n";
  503. }
  504. echo "</head>\n";
  505. echo "<body>\n";
  506. echo self::FilterXSS($this->s_content);
  507. if (trim($s_captured_output) != "")
  508. {
  509. echo "<div class=\"raw_output\">".self::FilterXSS($s_captured_output)."</div>\n";
  510. }
  511. echo '<div id="at_the_end">'.self::FilterXSS($this->s_deferred_content).'</div>';
  512. echo "</body>\n";
  513. echo "</html>\n";
  514. if (class_exists('MetaModel'))
  515. {
  516. MetaModel::RecordQueryTrace();
  517. }
  518. if (class_exists('ExecutionKPI'))
  519. {
  520. ExecutionKPI::ReportStats();
  521. }
  522. }
  523. /**
  524. * Build a series of hidden field[s] from an array
  525. */
  526. public function add_input_hidden($sLabel, $aData)
  527. {
  528. foreach($aData as $sKey => $sValue)
  529. {
  530. // Note: protection added to protect against the Notice 'array to string conversion' that appeared with PHP 5.4
  531. // (this function seems unused though!)
  532. if (is_scalar($sValue))
  533. {
  534. $this->add("<input type=\"hidden\" name=\"".$sLabel."[$sKey]\" value=\"$sValue\">");
  535. }
  536. }
  537. }
  538. protected function get_base_tag()
  539. {
  540. $sTag = '';
  541. if (($this->a_base['href'] != '') || ($this->a_base['target'] != ''))
  542. {
  543. $sTag = '<base ';
  544. if (($this->a_base['href'] != ''))
  545. {
  546. $sTag .= "href =\"{$this->a_base['href']}\" ";
  547. }
  548. if (($this->a_base['target'] != ''))
  549. {
  550. $sTag .= "target =\"{$this->a_base['target']}\" ";
  551. }
  552. $sTag .= " />\n";
  553. }
  554. return $sTag;
  555. }
  556. /**
  557. * Get an ID (for any kind of HTML tag) that is guaranteed unique in this page
  558. * @return int The unique ID (in this page)
  559. */
  560. public function GetUniqueId()
  561. {
  562. return $this->iNextId++;
  563. }
  564. /**
  565. * Set the content-type (mime type) for the page's content
  566. * @param $sContentType string
  567. * @return void
  568. */
  569. public function SetContentType($sContentType)
  570. {
  571. $this->sContentType = $sContentType;
  572. }
  573. /**
  574. * Set the content-disposition (mime type) for the page's content
  575. * @param $sDisposition string The disposition: 'inline' or 'attachment'
  576. * @param $sFileName string The original name of the file
  577. * @return void
  578. */
  579. public function SetContentDisposition($sDisposition, $sFileName)
  580. {
  581. $this->sContentDisposition = $sDisposition;
  582. $this->sContentFileName = $sFileName;
  583. }
  584. /**
  585. * Set the transactionId of the current form
  586. * @param $iTransactionId integer
  587. * @return void
  588. */
  589. public function SetTransactionId($iTransactionId)
  590. {
  591. $this->iTransactionId = $iTransactionId;
  592. }
  593. /**
  594. * Returns the transactionId of the current form
  595. * @return integer The current transactionID
  596. */
  597. public function GetTransactionId()
  598. {
  599. return $this->iTransactionId;
  600. }
  601. public static function FilterXSS($sHTML)
  602. {
  603. return str_ireplace('<script', '&lt;script', $sHTML);
  604. }
  605. /**
  606. * What is the currently selected output format
  607. * @return string The selected output format: html, pdf...
  608. */
  609. public function GetOutputFormat()
  610. {
  611. return $this->s_OutputFormat;
  612. }
  613. /**
  614. * Check whether the desired output format is possible or not
  615. * @param string $sOutputFormat The desired output format: html, pdf...
  616. * @return bool True if the format is Ok, false otherwise
  617. */
  618. function IsOutputFormatAvailable($sOutputFormat)
  619. {
  620. $bResult = false;
  621. switch($sOutputFormat)
  622. {
  623. case 'html':
  624. $bResult = true; // Always supported
  625. break;
  626. case 'pdf':
  627. $bResult = @is_readable(APPROOT.'lib/MPDF/mpdf.php');
  628. break;
  629. }
  630. return $bResult;
  631. }
  632. /**
  633. * Retrieves the value of a named output option for the given format
  634. * @param string $sFormat The format: html or pdf
  635. * @param string $sOptionName The name of the option
  636. * @return mixed false if the option was never set or the options's value
  637. */
  638. public function GetOutputOption($sFormat, $sOptionName)
  639. {
  640. if (isset($this->a_OutputOptions[$sFormat][$sOptionName]))
  641. {
  642. return $this->a_OutputOptions[$sFormat][$sOptionName];
  643. }
  644. return false;
  645. }
  646. /**
  647. * Sets a named output option for the given format
  648. * @param string $sFormat The format for which to set the option: html or pdf
  649. * @param string $sOptionName the name of the option
  650. * @param mixed $sValue The value of the option
  651. */
  652. public function SetOutputOption($sFormat, $sOptionName, $sValue)
  653. {
  654. if (!isset($this->a_OutputOptions[$sFormat]))
  655. {
  656. $this->a_OutputOptions[$sFormat] = array($sOptionName => $sValue);
  657. }
  658. else
  659. {
  660. $this->a_OutputOptions[$sFormat][$sOptionName] = $sValue;
  661. }
  662. }
  663. public function RenderPopupMenuItems($aActions, $aFavoriteActions = array())
  664. {
  665. $sPrevUrl = '';
  666. $sHtml = '';
  667. foreach ($aActions as $aAction)
  668. {
  669. $sClass = isset($aAction['class']) ? " class=\"{$aAction['class']}\"" : "";
  670. $sOnClick = isset($aAction['onclick']) ? ' onclick="'.htmlspecialchars($aAction['onclick'], ENT_QUOTES, "UTF-8").'"' : '';
  671. $sTarget = isset($aAction['target']) ? " target=\"{$aAction['target']}\"" : "";
  672. if (empty($aAction['url']))
  673. {
  674. if ($sPrevUrl != '') // Don't output consecutively two separators...
  675. {
  676. $sHtml .= "<li>{$aAction['label']}</li>";
  677. }
  678. $sPrevUrl = '';
  679. }
  680. else
  681. {
  682. $sHtml .= "<li><a $sTarget href=\"{$aAction['url']}\"$sClass $sOnClick>{$aAction['label']}</a></li>";
  683. $sPrevUrl = $aAction['url'];
  684. }
  685. }
  686. $sHtml .= "</ul></li></ul></div>";
  687. foreach(array_reverse($aFavoriteActions) as $aAction)
  688. {
  689. $sTarget = isset($aAction['target']) ? " target=\"{$aAction['target']}\"" : "";
  690. $sHtml .= "<div class=\"actions_button\"><a $sTarget href='{$aAction['url']}'>{$aAction['label']}</a></div>";
  691. }
  692. return $sHtml;
  693. }
  694. protected function output_dict_entries($bReturnOutput = false)
  695. {
  696. $sHtml = '';
  697. if (count($this->a_dict_entries)>0)
  698. {
  699. $sHtml .= "<script type=\"text/javascript\">\n";
  700. $sHtml .= "var Dict = {};\n";
  701. $sHtml .= "Dict._entries = {};\n";
  702. $sHtml .= "Dict.S = function(sEntry) {\n";
  703. $sHtml .= " if (sEntry in Dict._entries)\n";
  704. $sHtml .= " {\n";
  705. $sHtml .= " return Dict._entries[sEntry];\n";
  706. $sHtml .= " }\n";
  707. $sHtml .= " else\n";
  708. $sHtml .= " {\n";
  709. $sHtml .= " return sEntry;\n";
  710. $sHtml .= " }\n";
  711. $sHtml .= "};\n";
  712. foreach($this->a_dict_entries as $s_entry => $s_value)
  713. {
  714. $sHtml .= "Dict._entries['$s_entry'] = '".addslashes($s_value)."';\n";
  715. }
  716. $sHtml .= "</script>\n";
  717. }
  718. if ($bReturnOutput)
  719. {
  720. return $sHtml;
  721. }
  722. else
  723. {
  724. echo $sHtml;
  725. }
  726. }
  727. }
  728. interface iTabbedPage
  729. {
  730. public function AddTabContainer($sTabContainer, $sPrefix = '');
  731. public function AddToTab($sTabContainer, $sTabLabel, $sHtml);
  732. public function SetCurrentTabContainer($sTabContainer = '');
  733. public function SetCurrentTab($sTabLabel = '');
  734. /**
  735. * Add a tab which content will be loaded asynchronously via the supplied URL
  736. *
  737. * Limitations:
  738. * Cross site scripting is not not allowed for security reasons. Use a normal tab with an IFRAME if you want to pull content from another server.
  739. * Static content cannot be added inside such tabs.
  740. *
  741. * @param string $sTabLabel The (localised) label of the tab
  742. * @param string $sUrl The URL to load (on the same server)
  743. * @param boolean $bCache Whether or not to cache the content of the tab once it has been loaded. flase will cause the tab to be reloaded upon each activation.
  744. * @since 2.0.3
  745. */
  746. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true);
  747. public function GetCurrentTab();
  748. public function RemoveTab($sTabLabel, $sTabContainer = null);
  749. /**
  750. * Finds the tab whose title matches a given pattern
  751. * @return mixed The name of the tab as a string or false if not found
  752. */
  753. public function FindTab($sPattern, $sTabContainer = null);
  754. }
  755. /**
  756. * Helper class to implement JQueryUI tabs inside a page
  757. */
  758. class TabManager
  759. {
  760. protected $m_aTabs;
  761. protected $m_sCurrentTabContainer;
  762. protected $m_sCurrentTab;
  763. public function __construct()
  764. {
  765. $this->m_aTabs = array();
  766. $this->m_sCurrentTabContainer = '';
  767. $this->m_sCurrentTab = '';
  768. }
  769. public function AddTabContainer($sTabContainer, $sPrefix = '')
  770. {
  771. $this->m_aTabs[$sTabContainer] = array('prefix' => $sPrefix, 'tabs' => array());
  772. return "\$Tabs:$sTabContainer\$";
  773. }
  774. public function AddToCurrentTab($sHtml)
  775. {
  776. $this->AddToTab($this->m_sCurrentTabContainer, $this->m_sCurrentTab, $sHtml);
  777. }
  778. public function GetCurrentTabLength($sHtml)
  779. {
  780. $iLength = isset($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html']) ? strlen($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html']): 0;
  781. return $iLength;
  782. }
  783. /**
  784. * Truncates the given tab to the specifed length and returns the truncated part
  785. * @param string $sTabContainer The tab container in which to truncate the tab
  786. * @param string $sTab The name/identifier of the tab to truncate
  787. * @param integer $iLength The length/offset at which to truncate the tab
  788. * @return string The truncated part
  789. */
  790. public function TruncateTab($sTabContainer, $sTab, $iLength)
  791. {
  792. $sResult = substr($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'], $iLength);
  793. $this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'] = substr($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'], 0, $iLength);
  794. return $sResult;
  795. }
  796. public function TabExists($sTabContainer, $sTab)
  797. {
  798. return isset($this->m_aTabs[$sTabContainer]['tabs'][$sTab]);
  799. }
  800. public function TabsContainerCount()
  801. {
  802. return count($this->m_aTabs);
  803. }
  804. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  805. {
  806. if (!isset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]))
  807. {
  808. // Set the content of the tab
  809. $this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel] = array(
  810. 'type' => 'html',
  811. 'html' => $sHtml,
  812. );
  813. }
  814. else
  815. {
  816. if ($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['type'] != 'html')
  817. {
  818. throw new Exception("Cannot add HTML content to the tab '$sTabLabel' of type '{$this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['type']}'");
  819. }
  820. // Append to the content of the tab
  821. $this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['html'] .= $sHtml;
  822. }
  823. return ''; // Nothing to add to the page for now
  824. }
  825. public function SetCurrentTabContainer($sTabContainer = '')
  826. {
  827. $sPreviousTabContainer = $this->m_sCurrentTabContainer;
  828. $this->m_sCurrentTabContainer = $sTabContainer;
  829. return $sPreviousTabContainer;
  830. }
  831. public function SetCurrentTab($sTabLabel = '')
  832. {
  833. $sPreviousTab = $this->m_sCurrentTab;
  834. $this->m_sCurrentTab = $sTabLabel;
  835. return $sPreviousTab;
  836. }
  837. /**
  838. * Add a tab which content will be loaded asynchronously via the supplied URL
  839. *
  840. * Limitations:
  841. * Cross site scripting is not not allowed for security reasons. Use a normal tab with an IFRAME if you want to pull content from another server.
  842. * Static content cannot be added inside such tabs.
  843. *
  844. * @param string $sTabLabel The (localised) label of the tab
  845. * @param string $sUrl The URL to load (on the same server)
  846. * @param boolean $bCache Whether or not to cache the content of the tab once it has been loaded. flase will cause the tab to be reloaded upon each activation.
  847. * @since 2.0.3
  848. */
  849. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true)
  850. {
  851. // Set the content of the tab
  852. $this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$sTabLabel] = array(
  853. 'type' => 'ajax',
  854. 'url' => $sUrl,
  855. 'cache' => $bCache,
  856. );
  857. return ''; // Nothing to add to the page for now
  858. }
  859. public function GetCurrentTabContainer()
  860. {
  861. return $this->m_sCurrentTabContainer;
  862. }
  863. public function GetCurrentTab()
  864. {
  865. return $this->m_sCurrentTab;
  866. }
  867. public function RemoveTab($sTabLabel, $sTabContainer = null)
  868. {
  869. if ($sTabContainer == null)
  870. {
  871. $sTabContainer = $this->m_sCurrentTabContainer;
  872. }
  873. if (isset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]))
  874. {
  875. // Delete the content of the tab
  876. unset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]);
  877. // If we just removed the active tab, let's reset the active tab
  878. if (($this->m_sCurrentTabContainer == $sTabContainer) && ($this->m_sCurrentTab == $sTabLabel))
  879. {
  880. $this->m_sCurrentTab = '';
  881. }
  882. }
  883. }
  884. /**
  885. * Finds the tab whose title matches a given pattern
  886. * @return mixed The actual name of the tab (as a string) or false if not found
  887. */
  888. public function FindTab($sPattern, $sTabContainer = null)
  889. {
  890. $result = false;
  891. if ($sTabContainer == null)
  892. {
  893. $sTabContainer = $this->m_sCurrentTabContainer;
  894. }
  895. foreach($this->m_aTabs[$sTabContainer]['tabs'] as $sTabLabel => $void)
  896. {
  897. if (preg_match($sPattern, $sTabLabel))
  898. {
  899. $result = $sTabLabel;
  900. break;
  901. }
  902. }
  903. return $result;
  904. }
  905. /**
  906. * Make the given tab the active one, as if it were clicked
  907. * DOES NOT WORK: apparently in the *old* version of jquery
  908. * that we are using this is not supported... TO DO upgrade
  909. * the whole jquery bundle...
  910. */
  911. public function SelectTab($sTabContainer, $sTabLabel)
  912. {
  913. $container_index = 0;
  914. $tab_index = 0;
  915. foreach($this->m_aTabs as $sCurrentTabContainerName => $aTabs)
  916. {
  917. if ($sTabContainer == $sCurrentTabContainerName)
  918. {
  919. foreach($aTabs['tabs'] as $sCurrentTabLabel => $void)
  920. {
  921. if ($sCurrentTabLabel == $sTabLabel)
  922. {
  923. break;
  924. }
  925. $tab_index++;
  926. }
  927. break;
  928. }
  929. $container_index++;
  930. }
  931. $sSelector = '#tabbedContent_'.$container_index.' > ul';
  932. return "window.setTimeout(\"$('$sSelector').tabs('select', $tab_index);\", 100);"; // Let the time to the tabs widget to initialize
  933. }
  934. public function RenderIntoContent($sContent)
  935. {
  936. // Render the tabs in the page (if any)
  937. foreach($this->m_aTabs as $sTabContainerName => $aTabs)
  938. {
  939. $sTabs = '';
  940. $sPrefix = $aTabs['prefix'];
  941. $container_index = 0;
  942. if (count($aTabs['tabs']) > 0)
  943. {
  944. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$container_index}\" class=\"light\">\n";
  945. $sTabs .= "<ul>\n";
  946. // Display the unordered list that will be rendered as the tabs
  947. $i = 0;
  948. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  949. {
  950. switch($aTabData['type'])
  951. {
  952. case 'ajax':
  953. $sTabs .= "<li data-cache=\"".($aTabData['cache'] ? 'true' : 'false')."\"><a href=\"{$aTabData['url']}\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  954. break;
  955. case 'html':
  956. default:
  957. $sTabs .= "<li><a href=\"#tab_{$sPrefix}{$container_index}$i\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  958. }
  959. $i++;
  960. }
  961. $sTabs .= "</ul>\n";
  962. // Now add the content of the tabs themselves
  963. $i = 0;
  964. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  965. {
  966. switch($aTabData['type'])
  967. {
  968. case 'ajax':
  969. // Nothing to add
  970. break;
  971. case 'html':
  972. default:
  973. $sTabs .= "<div id=\"tab_{$sPrefix}{$container_index}$i\">".$aTabData['html']."</div>\n";
  974. }
  975. $i++;
  976. }
  977. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  978. }
  979. $sContent = str_replace("\$Tabs:$sTabContainerName\$", $sTabs, $sContent);
  980. $container_index++;
  981. }
  982. return $sContent;
  983. }
  984. }