admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.3.1 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * HTML Table Generating Class |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 20 | * |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 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 Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/libraries/uri.html |
| 28 | */ |
| 29 | class CI_Table { |
| 30 | |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 31 | var $rows = array(); |
| 32 | var $heading = array(); |
| 33 | var $auto_heading = TRUE; |
| 34 | var $template = NULL; |
| 35 | var $newline = "\n"; |
| 36 | var $empty_cells = ""; |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | function CI_Table() |
| 40 | { |
| 41 | log_message('debug', "Table Class Initialized"); |
| 42 | } |
| 43 | |
| 44 | // -------------------------------------------------------------------- |
| 45 | |
| 46 | /** |
| 47 | * Set the template |
| 48 | * |
| 49 | * @access public |
| 50 | * @param array |
| 51 | * @return void |
| 52 | */ |
| 53 | function set_template($template) |
| 54 | { |
admin | 2799120 | 2006-10-11 01:38:08 +0000 | [diff] [blame] | 55 | if ( ! is_array($template)) |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 56 | { |
| 57 | return FALSE; |
| 58 | } |
| 59 | |
| 60 | $this->template = $template; |
| 61 | } |
| 62 | |
| 63 | // -------------------------------------------------------------------- |
| 64 | |
| 65 | /** |
admin | a1931ad | 2006-10-09 04:17:38 +0000 | [diff] [blame] | 66 | * Set the table heading |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 67 | * |
| 68 | * Can be passed as an array or discreet params |
| 69 | * |
| 70 | * @access public |
| 71 | * @param mixed |
| 72 | * @return void |
| 73 | */ |
admin | a1931ad | 2006-10-09 04:17:38 +0000 | [diff] [blame] | 74 | function set_heading() |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 75 | { |
| 76 | $args = func_get_args(); |
| 77 | $this->heading = (is_array($args[0])) ? $args[0] : $args; |
| 78 | } |
| 79 | |
| 80 | // -------------------------------------------------------------------- |
| 81 | |
| 82 | /** |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 83 | * Set columns. Takes a one-dimensional array as input and creates |
| 84 | * a multi-dimensional array with a depth equal to the number of |
| 85 | * columns. This allows a single array with many elements to be |
| 86 | * displayed in a table that has a fixed column count. |
| 87 | * |
| 88 | * @access public |
| 89 | * @param array |
| 90 | * @param int |
| 91 | * @return void |
| 92 | */ |
| 93 | function make_columns($array = array(), $col_limit = 0) |
| 94 | { |
| 95 | if ( ! is_array($array) OR count($array) == 0) |
| 96 | { |
| 97 | return FALSE; |
| 98 | } |
| 99 | |
| 100 | // Turn off the auto-heading feature since it's doubtful we |
| 101 | // will want headings from a one-dimensional array |
| 102 | $this->auto_heading = FALSE; |
| 103 | |
| 104 | if ($col_limit == 0) |
| 105 | { |
| 106 | return $array; |
| 107 | } |
| 108 | |
| 109 | $new = array(); |
| 110 | while(count($array) > 0) |
| 111 | { |
| 112 | $temp = array_slice($array, 0, $col_limit); |
| 113 | $array = array_diff($array, $temp); |
| 114 | |
| 115 | if (count($temp) < $col_limit) |
| 116 | { |
| 117 | for ($i = count($temp); $i < $col_limit; $i++) |
| 118 | { |
| 119 | $temp[] = ' '; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | $new[] = $temp; |
| 124 | } |
| 125 | |
| 126 | return $new; |
| 127 | } |
| 128 | |
| 129 | // -------------------------------------------------------------------- |
| 130 | |
| 131 | /** |
admin | 2bc1385 | 2006-10-24 23:16:17 +0000 | [diff] [blame] | 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 | } |
| 144 | |
| 145 | // -------------------------------------------------------------------- |
| 146 | |
| 147 | /** |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 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(); |
| 159 | $this->rows[] = (is_array($args[0])) ? $args[0] : $args; |
| 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Generate the table |
| 166 | * |
| 167 | * @access public |
| 168 | * @param mixed |
| 169 | * @return string |
| 170 | */ |
| 171 | function generate($table_data = NULL) |
| 172 | { |
| 173 | // The table data can optionally be passed to this function |
| 174 | // either as a database result object or an array |
| 175 | if ( ! is_null($table_data)) |
| 176 | { |
| 177 | if (is_object($table_data)) |
| 178 | { |
| 179 | $this->_set_from_object($table_data); |
| 180 | } |
| 181 | elseif (is_array($table_data)) |
| 182 | { |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 183 | $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE; |
| 184 | $this->_set_from_array($table_data, $set_heading); |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | // Is there anything to display? No? Smite them! |
| 189 | if (count($this->heading) == 0 AND count($this->rows) == 0) |
| 190 | { |
| 191 | return 'Undefined table data'; |
| 192 | } |
| 193 | |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 194 | // Compile and validate the template date |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 195 | $this->_compile_template(); |
| 196 | |
| 197 | |
| 198 | // Build the table! |
| 199 | |
| 200 | $out = $this->template['table_open']; |
| 201 | $out .= $this->newline; |
| 202 | |
| 203 | // Is there a table heading to display? |
| 204 | if (count($this->heading) > 0) |
| 205 | { |
| 206 | $out .= $this->template['heading_row_start']; |
| 207 | $out .= $this->newline; |
| 208 | |
| 209 | foreach($this->heading as $heading) |
| 210 | { |
| 211 | $out .= $this->template['heading_cell_start']; |
| 212 | $out .= $heading; |
| 213 | $out .= $this->template['heading_cell_end']; |
| 214 | } |
| 215 | |
| 216 | $out .= $this->template['heading_row_end']; |
| 217 | $out .= $this->newline; |
| 218 | } |
| 219 | |
| 220 | // Build the table rows |
| 221 | if (count($this->rows) > 0) |
| 222 | { |
| 223 | $i = 1; |
| 224 | foreach($this->rows as $row) |
| 225 | { |
| 226 | if ( ! is_array($row)) |
| 227 | { |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | // We use modulus to alternate the row colors |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 232 | $name = (fmod($i++, 2)) ? '' : 'alt_'; |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 233 | |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 234 | $out .= $this->template['row_'.$name.'start']; |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 235 | $out .= $this->newline; |
| 236 | |
admin | 2bc1385 | 2006-10-24 23:16:17 +0000 | [diff] [blame] | 237 | foreach($row as $cell) |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 238 | { |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 239 | $out .= $this->template['cell_'.$name.'start']; |
admin | 2bc1385 | 2006-10-24 23:16:17 +0000 | [diff] [blame] | 240 | |
| 241 | if ($cell == "") |
| 242 | { |
| 243 | $out .= $this->empty_cells; |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | $out .= $cell; |
| 248 | } |
| 249 | |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 250 | $out .= $this->template['cell_'.$name.'end']; |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 253 | $out .= $this->template['row_'.$name.'end']; |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 254 | $out .= $this->newline; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | $out .= $this->template['table_close']; |
| 259 | |
| 260 | return $out; |
| 261 | } |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 262 | |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 263 | // -------------------------------------------------------------------- |
| 264 | |
| 265 | /** |
admin | 41cece9 | 2006-10-28 19:06:45 +0000 | [diff] [blame] | 266 | * Clears the table arrays. Useful if multiple tables are beting generated |
| 267 | * |
| 268 | * @access public |
| 269 | * @return void |
| 270 | */ |
admin | f3a6204 | 2006-10-29 20:24:11 +0000 | [diff] [blame] | 271 | function clear() |
admin | 41cece9 | 2006-10-28 19:06:45 +0000 | [diff] [blame] | 272 | { |
| 273 | $this->rows = array(); |
| 274 | $this->heading = array(); |
| 275 | $this->auto_heading = TRUE; |
| 276 | } |
| 277 | |
| 278 | // -------------------------------------------------------------------- |
| 279 | |
| 280 | /** |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 281 | * Set table data from a database result object |
| 282 | * |
| 283 | * @access public |
| 284 | * @param object |
| 285 | * @return void |
| 286 | */ |
| 287 | function _set_from_object($query) |
| 288 | { |
| 289 | if ( ! is_object($query)) |
| 290 | { |
| 291 | return FALSE; |
| 292 | } |
| 293 | |
| 294 | // First generate the headings from the table column names |
| 295 | if (count($this->heading) == 0) |
| 296 | { |
admin | 606f99c | 2006-10-11 23:48:41 +0000 | [diff] [blame] | 297 | if ( ! method_exists($query, 'list_fields')) |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 298 | { |
| 299 | return FALSE; |
| 300 | } |
| 301 | |
admin | 606f99c | 2006-10-11 23:48:41 +0000 | [diff] [blame] | 302 | $this->heading = $query->list_fields(); |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Next blast through the result array and build out the rows |
admin | 4003718 | 2006-10-11 19:16:58 +0000 | [diff] [blame] | 306 | |
| 307 | if ($query->num_rows() > 0) |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 308 | { |
admin | 4003718 | 2006-10-11 19:16:58 +0000 | [diff] [blame] | 309 | foreach ($query->result_array() as $row) |
| 310 | { |
| 311 | $this->rows[] = $row; |
| 312 | } |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | // -------------------------------------------------------------------- |
| 317 | |
| 318 | /** |
| 319 | * Set table data from an array |
| 320 | * |
| 321 | * @access public |
| 322 | * @param array |
| 323 | * @return void |
| 324 | */ |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 325 | function _set_from_array($data, $set_heading = TRUE) |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 326 | { |
| 327 | if ( ! is_array($data) OR count($data) == 0) |
| 328 | { |
| 329 | return FALSE; |
| 330 | } |
| 331 | |
| 332 | $i = 0; |
| 333 | foreach ($data as $row) |
| 334 | { |
| 335 | if ( ! is_array($row)) |
| 336 | { |
| 337 | $this->rows[] = $data; |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | // If a heading hasn't already been set we'll use the first row of the array as the heading |
admin | 26364b9 | 2006-10-28 05:22:02 +0000 | [diff] [blame] | 342 | if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE) |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 343 | { |
| 344 | $this->heading = $row; |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | $this->rows[] = $row; |
| 349 | } |
| 350 | |
| 351 | $i++; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // -------------------------------------------------------------------- |
| 356 | |
| 357 | /** |
| 358 | * Compile Template |
| 359 | * |
| 360 | * @access private |
| 361 | * @return void |
| 362 | */ |
| 363 | function _compile_template() |
| 364 | { |
| 365 | if ($this->template == NULL) |
| 366 | { |
| 367 | $this->template = $this->_default_template(); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | $this->temp = $this->_default_template(); |
| 372 | foreach (array('table_open','heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val) |
| 373 | { |
| 374 | if ( ! isset($this->template[$val])) |
| 375 | { |
| 376 | $this->template[$val] = $this->temp[$val]; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // -------------------------------------------------------------------- |
| 382 | |
| 383 | /** |
| 384 | * Default Template |
| 385 | * |
| 386 | * @access private |
| 387 | * @return void |
| 388 | */ |
| 389 | function _default_template() |
| 390 | { |
| 391 | return array ( |
| 392 | 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', |
| 393 | |
| 394 | 'heading_row_start' => '<tr>', |
| 395 | 'heading_row_end' => '</tr>', |
| 396 | 'heading_cell_start' => '<th>', |
| 397 | 'heading_cell_end' => '</th>', |
| 398 | |
| 399 | 'row_start' => '<tr>', |
| 400 | 'row_end' => '</tr>', |
| 401 | 'cell_start' => '<td>', |
| 402 | 'cell_end' => '</td>', |
| 403 | |
| 404 | 'row_alt_start' => '<tr>', |
| 405 | 'row_alt_end' => '</tr>', |
| 406 | 'cell_alt_start' => '<td>', |
| 407 | 'cell_alt_end' => '</td>', |
| 408 | |
| 409 | 'table_close' => '</table>' |
| 410 | ); |
| 411 | } |
admin | a1931ad | 2006-10-09 04:17:38 +0000 | [diff] [blame] | 412 | |
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 413 | |
| 414 | } |
| 415 | |
| 416 | ?> |