RTG.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * HSDN Recursive Table Generator
  4. *
  5. * @version 0.07.0b
  6. * @author HSDN Team
  7. * @copyright Copyright (c) 2015-2017, Information Networks Ltd.
  8. * @link http://www.hsdn.org
  9. */
  10. class RTG
  11. {
  12. /*
  13. * Array of the CSS styles
  14. */
  15. private static $css = array
  16. (
  17. 'table.rtg_outer { border: 0; border-collapse: separate; border-spacing: 0; border: 2px solid #000; padding: 5px; background: #fff; }',
  18. 'table.rtg_inner { border: 0; border-collapse: separate; border-spacing: 0; width: 100%; }',
  19. 'th.rtg_outer { border-bottom: 1px solid #000; padding: 0 3px 2px 3px; }',
  20. 'td.rtg_inner { border-bottom: 1px solid #000; padding: 3px 4px 1px 4px; }',
  21. 'td.rtg_outer { border-bottom: 1px solid #000; border-left: 1px solid #000; padding: 3px 4px 1px 4px; }',
  22. 'td.rtg_outer:last-child { border-right: 1px solid #000; }',
  23. 'td.rtg_inner:last-child { border-left: 1px solid #000; }',
  24. );
  25. /*
  26. * Table settings array
  27. */
  28. private static $settings = array
  29. (
  30. 'table' => '100%',
  31. 'color' => '#ccffcc',
  32. 'color_raw' => '#ccccff',
  33. 'color_null' => '#ffcccc',
  34. 'color_bool' => '#ccc',
  35. 'raw_params' => array
  36. (
  37. 'raw',
  38. 'header',
  39. 'banner',
  40. ),
  41. );
  42. // --------------------------------------------------------------------
  43. // Public methods
  44. // --------------------------------------------------------------------
  45. /**
  46. * Returns or print the CSS style
  47. *
  48. * @access public
  49. * @param bool
  50. * @return string|void
  51. */
  52. public static function css($print = FALSE)
  53. {
  54. $css = implode(' ', self::$css);
  55. if (!$print)
  56. {
  57. return $css;
  58. }
  59. echo $css;
  60. }
  61. /**
  62. * Returns or print the table
  63. *
  64. * @access public
  65. * @param array
  66. * @param bool
  67. * @param array
  68. * @return string|void
  69. */
  70. public static function table($content, $print = FALSE, $settings = array())
  71. {
  72. self::$settings = array_merge($settings, self::$settings);
  73. $table = self::build_table($content);
  74. if (!$print)
  75. {
  76. return $table;
  77. }
  78. echo $table;
  79. }
  80. // --------------------------------------------------------------------
  81. // Private methods
  82. // --------------------------------------------------------------------
  83. /**
  84. * Builds table
  85. *
  86. * @access private
  87. * @param array
  88. * @param int
  89. * @param string
  90. * @param string
  91. * @param bool
  92. * @return string
  93. */
  94. private static function build_table($content, $width = NULL, $td_width = '50%', $class = 'rtg_outer', $build_th = TRUE)
  95. {
  96. if (!$width)
  97. {
  98. $width = self::$settings['width'];
  99. }
  100. $table = '<table class="'.$class.'" style="width:'.$width.'">';
  101. if ($build_th)
  102. {
  103. $table .= '<tr><th class="'.$class.'">Parameter</th><th colspan="2" class="'.$class.'">Value</th></tr>';
  104. }
  105. foreach ($content as $param => $value)
  106. {
  107. $table .= self::build_tr($param, $value, $class, $td_width);
  108. }
  109. if ($build_th)
  110. {
  111. $table .=
  112. '<tr><th colspan="2" style="padding-top:4px;font-size:9px;font-weight:normal;text-align:right">'.
  113. '<i>HSDN Recursive Table Generator</i>'.
  114. '</th></tr>';
  115. }
  116. $table .= '</table>';
  117. return $table;
  118. }
  119. /**
  120. * Builds table tr
  121. *
  122. * @access private
  123. * @param string
  124. * @param mixed
  125. * @param string
  126. * @param string
  127. * @return string
  128. */
  129. private static function build_tr($param, $value, $class, $width)
  130. {
  131. $bg_color = self::$settings['color'];
  132. list($color, $param) = self::build_param($param);
  133. if (!is_null($color))
  134. {
  135. $bg_color = $color;
  136. }
  137. list($color, $value) = self::build_value($value);
  138. if (!is_null($color))
  139. {
  140. $bg_color = $color;
  141. }
  142. $tr = '<tr>';
  143. $tr .= '<td style="background:#fff;width:'.$width.'" class="'.$class.'"><b>'.$param.'</b></td>';
  144. if (is_array($value))
  145. {
  146. $tr .= '<td style="background:'.$bg_color.';padding:0;border-bottom:0" class="'.$class.'">';
  147. $tr .= self::build_table($value, '100%', '1%', 'rtg_inner', FALSE);
  148. $tr .= '</td>';
  149. }
  150. else
  151. {
  152. $tr .= '<td style="background:'.$bg_color.'" class="'.$class.'">';
  153. $tr .= $value;
  154. $tr .= '</td>';
  155. }
  156. $tr .= '</tr>';
  157. return $tr;
  158. }
  159. /**
  160. * Build param
  161. *
  162. * @access private
  163. * @param string
  164. * @return array
  165. */
  166. private static function build_param($param)
  167. {
  168. $color = NULL;
  169. foreach (self::$settings['raw_params'] as $raw_param)
  170. {
  171. if ($param === $raw_param)
  172. {
  173. $color = self::$settings['color_raw'];
  174. break;
  175. }
  176. }
  177. if (is_numeric($param))
  178. {
  179. $param = 'Entry&nbsp;'.($param + 1);
  180. }
  181. else
  182. {
  183. $param = str_replace(' ', '&nbsp;', ucwords(str_replace('_', ' ', htmlspecialchars($param))));
  184. }
  185. return array($color, $param);
  186. }
  187. /**
  188. * Build value
  189. *
  190. * @access private
  191. * @param string
  192. * @return array
  193. */
  194. private static function build_value($value)
  195. {
  196. $color = NULL;
  197. if (!is_array($value))
  198. {
  199. if (is_null($value))
  200. {
  201. $color = self::$settings['color_null'];
  202. $value = '&lt;no data&gt;';
  203. }
  204. elseif (is_bool($value))
  205. {
  206. $color = self::$settings['color_bool'];
  207. if ($value === TRUE)
  208. {
  209. $value = '&lt;data boolean: <b>TRUE</b>&gt;';
  210. }
  211. else
  212. {
  213. $value = '&lt;data boolean: <b>FALSE</b>&gt;';
  214. }
  215. }
  216. else
  217. {
  218. $value = nl2br(htmlspecialchars($value));
  219. }
  220. }
  221. return array($color, $value);
  222. }
  223. }
  224. /* End of File */