webpage.class.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 WebPage
  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. /**
  25. * Simple helper class to ease the production of HTML pages
  26. *
  27. * This class provide methods to add content, scripts, includes... to a web page
  28. * and renders the full web page by putting the elements in the proper place & order
  29. * when the output() method is called.
  30. * Usage:
  31. * $oPage = new WebPage("Title of my page");
  32. * $oPage->p("Hello World !");
  33. * $oPage->output();
  34. */
  35. class WebPage
  36. {
  37. protected $s_title;
  38. protected $s_content;
  39. protected $s_deferred_content;
  40. protected $a_scripts;
  41. protected $a_styles;
  42. protected $a_include_scripts;
  43. protected $a_include_stylesheets;
  44. protected $a_headers;
  45. protected $a_base;
  46. protected $iNextId;
  47. protected $iTransactionId;
  48. protected $sContentType;
  49. protected $sContentDisposition;
  50. protected $sContentFileName;
  51. protected $s_sOutputFormat;
  52. protected $a_OutputOptions;
  53. public function __construct($s_title)
  54. {
  55. $this->s_title = $s_title;
  56. $this->s_content = "";
  57. $this->s_deferred_content = '';
  58. $this->a_scripts = array();
  59. $this->a_styles = array();
  60. $this->a_linked_scripts = array();
  61. $this->a_linked_stylesheets = array();
  62. $this->a_headers = array();
  63. $this->a_base = array( 'href' => '', 'target' => '');
  64. $this->iNextId = 0;
  65. $this->iTransactionId = 0;
  66. $this->sContentType = '';
  67. $this->sContentDisposition = '';
  68. $this->sContentFileName = '';
  69. $this->s_OutputFormat = utils::ReadParam('output_format', 'html');
  70. $this->a_OutputOptions = array();
  71. ob_start(); // Start capturing the output
  72. }
  73. /**
  74. * Change the title of the page after its creation
  75. */
  76. public function set_title($s_title)
  77. {
  78. $this->s_title = $s_title;
  79. }
  80. /**
  81. * Specify a default URL and a default target for all links on a page
  82. */
  83. public function set_base($s_href = '', $s_target = '')
  84. {
  85. $this->a_base['href'] = $s_href;
  86. $this->a_base['target'] = $s_target;
  87. }
  88. /**
  89. * Add any text or HTML fragment to the body of the page
  90. */
  91. public function add($s_html)
  92. {
  93. $this->s_content .= $s_html;
  94. }
  95. /**
  96. * Add any text or HTML fragment (identified by an ID) at the end of the body of the page
  97. * This is useful to add hidden content, DIVs or FORMs that should not
  98. * be embedded into each other.
  99. */
  100. public function add_at_the_end($s_html, $sId = '')
  101. {
  102. $this->s_deferred_content .= $s_html;
  103. }
  104. /**
  105. * Add a paragraph to the body of the page
  106. */
  107. public function p($s_html)
  108. {
  109. $this->add($this->GetP($s_html));
  110. }
  111. /**
  112. * Add a pre-formatted text to the body of the page
  113. */
  114. public function pre($s_html)
  115. {
  116. $this->add('<pre>'.$s_html.'</pre>');
  117. }
  118. /**
  119. * Add a paragraph to the body of the page
  120. */
  121. public function GetP($s_html)
  122. {
  123. return "<p>$s_html</p>\n";
  124. }
  125. /**
  126. * Adds a tabular content to the web page
  127. * @param Hash $aConfig Configuration of the table: hash array of 'column_id' => 'Column Label'
  128. * @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
  129. * @param Hash $aParams Hash array. Extra parameters for the table.
  130. * @return void
  131. */
  132. public function table($aConfig, $aData, $aParams = array())
  133. {
  134. $this->add($this->GetTable($aConfig, $aData, $aParams));
  135. }
  136. public function GetTable($aConfig, $aData, $aParams = array())
  137. {
  138. $oAppContext = new ApplicationContext();
  139. static $iNbTables = 0;
  140. $iNbTables++;
  141. $sHtml = "";
  142. $sHtml .= "<table class=\"listResults\">\n";
  143. $sHtml .= "<thead>\n";
  144. $sHtml .= "<tr>\n";
  145. foreach($aConfig as $sName=>$aDef)
  146. {
  147. $sHtml .= "<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n";
  148. }
  149. $sHtml .= "</tr>\n";
  150. $sHtml .= "</thead>\n";
  151. $sHtml .= "<tbody>\n";
  152. foreach($aData as $aRow)
  153. {
  154. $sHtml .= $this->GetTableRow($aRow, $aConfig);
  155. }
  156. $sHtml .= "</tbody>\n";
  157. $sHtml .= "</table>\n";
  158. return $sHtml;
  159. }
  160. public function GetTableRow($aRow, $aConfig)
  161. {
  162. $sHtml = '';
  163. if (isset($aRow['@class'])) // Row specific class, for hilighting certain rows
  164. {
  165. $sHtml .= "<tr class=\"{$aRow['@class']}\">";
  166. }
  167. else
  168. {
  169. $sHtml .= "<tr>";
  170. }
  171. foreach($aConfig as $sName=>$aAttribs)
  172. {
  173. $sClass = isset($aAttribs['class']) ? 'class="'.$aAttribs['class'].'"' : '';
  174. $sValue = ($aRow[$sName] === '') ? '&nbsp;' : $aRow[$sName];
  175. $sHtml .= "<td $sClass>$sValue</td>";
  176. }
  177. $sHtml .= "</tr>";
  178. return $sHtml;
  179. }
  180. /**
  181. * Add some Javascript to the header of the page
  182. */
  183. public function add_script($s_script)
  184. {
  185. $this->a_scripts[] = $s_script;
  186. }
  187. /**
  188. * Add some Javascript to the header of the page
  189. */
  190. public function add_ready_script($s_script)
  191. {
  192. // Do nothing silently... this is not supported by this type of page...
  193. }
  194. /**
  195. * Add some CSS definitions to the header of the page
  196. */
  197. public function add_style($s_style)
  198. {
  199. $this->a_styles[] = $s_style;
  200. }
  201. /**
  202. * Add a script (as an include, i.e. link) to the header of the page
  203. */
  204. public function add_linked_script($s_linked_script)
  205. {
  206. $this->a_linked_scripts[$s_linked_script] = $s_linked_script;
  207. }
  208. /**
  209. * Add a CSS stylesheet (as an include, i.e. link) to the header of the page
  210. */
  211. public function add_linked_stylesheet($s_linked_stylesheet, $s_condition = "")
  212. {
  213. $this->a_linked_stylesheets[] = array( 'link' => $s_linked_stylesheet, 'condition' => $s_condition);
  214. }
  215. /**
  216. * Add some custom header to the page
  217. */
  218. public function add_header($s_header)
  219. {
  220. $this->a_headers[] = $s_header;
  221. }
  222. /**
  223. * Add needed headers to the page so that it will no be cached
  224. */
  225. public function no_cache()
  226. {
  227. $this->add_header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  228. $this->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  229. }
  230. /**
  231. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  232. */
  233. public function details($aFields)
  234. {
  235. $this->add($this->GetDetails($aFields));
  236. }
  237. /**
  238. * Records the current state of the 'html' part of the page output
  239. * @return mixed The current state of the 'html' output
  240. */
  241. public function start_capture()
  242. {
  243. return strlen($this->s_content);
  244. }
  245. /**
  246. * Returns the part of the html output that occurred since the call to start_capture
  247. * and removes this part from the current html output
  248. * @param $offset mixed The value returned by start_capture
  249. * @return string The part of the html output that was added since the call to start_capture
  250. */
  251. public function end_capture($offset)
  252. {
  253. $sCaptured = substr($this->s_content, $offset);
  254. $this->s_content = substr($this->s_content, 0, $offset);
  255. return $sCaptured;
  256. }
  257. /**
  258. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  259. */
  260. public function GetDetails($aFields)
  261. {
  262. $sHtml = "<table class=\"details\">\n";
  263. $sHtml .= "<tbody>\n";
  264. foreach($aFields as $aAttrib)
  265. {
  266. $sHtml .= "<tr>\n";
  267. // By Rom, for csv import, proposed to show several values for column selection
  268. if (is_array($aAttrib['value']))
  269. {
  270. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".implode("</td><td>", $aAttrib['value'])."</td>\n";
  271. }
  272. else
  273. {
  274. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".$aAttrib['value']."</td>\n";
  275. }
  276. $sComment = (isset($aAttrib['comments'])) ? $aAttrib['comments'] : '&nbsp;';
  277. $sInfo = (isset($aAttrib['infos'])) ? $aAttrib['infos'] : '&nbsp;';
  278. $sHtml .= "<td>$sComment</td><td>$sInfo</td>\n";
  279. $sHtml .= "</tr>\n";
  280. }
  281. $sHtml .= "</tbody>\n";
  282. $sHtml .= "</table>\n";
  283. return $sHtml;
  284. }
  285. /**
  286. * Build a set of radio buttons suitable for editing a field/attribute of an object (including its validation)
  287. * @param $aAllowedValues hash Array of value => display_value
  288. * @param $value mixed Current value for the field/attribute
  289. * @param $iId mixed Unique Id for the input control in the page
  290. * @param $sFieldName string The name of the field, attr_<$sFieldName> will hold the value for the field
  291. * @param $bMandatory bool Whether or not the field is mandatory
  292. * @param $bVertical bool Disposition of the radio buttons vertical or horizontal
  293. * @param $sValidationField string HTML fragment holding the validation field (exclamation icon...)
  294. * @return string The HTML fragment corresponding to the radio buttons
  295. */
  296. public function GetRadioButtons($aAllowedValues, $value, $iId, $sFieldName, $bMandatory, $bVertical, $sValidationField)
  297. {
  298. $idx = 0;
  299. $sHTMLValue = '';
  300. foreach($aAllowedValues as $key => $display_value)
  301. {
  302. if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
  303. {
  304. // When there is only once choice, select it by default
  305. $sSelected = ' checked';
  306. }
  307. else
  308. {
  309. $sSelected = ($value == $key) ? ' checked' : '';
  310. }
  311. $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;";
  312. if ($bVertical)
  313. {
  314. if ($idx == 0)
  315. {
  316. // Validation icon at the end of the first line
  317. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  318. }
  319. $sHTMLValue .= "<br>\n";
  320. }
  321. $idx++;
  322. }
  323. $sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"$sFieldName\" value=\"$value\"/>";
  324. if (!$bVertical)
  325. {
  326. // Validation icon at the end of the line
  327. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  328. }
  329. return $sHTMLValue;
  330. }
  331. /**
  332. * Outputs (via some echo) the complete HTML page by assembling all its elements
  333. */
  334. public function output()
  335. {
  336. foreach($this->a_headers as $s_header)
  337. {
  338. header($s_header);
  339. }
  340. $s_captured_output = ob_get_contents();
  341. ob_end_clean();
  342. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  343. echo "<html>\n";
  344. echo "<head>\n";
  345. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
  346. echo "<title>".htmlentities($this->s_title, ENT_QUOTES, 'UTF-8')."</title>\n";
  347. echo $this->get_base_tag();
  348. foreach($this->a_linked_scripts as $s_script)
  349. {
  350. // Make sure that the URL to the script contains the application's version number
  351. // so that the new script do NOT get reloaded from the cache when the application is upgraded
  352. if (strpos($s_script, '?') === false)
  353. {
  354. $s_script .= "?itopversion=".ITOP_VERSION;
  355. }
  356. else
  357. {
  358. $s_script .= "&itopversion=".ITOP_VERSION;
  359. }
  360. echo "<script type=\"text/javascript\" src=\"$s_script\"></script>\n";
  361. }
  362. if (count($this->a_scripts)>0)
  363. {
  364. echo "<script type=\"text/javascript\">\n";
  365. foreach($this->a_scripts as $s_script)
  366. {
  367. echo "$s_script\n";
  368. }
  369. echo "</script>\n";
  370. }
  371. foreach($this->a_linked_stylesheets as $a_stylesheet)
  372. {
  373. if ($a_stylesheet['condition'] != "")
  374. {
  375. echo "<!--[if {$a_stylesheet['condition']}]>\n";
  376. }
  377. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$a_stylesheet['link']}\" />\n";
  378. if ($a_stylesheet['condition'] != "")
  379. {
  380. echo "<![endif]-->\n";
  381. }
  382. }
  383. if (count($this->a_styles)>0)
  384. {
  385. echo "<style>\n";
  386. foreach($this->a_styles as $s_style)
  387. {
  388. echo "$s_style\n";
  389. }
  390. echo "</style>\n";
  391. }
  392. if (class_exists('MetaModel') && MetaModel::GetConfig())
  393. {
  394. echo "<link rel=\"shortcut icon\" href=\"".utils::GetAbsoluteUrlAppRoot()."images/favicon.ico\" />\n";
  395. }
  396. echo "</head>\n";
  397. echo "<body>\n";
  398. echo self::FilterXSS($this->s_content);
  399. if (trim($s_captured_output) != "")
  400. {
  401. echo "<div class=\"raw_output\">".self::FilterXSS($s_captured_output)."</div>\n";
  402. }
  403. echo '<div id="at_the_end">'.self::FilterXSS($this->s_deferred_content).'</div>';
  404. echo "</body>\n";
  405. echo "</html>\n";
  406. }
  407. /**
  408. * Build a series of hidden field[s] from an array
  409. */
  410. // By Rom - je verrais bien une serie d'outils pour gerer des parametres que l'on retransmet entre pages d'un wizard...
  411. // ptet deriver webpage en webwizard
  412. public function add_input_hidden($sLabel, $aData)
  413. {
  414. foreach($aData as $sKey=>$sValue)
  415. {
  416. $this->add("<input type=\"hidden\" name=\"".$sLabel."[$sKey]\" value=\"$sValue\">");
  417. }
  418. }
  419. protected function get_base_tag()
  420. {
  421. $sTag = '';
  422. if (($this->a_base['href'] != '') || ($this->a_base['target'] != ''))
  423. {
  424. $sTag = '<base ';
  425. if (($this->a_base['href'] != ''))
  426. {
  427. $sTag .= "href =\"{$this->a_base['href']}\" ";
  428. }
  429. if (($this->a_base['target'] != ''))
  430. {
  431. $sTag .= "target =\"{$this->a_base['target']}\" ";
  432. }
  433. $sTag .= " />\n";
  434. }
  435. return $sTag;
  436. }
  437. /**
  438. * Get an ID (for any kind of HTML tag) that is guaranteed unique in this page
  439. * @return int The unique ID (in this page)
  440. */
  441. public function GetUniqueId()
  442. {
  443. return $this->iNextId++;
  444. }
  445. /**
  446. * Set the content-type (mime type) for the page's content
  447. * @param $sContentType string
  448. * @return void
  449. */
  450. public function SetContentType($sContentType)
  451. {
  452. $this->sContentType = $sContentType;
  453. }
  454. /**
  455. * Set the content-disposition (mime type) for the page's content
  456. * @param $sDisposition string The disposition: 'inline' or 'attachment'
  457. * @param $sFileName string The original name of the file
  458. * @return void
  459. */
  460. public function SetContentDisposition($sDisposition, $sFileName)
  461. {
  462. $this->sContentDisposition = $sDisposition;
  463. $this->sContentFileName = $sFileName;
  464. }
  465. /**
  466. * Set the transactionId of the current form
  467. * @param $iTransactionId integer
  468. * @return void
  469. */
  470. public function SetTransactionId($iTransactionId)
  471. {
  472. $this->iTransactionId = $iTransactionId;
  473. }
  474. /**
  475. * Returns the transactionId of the current form
  476. * @return integer The current transactionID
  477. */
  478. public function GetTransactionId()
  479. {
  480. return $this->iTransactionId;
  481. }
  482. public static function FilterXSS($sHTML)
  483. {
  484. return str_ireplace('<script', '&lt;script', $sHTML);
  485. }
  486. /**
  487. * What is the currently selected output format
  488. * @return string The selected output format: html, pdf...
  489. */
  490. public function GetOutputFormat()
  491. {
  492. return $this->s_OutputFormat;
  493. }
  494. /**
  495. * Check whether the desired output format is possible or not
  496. * @param string $sOutputFormat The desired output format: html, pdf...
  497. * @return bool True if the format is Ok, false otherwise
  498. */
  499. function IsOutputFormatAvailable($sOutputFormat)
  500. {
  501. $bResult = false;
  502. switch($sOutputFormat)
  503. {
  504. case 'html':
  505. $bResult = true; // Always supported
  506. break;
  507. case 'pdf':
  508. $bResult = @is_readable(APPROOT.'lib/MPDF/mpdf.php');
  509. break;
  510. }
  511. return $bResult;
  512. }
  513. /**
  514. * Retrieves the value of a named output option for the given format
  515. * @param string $sFormat The format: html or pdf
  516. * @param string $sOptionName The name of the option
  517. * @return mixed false if the option was never set or the options's value
  518. */
  519. public function GetOutputOption($sFormat, $sOptionName)
  520. {
  521. if (isset($this->a_OutputOptions[$sFormat][$sOptionName]))
  522. {
  523. return $this->a_OutputOptions[$sFormat][$sOptionName];
  524. }
  525. return false;
  526. }
  527. /**
  528. * Sets a named output option for the given format
  529. * @param string $sFormat The format for which to set the option: html or pdf
  530. * @param string $sOptionName the name of the option
  531. * @param mixed $sValue The value of the option
  532. */
  533. public function SetOutputOption($sFormat, $sOptionName, $sValue)
  534. {
  535. if (!isset($this->a_OutputOptions[$sFormat]))
  536. {
  537. $this->a_OutputOptions[$sFormat] = array($sOptionName => $sValue);
  538. }
  539. else
  540. {
  541. $this->a_OutputOptions[$sFormat][$sOptionName] = $sValue;
  542. }
  543. }
  544. }
  545. ?>