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