PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/system/libraries/Table.php

https://github.com/holsinger/openfloor
PHP | 439 lines | 228 code | 69 blank | 142 comment | 36 complexity | b6becbff4177c6368972af6d5efbd32d 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 Rick Ellis
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.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 Rick Ellis
  25. * @link http://www.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_slice($array, 0, $col_limit);
  99. $array = array_diff($array, $temp);
  100. if (count($temp) < $col_limit)
  101. {
  102. for ($i = count($temp); $i < $col_limit; $i++)
  103. {
  104. $temp[] = '&nbsp;';
  105. }
  106. }
  107. $new[] = $temp;
  108. }
  109. return $new;
  110. }
  111. // --------------------------------------------------------------------
  112. /**
  113. * Set "empty" cells
  114. *
  115. * Can be passed as an array or discreet params
  116. *
  117. * @access public
  118. * @param mixed
  119. * @return void
  120. */
  121. function set_empty($value)
  122. {
  123. $this->empty_cells = $value;
  124. }
  125. // --------------------------------------------------------------------
  126. /**
  127. * Add a table row
  128. *
  129. * Can be passed as an array or discreet params
  130. *
  131. * @access public
  132. * @param mixed
  133. * @return void
  134. */
  135. function add_row()
  136. {
  137. $args = func_get_args();
  138. $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
  139. }
  140. // --------------------------------------------------------------------
  141. /**
  142. * Add a table caption
  143. *
  144. * @access public
  145. * @param string
  146. * @return void
  147. */
  148. function set_caption($caption)
  149. {
  150. $this->caption = $caption;
  151. }
  152. // --------------------------------------------------------------------
  153. /**
  154. * Generate the table
  155. *
  156. * @access public
  157. * @param mixed
  158. * @return string
  159. */
  160. function generate($table_data = NULL)
  161. {
  162. // The table data can optionally be passed to this function
  163. // either as a database result object or an array
  164. if ( ! is_null($table_data))
  165. {
  166. if (is_object($table_data))
  167. {
  168. $this->_set_from_object($table_data);
  169. }
  170. elseif (is_array($table_data))
  171. {
  172. $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
  173. $this->_set_from_array($table_data, $set_heading);
  174. }
  175. }
  176. // Is there anything to display? No? Smite them!
  177. if (count($this->heading) == 0 AND count($this->rows) == 0)
  178. {
  179. return 'Undefined table data';
  180. }
  181. // Compile and validate the template date
  182. $this->_compile_template();
  183. // Build the table!
  184. $out = $this->template['table_open'];
  185. $out .= $this->newline;
  186. // Add any caption here
  187. if ($this->caption)
  188. {
  189. $out .= $this->newline;
  190. $out .= '<caption>' . $this->caption . '</caption>';
  191. $out .= $this->newline;
  192. }
  193. // Is there a table heading to display?
  194. if (count($this->heading) > 0)
  195. {
  196. $out .= $this->template['heading_row_start'];
  197. $out .= $this->newline;
  198. foreach($this->heading as $heading)
  199. {
  200. $out .= $this->template['heading_cell_start'];
  201. $out .= $heading;
  202. $out .= $this->template['heading_cell_end'];
  203. }
  204. $out .= $this->template['heading_row_end'];
  205. $out .= $this->newline;
  206. }
  207. // Build the table rows
  208. if (count($this->rows) > 0)
  209. {
  210. $i = 1;
  211. foreach($this->rows as $row)
  212. {
  213. if ( ! is_array($row))
  214. {
  215. break;
  216. }
  217. // We use modulus to alternate the row colors
  218. $name = (fmod($i++, 2)) ? '' : 'alt_';
  219. $out .= $this->template['row_'.$name.'start'];
  220. $out .= $this->newline;
  221. foreach($row as $cell)
  222. {
  223. $out .= $this->template['cell_'.$name.'start'];
  224. if ($cell == "")
  225. {
  226. $out .= $this->empty_cells;
  227. }
  228. else
  229. {
  230. $out .= $cell;
  231. }
  232. $out .= $this->template['cell_'.$name.'end'];
  233. }
  234. $out .= $this->template['row_'.$name.'end'];
  235. $out .= $this->newline;
  236. }
  237. }
  238. $out .= $this->template['table_close'];
  239. return $out;
  240. }
  241. // --------------------------------------------------------------------
  242. /**
  243. * Clears the table arrays. Useful if multiple tables are being generated
  244. *
  245. * @access public
  246. * @return void
  247. */
  248. function clear()
  249. {
  250. $this->rows = array();
  251. $this->heading = array();
  252. $this->auto_heading = TRUE;
  253. }
  254. // --------------------------------------------------------------------
  255. /**
  256. * Set table data from a database result object
  257. *
  258. * @access public
  259. * @param object
  260. * @return void
  261. */
  262. function _set_from_object($query)
  263. {
  264. if ( ! is_object($query))
  265. {
  266. return FALSE;
  267. }
  268. // First generate the headings from the table column names
  269. if (count($this->heading) == 0)
  270. {
  271. if ( ! method_exists($query, 'list_fields'))
  272. {
  273. return FALSE;
  274. }
  275. $this->heading = $query->list_fields();
  276. }
  277. // Next blast through the result array and build out the rows
  278. if ($query->num_rows() > 0)
  279. {
  280. foreach ($query->result_array() as $row)
  281. {
  282. $this->rows[] = $row;
  283. }
  284. }
  285. }
  286. // --------------------------------------------------------------------
  287. /**
  288. * Set table data from an array
  289. *
  290. * @access public
  291. * @param array
  292. * @return void
  293. */
  294. function _set_from_array($data, $set_heading = TRUE)
  295. {
  296. if ( ! is_array($data) OR count($data) == 0)
  297. {
  298. return FALSE;
  299. }
  300. $i = 0;
  301. foreach ($data as $row)
  302. {
  303. if ( ! is_array($row))
  304. {
  305. $this->rows[] = $data;
  306. break;
  307. }
  308. // If a heading hasn't already been set we'll use the first row of the array as the heading
  309. if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
  310. {
  311. $this->heading = $row;
  312. }
  313. else
  314. {
  315. $this->rows[] = $row;
  316. }
  317. $i++;
  318. }
  319. }
  320. // --------------------------------------------------------------------
  321. /**
  322. * Compile Template
  323. *
  324. * @access private
  325. * @return void
  326. */
  327. function _compile_template()
  328. {
  329. if ($this->template == NULL)
  330. {
  331. $this->template = $this->_default_template();
  332. return;
  333. }
  334. $this->temp = $this->_default_template();
  335. 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)
  336. {
  337. if ( ! isset($this->template[$val]))
  338. {
  339. $this->template[$val] = $this->temp[$val];
  340. }
  341. }
  342. }
  343. // --------------------------------------------------------------------
  344. /**
  345. * Default Template
  346. *
  347. * @access private
  348. * @return void
  349. */
  350. function _default_template()
  351. {
  352. return array (
  353. 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
  354. 'heading_row_start' => '<tr>',
  355. 'heading_row_end' => '</tr>',
  356. 'heading_cell_start' => '<th>',
  357. 'heading_cell_end' => '</th>',
  358. 'row_start' => '<tr>',
  359. 'row_end' => '</tr>',
  360. 'cell_start' => '<td>',
  361. 'cell_end' => '</td>',
  362. 'row_alt_start' => '<tr>',
  363. 'row_alt_end' => '</tr>',
  364. 'cell_alt_start' => '<td>',
  365. 'cell_alt_end' => '</td>',
  366. 'table_close' => '</table>'
  367. );
  368. }
  369. }
  370. ?>