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