webpage.class.inc.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 WebPage("Title of my page");
  10. * $oPage->p("Hello World !");
  11. * $oPage->output();
  12. */
  13. class WebPage
  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. $aMatches = array();
  119. $sClass = isset($aAttribs['class']) ? 'class="'.$aAttribs['class'].'"' : '';
  120. if (preg_match('/^key_(.+)$/', $sName, $aMatches) > 0)
  121. {
  122. $sAlias = $aMatches[1];
  123. $sClass = $aParams['class'][$sAlias];
  124. $sUIPage = cmdbAbstractObject::ComputeUIPage($sClass);
  125. $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";
  126. }
  127. else if ($sName == 'key')
  128. {
  129. $sUIPage = cmdbAbstractObject::ComputeUIPage($aParams['class']);
  130. $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";
  131. }
  132. else
  133. {
  134. $sValue = ($aRow[$sName] === '') ? '&nbsp;' : $aRow[$sName];
  135. $sHtml .= "<td $sClass>$sValue</td>\n";
  136. }
  137. }
  138. $sHtml .= "</tr>\n";
  139. }
  140. $sHtml .= "</tbody>\n";
  141. $sHtml .= "</table>\n";
  142. return $sHtml;
  143. }
  144. /**
  145. * Add some Javascript to the header of the page
  146. */
  147. public function add_script($s_script)
  148. {
  149. $this->a_scripts[] = $s_script;
  150. }
  151. /**
  152. * Add some Javascript to the header of the page
  153. */
  154. public function add_ready_script($s_script)
  155. {
  156. // Do nothing silently... this is not supported by this type of page...
  157. }
  158. /**
  159. * Add some CSS definitions to the header of the page
  160. */
  161. public function add_style($s_style)
  162. {
  163. $this->a_styles[] = $s_style;
  164. }
  165. /**
  166. * Add a script (as an include, i.e. link) to the header of the page
  167. */
  168. public function add_linked_script($s_linked_script)
  169. {
  170. $this->a_linked_scripts[] = $s_linked_script;
  171. }
  172. /**
  173. * Add a CSS stylesheet (as an include, i.e. link) to the header of the page
  174. */
  175. public function add_linked_stylesheet($s_linked_stylesheet, $s_condition = "")
  176. {
  177. $this->a_linked_stylesheets[] = array( 'link' => $s_linked_stylesheet, 'condition' => $s_condition);
  178. }
  179. /**
  180. * Add some custom header to the page
  181. */
  182. public function add_header($s_header)
  183. {
  184. $this->a_headers[] = $s_header;
  185. }
  186. /**
  187. * Add needed headers to the page so that it will no be cached
  188. */
  189. public function no_cache()
  190. {
  191. $this->add_header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  192. $this->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  193. }
  194. /**
  195. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  196. */
  197. public function details($aFields)
  198. {
  199. $this->add($this->GetDetails($aFields));
  200. }
  201. /**
  202. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  203. */
  204. public function GetDetails($aFields)
  205. {
  206. $sHtml = "<table>\n";
  207. $sHtml .= "<tbody>\n";
  208. foreach($aFields as $aAttrib)
  209. {
  210. $sHtml .= "<tr>\n";
  211. // By Rom, for csv import, proposed to show several values for column selection
  212. if (is_array($aAttrib['value']))
  213. {
  214. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".implode("</td><td>", $aAttrib['value'])."</td>\n";
  215. }
  216. else
  217. {
  218. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".$aAttrib['value']."</td>\n";
  219. }
  220. $sHtml .= "</tr>\n";
  221. }
  222. $sHtml .= "</tbody>\n";
  223. $sHtml .= "</table>\n";
  224. return $sHtml;
  225. }
  226. /**
  227. * Outputs (via some echo) the complete HTML page by assembling all its elements
  228. */
  229. public function output()
  230. {
  231. foreach($this->a_headers as $s_header)
  232. {
  233. header($s_header);
  234. }
  235. $s_captured_output = ob_get_contents();
  236. ob_end_clean();
  237. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  238. echo "<html>\n";
  239. echo "<head>\n";
  240. echo "<title>{$this->s_title}</title>\n";
  241. echo $this->get_base_tag();
  242. foreach($this->a_linked_scripts as $s_script)
  243. {
  244. echo "<script type=\"text/javascript\" src=\"$s_script\"></script>\n";
  245. }
  246. if (count($this->a_scripts)>0)
  247. {
  248. echo "<script type=\"text/javascript\">\n";
  249. foreach($this->a_scripts as $s_script)
  250. {
  251. echo "$s_script\n";
  252. }
  253. echo "</script>\n";
  254. }
  255. foreach($this->a_linked_stylesheets as $a_stylesheet)
  256. {
  257. if ($a_stylesheet['condition'] != "")
  258. {
  259. echo "<!--[if {$a_stylesheet['condition']}]>\n";
  260. }
  261. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$a_stylesheet['link']}\" />\n";
  262. if ($a_stylesheet['condition'] != "")
  263. {
  264. echo "<![endif]-->\n";
  265. }
  266. }
  267. if (count($this->a_styles)>0)
  268. {
  269. echo "<style>\n";
  270. foreach($this->a_styles as $s_style)
  271. {
  272. echo "$s_style\n";
  273. }
  274. echo "</style>\n";
  275. }
  276. echo "</head>\n";
  277. echo "<body>\n";
  278. echo $this->s_content;
  279. if (trim($s_captured_output) != "")
  280. {
  281. echo "<div class=\"raw_output\">$s_captured_output</div>\n";
  282. }
  283. echo $this->s_deferred_content;
  284. echo "</body>\n";
  285. echo "</html>\n";
  286. }
  287. /**
  288. * Build a series of hidden field[s] from an array
  289. */
  290. // By Rom - je verrais bien une serie d'outils pour gerer des parametres que l'on retransmet entre pages d'un wizard...
  291. // ptet deriver webpage en webwizard
  292. public function add_input_hidden($sLabel, $aData)
  293. {
  294. foreach($aData as $sKey=>$sValue)
  295. {
  296. $this->add("<input type=\"hidden\" name=\"".$sLabel."[$sKey]\" value=\"$sValue\">");
  297. }
  298. }
  299. protected function get_base_tag()
  300. {
  301. $sTag = '';
  302. if (($this->a_base['href'] != '') || ($this->a_base['target'] != ''))
  303. {
  304. $sTag = '<base ';
  305. if (($this->a_base['href'] != ''))
  306. {
  307. $sTag .= "href =\"{$this->a_base['href']}\" ";
  308. }
  309. if (($this->a_base['target'] != ''))
  310. {
  311. $sTag .= "target =\"{$this->a_base['target']}\" ";
  312. }
  313. $sTag .= " />\n";
  314. }
  315. return $sTag;
  316. }
  317. }
  318. ?>