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