webpage.class.inc.php 9.3 KB

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