webpage.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. public function __construct($s_title)
  47. {
  48. $this->s_title = $s_title;
  49. $this->s_content = "";
  50. $this->s_deferred_content = '';
  51. $this->a_scripts = array();
  52. $this->a_styles = array();
  53. $this->a_linked_scripts = array();
  54. $this->a_linked_stylesheets = array();
  55. $this->a_headers = array();
  56. $this->a_base = array( 'href' => '', 'target' => '');
  57. ob_start(); // Start capturing the output
  58. }
  59. /**
  60. * Change the title of the page after its creation
  61. */
  62. public function set_title($s_title)
  63. {
  64. $this->s_title = $s_title;
  65. }
  66. /**
  67. * Specify a default URL and a default target for all links on a page
  68. */
  69. public function set_base($s_href = '', $s_target = '')
  70. {
  71. $this->a_base['href'] = $s_href;
  72. $this->a_base['target'] = $s_target;
  73. }
  74. /**
  75. * Add any text or HTML fragment to the body of the page
  76. */
  77. public function add($s_html)
  78. {
  79. $this->s_content .= $s_html;
  80. }
  81. /**
  82. * Add any text or HTML fragment at the end of the body of the page
  83. * This is useful to add hidden content, DIVs or FORMs that should not
  84. * be embedded into each other.
  85. */
  86. public function add_at_the_end($s_html)
  87. {
  88. $this->s_deferred_content .= $s_html;
  89. }
  90. /**
  91. * Add a paragraph to the body of the page
  92. */
  93. public function p($s_html)
  94. {
  95. $this->add($this->GetP($s_html));
  96. }
  97. /**
  98. * Add a paragraph to the body of the page
  99. */
  100. public function GetP($s_html)
  101. {
  102. return "<p>$s_html</p>\n";
  103. }
  104. public function table($aConfig, $aData, $aParams = array())
  105. {
  106. $this->add($this->GetTable($aConfig, $aData, $aParams));
  107. }
  108. public function GetTable($aConfig, $aData, $aParams = array())
  109. {
  110. $oAppContext = new ApplicationContext();
  111. static $iNbTables = 0;
  112. $iNbTables++;
  113. $sHtml = "";
  114. $sHtml .= "<table class=\"listResults\">\n";
  115. $sHtml .= "<thead>\n";
  116. $sHtml .= "<tr>\n";
  117. foreach($aConfig as $sName=>$aDef)
  118. {
  119. $sHtml .= "<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n";
  120. }
  121. $sHtml .= "</tr>\n";
  122. $sHtml .= "</thead>\n";
  123. $sHtml .= "<tbody>\n";
  124. foreach($aData as $aRow)
  125. {
  126. if (false) //(isset($aParams['preview']) && $aParams['preview'])
  127. {
  128. $sHtml .= "<tr id=\"Row_".$iNbTables."_".$aRow['key']."\" onClick=\"DisplayPreview(".$iNbTables.",".$aRow['key'].",'".$aParams['class']."')\">\n";
  129. }
  130. else if (isset($aRow['key']))
  131. {
  132. $sHtml .= "<tr onDblClick=\"DisplayDetails(".$aRow['key'].",'".$aParams['class']."')\">\n";
  133. }
  134. else
  135. {
  136. $sHtml .= "<tr>\n";
  137. }
  138. foreach($aConfig as $sName=>$aAttribs)
  139. {
  140. $aMatches = array();
  141. $sClass = isset($aAttribs['class']) ? 'class="'.$aAttribs['class'].'"' : '';
  142. if (preg_match('/^key_(.+)$/', $sName, $aMatches) > 0)
  143. {
  144. $sAlias = $aMatches[1];
  145. $sClass = $aParams['class'][$sAlias];
  146. $sUIPage = cmdbAbstractObject::ComputeUIPage($sClass);
  147. $sHtml .= "<td><a class=\"no-arrow\" href=\"$sUIPage?operation=details&id=".$aRow[$sName]."&class=".$sClass."&".$oAppContext->GetForLink()."\"><img src=\"../images/zoom.gif\" title=\"".Dict::S('UI:Details+')."\" border=\"0\"></a></td>\n";
  148. }
  149. else if ($sName == 'key')
  150. {
  151. $sUIPage = cmdbAbstractObject::ComputeUIPage($aParams['class']);
  152. $sHtml .= "<td><a class=\"no-arrow\" href=\"$sUIPage?operation=details&id=".$aRow['key']."&class=".$aParams['class']."&".$oAppContext->GetForLink()."\"><img src=\"../images/zoom.gif\" title=\"".Dict::S('UI:Details+')."\" border=\"0\"></a></td>\n";
  153. }
  154. else
  155. {
  156. $sValue = ($aRow[$sName] === '') ? '&nbsp;' : $aRow[$sName];
  157. $sHtml .= "<td $sClass>$sValue</td>\n";
  158. }
  159. }
  160. $sHtml .= "</tr>\n";
  161. }
  162. $sHtml .= "</tbody>\n";
  163. $sHtml .= "</table>\n";
  164. return $sHtml;
  165. }
  166. /**
  167. * Add some Javascript to the header of the page
  168. */
  169. public function add_script($s_script)
  170. {
  171. $this->a_scripts[] = $s_script;
  172. }
  173. /**
  174. * Add some Javascript to the header of the page
  175. */
  176. public function add_ready_script($s_script)
  177. {
  178. // Do nothing silently... this is not supported by this type of page...
  179. }
  180. /**
  181. * Add some CSS definitions to the header of the page
  182. */
  183. public function add_style($s_style)
  184. {
  185. $this->a_styles[] = $s_style;
  186. }
  187. /**
  188. * Add a script (as an include, i.e. link) to the header of the page
  189. */
  190. public function add_linked_script($s_linked_script)
  191. {
  192. $this->a_linked_scripts[] = $s_linked_script;
  193. }
  194. /**
  195. * Add a CSS stylesheet (as an include, i.e. link) to the header of the page
  196. */
  197. public function add_linked_stylesheet($s_linked_stylesheet, $s_condition = "")
  198. {
  199. $this->a_linked_stylesheets[] = array( 'link' => $s_linked_stylesheet, 'condition' => $s_condition);
  200. }
  201. /**
  202. * Add some custom header to the page
  203. */
  204. public function add_header($s_header)
  205. {
  206. $this->a_headers[] = $s_header;
  207. }
  208. /**
  209. * Add needed headers to the page so that it will no be cached
  210. */
  211. public function no_cache()
  212. {
  213. $this->add_header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  214. $this->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  215. }
  216. /**
  217. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  218. */
  219. public function details($aFields)
  220. {
  221. $this->add($this->GetDetails($aFields));
  222. }
  223. /**
  224. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  225. */
  226. public function GetDetails($aFields)
  227. {
  228. $sHtml = "<table>\n";
  229. $sHtml .= "<tbody>\n";
  230. foreach($aFields as $aAttrib)
  231. {
  232. $sHtml .= "<tr>\n";
  233. // By Rom, for csv import, proposed to show several values for column selection
  234. if (is_array($aAttrib['value']))
  235. {
  236. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".implode("</td><td>", $aAttrib['value'])."</td>\n";
  237. }
  238. else
  239. {
  240. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".$aAttrib['value']."</td>\n";
  241. }
  242. $sHtml .= "</tr>\n";
  243. }
  244. $sHtml .= "</tbody>\n";
  245. $sHtml .= "</table>\n";
  246. return $sHtml;
  247. }
  248. /**
  249. * Outputs (via some echo) the complete HTML page by assembling all its elements
  250. */
  251. public function output()
  252. {
  253. foreach($this->a_headers as $s_header)
  254. {
  255. header($s_header);
  256. }
  257. $s_captured_output = ob_get_contents();
  258. ob_end_clean();
  259. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  260. echo "<html>\n";
  261. echo "<head>\n";
  262. echo "<title>{$this->s_title}</title>\n";
  263. echo $this->get_base_tag();
  264. foreach($this->a_linked_scripts as $s_script)
  265. {
  266. echo "<script type=\"text/javascript\" src=\"$s_script\"></script>\n";
  267. }
  268. if (count($this->a_scripts)>0)
  269. {
  270. echo "<script type=\"text/javascript\">\n";
  271. foreach($this->a_scripts as $s_script)
  272. {
  273. echo "$s_script\n";
  274. }
  275. echo "</script>\n";
  276. }
  277. foreach($this->a_linked_stylesheets as $a_stylesheet)
  278. {
  279. if ($a_stylesheet['condition'] != "")
  280. {
  281. echo "<!--[if {$a_stylesheet['condition']}]>\n";
  282. }
  283. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$a_stylesheet['link']}\" />\n";
  284. if ($a_stylesheet['condition'] != "")
  285. {
  286. echo "<![endif]-->\n";
  287. }
  288. }
  289. if (count($this->a_styles)>0)
  290. {
  291. echo "<style>\n";
  292. foreach($this->a_styles as $s_style)
  293. {
  294. echo "$s_style\n";
  295. }
  296. echo "</style>\n";
  297. }
  298. echo "</head>\n";
  299. echo "<body>\n";
  300. echo $this->s_content;
  301. if (trim($s_captured_output) != "")
  302. {
  303. echo "<div class=\"raw_output\">$s_captured_output</div>\n";
  304. }
  305. echo $this->s_deferred_content;
  306. echo "</body>\n";
  307. echo "</html>\n";
  308. }
  309. /**
  310. * Build a series of hidden field[s] from an array
  311. */
  312. // By Rom - je verrais bien une serie d'outils pour gerer des parametres que l'on retransmet entre pages d'un wizard...
  313. // ptet deriver webpage en webwizard
  314. public function add_input_hidden($sLabel, $aData)
  315. {
  316. foreach($aData as $sKey=>$sValue)
  317. {
  318. $this->add("<input type=\"hidden\" name=\"".$sLabel."[$sKey]\" value=\"$sValue\">");
  319. }
  320. }
  321. protected function get_base_tag()
  322. {
  323. $sTag = '';
  324. if (($this->a_base['href'] != '') || ($this->a_base['target'] != ''))
  325. {
  326. $sTag = '<base ';
  327. if (($this->a_base['href'] != ''))
  328. {
  329. $sTag .= "href =\"{$this->a_base['href']}\" ";
  330. }
  331. if (($this->a_base['target'] != ''))
  332. {
  333. $sTag .= "target =\"{$this->a_base['target']}\" ";
  334. }
  335. $sTag .= " />\n";
  336. }
  337. return $sTag;
  338. }
  339. }
  340. ?>