webpage.class.inc.php 16 KB

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