blob: 179eb8de7ae63e69b9c57bf3a094c64e09c2fb56 [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 Andreeva49c8ad2014-02-11 17:15:07 +020096 public $function = NULL;
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 *
Andrey Andreev3fcc3f62014-02-11 17:28:36 +0200141 * @param mixed
Andrey Andreev1f590952014-02-08 19:50:26 +0200142 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 */
Andrey Andreev3fcc3f62014-02-11 17:28:36 +0200144 public function set_heading($args = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Andrey Andreev3fcc3f62014-02-11 17:28:36 +0200146 $this->heading = $this->_prep_args(func_get_args());
Andrey Andreev1f590952014-02-08 19:50:26 +0200147 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
149
150 // --------------------------------------------------------------------
151
152 /**
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300153 * Set columns. Takes a one-dimensional array as input and creates
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * a multi-dimensional array with a depth equal to the number of
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300155 * columns. This allows a single array with many elements to be
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * displayed in a table that has a fixed column count.
157 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200158 * @param array $array
159 * @param int $col_limit
160 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200162 public function make_columns($array = array(), $col_limit = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700164 if ( ! is_array($array) OR count($array) === 0 OR ! is_int($col_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
166 return FALSE;
167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
169 // Turn off the auto-heading feature since it's doubtful we
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 // will want headings from a one-dimensional array
171 $this->auto_heading = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200172
Alex Bilbied261b1e2012-06-02 11:12:16 +0100173 if ($col_limit === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 return $array;
176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 $new = array();
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200179 do
Barry Mienydd671972010-10-04 16:33:58 +0200180 {
181 $temp = array_splice($array, 0, $col_limit);
182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 if (count($temp) < $col_limit)
184 {
185 for ($i = count($temp); $i < $col_limit; $i++)
186 {
187 $temp[] = '&nbsp;';
188 }
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 $new[] = $temp;
192 }
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200193 while (count($array) > 0);
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 return $new;
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Set "empty" cells
202 *
203 * Can be passed as an array or discreet params
204 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200205 * @param mixed $value
206 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200208 public function set_empty($value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 $this->empty_cells = $value;
Andrey Andreev1f590952014-02-08 19:50:26 +0200211 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // --------------------------------------------------------------------
215
216 /**
217 * Add a table row
218 *
219 * Can be passed as an array or discreet params
220 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 * @param mixed
Andrey Andreev1f590952014-02-08 19:50:26 +0200222 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 */
Timothy Warren3182a762012-04-26 18:09:25 -0400224 public function add_row($args = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Andrey Andreev3fcc3f62014-02-11 17:28:36 +0200226 $this->rows[] = $this->_prep_args(func_get_args());
Andrey Andreev1f590952014-02-08 19:50:26 +0200227 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
229
230 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Jones7b5b0e22010-03-02 22:48:53 -0600232 /**
233 * Prep Args
234 *
235 * Ensures a standard associative array format for all cell data
236 *
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300237 * @param array
238 * @return array
Derek Jones7b5b0e22010-03-02 22:48:53 -0600239 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200240 protected function _prep_args($args)
Derek Jones7b5b0e22010-03-02 22:48:53 -0600241 {
242 // If there is no $args[0], skip this and treat as an associative array
243 // This can happen if there is only a single key, for example this is passed to table->generate
244 // array(array('foo'=>'bar'))
Andrey Andreevf0b69a82014-02-11 17:56:44 +0200245 if (isset($args[0]) && count($args) === 1 && is_array($args[0]) && ! isset($args[0]['data']))
Derek Jones7b5b0e22010-03-02 22:48:53 -0600246 {
Andrey Andreevf0b69a82014-02-11 17:56:44 +0200247 $args = $args[0];
Derek Jones7b5b0e22010-03-02 22:48:53 -0600248 }
Andrey Andreevf0b69a82014-02-11 17:56:44 +0200249
250 foreach ($args as $key => $val)
Derek Jones7b5b0e22010-03-02 22:48:53 -0600251 {
Andrey Andreevf0b69a82014-02-11 17:56:44 +0200252 is_array($val) OR $args[$key] = array('data' => $val);
Derek Jones7b5b0e22010-03-02 22:48:53 -0600253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Jones7b5b0e22010-03-02 22:48:53 -0600255 return $args;
256 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000257
Derek Jones7b5b0e22010-03-02 22:48:53 -0600258 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 /**
261 * Add a table caption
262 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200263 * @param string $caption
264 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200266 public function set_caption($caption)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 $this->caption = $caption;
Barry Mienydd671972010-10-04 16:33:58 +0200269 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000270
271 // --------------------------------------------------------------------
272
273 /**
274 * Generate the table
275 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200276 * @param mixed $table_data
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @return string
278 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200279 public function generate($table_data = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 // The table data can optionally be passed to this function
282 // either as a database result object or an array
Andrey Andreev05983fc2014-02-11 16:51:43 +0200283 if ( ! empty($table_data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200285 if ($table_data instanceof CI_DB_result)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200287 $this->_set_from_db_result($table_data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
289 elseif (is_array($table_data))
290 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200291 $this->_set_from_array($table_data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300295 // Is there anything to display? No? Smite them!
Andrey Andreev05983fc2014-02-11 16:51:43 +0200296 if (empty($this->heading) && empty($this->rows))
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 return 'Undefined table data';
299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // Compile and validate the template date
302 $this->_compile_template();
Barry Mienydd671972010-10-04 16:33:58 +0200303
Andrey Andreev05983fc2014-02-11 16:51:43 +0200304 // Validate a possibly existing custom cell manipulation function
Andrey Andreeva49c8ad2014-02-11 17:15:07 +0200305 if (isset($this->function) && ! is_callable($this->function))
Andrey Andreev05983fc2014-02-11 16:51:43 +0200306 {
Andrey Andreeva49c8ad2014-02-11 17:15:07 +0200307 $this->function = NULL;
Andrey Andreev05983fc2014-02-11 16:51:43 +0200308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // Build the table!
Barry Mienydd671972010-10-04 16:33:58 +0200311
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200312 $out = $this->template['table_open'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000313
314 // Add any caption here
315 if ($this->caption)
316 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200317 $out .= '<caption>'.$this->caption.'</caption>'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
319
320 // Is there a table heading to display?
Andrey Andreev05983fc2014-02-11 16:51:43 +0200321 if ( ! empty($this->heading))
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200323 $out .= $this->template['thead_open'].$this->newline.$this->template['heading_row_start'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000324
Pascal Kriete14287f32011-02-14 13:39:34 -0500325 foreach ($this->heading as $heading)
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600327 $temp = $this->template['heading_cell_start'];
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Jones7b5b0e22010-03-02 22:48:53 -0600329 foreach ($heading as $key => $val)
330 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100331 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600332 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300333 $temp = str_replace('<th', '<th '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200334 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600335 }
336
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200337 $out .= $temp.(isset($heading['data']) ? $heading['data'] : '').$this->template['heading_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200340 $out .= $this->template['heading_row_end'].$this->newline.$this->template['thead_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // Build the table rows
Andrey Andreev05983fc2014-02-11 16:51:43 +0200344 if ( ! empty($this->rows))
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200346 $out .= $this->template['tbody_open'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $i = 1;
Pascal Kriete14287f32011-02-14 13:39:34 -0500349 foreach ($this->rows as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 if ( ! is_array($row))
352 {
353 break;
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // We use modulus to alternate the row colors
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300357 $name = fmod($i++, 2) ? '' : 'alt_';
Barry Mienydd671972010-10-04 16:33:58 +0200358
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200359 $out .= $this->template['row_'.$name.'start'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200360
Pascal Kriete14287f32011-02-14 13:39:34 -0500361 foreach ($row as $cell)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600363 $temp = $this->template['cell_'.$name.'start'];
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Jones7b5b0e22010-03-02 22:48:53 -0600365 foreach ($cell as $key => $val)
366 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200367 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600368 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300369 $temp = str_replace('<td', '<td '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200370 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Jones7b5b0e22010-03-02 22:48:53 -0600373 $cell = isset($cell['data']) ? $cell['data'] : '';
374 $out .= $temp;
375
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300376 if ($cell === '' OR $cell === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 $out .= $this->empty_cells;
379 }
Andrey Andreeva49c8ad2014-02-11 17:15:07 +0200380 elseif (isset($this->function))
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300381 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200382 $out .= call_user_func($this->function, $cell);
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300383 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 else
385 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300386 $out .= $cell;
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 $out .= $this->template['cell_'.$name.'end'];
390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200392 $out .= $this->template['row_'.$name.'end'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200395 $out .= $this->template['tbody_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397
398 $out .= $this->template['table_close'];
Barry Mienydd671972010-10-04 16:33:58 +0200399
Greg Aker02b3a5b2011-02-01 01:29:21 -0600400 // Clear table class properties before generating the table
401 $this->clear();
402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return $out;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // --------------------------------------------------------------------
407
408 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500409 * Clears the table arrays. Useful if multiple tables are being generated
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 *
Andrey Andreev1f590952014-02-08 19:50:26 +0200411 * @return CI_Table
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200413 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreev1f590952014-02-08 19:50:26 +0200415 $this->rows = array();
416 $this->heading = array();
417 $this->auto_heading = TRUE;
418 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // --------------------------------------------------------------------
422
423 /**
424 * Set table data from a database result object
425 *
Andrey Andreev05983fc2014-02-11 16:51:43 +0200426 * @param CI_DB_result $db_result Database result object
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @return void
428 */
Andrey Andreev05983fc2014-02-11 16:51:43 +0200429 protected function _set_from_db_result($object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // First generate the headings from the table column names
Andrey Andreev05983fc2014-02-11 16:51:43 +0200432 if ($this->auto_heading === TRUE && empty($this->heading))
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200434 $this->heading = $this->_prep_args($object->list_fields());
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Andrey Andreev05983fc2014-02-11 16:51:43 +0200437 foreach ($object->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200439 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Set table data from an array
447 *
Andrey Andreev05983fc2014-02-11 16:51:43 +0200448 * @param array $data
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 * @return void
450 */
Andrey Andreev05983fc2014-02-11 16:51:43 +0200451 protected function _set_from_array($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200453 if ($this->auto_heading === TRUE && empty($this->heading))
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200455 $this->heading = $this->_prep_args(array_shift($data));
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Andrey Andreev05983fc2014-02-11 16:51:43 +0200458 foreach ($data as &$row)
Barry Mienydd671972010-10-04 16:33:58 +0200459 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200460 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Compile Template
468 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * @return void
470 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200471 protected function _compile_template()
Barry Mienydd671972010-10-04 16:33:58 +0200472 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100473 if ($this->template === NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200474 {
475 $this->template = $this->_default_template();
476 return;
477 }
478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 $this->temp = $this->_default_template();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600480 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 +0000481 {
482 if ( ! isset($this->template[$val]))
483 {
484 $this->template[$val] = $this->temp[$val];
485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486 }
487 }
488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
490
491 /**
492 * Default Template
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @return void
495 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200496 protected function _default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200498 return array(
Andrey Andreev1f590952014-02-08 19:50:26 +0200499 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
Barry Mienydd671972010-10-04 16:33:58 +0200500
Andrey Andreev1f590952014-02-08 19:50:26 +0200501 'thead_open' => '<thead>',
502 'thead_close' => '</thead>',
Barry Mienydd671972010-10-04 16:33:58 +0200503
Andrey Andreev1f590952014-02-08 19:50:26 +0200504 'heading_row_start' => '<tr>',
505 'heading_row_end' => '</tr>',
506 'heading_cell_start' => '<th>',
507 'heading_cell_end' => '</th>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000508
Andrey Andreev1f590952014-02-08 19:50:26 +0200509 'tbody_open' => '<tbody>',
510 'tbody_close' => '</tbody>',
Barry Mienydd671972010-10-04 16:33:58 +0200511
Andrey Andreev1f590952014-02-08 19:50:26 +0200512 'row_start' => '<tr>',
513 'row_end' => '</tr>',
514 'cell_start' => '<td>',
515 'cell_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000516
Andrey Andreev1f590952014-02-08 19:50:26 +0200517 'row_alt_start' => '<tr>',
518 'row_alt_end' => '</tr>',
519 'cell_alt_start' => '<td>',
520 'cell_alt_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000521
Andrey Andreev1f590952014-02-08 19:50:26 +0200522 'table_close' => '</table>'
523 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526}
527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528/* End of file Table.php */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300529/* Location: ./system/libraries/Table.php */