webpage.class.inc.php 8.9 KB

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