webpage.class.inc.php 8.1 KB

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