Table.php 11 KB

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