blob: 3bce294d8c4ef4550b363d808f27491a2b9469ec [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevfe9b9a92011-12-25 16:11:01 +02008 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
Andrey Andreevfe9b9a92011-12-25 16:11:01 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.3.1
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * HTML Table Generating Class
42 *
43 * Lets you create tables manually or from database result objects, or arrays.
44 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category HTML Tables
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/libraries/table.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
51class CI_Table {
52
Timothy Warren3182a762012-04-26 18:09:25 -040053 /**
54 * Data for table rows
55 *
56 * @var array
57 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030058 public $rows = array();
Andrey Andreev56454792012-05-17 14:32:19 +030059
Timothy Warren3182a762012-04-26 18:09:25 -040060 /**
61 * Data for table heading
62 *
63 * @var array
64 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030065 public $heading = array();
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren3182a762012-04-26 18:09:25 -040067 /**
68 * Whether or not to automatically create the table header
69 *
70 * @var bool
71 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030072 public $auto_heading = TRUE;
Andrey Andreev56454792012-05-17 14:32:19 +030073
Timothy Warren3182a762012-04-26 18:09:25 -040074 /**
75 * Table caption
76 *
77 * @var string
78 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030079 public $caption = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030080
Timothy Warren3182a762012-04-26 18:09:25 -040081 /**
Andrey Andreev56454792012-05-17 14:32:19 +030082 * Table layout template
Timothy Warren3182a762012-04-26 18:09:25 -040083 *
84 * @var array
85 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030086 public $template = NULL;
Andrey Andreev56454792012-05-17 14:32:19 +030087
Timothy Warren3182a762012-04-26 18:09:25 -040088 /**
89 * Newline setting
90 *
91 * @var string
92 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030093 public $newline = "\n";
Andrey Andreev56454792012-05-17 14:32:19 +030094
Timothy Warren3182a762012-04-26 18:09:25 -040095 /**
96 * Contents of empty cells
97 *
98 * @var string
99 */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300100 public $empty_cells = '';
Andrey Andreev56454792012-05-17 14:32:19 +0300101
Timothy Warren3182a762012-04-26 18:09:25 -0400102 /**
103 * Callback for custom table layout
104 *
105 * @var function
106 */
Andrey Andreeva49c8ad2014-02-11 17:15:07 +0200107 public $function = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200108
Mike Funk46e3a9a2012-02-24 09:38:35 -0500109 /**
110 * Set the template from the table config file if it exists
Andrey Andreev80295322012-03-12 16:29:16 +0200111 *
Mike Funk5fbaf272012-03-12 10:19:46 -0400112 * @param array $config (default: array())
Mike Funkaa20f5b2012-02-28 13:43:16 -0500113 * @return void
Mike Funk46e3a9a2012-02-24 09:38:35 -0500114 */
115 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Mike Funk46e3a9a2012-02-24 09:38:35 -0500117 // initialize config
118 foreach ($config as $key => $val)
119 {
120 $this->template[$key] = $val;
121 }
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300122
Andrey Andreev90726b82015-01-20 12:39:22 +0200123 log_message('info', 'Table Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
125
126 // --------------------------------------------------------------------
127
128 /**
129 * Set the template
130 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200131 * @param array $template
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300132 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200134 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
136 if ( ! is_array($template))
137 {
138 return FALSE;
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 $this->template = $template;
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300142 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Set the table heading
149 *
150 * Can be passed as an array or discreet params
151 *
Andrey Andreev3fcc3f62014-02-11 17:28:36 +0200152 * @param mixed
Andrey Andreev1f590952014-02-08 19:50:26 +0200153 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 */
Andrey Andreev3fcc3f62014-02-11 17:28:36 +0200155 public function set_heading($args = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Andrey Andreev3fcc3f62014-02-11 17:28:36 +0200157 $this->heading = $this->_prep_args(func_get_args());
Andrey Andreev1f590952014-02-08 19:50:26 +0200158 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160
161 // --------------------------------------------------------------------
162
163 /**
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300164 * Set columns. Takes a one-dimensional array as input and creates
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * a multi-dimensional array with a depth equal to the number of
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300166 * columns. This allows a single array with many elements to be
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * displayed in a table that has a fixed column count.
168 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200169 * @param array $array
170 * @param int $col_limit
171 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200173 public function make_columns($array = array(), $col_limit = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700175 if ( ! is_array($array) OR count($array) === 0 OR ! is_int($col_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 return FALSE;
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
180 // Turn off the auto-heading feature since it's doubtful we
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // will want headings from a one-dimensional array
182 $this->auto_heading = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200183
Alex Bilbied261b1e2012-06-02 11:12:16 +0100184 if ($col_limit === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 return $array;
187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 $new = array();
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200190 do
Barry Mienydd671972010-10-04 16:33:58 +0200191 {
192 $temp = array_splice($array, 0, $col_limit);
193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 if (count($temp) < $col_limit)
195 {
196 for ($i = count($temp); $i < $col_limit; $i++)
197 {
198 $temp[] = '&nbsp;';
199 }
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $new[] = $temp;
203 }
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200204 while (count($array) > 0);
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 return $new;
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Set "empty" cells
213 *
214 * Can be passed as an array or discreet params
215 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200216 * @param mixed $value
217 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200219 public function set_empty($value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
221 $this->empty_cells = $value;
Andrey Andreev1f590952014-02-08 19:50:26 +0200222 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * Add a table row
229 *
230 * Can be passed as an array or discreet params
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @param mixed
Andrey Andreev1f590952014-02-08 19:50:26 +0200233 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 */
Timothy Warren3182a762012-04-26 18:09:25 -0400235 public function add_row($args = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Andrey Andreev3fcc3f62014-02-11 17:28:36 +0200237 $this->rows[] = $this->_prep_args(func_get_args());
Andrey Andreev1f590952014-02-08 19:50:26 +0200238 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
240
241 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Jones7b5b0e22010-03-02 22:48:53 -0600243 /**
244 * Prep Args
245 *
246 * Ensures a standard associative array format for all cell data
247 *
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300248 * @param array
249 * @return array
Derek Jones7b5b0e22010-03-02 22:48:53 -0600250 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200251 protected function _prep_args($args)
Derek Jones7b5b0e22010-03-02 22:48:53 -0600252 {
253 // If there is no $args[0], skip this and treat as an associative array
254 // This can happen if there is only a single key, for example this is passed to table->generate
255 // array(array('foo'=>'bar'))
Andrey Andreevf0b69a82014-02-11 17:56:44 +0200256 if (isset($args[0]) && count($args) === 1 && is_array($args[0]) && ! isset($args[0]['data']))
Derek Jones7b5b0e22010-03-02 22:48:53 -0600257 {
Andrey Andreevf0b69a82014-02-11 17:56:44 +0200258 $args = $args[0];
Derek Jones7b5b0e22010-03-02 22:48:53 -0600259 }
Andrey Andreevf0b69a82014-02-11 17:56:44 +0200260
261 foreach ($args as $key => $val)
Derek Jones7b5b0e22010-03-02 22:48:53 -0600262 {
Andrey Andreevf0b69a82014-02-11 17:56:44 +0200263 is_array($val) OR $args[$key] = array('data' => $val);
Derek Jones7b5b0e22010-03-02 22:48:53 -0600264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Jones7b5b0e22010-03-02 22:48:53 -0600266 return $args;
267 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000268
Derek Jones7b5b0e22010-03-02 22:48:53 -0600269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 /**
272 * Add a table caption
273 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200274 * @param string $caption
275 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200277 public function set_caption($caption)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 $this->caption = $caption;
Barry Mienydd671972010-10-04 16:33:58 +0200280 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000281
282 // --------------------------------------------------------------------
283
284 /**
285 * Generate the table
286 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200287 * @param mixed $table_data
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @return string
289 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200290 public function generate($table_data = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 // The table data can optionally be passed to this function
293 // either as a database result object or an array
Andrey Andreev05983fc2014-02-11 16:51:43 +0200294 if ( ! empty($table_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200296 if ($table_data instanceof CI_DB_result)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200298 $this->_set_from_db_result($table_data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300 elseif (is_array($table_data))
301 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200302 $this->_set_from_array($table_data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
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!
Andrey Andreev05983fc2014-02-11 16:51:43 +0200307 if (empty($this->heading) && empty($this->rows))
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
Andrey Andreev05983fc2014-02-11 16:51:43 +0200315 // Validate a possibly existing custom cell manipulation function
Andrey Andreeva49c8ad2014-02-11 17:15:07 +0200316 if (isset($this->function) && ! is_callable($this->function))
Andrey Andreev05983fc2014-02-11 16:51:43 +0200317 {
Andrey Andreeva49c8ad2014-02-11 17:15:07 +0200318 $this->function = NULL;
Andrey Andreev05983fc2014-02-11 16:51:43 +0200319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // Build the table!
Barry Mienydd671972010-10-04 16:33:58 +0200322
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200323 $out = $this->template['table_open'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000324
325 // Add any caption here
326 if ($this->caption)
327 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200328 $out .= '<caption>'.$this->caption.'</caption>'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330
331 // Is there a table heading to display?
Andrey Andreev05983fc2014-02-11 16:51:43 +0200332 if ( ! empty($this->heading))
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200334 $out .= $this->template['thead_open'].$this->newline.$this->template['heading_row_start'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000335
Pascal Kriete14287f32011-02-14 13:39:34 -0500336 foreach ($this->heading as $heading)
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600338 $temp = $this->template['heading_cell_start'];
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Jones7b5b0e22010-03-02 22:48:53 -0600340 foreach ($heading as $key => $val)
341 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100342 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600343 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300344 $temp = str_replace('<th', '<th '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200345 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600346 }
347
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200348 $out .= $temp.(isset($heading['data']) ? $heading['data'] : '').$this->template['heading_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200351 $out .= $this->template['heading_row_end'].$this->newline.$this->template['thead_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // Build the table rows
Andrey Andreev05983fc2014-02-11 16:51:43 +0200355 if ( ! empty($this->rows))
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200357 $out .= $this->template['tbody_open'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 $i = 1;
Pascal Kriete14287f32011-02-14 13:39:34 -0500360 foreach ($this->rows as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 if ( ! is_array($row))
363 {
364 break;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // We use modulus to alternate the row colors
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300368 $name = fmod($i++, 2) ? '' : 'alt_';
Barry Mienydd671972010-10-04 16:33:58 +0200369
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200370 $out .= $this->template['row_'.$name.'start'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200371
Pascal Kriete14287f32011-02-14 13:39:34 -0500372 foreach ($row as $cell)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600374 $temp = $this->template['cell_'.$name.'start'];
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Jones7b5b0e22010-03-02 22:48:53 -0600376 foreach ($cell as $key => $val)
377 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200378 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600379 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300380 $temp = str_replace('<td', '<td '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200381 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Jones7b5b0e22010-03-02 22:48:53 -0600384 $cell = isset($cell['data']) ? $cell['data'] : '';
385 $out .= $temp;
386
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300387 if ($cell === '' OR $cell === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 $out .= $this->empty_cells;
390 }
Andrey Andreeva49c8ad2014-02-11 17:15:07 +0200391 elseif (isset($this->function))
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300392 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200393 $out .= call_user_func($this->function, $cell);
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300394 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 else
396 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300397 $out .= $cell;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 $out .= $this->template['cell_'.$name.'end'];
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200403 $out .= $this->template['row_'.$name.'end'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200406 $out .= $this->template['tbody_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408
409 $out .= $this->template['table_close'];
Barry Mienydd671972010-10-04 16:33:58 +0200410
Greg Aker02b3a5b2011-02-01 01:29:21 -0600411 // Clear table class properties before generating the table
412 $this->clear();
413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 return $out;
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 // --------------------------------------------------------------------
418
419 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500420 * Clears the table arrays. Useful if multiple tables are being generated
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200422 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200424 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Andrey Andreev1f590952014-02-08 19:50:26 +0200426 $this->rows = array();
427 $this->heading = array();
428 $this->auto_heading = TRUE;
429 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // --------------------------------------------------------------------
433
434 /**
435 * Set table data from a database result object
436 *
Andrey Andreev05983fc2014-02-11 16:51:43 +0200437 * @param CI_DB_result $db_result Database result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @return void
439 */
Andrey Andreev05983fc2014-02-11 16:51:43 +0200440 protected function _set_from_db_result($object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // First generate the headings from the table column names
Andrey Andreev05983fc2014-02-11 16:51:43 +0200443 if ($this->auto_heading === TRUE && empty($this->heading))
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200445 $this->heading = $this->_prep_args($object->list_fields());
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Andrey Andreev05983fc2014-02-11 16:51:43 +0200448 foreach ($object->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200450 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452 }
453
454 // --------------------------------------------------------------------
455
456 /**
457 * Set table data from an array
458 *
Andrey Andreev05983fc2014-02-11 16:51:43 +0200459 * @param array $data
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * @return void
461 */
Andrey Andreev05983fc2014-02-11 16:51:43 +0200462 protected function _set_from_array($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200464 if ($this->auto_heading === TRUE && empty($this->heading))
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200466 $this->heading = $this->_prep_args(array_shift($data));
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Andrey Andreev05983fc2014-02-11 16:51:43 +0200469 foreach ($data as &$row)
Barry Mienydd671972010-10-04 16:33:58 +0200470 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200471 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473 }
474
475 // --------------------------------------------------------------------
476
477 /**
478 * Compile Template
479 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @return void
481 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200482 protected function _compile_template()
Barry Mienydd671972010-10-04 16:33:58 +0200483 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100484 if ($this->template === NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200485 {
486 $this->template = $this->_default_template();
487 return;
488 }
489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 $this->temp = $this->_default_template();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600491 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 +0000492 {
493 if ( ! isset($this->template[$val]))
494 {
495 $this->template[$val] = $this->temp[$val];
496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497 }
498 }
499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 // --------------------------------------------------------------------
501
502 /**
503 * Default Template
504 *
Gabriel Potkány1fb50002015-02-04 01:45:59 +0100505 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200507 protected function _default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200509 return array(
Andrey Andreev1f590952014-02-08 19:50:26 +0200510 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
Barry Mienydd671972010-10-04 16:33:58 +0200511
Andrey Andreev1f590952014-02-08 19:50:26 +0200512 'thead_open' => '<thead>',
513 'thead_close' => '</thead>',
Barry Mienydd671972010-10-04 16:33:58 +0200514
Andrey Andreev1f590952014-02-08 19:50:26 +0200515 'heading_row_start' => '<tr>',
516 'heading_row_end' => '</tr>',
517 'heading_cell_start' => '<th>',
518 'heading_cell_end' => '</th>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000519
Andrey Andreev1f590952014-02-08 19:50:26 +0200520 'tbody_open' => '<tbody>',
521 'tbody_close' => '</tbody>',
Barry Mienydd671972010-10-04 16:33:58 +0200522
Andrey Andreev1f590952014-02-08 19:50:26 +0200523 'row_start' => '<tr>',
524 'row_end' => '</tr>',
525 'cell_start' => '<td>',
526 'cell_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000527
Andrey Andreev1f590952014-02-08 19:50:26 +0200528 'row_alt_start' => '<tr>',
529 'row_alt_end' => '</tr>',
530 'cell_alt_start' => '<td>',
531 'cell_alt_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000532
Andrey Andreev1f590952014-02-08 19:50:26 +0200533 'table_close' => '</table>'
534 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537}