blob: 0f8404d85553dc580b0291ee7a314436b18c1806 [file] [log] [blame]
Andrey Andreevfe9b9a92011-12-25 16:11:01 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * HTML Table Generating Class
30 *
31 * Lets you create tables manually or from database result objects, or arrays.
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category HTML Tables
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Gerry6b590892011-09-25 00:16:39 +080037 * @link http://codeigniter.com/user_guide/libraries/table.html
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
39class CI_Table {
40
Timothy Warren3182a762012-04-26 18:09:25 -040041 /**
42 * Data for table rows
43 *
44 * @var array
45 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030046 public $rows = array();
Andrey Andreev56454792012-05-17 14:32:19 +030047
Timothy Warren3182a762012-04-26 18:09:25 -040048 /**
49 * Data for table heading
50 *
51 * @var array
52 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030053 public $heading = array();
Andrey Andreev56454792012-05-17 14:32:19 +030054
Timothy Warren3182a762012-04-26 18:09:25 -040055 /**
56 * Whether or not to automatically create the table header
57 *
58 * @var bool
59 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030060 public $auto_heading = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +030061
Timothy Warren3182a762012-04-26 18:09:25 -040062 /**
63 * Table caption
64 *
65 * @var string
66 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030067 public $caption = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030068
Timothy Warren3182a762012-04-26 18:09:25 -040069 /**
Andrey Andreev56454792012-05-17 14:32:19 +030070 * Table layout template
Timothy Warren3182a762012-04-26 18:09:25 -040071 *
72 * @var array
73 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030074 public $template = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030075
Timothy Warren3182a762012-04-26 18:09:25 -040076 /**
77 * Newline setting
78 *
79 * @var string
80 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030081 public $newline = "\n";
Andrey Andreev56454792012-05-17 14:32:19 +030082
Timothy Warren3182a762012-04-26 18:09:25 -040083 /**
84 * Contents of empty cells
85 *
86 * @var string
87 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030088 public $empty_cells = '';
Andrey Andreev56454792012-05-17 14:32:19 +030089
Timothy Warren3182a762012-04-26 18:09:25 -040090 /**
91 * Callback for custom table layout
92 *
93 * @var function
94 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030095 public $function = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020096
Mike Funk46e3a9a2012-02-24 09:38:35 -050097 /**
98 * Set the template from the table config file if it exists
Andrey Andreev80295322012-03-12 16:29:16 +020099 *
Mike Funk5fbaf272012-03-12 10:19:46 -0400100 * @param array $config (default: array())
Mike Funkaa20f5b2012-02-28 13:43:16 -0500101 * @return void
Mike Funk46e3a9a2012-02-24 09:38:35 -0500102 */
103 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Mike Funk46e3a9a2012-02-24 09:38:35 -0500105 // initialize config
106 foreach ($config as $key => $val)
107 {
108 $this->template[$key] = $val;
109 }
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300110
111 log_message('debug', 'Table Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Set the template
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @param array
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300120 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200122 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
124 if ( ! is_array($template))
125 {
126 return FALSE;
127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 $this->template = $template;
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300130 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
132
133 // --------------------------------------------------------------------
134
135 /**
136 * Set the table heading
137 *
138 * Can be passed as an array or discreet params
139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @param mixed
141 * @return void
142 */
Timothy Warren68f09812012-04-27 10:38:32 -0400143 public function set_heading($args = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 $args = func_get_args();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600146 $this->heading = $this->_prep_args($args);
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148
149 // --------------------------------------------------------------------
150
151 /**
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300152 * Set columns. Takes a one-dimensional array as input and creates
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 * a multi-dimensional array with a depth equal to the number of
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300154 * columns. This allows a single array with many elements to be
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * displayed in a table that has a fixed column count.
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param array
158 * @param int
159 * @return void
160 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200161 public function make_columns($array = array(), $col_limit = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700163 if ( ! is_array($array) OR count($array) === 0 OR ! is_int($col_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 return FALSE;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
168 // Turn off the auto-heading feature since it's doubtful we
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // will want headings from a one-dimensional array
170 $this->auto_heading = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200171
Alex Bilbied261b1e2012-06-02 11:12:16 +0100172 if ($col_limit === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
174 return $array;
175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 $new = array();
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200178 do
Barry Mienydd671972010-10-04 16:33:58 +0200179 {
180 $temp = array_splice($array, 0, $col_limit);
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 if (count($temp) < $col_limit)
183 {
184 for ($i = count($temp); $i < $col_limit; $i++)
185 {
186 $temp[] = '&nbsp;';
187 }
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 $new[] = $temp;
191 }
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200192 while (count($array) > 0);
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 return $new;
195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Set "empty" cells
201 *
202 * Can be passed as an array or discreet params
203 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @param mixed
205 * @return void
206 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200207 public function set_empty($value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
209 $this->empty_cells = $value;
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // --------------------------------------------------------------------
213
214 /**
215 * Add a table row
216 *
217 * Can be passed as an array or discreet params
218 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * @param mixed
220 * @return void
221 */
Timothy Warren3182a762012-04-26 18:09:25 -0400222 public function add_row($args = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
224 $args = func_get_args();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600225 $this->rows[] = $this->_prep_args($args);
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
227
228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Jones7b5b0e22010-03-02 22:48:53 -0600230 /**
231 * Prep Args
232 *
233 * Ensures a standard associative array format for all cell data
234 *
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300235 * @param array
236 * @return array
Derek Jones7b5b0e22010-03-02 22:48:53 -0600237 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200238 protected function _prep_args($args)
Derek Jones7b5b0e22010-03-02 22:48:53 -0600239 {
240 // If there is no $args[0], skip this and treat as an associative array
241 // This can happen if there is only a single key, for example this is passed to table->generate
242 // array(array('foo'=>'bar'))
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300243 if (isset($args[0]) && count($args) === 1 && is_array($args[0]))
Derek Jones7b5b0e22010-03-02 22:48:53 -0600244 {
245 // args sent as indexed array
246 if ( ! isset($args[0]['data']))
247 {
248 foreach ($args[0] as $key => $val)
249 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300250 $args[$key] = (is_array($val) && isset($val['data'])) ? $val : array('data' => $val);
Barry Mienydd671972010-10-04 16:33:58 +0200251 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600252 }
253 }
254 else
255 {
256 foreach ($args as $key => $val)
257 {
258 if ( ! is_array($val))
259 {
260 $args[$key] = array('data' => $val);
261 }
262 }
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Jones7b5b0e22010-03-02 22:48:53 -0600265 return $args;
266 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000267
Derek Jones7b5b0e22010-03-02 22:48:53 -0600268 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 /**
271 * Add a table caption
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param string
274 * @return void
275 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200276 public function set_caption($caption)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 $this->caption = $caption;
Barry Mienydd671972010-10-04 16:33:58 +0200279 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000280
281 // --------------------------------------------------------------------
282
283 /**
284 * Generate the table
285 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @param mixed
287 * @return string
288 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200289 public function generate($table_data = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 // The table data can optionally be passed to this function
292 // either as a database result object or an array
293 if ( ! is_null($table_data))
294 {
295 if (is_object($table_data))
296 {
297 $this->_set_from_object($table_data);
298 }
299 elseif (is_array($table_data))
300 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100301 $set_heading = (count($this->heading) !== 0 OR $this->auto_heading !== FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->_set_from_array($table_data, $set_heading);
303 }
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300306 // Is there anything to display? No? Smite them!
307 if (count($this->heading) === 0 && count($this->rows) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 return 'Undefined table data';
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // Compile and validate the template date
313 $this->_compile_template();
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Jones7b5b0e22010-03-02 22:48:53 -0600315 // set a custom cell manipulation function to a locally scoped variable so its callable
316 $function = $this->function;
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // Build the table!
Barry Mienydd671972010-10-04 16:33:58 +0200319
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200320 $out = $this->template['table_open'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000321
322 // Add any caption here
323 if ($this->caption)
324 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200325 $out .= $this->newline.'<caption>'.$this->caption.'</caption>'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
328 // Is there a table heading to display?
329 if (count($this->heading) > 0)
330 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200331 $out .= $this->template['thead_open'].$this->newline.$this->template['heading_row_start'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
Pascal Kriete14287f32011-02-14 13:39:34 -0500333 foreach ($this->heading as $heading)
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600335 $temp = $this->template['heading_cell_start'];
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Jones7b5b0e22010-03-02 22:48:53 -0600337 foreach ($heading as $key => $val)
338 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100339 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600340 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300341 $temp = str_replace('<th', '<th '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200342 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600343 }
344
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200345 $out .= $temp.(isset($heading['data']) ? $heading['data'] : '').$this->template['heading_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
347
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200348 $out .= $this->template['heading_row_end'].$this->newline.$this->template['thead_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Build the table rows
352 if (count($this->rows) > 0)
353 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200354 $out .= $this->template['tbody_open'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 $i = 1;
Pascal Kriete14287f32011-02-14 13:39:34 -0500357 foreach ($this->rows as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 if ( ! is_array($row))
360 {
361 break;
362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 // We use modulus to alternate the row colors
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300365 $name = fmod($i++, 2) ? '' : 'alt_';
Barry Mienydd671972010-10-04 16:33:58 +0200366
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200367 $out .= $this->template['row_'.$name.'start'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200368
Pascal Kriete14287f32011-02-14 13:39:34 -0500369 foreach ($row as $cell)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600371 $temp = $this->template['cell_'.$name.'start'];
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Jones7b5b0e22010-03-02 22:48:53 -0600373 foreach ($cell as $key => $val)
374 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200375 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600376 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300377 $temp = str_replace('<td', '<td '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200378 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Jones7b5b0e22010-03-02 22:48:53 -0600381 $cell = isset($cell['data']) ? $cell['data'] : '';
382 $out .= $temp;
383
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300384 if ($cell === '' OR $cell === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 $out .= $this->empty_cells;
387 }
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300388 elseif ($function !== FALSE && is_callable($function))
389 {
390 $out .= call_user_func($function, $cell);
391 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 else
393 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300394 $out .= $cell;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 $out .= $this->template['cell_'.$name.'end'];
398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200400 $out .= $this->template['row_'.$name.'end'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200403 $out .= $this->template['tbody_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405
406 $out .= $this->template['table_close'];
Barry Mienydd671972010-10-04 16:33:58 +0200407
Greg Aker02b3a5b2011-02-01 01:29:21 -0600408 // Clear table class properties before generating the table
409 $this->clear();
410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 return $out;
412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // --------------------------------------------------------------------
415
416 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500417 * Clears the table arrays. Useful if multiple tables are being generated
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @return void
420 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200421 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300423 $this->rows = array();
424 $this->heading = array();
425 $this->auto_heading = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 // --------------------------------------------------------------------
429
430 /**
431 * Set table data from a database result object
432 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 * @param object
434 * @return void
435 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200436 protected function _set_from_object($query)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 if ( ! is_object($query))
439 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300440 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // First generate the headings from the table column names
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200444 if (count($this->heading) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700446 if ( ! is_callable(array($query, 'list_fields')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300448 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Jones7b5b0e22010-03-02 22:48:53 -0600451 $this->heading = $this->_prep_args($query->list_fields());
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 // Next blast through the result array and build out the rows
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 if ($query->num_rows() > 0)
456 {
457 foreach ($query->result_array() as $row)
458 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600459 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
461 }
462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Set table data from an array
468 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * @param array
Timothy Warren3182a762012-04-26 18:09:25 -0400470 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @return void
472 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200473 protected function _set_from_array($data, $set_heading = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200475 if ( ! is_array($data) OR count($data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
477 return FALSE;
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 $i = 0;
481 foreach ($data as $row)
Barry Mienydd671972010-10-04 16:33:58 +0200482 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // 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 +0100484 if ($i++ === 0 && count($data) > 1 && count($this->heading) === 0 && $set_heading === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600486 $this->heading = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488 else
489 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600490 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Compile Template
499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @return void
501 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200502 protected function _compile_template()
Barry Mienydd671972010-10-04 16:33:58 +0200503 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100504 if ($this->template === NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200505 {
506 $this->template = $this->_default_template();
507 return;
508 }
509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 $this->temp = $this->_default_template();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600511 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 +0000512 {
513 if ( ! isset($this->template[$val]))
514 {
515 $this->template[$val] = $this->temp[$val];
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517 }
518 }
519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Default Template
524 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 * @return void
526 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200527 protected function _default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300529 return array(
530 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
Barry Mienydd671972010-10-04 16:33:58 +0200531
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300532 'thead_open' => '<thead>',
533 'thead_close' => '</thead>',
Barry Mienydd671972010-10-04 16:33:58 +0200534
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300535 'heading_row_start' => '<tr>',
536 'heading_row_end' => '</tr>',
537 'heading_cell_start' => '<th>',
538 'heading_cell_end' => '</th>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000539
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300540 'tbody_open' => '<tbody>',
541 'tbody_close' => '</tbody>',
Barry Mienydd671972010-10-04 16:33:58 +0200542
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300543 'row_start' => '<tr>',
544 'row_end' => '</tr>',
545 'cell_start' => '<td>',
546 'cell_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000547
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300548 'row_alt_start' => '<tr>',
549 'row_alt_end' => '</tr>',
550 'cell_alt_start' => '<td>',
551 'cell_alt_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000552
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300553 'table_close' => '</table>'
554 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557}
558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559/* End of file Table.php */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300560/* Location: ./system/libraries/Table.php */