Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [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 |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 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 | |
| 43 | var $rows = array(); |
| 44 | var $heading = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 45 | var $auto_heading = TRUE; |
| 46 | var $caption = NULL; |
| 47 | var $template = NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | var $newline = "\n"; |
| 49 | var $empty_cells = ""; |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 50 | var $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 | */ |
| 66 | function set_template($template) |
| 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 | */ |
| 87 | function set_heading() |
| 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 | */ |
| 106 | function make_columns($array = array(), $col_limit = 0) |
| 107 | { |
| 108 | if ( ! is_array($array) OR count($array) == 0) |
| 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(); |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 123 | while (count($array) > 0) |
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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | return $new; |
| 139 | } |
| 140 | |
| 141 | // -------------------------------------------------------------------- |
| 142 | |
| 143 | /** |
| 144 | * Set "empty" cells |
| 145 | * |
| 146 | * Can be passed as an array or discreet params |
| 147 | * |
| 148 | * @access public |
| 149 | * @param mixed |
| 150 | * @return void |
| 151 | */ |
| 152 | function set_empty($value) |
| 153 | { |
| 154 | $this->empty_cells = $value; |
| 155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | // -------------------------------------------------------------------- |
| 158 | |
| 159 | /** |
| 160 | * Add a table row |
| 161 | * |
| 162 | * Can be passed as an array or discreet params |
| 163 | * |
| 164 | * @access public |
| 165 | * @param mixed |
| 166 | * @return void |
| 167 | */ |
| 168 | function add_row() |
| 169 | { |
| 170 | $args = func_get_args(); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 171 | $this->rows[] = $this->_prep_args($args); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 176 | /** |
| 177 | * Prep Args |
| 178 | * |
| 179 | * Ensures a standard associative array format for all cell data |
| 180 | * |
| 181 | * @access public |
| 182 | * @param type |
| 183 | * @return type |
| 184 | */ |
| 185 | function _prep_args($args) |
| 186 | { |
| 187 | // If there is no $args[0], skip this and treat as an associative array |
| 188 | // This can happen if there is only a single key, for example this is passed to table->generate |
| 189 | // array(array('foo'=>'bar')) |
| 190 | if (isset($args[0]) AND (count($args) == 1 && is_array($args[0]))) |
| 191 | { |
| 192 | // args sent as indexed array |
| 193 | if ( ! isset($args[0]['data'])) |
| 194 | { |
| 195 | foreach ($args[0] as $key => $val) |
| 196 | { |
| 197 | if (is_array($val) && isset($val['data'])) |
| 198 | { |
| 199 | $args[$key] = $val; |
| 200 | } |
| 201 | else |
| 202 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | $args[$key] = array('data' => $val); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 204 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | } |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | foreach ($args as $key => $val) |
| 211 | { |
| 212 | if ( ! is_array($val)) |
| 213 | { |
| 214 | $args[$key] = array('data' => $val); |
| 215 | } |
| 216 | } |
| 217 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 219 | return $args; |
| 220 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 222 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | /** |
| 225 | * Add a table caption |
| 226 | * |
| 227 | * @access public |
| 228 | * @param string |
| 229 | * @return void |
| 230 | */ |
| 231 | function set_caption($caption) |
| 232 | { |
| 233 | $this->caption = $caption; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
| 239 | * Generate the table |
| 240 | * |
| 241 | * @access public |
| 242 | * @param mixed |
| 243 | * @return string |
| 244 | */ |
| 245 | function generate($table_data = NULL) |
| 246 | { |
| 247 | // The table data can optionally be passed to this function |
| 248 | // either as a database result object or an array |
| 249 | if ( ! is_null($table_data)) |
| 250 | { |
| 251 | if (is_object($table_data)) |
| 252 | { |
| 253 | $this->_set_from_object($table_data); |
| 254 | } |
| 255 | elseif (is_array($table_data)) |
| 256 | { |
| 257 | $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE; |
| 258 | $this->_set_from_array($table_data, $set_heading); |
| 259 | } |
| 260 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 262 | // Is there anything to display? No? Smite them! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | if (count($this->heading) == 0 AND count($this->rows) == 0) |
| 264 | { |
| 265 | return 'Undefined table data'; |
| 266 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | // Compile and validate the template date |
| 269 | $this->_compile_template(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 271 | // set a custom cell manipulation function to a locally scoped variable so its callable |
| 272 | $function = $this->function; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 273 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | // Build the table! |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | $out = $this->template['table_open']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | $out .= $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 | { |
| 282 | $out .= $this->newline; |
| 283 | $out .= '<caption>' . $this->caption . '</caption>'; |
| 284 | $out .= $this->newline; |
| 285 | } |
| 286 | |
| 287 | // Is there a table heading to display? |
| 288 | if (count($this->heading) > 0) |
| 289 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 290 | $out .= $this->template['thead_open']; |
| 291 | $out .= $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | $out .= $this->template['heading_row_start']; |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 293 | $out .= $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 295 | foreach ($this->heading as $heading) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 297 | $temp = $this->template['heading_cell_start']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 298 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 299 | foreach ($heading as $key => $val) |
| 300 | { |
| 301 | if ($key != 'data') |
| 302 | { |
| 303 | $temp = str_replace('<th', "<th $key='$val'", $temp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 304 | } |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 305 | } |
| 306 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | $out .= $temp; |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 308 | $out .= isset($heading['data']) ? $heading['data'] : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | $out .= $this->template['heading_cell_end']; |
| 310 | } |
| 311 | |
| 312 | $out .= $this->template['heading_row_end']; |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 313 | $out .= $this->newline; |
| 314 | $out .= $this->template['thead_close']; |
| 315 | $out .= $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | // Build the table rows |
| 319 | if (count($this->rows) > 0) |
| 320 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 321 | $out .= $this->template['tbody_open']; |
| 322 | $out .= $this->newline; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | $i = 1; |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 325 | foreach ($this->rows as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | { |
| 327 | if ( ! is_array($row)) |
| 328 | { |
| 329 | break; |
| 330 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | // We use modulus to alternate the row colors |
| 333 | $name = (fmod($i++, 2)) ? '' : 'alt_'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 334 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | $out .= $this->template['row_'.$name.'start']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 336 | $out .= $this->newline; |
| 337 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 338 | foreach ($row as $cell) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 340 | $temp = $this->template['cell_'.$name.'start']; |
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 | foreach ($cell as $key => $val) |
| 343 | { |
| 344 | if ($key != 'data') |
| 345 | { |
| 346 | $temp = str_replace('<td', "<td $key='$val'", $temp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 347 | } |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 348 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 349 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 350 | $cell = isset($cell['data']) ? $cell['data'] : ''; |
| 351 | $out .= $temp; |
| 352 | |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 353 | if ($cell === "" OR $cell === NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | { |
| 355 | $out .= $this->empty_cells; |
| 356 | } |
| 357 | else |
| 358 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 359 | if ($function !== FALSE && is_callable($function)) |
| 360 | { |
Phil Sturgeon | 6c597f8 | 2010-12-27 17:35:35 +0000 | [diff] [blame] | 361 | $out .= call_user_func($function, $cell); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 362 | } |
| 363 | else |
| 364 | { |
| 365 | $out .= $cell; |
| 366 | } |
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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | $out .= $this->template['cell_'.$name.'end']; |
| 370 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 371 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | $out .= $this->template['row_'.$name.'end']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | $out .= $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 375 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 376 | $out .= $this->template['tbody_close']; |
| 377 | $out .= $this->newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | $out .= $this->template['table_close']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
Greg Aker | 02b3a5b | 2011-02-01 01:29:21 -0600 | [diff] [blame] | 382 | // Clear table class properties before generating the table |
| 383 | $this->clear(); |
| 384 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | return $out; |
| 386 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 387 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | // -------------------------------------------------------------------- |
| 389 | |
| 390 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 391 | * Clears the table arrays. Useful if multiple tables are being generated |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | * |
| 393 | * @access public |
| 394 | * @return void |
| 395 | */ |
| 396 | function clear() |
| 397 | { |
| 398 | $this->rows = array(); |
| 399 | $this->heading = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | $this->auto_heading = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | // -------------------------------------------------------------------- |
| 404 | |
| 405 | /** |
| 406 | * Set table data from a database result object |
| 407 | * |
| 408 | * @access public |
| 409 | * @param object |
| 410 | * @return void |
| 411 | */ |
| 412 | function _set_from_object($query) |
| 413 | { |
| 414 | if ( ! is_object($query)) |
| 415 | { |
| 416 | return FALSE; |
| 417 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | // First generate the headings from the table column names |
| 420 | if (count($this->heading) == 0) |
| 421 | { |
| 422 | if ( ! method_exists($query, 'list_fields')) |
| 423 | { |
| 424 | return FALSE; |
| 425 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 427 | $this->heading = $this->_prep_args($query->list_fields()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 429 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | // Next blast through the result array and build out the rows |
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 | if ($query->num_rows() > 0) |
| 433 | { |
| 434 | foreach ($query->result_array() as $row) |
| 435 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 436 | $this->rows[] = $this->_prep_args($row); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // -------------------------------------------------------------------- |
| 442 | |
| 443 | /** |
| 444 | * Set table data from an array |
| 445 | * |
| 446 | * @access public |
| 447 | * @param array |
| 448 | * @return void |
| 449 | */ |
| 450 | function _set_from_array($data, $set_heading = TRUE) |
| 451 | { |
| 452 | if ( ! is_array($data) OR count($data) == 0) |
| 453 | { |
| 454 | return FALSE; |
| 455 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 456 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | $i = 0; |
| 458 | foreach ($data as $row) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | // If a heading hasn't already been set we'll use the first row of the array as the heading |
| 461 | if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE) |
| 462 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 463 | $this->heading = $this->_prep_args($row); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | } |
| 465 | else |
| 466 | { |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 467 | $this->rows[] = $this->_prep_args($row); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | $i++; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // -------------------------------------------------------------------- |
| 475 | |
| 476 | /** |
| 477 | * Compile Template |
| 478 | * |
| 479 | * @access private |
| 480 | * @return void |
| 481 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | function _compile_template() |
| 483 | { |
| 484 | if ($this->template == NULL) |
| 485 | { |
| 486 | $this->template = $this->_default_template(); |
| 487 | return; |
| 488 | } |
| 489 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | $this->temp = $this->_default_template(); |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 491 | 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] | 492 | { |
| 493 | if ( ! isset($this->template[$val])) |
| 494 | { |
| 495 | $this->template[$val] = $this->temp[$val]; |
| 496 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | // -------------------------------------------------------------------- |
| 501 | |
| 502 | /** |
| 503 | * Default Template |
| 504 | * |
| 505 | * @access private |
| 506 | * @return void |
| 507 | */ |
| 508 | function _default_template() |
| 509 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 510 | return array ( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 511 | 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', |
| 512 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 513 | 'thead_open' => '<thead>', |
| 514 | 'thead_close' => '</thead>', |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 515 | |
| 516 | 'heading_row_start' => '<tr>', |
| 517 | 'heading_row_end' => '</tr>', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 518 | 'heading_cell_start' => '<th>', |
| 519 | 'heading_cell_end' => '</th>', |
| 520 | |
Derek Jones | 7b5b0e2 | 2010-03-02 22:48:53 -0600 | [diff] [blame] | 521 | 'tbody_open' => '<tbody>', |
| 522 | 'tbody_close' => '</tbody>', |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | |
| 524 | 'row_start' => '<tr>', |
| 525 | 'row_end' => '</tr>', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 526 | 'cell_start' => '<td>', |
| 527 | 'cell_end' => '</td>', |
| 528 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 529 | 'row_alt_start' => '<tr>', |
| 530 | 'row_alt_end' => '</tr>', |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 531 | 'cell_alt_start' => '<td>', |
| 532 | 'cell_alt_end' => '</td>', |
| 533 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 534 | 'table_close' => '</table>' |
| 535 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 537 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | |
| 539 | } |
| 540 | |
| 541 | |
| 542 | /* End of file Table.php */ |
Gerry | 6b59089 | 2011-09-25 00:16:39 +0800 | [diff] [blame] | 543 | /* Location: ./system/libraries/Table.php */ |