Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.3.1 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * HTML Table Generating Class |
| 32 | * |
| 33 | * Lets you create tables manually or from database result objects, or arrays. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Libraries |
| 37 | * @category HTML Tables |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Gerry | 6b59089 | 2011-09-25 00:16:39 +0800 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/libraries/table.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | */ |
| 41 | class CI_Table { |
| 42 | |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 43 | public $rows = array(); |
| 44 | public $heading = array(); |
| 45 | public $auto_heading = TRUE; |
| 46 | public $caption = NULL; |
| 47 | public $template = NULL; |
| 48 | public $newline = "\n"; |
| 49 | public $empty_cells = ''; |
| 50 | public $function = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 51 | |
Mike Funk | 46e3a9a | 2012-02-24 09:38:35 -0500 | [diff] [blame] | 52 | // -------------------------------------------------------------------------- |
| 53 | |
| 54 | /** |
| 55 | * Set the template from the table config file if it exists |
| 56 | * |
Mike Funk | aa20f5b | 2012-02-28 13:43:16 -0500 | [diff] [blame^] | 57 | * @param array $config (default: array()) |
| 58 | * @return void |
Mike Funk | 46e3a9a | 2012-02-24 09:38:35 -0500 | [diff] [blame] | 59 | */ |
| 60 | public function __construct($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | { |
| 62 | log_message('debug', "Table Class Initialized"); |
Mike Funk | 46e3a9a | 2012-02-24 09:38:35 -0500 | [diff] [blame] | 63 | |
| 64 | // initialize config |
| 65 | foreach ($config as $key => $val) |
| 66 | { |
| 67 | $this->template[$key] = $val; |
| 68 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // -------------------------------------------------------------------- |
| 72 | |
| 73 | /** |
| 74 | * Set the template |
| 75 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | * @param array |
| 77 | * @return void |
| 78 | */ |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 79 | public function set_template($template) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | { |
| 81 | if ( ! is_array($template)) |
| 82 | { |
| 83 | return FALSE; |
| 84 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | $this->template = $template; |
| 87 | } |
| 88 | |
| 89 | // -------------------------------------------------------------------- |
| 90 | |
| 91 | /** |
| 92 | * Set the table heading |
| 93 | * |
| 94 | * Can be passed as an array or discreet params |
| 95 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | * @param mixed |
| 97 | * @return void |
| 98 | */ |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 99 | public function set_heading() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | { |
| 101 | $args = func_get_args(); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 102 | $this->heading = $this->_prep_args($args); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 108 | * Set columns. Takes a one-dimensional array as input and creates |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | * a multi-dimensional array with a depth equal to the number of |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 110 | * columns. This allows a single array with many elements to be |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | * displayed in a table that has a fixed column count. |
| 112 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | * @param array |
| 114 | * @param int |
| 115 | * @return void |
| 116 | */ |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 117 | public function make_columns($array = array(), $col_limit = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | { |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 119 | if ( ! is_array($array) OR count($array) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | { |
| 121 | return FALSE; |
| 122 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
| 124 | // Turn off the auto-heading feature since it's doubtful we |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | // will want headings from a one-dimensional array |
| 126 | $this->auto_heading = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | if ($col_limit == 0) |
| 129 | { |
| 130 | return $array; |
| 131 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | $new = array(); |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 134 | do |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | { |
| 136 | $temp = array_splice($array, 0, $col_limit); |
| 137 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | if (count($temp) < $col_limit) |
| 139 | { |
| 140 | for ($i = count($temp); $i < $col_limit; $i++) |
| 141 | { |
| 142 | $temp[] = ' '; |
| 143 | } |
| 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | $new[] = $temp; |
| 147 | } |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 148 | while (count($array) > 0); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | return $new; |
| 151 | } |
| 152 | |
| 153 | // -------------------------------------------------------------------- |
| 154 | |
| 155 | /** |
| 156 | * Set "empty" cells |
| 157 | * |
| 158 | * Can be passed as an array or discreet params |
| 159 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | * @param mixed |
| 161 | * @return void |
| 162 | */ |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 163 | public function set_empty($value) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | { |
| 165 | $this->empty_cells = $value; |
| 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | /** |
| 171 | * Add a table row |
| 172 | * |
| 173 | * Can be passed as an array or discreet params |
| 174 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | * @param mixed |
| 176 | * @return void |
| 177 | */ |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 178 | public function add_row() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | { |
| 180 | $args = func_get_args(); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 181 | $this->rows[] = $this->_prep_args($args); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 186 | /** |
| 187 | * Prep Args |
| 188 | * |
| 189 | * Ensures a standard associative array format for all cell data |
| 190 | * |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 191 | * @param type |
| 192 | * @return type |
| 193 | */ |
Andrey Andreev | 49ddaa3 | 2011-12-26 16:54:10 +0200 | [diff] [blame] | 194 | protected function _prep_args($args) |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 195 | { |
| 196 | // If there is no $args[0], skip this and treat as an associative array |
| 197 | // This can happen if there is only a single key, for example this is passed to table->generate |
| 198 | // array(array('foo'=>'bar')) |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 199 | if (isset($args[0]) AND (count($args) === 1 && is_array($args[0]))) |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 200 | { |
| 201 | // args sent as indexed array |
| 202 | if ( ! isset($args[0]['data'])) |
| 203 | { |
| 204 | foreach ($args[0] as $key => $val) |
| 205 | { |
| 206 | if (is_array($val) && isset($val['data'])) |
| 207 | { |
| 208 | $args[$key] = $val; |
| 209 | } |
| 210 | else |
| 211 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 212 | $args[$key] = array('data' => $val); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 213 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 214 | } |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | foreach ($args as $key => $val) |
| 220 | { |
| 221 | if ( ! is_array($val)) |
| 222 | { |
| 223 | $args[$key] = array('data' => $val); |
| 224 | } |
| 225 | } |
| 226 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 228 | return $args; |
| 229 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 231 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | /** |
| 234 | * Add a table caption |
| 235 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | * @param string |
| 237 | * @return void |
| 238 | */ |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 239 | public function set_caption($caption) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | { |
| 241 | $this->caption = $caption; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
| 247 | * Generate the table |
| 248 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | * @param mixed |
| 250 | * @return string |
| 251 | */ |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 252 | public function generate($table_data = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | { |
| 254 | // The table data can optionally be passed to this function |
| 255 | // either as a database result object or an array |
| 256 | if ( ! is_null($table_data)) |
| 257 | { |
| 258 | if (is_object($table_data)) |
| 259 | { |
| 260 | $this->_set_from_object($table_data); |
| 261 | } |
| 262 | elseif (is_array($table_data)) |
| 263 | { |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 264 | $set_heading = (count($this->heading) === 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | $this->_set_from_array($table_data, $set_heading); |
| 266 | } |
| 267 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 268 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 269 | // Is there anything to display? No? Smite them! |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 270 | if (count($this->heading) === 0 AND count($this->rows) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | { |
| 272 | return 'Undefined table data'; |
| 273 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | // Compile and validate the template date |
| 276 | $this->_compile_template(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 278 | // set a custom cell manipulation function to a locally scoped variable so its callable |
| 279 | $function = $this->function; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 280 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | // Build the table! |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 283 | $out = $this->template['table_open'].$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | |
| 285 | // Add any caption here |
| 286 | if ($this->caption) |
| 287 | { |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 288 | $out .= $this->newline.'<caption>'.$this->caption.'</caption>'.$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Is there a table heading to display? |
| 292 | if (count($this->heading) > 0) |
| 293 | { |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 294 | $out .= $this->template['thead_open'].$this->newline.$this->template['heading_row_start'].$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 296 | foreach ($this->heading as $heading) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 298 | $temp = $this->template['heading_cell_start']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 300 | foreach ($heading as $key => $val) |
| 301 | { |
| 302 | if ($key != 'data') |
| 303 | { |
| 304 | $temp = str_replace('<th', "<th $key='$val'", $temp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 305 | } |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 306 | } |
| 307 | |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 308 | $out .= $temp.(isset($heading['data']) ? $heading['data'] : '').$this->template['heading_cell_end']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 311 | $out .= $this->template['heading_row_end'].$this->newline.$this->template['thead_close'].$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 313 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | // Build the table rows |
| 315 | if (count($this->rows) > 0) |
| 316 | { |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 317 | $out .= $this->template['tbody_open'].$this->newline; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | $i = 1; |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 320 | foreach ($this->rows as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | { |
| 322 | if ( ! is_array($row)) |
| 323 | { |
| 324 | break; |
| 325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | // We use modulus to alternate the row colors |
| 328 | $name = (fmod($i++, 2)) ? '' : 'alt_'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 330 | $out .= $this->template['row_'.$name.'start'].$this->newline; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 332 | foreach ($row as $cell) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 334 | $temp = $this->template['cell_'.$name.'start']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 336 | foreach ($cell as $key => $val) |
| 337 | { |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 338 | if ($key !== 'data') |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 339 | { |
| 340 | $temp = str_replace('<td', "<td $key='$val'", $temp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 341 | } |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 342 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 344 | $cell = isset($cell['data']) ? $cell['data'] : ''; |
| 345 | $out .= $temp; |
| 346 | |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 347 | if ($cell === "" OR $cell === NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | { |
| 349 | $out .= $this->empty_cells; |
| 350 | } |
| 351 | else |
| 352 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 353 | if ($function !== FALSE && is_callable($function)) |
| 354 | { |
Phil Sturgeon | 6c597f8 | 2010-12-27 17:35:35 +0000 | [diff] [blame] | 355 | $out .= call_user_func($function, $cell); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 356 | } |
| 357 | else |
| 358 | { |
| 359 | $out .= $cell; |
| 360 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | $out .= $this->template['cell_'.$name.'end']; |
| 364 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 365 | |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 366 | $out .= $this->template['row_'.$name.'end'].$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 368 | |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 369 | $out .= $this->template['tbody_close'].$this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | $out .= $this->template['table_close']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | |
Greg Aker | 02b3a5b | 2011-02-01 01:29:21 -0600 | [diff] [blame] | 374 | // Clear table class properties before generating the table |
| 375 | $this->clear(); |
| 376 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | return $out; |
| 378 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 379 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | // -------------------------------------------------------------------- |
| 381 | |
| 382 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 383 | * Clears the table arrays. Useful if multiple tables are being generated |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | * @return void |
| 386 | */ |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 387 | public function clear() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | { |
| 389 | $this->rows = array(); |
| 390 | $this->heading = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | $this->auto_heading = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | // -------------------------------------------------------------------- |
| 395 | |
| 396 | /** |
| 397 | * Set table data from a database result object |
| 398 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | * @param object |
| 400 | * @return void |
| 401 | */ |
Andrey Andreev | 49ddaa3 | 2011-12-26 16:54:10 +0200 | [diff] [blame] | 402 | protected function _set_from_object($query) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | { |
| 404 | if ( ! is_object($query)) |
| 405 | { |
| 406 | return FALSE; |
| 407 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | // First generate the headings from the table column names |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 410 | if (count($this->heading) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | { |
| 412 | if ( ! method_exists($query, 'list_fields')) |
| 413 | { |
| 414 | return FALSE; |
| 415 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 417 | $this->heading = $this->_prep_args($query->list_fields()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 419 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | // Next blast through the result array and build out the rows |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 421 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | if ($query->num_rows() > 0) |
| 423 | { |
| 424 | foreach ($query->result_array() as $row) |
| 425 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 426 | $this->rows[] = $this->_prep_args($row); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // -------------------------------------------------------------------- |
| 432 | |
| 433 | /** |
| 434 | * Set table data from an array |
| 435 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | * @param array |
| 437 | * @return void |
| 438 | */ |
Andrey Andreev | 49ddaa3 | 2011-12-26 16:54:10 +0200 | [diff] [blame] | 439 | protected function _set_from_array($data, $set_heading = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | { |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 441 | if ( ! is_array($data) OR count($data) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | { |
| 443 | return FALSE; |
| 444 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | $i = 0; |
| 447 | foreach ($data as $row) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 448 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | // If a heading hasn't already been set we'll use the first row of the array as the heading |
Andrey Andreev | fe9b9a9 | 2011-12-25 16:11:01 +0200 | [diff] [blame] | 450 | if ($i++ === 0 AND count($data) > 1 AND count($this->heading) === 0 AND $set_heading == TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 452 | $this->heading = $this->_prep_args($row); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | } |
| 454 | else |
| 455 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 456 | $this->rows[] = $this->_prep_args($row); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | // -------------------------------------------------------------------- |
| 462 | |
| 463 | /** |
| 464 | * Compile Template |
| 465 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | * @return void |
| 467 | */ |
Andrey Andreev | 49ddaa3 | 2011-12-26 16:54:10 +0200 | [diff] [blame] | 468 | protected function _compile_template() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | { |
| 470 | if ($this->template == NULL) |
| 471 | { |
| 472 | $this->template = $this->_default_template(); |
| 473 | return; |
| 474 | } |
| 475 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | $this->temp = $this->_default_template(); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 477 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | { |
| 479 | if ( ! isset($this->template[$val])) |
| 480 | { |
| 481 | $this->template[$val] = $this->temp[$val]; |
| 482 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | // -------------------------------------------------------------------- |
| 487 | |
| 488 | /** |
| 489 | * Default Template |
| 490 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | * @return void |
| 492 | */ |
Andrey Andreev | 49ddaa3 | 2011-12-26 16:54:10 +0200 | [diff] [blame] | 493 | protected function _default_template() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 495 | return array ( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 496 | 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', |
| 497 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 498 | 'thead_open' => '<thead>', |
| 499 | 'thead_close' => '</thead>', |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | |
| 501 | 'heading_row_start' => '<tr>', |
| 502 | 'heading_row_end' => '</tr>', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | 'heading_cell_start' => '<th>', |
| 504 | 'heading_cell_end' => '</th>', |
| 505 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 506 | 'tbody_open' => '<tbody>', |
| 507 | 'tbody_close' => '</tbody>', |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | |
| 509 | 'row_start' => '<tr>', |
| 510 | 'row_end' => '</tr>', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | 'cell_start' => '<td>', |
| 512 | 'cell_end' => '</td>', |
| 513 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 514 | 'row_alt_start' => '<tr>', |
| 515 | 'row_alt_end' => '</tr>', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 516 | 'cell_alt_start' => '<td>', |
| 517 | 'cell_alt_end' => '</td>', |
| 518 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 519 | 'table_close' => '</table>' |
| 520 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 521 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 522 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | |
| 524 | } |
| 525 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 526 | /* End of file Table.php */ |
Gerry | 6b59089 | 2011-09-25 00:16:39 +0800 | [diff] [blame] | 527 | /* Location: ./system/libraries/Table.php */ |