blob: 1d4320855a11d326c6c0202d3e1d6e7c2956bc44 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevfe9b9a92011-12-25 16:11:01 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevfe9b9a92011-12-25 16:11:01 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.3.1
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * HTML Table Generating Class
31 *
32 * Lets you create tables manually or from database result objects, or arrays.
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category HTML Tables
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Gerry6b590892011-09-25 00:16:39 +080038 * @link http://codeigniter.com/user_guide/libraries/table.html
Derek Allard2067d1a2008-11-13 22:59:24 +000039 */
40class CI_Table {
41
Timothy Warren3182a762012-04-26 18:09:25 -040042 /**
43 * Data for table rows
44 *
45 * @var array
46 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030047 public $rows = array();
Andrey Andreev56454792012-05-17 14:32:19 +030048
Timothy Warren3182a762012-04-26 18:09:25 -040049 /**
50 * Data for table heading
51 *
52 * @var array
53 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030054 public $heading = array();
Andrey Andreev56454792012-05-17 14:32:19 +030055
Timothy Warren3182a762012-04-26 18:09:25 -040056 /**
57 * Whether or not to automatically create the table header
58 *
59 * @var bool
60 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030061 public $auto_heading = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +030062
Timothy Warren3182a762012-04-26 18:09:25 -040063 /**
64 * Table caption
65 *
66 * @var string
67 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030068 public $caption = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030069
Timothy Warren3182a762012-04-26 18:09:25 -040070 /**
Andrey Andreev56454792012-05-17 14:32:19 +030071 * Table layout template
Timothy Warren3182a762012-04-26 18:09:25 -040072 *
73 * @var array
74 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030075 public $template = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030076
Timothy Warren3182a762012-04-26 18:09:25 -040077 /**
78 * Newline setting
79 *
80 * @var string
81 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030082 public $newline = "\n";
Andrey Andreev56454792012-05-17 14:32:19 +030083
Timothy Warren3182a762012-04-26 18:09:25 -040084 /**
85 * Contents of empty cells
86 *
87 * @var string
88 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030089 public $empty_cells = '';
Andrey Andreev56454792012-05-17 14:32:19 +030090
Timothy Warren3182a762012-04-26 18:09:25 -040091 /**
92 * Callback for custom table layout
93 *
94 * @var function
95 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030096 public $function = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020097
Mike Funk46e3a9a2012-02-24 09:38:35 -050098 /**
99 * Set the template from the table config file if it exists
Andrey Andreev80295322012-03-12 16:29:16 +0200100 *
Mike Funk5fbaf272012-03-12 10:19:46 -0400101 * @param array $config (default: array())
Mike Funkaa20f5b2012-02-28 13:43:16 -0500102 * @return void
Mike Funk46e3a9a2012-02-24 09:38:35 -0500103 */
104 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Mike Funk46e3a9a2012-02-24 09:38:35 -0500106 // initialize config
107 foreach ($config as $key => $val)
108 {
109 $this->template[$key] = $val;
110 }
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300111
112 log_message('debug', 'Table Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Set the template
119 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200120 * @param array $template
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300121 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200123 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
125 if ( ! is_array($template))
126 {
127 return FALSE;
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 $this->template = $template;
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300131 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
133
134 // --------------------------------------------------------------------
135
136 /**
137 * Set the table heading
138 *
139 * Can be passed as an array or discreet params
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param mixed
Andrey Andreev1f590952014-02-08 19:50:26 +0200142 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 */
Timothy Warren68f09812012-04-27 10:38:32 -0400144 public function set_heading($args = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 $args = func_get_args();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600147 $this->heading = $this->_prep_args($args);
Andrey Andreev1f590952014-02-08 19:50:26 +0200148 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
151 // --------------------------------------------------------------------
152
153 /**
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300154 * Set columns. Takes a one-dimensional array as input and creates
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * a multi-dimensional array with a depth equal to the number of
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300156 * columns. This allows a single array with many elements to be
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * displayed in a table that has a fixed column count.
158 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200159 * @param array $array
160 * @param int $col_limit
161 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200163 public function make_columns($array = array(), $col_limit = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700165 if ( ! is_array($array) OR count($array) === 0 OR ! is_int($col_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
167 return FALSE;
168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
170 // Turn off the auto-heading feature since it's doubtful we
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // will want headings from a one-dimensional array
172 $this->auto_heading = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200173
Alex Bilbied261b1e2012-06-02 11:12:16 +0100174 if ($col_limit === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 return $array;
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 $new = array();
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200180 do
Barry Mienydd671972010-10-04 16:33:58 +0200181 {
182 $temp = array_splice($array, 0, $col_limit);
183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 if (count($temp) < $col_limit)
185 {
186 for ($i = count($temp); $i < $col_limit; $i++)
187 {
188 $temp[] = '&nbsp;';
189 }
190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 $new[] = $temp;
193 }
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200194 while (count($array) > 0);
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 return $new;
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * Set "empty" cells
203 *
204 * Can be passed as an array or discreet params
205 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200206 * @param mixed $value
207 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200209 public function set_empty($value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
211 $this->empty_cells = $value;
Andrey Andreev1f590952014-02-08 19:50:26 +0200212 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 // --------------------------------------------------------------------
216
217 /**
218 * Add a table row
219 *
220 * Can be passed as an array or discreet params
221 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 * @param mixed
Andrey Andreev1f590952014-02-08 19:50:26 +0200223 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 */
Timothy Warren3182a762012-04-26 18:09:25 -0400225 public function add_row($args = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 $args = func_get_args();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600228 $this->rows[] = $this->_prep_args($args);
Andrey Andreev1f590952014-02-08 19:50:26 +0200229 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231
232 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Jones7b5b0e22010-03-02 22:48:53 -0600234 /**
235 * Prep Args
236 *
237 * Ensures a standard associative array format for all cell data
238 *
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300239 * @param array
240 * @return array
Derek Jones7b5b0e22010-03-02 22:48:53 -0600241 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200242 protected function _prep_args($args)
Derek Jones7b5b0e22010-03-02 22:48:53 -0600243 {
244 // If there is no $args[0], skip this and treat as an associative array
245 // This can happen if there is only a single key, for example this is passed to table->generate
246 // array(array('foo'=>'bar'))
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300247 if (isset($args[0]) && count($args) === 1 && is_array($args[0]))
Derek Jones7b5b0e22010-03-02 22:48:53 -0600248 {
249 // args sent as indexed array
250 if ( ! isset($args[0]['data']))
251 {
252 foreach ($args[0] as $key => $val)
253 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300254 $args[$key] = (is_array($val) && isset($val['data'])) ? $val : array('data' => $val);
Barry Mienydd671972010-10-04 16:33:58 +0200255 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600256 }
257 }
258 else
259 {
260 foreach ($args as $key => $val)
261 {
262 if ( ! is_array($val))
263 {
264 $args[$key] = array('data' => $val);
265 }
266 }
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Jones7b5b0e22010-03-02 22:48:53 -0600269 return $args;
270 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000271
Derek Jones7b5b0e22010-03-02 22:48:53 -0600272 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 /**
275 * Add a table caption
276 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200277 * @param string $caption
278 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200280 public function set_caption($caption)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 $this->caption = $caption;
Barry Mienydd671972010-10-04 16:33:58 +0200283 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
285 // --------------------------------------------------------------------
286
287 /**
288 * Generate the table
289 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200290 * @param mixed $table_data
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @return string
292 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200293 public function generate($table_data = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 // The table data can optionally be passed to this function
296 // either as a database result object or an array
vlakoff1228fe22013-01-14 01:30:09 +0100297 if ($table_data !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 if (is_object($table_data))
300 {
301 $this->_set_from_object($table_data);
302 }
303 elseif (is_array($table_data))
304 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100305 $set_heading = (count($this->heading) !== 0 OR $this->auto_heading !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 $this->_set_from_array($table_data, $set_heading);
307 }
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300310 // Is there anything to display? No? Smite them!
311 if (count($this->heading) === 0 && count($this->rows) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 return 'Undefined table data';
314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // Compile and validate the template date
317 $this->_compile_template();
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Jones7b5b0e22010-03-02 22:48:53 -0600319 // set a custom cell manipulation function to a locally scoped variable so its callable
320 $function = $this->function;
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // Build the table!
Barry Mienydd671972010-10-04 16:33:58 +0200323
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200324 $out = $this->template['table_open'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000325
326 // Add any caption here
327 if ($this->caption)
328 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200329 $out .= $this->newline.'<caption>'.$this->caption.'</caption>'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331
332 // Is there a table heading to display?
333 if (count($this->heading) > 0)
334 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200335 $out .= $this->template['thead_open'].$this->newline.$this->template['heading_row_start'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
Pascal Kriete14287f32011-02-14 13:39:34 -0500337 foreach ($this->heading as $heading)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600339 $temp = $this->template['heading_cell_start'];
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Jones7b5b0e22010-03-02 22:48:53 -0600341 foreach ($heading as $key => $val)
342 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100343 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600344 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300345 $temp = str_replace('<th', '<th '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200346 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600347 }
348
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200349 $out .= $temp.(isset($heading['data']) ? $heading['data'] : '').$this->template['heading_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200352 $out .= $this->template['heading_row_end'].$this->newline.$this->template['thead_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Build the table rows
356 if (count($this->rows) > 0)
357 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200358 $out .= $this->template['tbody_open'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 $i = 1;
Pascal Kriete14287f32011-02-14 13:39:34 -0500361 foreach ($this->rows as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
363 if ( ! is_array($row))
364 {
365 break;
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 // We use modulus to alternate the row colors
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300369 $name = fmod($i++, 2) ? '' : 'alt_';
Barry Mienydd671972010-10-04 16:33:58 +0200370
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200371 $out .= $this->template['row_'.$name.'start'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200372
Pascal Kriete14287f32011-02-14 13:39:34 -0500373 foreach ($row as $cell)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600375 $temp = $this->template['cell_'.$name.'start'];
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Jones7b5b0e22010-03-02 22:48:53 -0600377 foreach ($cell as $key => $val)
378 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200379 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600380 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300381 $temp = str_replace('<td', '<td '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200382 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Jones7b5b0e22010-03-02 22:48:53 -0600385 $cell = isset($cell['data']) ? $cell['data'] : '';
386 $out .= $temp;
387
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300388 if ($cell === '' OR $cell === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $out .= $this->empty_cells;
391 }
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300392 elseif ($function !== FALSE && is_callable($function))
393 {
394 $out .= call_user_func($function, $cell);
395 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 else
397 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300398 $out .= $cell;
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 $out .= $this->template['cell_'.$name.'end'];
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200404 $out .= $this->template['row_'.$name.'end'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200407 $out .= $this->template['tbody_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409
410 $out .= $this->template['table_close'];
Barry Mienydd671972010-10-04 16:33:58 +0200411
Greg Aker02b3a5b2011-02-01 01:29:21 -0600412 // Clear table class properties before generating the table
413 $this->clear();
414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 return $out;
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
419
420 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500421 * Clears the table arrays. Useful if multiple tables are being generated
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200423 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200425 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Andrey Andreev1f590952014-02-08 19:50:26 +0200427 $this->rows = array();
428 $this->heading = array();
429 $this->auto_heading = TRUE;
430 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 // --------------------------------------------------------------------
434
435 /**
436 * Set table data from a database result object
437 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @param object
439 * @return void
440 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200441 protected function _set_from_object($query)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 if ( ! is_object($query))
444 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300445 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // First generate the headings from the table column names
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200449 if (count($this->heading) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700451 if ( ! is_callable(array($query, 'list_fields')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300453 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Jones7b5b0e22010-03-02 22:48:53 -0600456 $this->heading = $this->_prep_args($query->list_fields());
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 // Next blast through the result array and build out the rows
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 if ($query->num_rows() > 0)
461 {
462 foreach ($query->result_array() as $row)
463 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600464 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
466 }
467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Set table data from an array
473 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * @param array
Timothy Warren3182a762012-04-26 18:09:25 -0400475 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @return void
477 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200478 protected function _set_from_array($data, $set_heading = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200480 if ( ! is_array($data) OR count($data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 return FALSE;
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 $i = 0;
486 foreach ($data as $row)
Barry Mienydd671972010-10-04 16:33:58 +0200487 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 // If a heading hasn't already been set we'll use the first row of the array as the heading
Alex Bilbied261b1e2012-06-02 11:12:16 +0100489 if ($i++ === 0 && count($data) > 1 && count($this->heading) === 0 && $set_heading === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600491 $this->heading = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493 else
494 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600495 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Compile Template
504 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @return void
506 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200507 protected function _compile_template()
Barry Mienydd671972010-10-04 16:33:58 +0200508 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100509 if ($this->template === NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200510 {
511 $this->template = $this->_default_template();
512 return;
513 }
514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 $this->temp = $this->_default_template();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600516 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)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 if ( ! isset($this->template[$val]))
519 {
520 $this->template[$val] = $this->temp[$val];
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522 }
523 }
524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // --------------------------------------------------------------------
526
527 /**
528 * Default Template
529 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @return void
531 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200532 protected function _default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200534 return array(
Andrey Andreev1f590952014-02-08 19:50:26 +0200535 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
Barry Mienydd671972010-10-04 16:33:58 +0200536
Andrey Andreev1f590952014-02-08 19:50:26 +0200537 'thead_open' => '<thead>',
538 'thead_close' => '</thead>',
Barry Mienydd671972010-10-04 16:33:58 +0200539
Andrey Andreev1f590952014-02-08 19:50:26 +0200540 'heading_row_start' => '<tr>',
541 'heading_row_end' => '</tr>',
542 'heading_cell_start' => '<th>',
543 'heading_cell_end' => '</th>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000544
Andrey Andreev1f590952014-02-08 19:50:26 +0200545 'tbody_open' => '<tbody>',
546 'tbody_close' => '</tbody>',
Barry Mienydd671972010-10-04 16:33:58 +0200547
Andrey Andreev1f590952014-02-08 19:50:26 +0200548 'row_start' => '<tr>',
549 'row_end' => '</tr>',
550 'cell_start' => '<td>',
551 'cell_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000552
Andrey Andreev1f590952014-02-08 19:50:26 +0200553 'row_alt_start' => '<tr>',
554 'row_alt_end' => '</tr>',
555 'cell_alt_start' => '<td>',
556 'cell_alt_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000557
Andrey Andreev1f590952014-02-08 19:50:26 +0200558 'table_close' => '</table>'
559 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562}
563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564/* End of file Table.php */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300565/* Location: ./system/libraries/Table.php */