blob: 992b057ad496720ca1884050e953f3a1f8581471 [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
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030041 public $rows = array();
42 public $heading = array();
43 public $auto_heading = TRUE;
44 public $caption = NULL;
45 public $template = NULL;
46 public $newline = "\n";
47 public $empty_cells = '';
48 public $function = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020049
Mike Funk46e3a9a2012-02-24 09:38:35 -050050 /**
51 * Set the template from the table config file if it exists
Andrey Andreev80295322012-03-12 16:29:16 +020052 *
Mike Funk5fbaf272012-03-12 10:19:46 -040053 * @param array $config (default: array())
Mike Funkaa20f5b2012-02-28 13:43:16 -050054 * @return void
Mike Funk46e3a9a2012-02-24 09:38:35 -050055 */
56 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000057 {
Mike Funk46e3a9a2012-02-24 09:38:35 -050058 // initialize config
59 foreach ($config as $key => $val)
60 {
61 $this->template[$key] = $val;
62 }
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030063
64 log_message('debug', 'Table Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000065 }
66
67 // --------------------------------------------------------------------
68
69 /**
70 * Set the template
71 *
Derek Allard2067d1a2008-11-13 22:59:24 +000072 * @param array
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030073 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +000074 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +020075 public function set_template($template)
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
77 if ( ! is_array($template))
78 {
79 return FALSE;
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 $this->template = $template;
Andrey Andreev2b39d9d2012-04-03 19:14:23 +030083 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000084 }
85
86 // --------------------------------------------------------------------
87
88 /**
89 * Set the table heading
90 *
91 * Can be passed as an array or discreet params
92 *
Derek Allard2067d1a2008-11-13 22:59:24 +000093 * @param mixed
94 * @return void
95 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +020096 public function set_heading()
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
98 $args = func_get_args();
Derek Jones7b5b0e22010-03-02 22:48:53 -060099 $this->heading = $this->_prep_args($args);
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
101
102 // --------------------------------------------------------------------
103
104 /**
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300105 * Set columns. Takes a one-dimensional array as input and creates
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * a multi-dimensional array with a depth equal to the number of
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300107 * columns. This allows a single array with many elements to be
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 * displayed in a table that has a fixed column count.
109 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 * @param array
111 * @param int
112 * @return void
113 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200114 public function make_columns($array = array(), $col_limit = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700116 if ( ! is_array($array) OR count($array) === 0 OR ! is_int($col_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
118 return FALSE;
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
121 // Turn off the auto-heading feature since it's doubtful we
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // will want headings from a one-dimensional array
123 $this->auto_heading = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 if ($col_limit == 0)
126 {
127 return $array;
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 $new = array();
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200131 do
Barry Mienydd671972010-10-04 16:33:58 +0200132 {
133 $temp = array_splice($array, 0, $col_limit);
134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 if (count($temp) < $col_limit)
136 {
137 for ($i = count($temp); $i < $col_limit; $i++)
138 {
139 $temp[] = '&nbsp;';
140 }
141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 $new[] = $temp;
144 }
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200145 while (count($array) > 0);
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 return $new;
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Set "empty" cells
154 *
155 * Can be passed as an array or discreet params
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param mixed
158 * @return void
159 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200160 public function set_empty($value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 $this->empty_cells = $value;
163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // --------------------------------------------------------------------
166
167 /**
168 * Add a table row
169 *
170 * Can be passed as an array or discreet params
171 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @param mixed
173 * @return void
174 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200175 public function add_row()
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 $args = func_get_args();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600178 $this->rows[] = $this->_prep_args($args);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180
181 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Jones7b5b0e22010-03-02 22:48:53 -0600183 /**
184 * Prep Args
185 *
186 * Ensures a standard associative array format for all cell data
187 *
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300188 * @param array
189 * @return array
Derek Jones7b5b0e22010-03-02 22:48:53 -0600190 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200191 protected function _prep_args($args)
Derek Jones7b5b0e22010-03-02 22:48:53 -0600192 {
193 // If there is no $args[0], skip this and treat as an associative array
194 // This can happen if there is only a single key, for example this is passed to table->generate
195 // array(array('foo'=>'bar'))
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300196 if (isset($args[0]) && count($args) === 1 && is_array($args[0]))
Derek Jones7b5b0e22010-03-02 22:48:53 -0600197 {
198 // args sent as indexed array
199 if ( ! isset($args[0]['data']))
200 {
201 foreach ($args[0] as $key => $val)
202 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300203 $args[$key] = (is_array($val) && isset($val['data'])) ? $val : array('data' => $val);
Barry Mienydd671972010-10-04 16:33:58 +0200204 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600205 }
206 }
207 else
208 {
209 foreach ($args as $key => $val)
210 {
211 if ( ! is_array($val))
212 {
213 $args[$key] = array('data' => $val);
214 }
215 }
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Jones7b5b0e22010-03-02 22:48:53 -0600218 return $args;
219 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000220
Derek Jones7b5b0e22010-03-02 22:48:53 -0600221 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 /**
224 * Add a table caption
225 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @param string
227 * @return void
228 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200229 public function set_caption($caption)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
231 $this->caption = $caption;
Barry Mienydd671972010-10-04 16:33:58 +0200232 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000233
234 // --------------------------------------------------------------------
235
236 /**
237 * Generate the table
238 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 * @param mixed
240 * @return string
241 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200242 public function generate($table_data = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
244 // The table data can optionally be passed to this function
245 // either as a database result object or an array
246 if ( ! is_null($table_data))
247 {
248 if (is_object($table_data))
249 {
250 $this->_set_from_object($table_data);
251 }
252 elseif (is_array($table_data))
253 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200254 $set_heading = (count($this->heading) === 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 $this->_set_from_array($table_data, $set_heading);
256 }
257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300259 // Is there anything to display? No? Smite them!
260 if (count($this->heading) === 0 && count($this->rows) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
262 return 'Undefined table data';
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // Compile and validate the template date
266 $this->_compile_template();
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Jones7b5b0e22010-03-02 22:48:53 -0600268 // set a custom cell manipulation function to a locally scoped variable so its callable
269 $function = $this->function;
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // Build the table!
Barry Mienydd671972010-10-04 16:33:58 +0200272
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200273 $out = $this->template['table_open'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
275 // Add any caption here
276 if ($this->caption)
277 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200278 $out .= $this->newline.'<caption>'.$this->caption.'</caption>'.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 // Is there a table heading to display?
282 if (count($this->heading) > 0)
283 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200284 $out .= $this->template['thead_open'].$this->newline.$this->template['heading_row_start'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000285
Pascal Kriete14287f32011-02-14 13:39:34 -0500286 foreach ($this->heading as $heading)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600288 $temp = $this->template['heading_cell_start'];
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Jones7b5b0e22010-03-02 22:48:53 -0600290 foreach ($heading as $key => $val)
291 {
292 if ($key != 'data')
293 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300294 $temp = str_replace('<th', '<th '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200295 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600296 }
297
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200298 $out .= $temp.(isset($heading['data']) ? $heading['data'] : '').$this->template['heading_cell_end'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200301 $out .= $this->template['heading_row_end'].$this->newline.$this->template['thead_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // Build the table rows
305 if (count($this->rows) > 0)
306 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200307 $out .= $this->template['tbody_open'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 $i = 1;
Pascal Kriete14287f32011-02-14 13:39:34 -0500310 foreach ($this->rows as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
312 if ( ! is_array($row))
313 {
314 break;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // We use modulus to alternate the row colors
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300318 $name = fmod($i++, 2) ? '' : 'alt_';
Barry Mienydd671972010-10-04 16:33:58 +0200319
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200320 $out .= $this->template['row_'.$name.'start'].$this->newline;
Barry Mienydd671972010-10-04 16:33:58 +0200321
Pascal Kriete14287f32011-02-14 13:39:34 -0500322 foreach ($row as $cell)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600324 $temp = $this->template['cell_'.$name.'start'];
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Jones7b5b0e22010-03-02 22:48:53 -0600326 foreach ($cell as $key => $val)
327 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200328 if ($key !== 'data')
Derek Jones7b5b0e22010-03-02 22:48:53 -0600329 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300330 $temp = str_replace('<td', '<td '.$key.'="'.$val.'"', $temp);
Barry Mienydd671972010-10-04 16:33:58 +0200331 }
Derek Jones7b5b0e22010-03-02 22:48:53 -0600332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Jones7b5b0e22010-03-02 22:48:53 -0600334 $cell = isset($cell['data']) ? $cell['data'] : '';
335 $out .= $temp;
336
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300337 if ($cell === '' OR $cell === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $out .= $this->empty_cells;
340 }
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300341 elseif ($function !== FALSE && is_callable($function))
342 {
343 $out .= call_user_func($function, $cell);
344 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 else
346 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300347 $out .= $cell;
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 $out .= $this->template['cell_'.$name.'end'];
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200353 $out .= $this->template['row_'.$name.'end'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200356 $out .= $this->template['tbody_close'].$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
358
359 $out .= $this->template['table_close'];
Barry Mienydd671972010-10-04 16:33:58 +0200360
Greg Aker02b3a5b2011-02-01 01:29:21 -0600361 // Clear table class properties before generating the table
362 $this->clear();
363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 return $out;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // --------------------------------------------------------------------
368
369 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500370 * Clears the table arrays. Useful if multiple tables are being generated
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @return void
373 */
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200374 public function clear()
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300376 $this->rows = array();
377 $this->heading = array();
378 $this->auto_heading = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // --------------------------------------------------------------------
382
383 /**
384 * Set table data from a database result object
385 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @param object
387 * @return void
388 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200389 protected function _set_from_object($query)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 if ( ! is_object($query))
392 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300393 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // First generate the headings from the table column names
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200397 if (count($this->heading) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700399 if ( ! is_callable(array($query, 'list_fields')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300401 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Jones7b5b0e22010-03-02 22:48:53 -0600404 $this->heading = $this->_prep_args($query->list_fields());
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // Next blast through the result array and build out the rows
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 if ($query->num_rows() > 0)
409 {
410 foreach ($query->result_array() as $row)
411 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600412 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414 }
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Set table data from an array
421 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 * @param array
423 * @return void
424 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200425 protected function _set_from_array($data, $set_heading = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Andrey Andreevfe9b9a92011-12-25 16:11:01 +0200427 if ( ! is_array($data) OR count($data) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 return FALSE;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 $i = 0;
433 foreach ($data as $row)
Barry Mienydd671972010-10-04 16:33:58 +0200434 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // If a heading hasn't already been set we'll use the first row of the array as the heading
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300436 if ($i++ === 0 && count($data) > 1 && count($this->heading) === 0 && $set_heading == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600438 $this->heading = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440 else
441 {
Derek Jones7b5b0e22010-03-02 22:48:53 -0600442 $this->rows[] = $this->_prep_args($row);
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445 }
446
447 // --------------------------------------------------------------------
448
449 /**
450 * Compile Template
451 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @return void
453 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200454 protected function _compile_template()
Barry Mienydd671972010-10-04 16:33:58 +0200455 {
456 if ($this->template == NULL)
457 {
458 $this->template = $this->_default_template();
459 return;
460 }
461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 $this->temp = $this->_default_template();
Derek Jones7b5b0e22010-03-02 22:48:53 -0600463 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 +0000464 {
465 if ( ! isset($this->template[$val]))
466 {
467 $this->template[$val] = $this->temp[$val];
468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469 }
470 }
471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 // --------------------------------------------------------------------
473
474 /**
475 * Default Template
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @return void
478 */
Andrey Andreev49ddaa32011-12-26 16:54:10 +0200479 protected function _default_template()
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300481 return array(
482 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
Barry Mienydd671972010-10-04 16:33:58 +0200483
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300484 'thead_open' => '<thead>',
485 'thead_close' => '</thead>',
Barry Mienydd671972010-10-04 16:33:58 +0200486
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300487 'heading_row_start' => '<tr>',
488 'heading_row_end' => '</tr>',
489 'heading_cell_start' => '<th>',
490 'heading_cell_end' => '</th>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000491
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300492 'tbody_open' => '<tbody>',
493 'tbody_close' => '</tbody>',
Barry Mienydd671972010-10-04 16:33:58 +0200494
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300495 'row_start' => '<tr>',
496 'row_end' => '</tr>',
497 'cell_start' => '<td>',
498 'cell_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300500 'row_alt_start' => '<tr>',
501 'row_alt_end' => '</tr>',
502 'cell_alt_start' => '<td>',
503 'cell_alt_end' => '</td>',
Derek Allard2067d1a2008-11-13 22:59:24 +0000504
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300505 'table_close' => '</table>'
506 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509}
510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511/* End of file Table.php */
Andrey Andreev2b39d9d2012-04-03 19:14:23 +0300512/* Location: ./system/libraries/Table.php */