PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Table.php

https://github.com/mkhairul/Presta
PHP | 440 lines | 226 code | 70 blank | 144 comment | 35 complexity | d211830b8f57c776cedf018576bb0d92 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.3.1
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * HTML Table Generating Class
  18. *
  19. * Lets you create tables manually or from database result objects, or arrays.
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @category HTML Tables
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/libraries/uri.html
  26. */
  27. class CI_Table {
  28. var $rows = array();
  29. var $heading = array();
  30. var $auto_heading = TRUE;
  31. var $caption = NULL;
  32. var $template = NULL;
  33. var $newline = "\n";
  34. var $empty_cells = "";
  35. function CI_Table()
  36. {
  37. log_message('debug', "Table Class Initialized");
  38. }
  39. // --------------------------------------------------------------------
  40. /**
  41. * Set the template
  42. *
  43. * @access public
  44. * @param array
  45. * @return void
  46. */
  47. function set_template($template)
  48. {
  49. if ( ! is_array($template))
  50. {
  51. return FALSE;
  52. }
  53. $this->template = $template;
  54. }
  55. // --------------------------------------------------------------------
  56. /**
  57. * Set the table heading
  58. *
  59. * Can be passed as an array or discreet params
  60. *
  61. * @access public
  62. * @param mixed
  63. * @return void
  64. */
  65. function set_heading()
  66. {
  67. $args = func_get_args();
  68. $this->heading = (is_array($args[0])) ? $args[0] : $args;
  69. }
  70. // --------------------------------------------------------------------
  71. /**
  72. * Set columns. Takes a one-dimensional array as input and creates
  73. * a multi-dimensional array with a depth equal to the number of
  74. * columns. This allows a single array with many elements to be
  75. * displayed in a table that has a fixed column count.
  76. *
  77. * @access public
  78. * @param array
  79. * @param int
  80. * @return void
  81. */
  82. function make_columns($array = array(), $col_limit = 0)
  83. {
  84. if ( ! is_array($array) OR count($array) == 0)
  85. {
  86. return FALSE;
  87. }
  88. // Turn off the auto-heading feature since it's doubtful we
  89. // will want headings from a one-dimensional array
  90. $this->auto_heading = FALSE;
  91. if ($col_limit == 0)
  92. {
  93. return $array;
  94. }
  95. $new = array();
  96. while(count($array) > 0)
  97. {
  98. $temp = array_splice($array, 0, $col_limit);
  99. if (count($temp) < $col_limit)
  100. {
  101. for ($i = count($temp); $i < $col_limit; $i++)
  102. {
  103. $temp[] = '&nbsp;';
  104. }
  105. }
  106. $new[] = $temp;
  107. }
  108. return $new;
  109. }
  110. // --------------------------------------------------------------------
  111. /**
  112. * Set "empty" cells
  113. *
  114. * Can be passed as an array or discreet params
  115. *
  116. * @access public
  117. * @param mixed
  118. * @return void
  119. */
  120. function set_empty($value)
  121. {
  122. $this->empty_cells = $value;
  123. }
  124. // --------------------------------------------------------------------
  125. /**
  126. * Add a table row
  127. *
  128. * Can be passed as an array or discreet params
  129. *
  130. * @access public
  131. * @param mixed
  132. * @return void
  133. */
  134. function add_row()
  135. {
  136. $args = func_get_args();
  137. $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
  138. }
  139. // --------------------------------------------------------------------
  140. /**
  141. * Add a table caption
  142. *
  143. * @access public
  144. * @param string
  145. * @return void
  146. */
  147. function set_caption($caption)
  148. {
  149. $this->caption = $caption;
  150. }
  151. // --------------------------------------------------------------------
  152. /**
  153. * Generate the table
  154. *
  155. * @access public
  156. * @param mixed
  157. * @return string
  158. */
  159. function generate($table_data = NULL)
  160. {
  161. // The table data can optionally be passed to this function
  162. // either as a database result object or an array
  163. if ( ! is_null($table_data))
  164. {
  165. if (is_object($table_data))
  166. {
  167. $this->_set_from_object($table_data);
  168. }
  169. elseif (is_array($table_data))
  170. {
  171. $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
  172. $this->_set_from_array($table_data, $set_heading);
  173. }
  174. }
  175. // Is there anything to display? No? Smite them!
  176. if (count($this->heading) == 0 AND count($this->rows) == 0)
  177. {
  178. return 'Undefined table data';
  179. }
  180. // Compile and validate the template date
  181. $this->_compile_template();
  182. // Build the table!
  183. $out = $this->template['table_open'];
  184. $out .= $this->newline;
  185. // Add any caption here
  186. if ($this->caption)
  187. {
  188. $out .= $this->newline;
  189. $out .= '<caption>' . $this->caption . '</caption>';
  190. $out .= $this->newline;
  191. }
  192. // Is there a table heading to display?
  193. if (count($this->heading) > 0)
  194. {
  195. $out .= $this->template['heading_row_start'];
  196. $out .= $this->newline;
  197. foreach($this->heading as $heading)
  198. {
  199. $out .= $this->template['heading_cell_start'];
  200. $out .= $heading;
  201. $out .= $this->template['heading_cell_end'];
  202. }
  203. $out .= $this->template['heading_row_end'];
  204. $out .= $this->newline;
  205. }
  206. // Build the table rows
  207. if (count($this->rows) > 0)
  208. {
  209. $i = 1;
  210. foreach($this->rows as $row)
  211. {
  212. if ( ! is_array($row))
  213. {
  214. break;
  215. }
  216. // We use modulus to alternate the row colors
  217. $name = (fmod($i++, 2)) ? '' : 'alt_';
  218. $out .= $this->template['row_'.$name.'start'];
  219. $out .= $this->newline;
  220. foreach($row as $cell)
  221. {
  222. $out .= $this->template['cell_'.$name.'start'];
  223. if ($cell === "")
  224. {
  225. $out .= $this->empty_cells;
  226. }
  227. else
  228. {
  229. $out .= $cell;
  230. }
  231. $out .= $this->template['cell_'.$name.'end'];
  232. }
  233. $out .= $this->template['row_'.$name.'end'];
  234. $out .= $this->newline;
  235. }
  236. }
  237. $out .= $this->template['table_close'];
  238. return $out;
  239. }
  240. // --------------------------------------------------------------------
  241. /**
  242. * Clears the table arrays. Useful if multiple tables are being generated
  243. *
  244. * @access public
  245. * @return void
  246. */
  247. function clear()
  248. {
  249. $this->rows = array();
  250. $this->heading = array();
  251. $this->auto_heading = TRUE;
  252. }
  253. // --------------------------------------------------------------------
  254. /**
  255. * Set table data from a database result object
  256. *
  257. * @access public
  258. * @param object
  259. * @return void
  260. */
  261. function _set_from_object($query)
  262. {
  263. if ( ! is_object($query))
  264. {
  265. return FALSE;
  266. }
  267. // First generate the headings from the table column names
  268. if (count($this->heading) == 0)
  269. {
  270. if ( ! method_exists($query, 'list_fields'))
  271. {
  272. return FALSE;
  273. }
  274. $this->heading = $query->list_fields();
  275. }
  276. // Next blast through the result array and build out the rows
  277. if ($query->num_rows() > 0)
  278. {
  279. foreach ($query->result_array() as $row)
  280. {
  281. $this->rows[] = $row;
  282. }
  283. }
  284. }
  285. // --------------------------------------------------------------------
  286. /**
  287. * Set table data from an array
  288. *
  289. * @access public
  290. * @param array
  291. * @return void
  292. */
  293. function _set_from_array($data, $set_heading = TRUE)
  294. {
  295. if ( ! is_array($data) OR count($data) == 0)
  296. {
  297. return FALSE;
  298. }
  299. $i = 0;
  300. foreach ($data as $row)
  301. {
  302. if ( ! is_array($row))
  303. {
  304. $this->rows[] = $data;
  305. break;
  306. }
  307. // If a heading hasn't already been set we'll use the first row of the array as the heading
  308. if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
  309. {
  310. $this->heading = $row;
  311. }
  312. else
  313. {
  314. $this->rows[] = $row;
  315. }
  316. $i++;
  317. }
  318. }
  319. // --------------------------------------------------------------------
  320. /**
  321. * Compile Template
  322. *
  323. * @access private
  324. * @return void
  325. */
  326. function _compile_template()
  327. {
  328. if ($this->template == NULL)
  329. {
  330. $this->template = $this->_default_template();
  331. return;
  332. }
  333. $this->temp = $this->_default_template();
  334. foreach (array('table_open','heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val)
  335. {
  336. if ( ! isset($this->template[$val]))
  337. {
  338. $this->template[$val] = $this->temp[$val];
  339. }
  340. }
  341. }
  342. // --------------------------------------------------------------------
  343. /**
  344. * Default Template
  345. *
  346. * @access private
  347. * @return void
  348. */
  349. function _default_template()
  350. {
  351. return array (
  352. 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
  353. 'heading_row_start' => '<tr>',
  354. 'heading_row_end' => '</tr>',
  355. 'heading_cell_start' => '<th>',
  356. 'heading_cell_end' => '</th>',
  357. 'row_start' => '<tr>',
  358. 'row_end' => '</tr>',
  359. 'cell_start' => '<td>',
  360. 'cell_end' => '</td>',
  361. 'row_alt_start' => '<tr>',
  362. 'row_alt_end' => '</tr>',
  363. 'cell_alt_start' => '<td>',
  364. 'cell_alt_end' => '</td>',
  365. 'table_close' => '</table>'
  366. );
  367. }
  368. }
  369. /* End of file Table.php */
  370. /* Location: ./system/libraries/Table.php */