Derek Allard | 5932c86 | 2007-01-27 17:44:54 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | 5932c86 | 2007-01-27 17:44:54 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
| 8 | * @author Rick Ellis
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | 5932c86 | 2007-01-27 17:44:54 +0000 | [diff] [blame] | 11 | * @link http://www.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 Rick Ellis
|
| 27 | * @link http://www.codeigniter.com/user_guide/libraries/uri.html
|
| 28 | */
|
| 29 | class CI_Table {
|
| 30 |
|
| 31 | var $rows = array();
|
| 32 | var $heading = array();
|
| 33 | var $auto_heading = TRUE;
|
| 34 | var $caption = NULL;
|
| 35 | var $template = NULL;
|
| 36 | var $newline = "\n";
|
| 37 | var $empty_cells = "";
|
| 38 |
|
| 39 |
|
| 40 | function CI_Table()
|
| 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 | }
|
| 60 |
|
| 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();
|
| 78 | $this->heading = (is_array($args[0])) ? $args[0] : $args;
|
| 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 | }
|
| 100 |
|
| 101 | // Turn off the auto-heading feature since it's doubtful we
|
| 102 | // will want headings from a one-dimensional array
|
| 103 | $this->auto_heading = FALSE;
|
| 104 |
|
| 105 | if ($col_limit == 0)
|
| 106 | {
|
| 107 | return $array;
|
| 108 | }
|
| 109 |
|
| 110 | $new = array();
|
| 111 | while(count($array) > 0)
|
| 112 | {
|
| 113 | $temp = array_slice($array, 0, $col_limit);
|
| 114 | $array = array_diff($array, $temp);
|
| 115 |
|
| 116 | if (count($temp) < $col_limit)
|
| 117 | {
|
| 118 | for ($i = count($temp); $i < $col_limit; $i++)
|
| 119 | {
|
| 120 | $temp[] = ' ';
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | $new[] = $temp;
|
| 125 | }
|
| 126 |
|
| 127 | return $new;
|
| 128 | }
|
| 129 |
|
| 130 | // --------------------------------------------------------------------
|
| 131 |
|
| 132 | /**
|
| 133 | * Set "empty" cells
|
| 134 | *
|
| 135 | * Can be passed as an array or discreet params
|
| 136 | *
|
| 137 | * @access public
|
| 138 | * @param mixed
|
| 139 | * @return void
|
| 140 | */
|
| 141 | function set_empty($value)
|
| 142 | {
|
| 143 | $this->empty_cells = $value;
|
| 144 | }
|
| 145 |
|
| 146 | // --------------------------------------------------------------------
|
| 147 |
|
| 148 | /**
|
| 149 | * Add a table row
|
| 150 | *
|
| 151 | * Can be passed as an array or discreet params
|
| 152 | *
|
| 153 | * @access public
|
| 154 | * @param mixed
|
| 155 | * @return void
|
| 156 | */
|
| 157 | function add_row()
|
| 158 | {
|
| 159 | $args = func_get_args();
|
| 160 | $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
|
| 161 | }
|
| 162 |
|
| 163 | // --------------------------------------------------------------------
|
| 164 |
|
| 165 | /**
|
| 166 | * Add a table caption
|
| 167 | *
|
| 168 | * @access public
|
| 169 | * @param string
|
| 170 | * @return void
|
| 171 | */
|
| 172 | function set_caption($caption)
|
| 173 | {
|
| 174 | $this->caption = $caption;
|
| 175 | }
|
| 176 |
|
| 177 | // --------------------------------------------------------------------
|
| 178 |
|
| 179 | /**
|
| 180 | * Generate the table
|
| 181 | *
|
| 182 | * @access public
|
| 183 | * @param mixed
|
| 184 | * @return string
|
| 185 | */
|
| 186 | function generate($table_data = NULL)
|
| 187 | {
|
| 188 | // The table data can optionally be passed to this function
|
| 189 | // either as a database result object or an array
|
| 190 | if ( ! is_null($table_data))
|
| 191 | {
|
| 192 | if (is_object($table_data))
|
| 193 | {
|
| 194 | $this->_set_from_object($table_data);
|
| 195 | }
|
| 196 | elseif (is_array($table_data))
|
| 197 | {
|
| 198 | $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
|
| 199 | $this->_set_from_array($table_data, $set_heading);
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | // Is there anything to display? No? Smite them!
|
| 204 | if (count($this->heading) == 0 AND count($this->rows) == 0)
|
| 205 | {
|
| 206 | return 'Undefined table data';
|
| 207 | }
|
| 208 |
|
| 209 | // Compile and validate the template date
|
| 210 | $this->_compile_template();
|
| 211 |
|
| 212 |
|
| 213 | // Build the table!
|
| 214 |
|
| 215 | $out = $this->template['table_open'];
|
| 216 | $out .= $this->newline;
|
| 217 |
|
| 218 | // Add any caption here
|
| 219 | if ($this->caption)
|
| 220 | {
|
| 221 | $out .= $this->newline;
|
| 222 | $out .= '<caption>' . $this->caption . '</caption>';
|
| 223 | $out .= $this->newline;
|
| 224 | }
|
| 225 |
|
| 226 | // Is there a table heading to display?
|
| 227 | if (count($this->heading) > 0)
|
| 228 | {
|
| 229 | $out .= $this->template['heading_row_start'];
|
| 230 | $out .= $this->newline;
|
| 231 |
|
| 232 | foreach($this->heading as $heading)
|
| 233 | {
|
| 234 | $out .= $this->template['heading_cell_start'];
|
| 235 | $out .= $heading;
|
| 236 | $out .= $this->template['heading_cell_end'];
|
| 237 | }
|
| 238 |
|
| 239 | $out .= $this->template['heading_row_end'];
|
| 240 | $out .= $this->newline;
|
| 241 | }
|
| 242 |
|
| 243 | // Build the table rows
|
| 244 | if (count($this->rows) > 0)
|
| 245 | {
|
| 246 | $i = 1;
|
| 247 | foreach($this->rows as $row)
|
| 248 | {
|
| 249 | if ( ! is_array($row))
|
| 250 | {
|
| 251 | break;
|
| 252 | }
|
| 253 |
|
| 254 | // We use modulus to alternate the row colors
|
| 255 | $name = (fmod($i++, 2)) ? '' : 'alt_';
|
| 256 |
|
| 257 | $out .= $this->template['row_'.$name.'start'];
|
| 258 | $out .= $this->newline;
|
| 259 |
|
| 260 | foreach($row as $cell)
|
| 261 | {
|
| 262 | $out .= $this->template['cell_'.$name.'start'];
|
| 263 |
|
| 264 | if ($cell == "")
|
| 265 | {
|
| 266 | $out .= $this->empty_cells;
|
| 267 | }
|
| 268 | else
|
| 269 | {
|
| 270 | $out .= $cell;
|
| 271 | }
|
| 272 |
|
| 273 | $out .= $this->template['cell_'.$name.'end'];
|
| 274 | }
|
| 275 |
|
| 276 | $out .= $this->template['row_'.$name.'end'];
|
| 277 | $out .= $this->newline;
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | $out .= $this->template['table_close'];
|
| 282 |
|
| 283 | return $out;
|
| 284 | }
|
| 285 |
|
| 286 | // --------------------------------------------------------------------
|
| 287 |
|
| 288 | /**
|
Derek Allard | 0e254f1 | 2007-04-10 21:53:38 +0000 | [diff] [blame] | 289 | * Clears the table arrays. Useful if multiple tables are being generated
|
Derek Allard | 5932c86 | 2007-01-27 17:44:54 +0000 | [diff] [blame] | 290 | *
|
| 291 | * @access public
|
| 292 | * @return void
|
| 293 | */
|
| 294 | function clear()
|
| 295 | {
|
| 296 | $this->rows = array();
|
| 297 | $this->heading = array();
|
| 298 | $this->auto_heading = TRUE;
|
| 299 | }
|
| 300 |
|
| 301 | // --------------------------------------------------------------------
|
| 302 |
|
| 303 | /**
|
| 304 | * Set table data from a database result object
|
| 305 | *
|
| 306 | * @access public
|
| 307 | * @param object
|
| 308 | * @return void
|
| 309 | */
|
| 310 | function _set_from_object($query)
|
| 311 | {
|
| 312 | if ( ! is_object($query))
|
| 313 | {
|
| 314 | return FALSE;
|
| 315 | }
|
| 316 |
|
| 317 | // First generate the headings from the table column names
|
| 318 | if (count($this->heading) == 0)
|
| 319 | {
|
| 320 | if ( ! method_exists($query, 'list_fields'))
|
| 321 | {
|
| 322 | return FALSE;
|
| 323 | }
|
| 324 |
|
| 325 | $this->heading = $query->list_fields();
|
| 326 | }
|
| 327 |
|
| 328 | // Next blast through the result array and build out the rows
|
| 329 |
|
| 330 | if ($query->num_rows() > 0)
|
| 331 | {
|
| 332 | foreach ($query->result_array() as $row)
|
| 333 | {
|
| 334 | $this->rows[] = $row;
|
| 335 | }
|
| 336 | }
|
| 337 | }
|
| 338 |
|
| 339 | // --------------------------------------------------------------------
|
| 340 |
|
| 341 | /**
|
| 342 | * Set table data from an array
|
| 343 | *
|
| 344 | * @access public
|
| 345 | * @param array
|
| 346 | * @return void
|
| 347 | */
|
| 348 | function _set_from_array($data, $set_heading = TRUE)
|
| 349 | {
|
| 350 | if ( ! is_array($data) OR count($data) == 0)
|
| 351 | {
|
| 352 | return FALSE;
|
| 353 | }
|
| 354 |
|
| 355 | $i = 0;
|
| 356 | foreach ($data as $row)
|
| 357 | {
|
| 358 | if ( ! is_array($row))
|
| 359 | {
|
| 360 | $this->rows[] = $data;
|
| 361 | break;
|
| 362 | }
|
| 363 |
|
| 364 | // If a heading hasn't already been set we'll use the first row of the array as the heading
|
| 365 | if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
|
| 366 | {
|
| 367 | $this->heading = $row;
|
| 368 | }
|
| 369 | else
|
| 370 | {
|
| 371 | $this->rows[] = $row;
|
| 372 | }
|
| 373 |
|
| 374 | $i++;
|
| 375 | }
|
| 376 | }
|
| 377 |
|
| 378 | // --------------------------------------------------------------------
|
| 379 |
|
| 380 | /**
|
| 381 | * Compile Template
|
| 382 | *
|
| 383 | * @access private
|
| 384 | * @return void
|
| 385 | */
|
| 386 | function _compile_template()
|
| 387 | {
|
| 388 | if ($this->template == NULL)
|
| 389 | {
|
| 390 | $this->template = $this->_default_template();
|
| 391 | return;
|
| 392 | }
|
| 393 |
|
| 394 | $this->temp = $this->_default_template();
|
| 395 | 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)
|
| 396 | {
|
| 397 | if ( ! isset($this->template[$val]))
|
| 398 | {
|
| 399 | $this->template[$val] = $this->temp[$val];
|
| 400 | }
|
| 401 | }
|
| 402 | }
|
| 403 |
|
| 404 | // --------------------------------------------------------------------
|
| 405 |
|
| 406 | /**
|
| 407 | * Default Template
|
| 408 | *
|
| 409 | * @access private
|
| 410 | * @return void
|
| 411 | */
|
| 412 | function _default_template()
|
| 413 | {
|
| 414 | return array (
|
| 415 | 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
|
| 416 |
|
| 417 | 'heading_row_start' => '<tr>',
|
| 418 | 'heading_row_end' => '</tr>',
|
| 419 | 'heading_cell_start' => '<th>',
|
| 420 | 'heading_cell_end' => '</th>',
|
| 421 |
|
| 422 | 'row_start' => '<tr>',
|
| 423 | 'row_end' => '</tr>',
|
| 424 | 'cell_start' => '<td>',
|
| 425 | 'cell_end' => '</td>',
|
| 426 |
|
| 427 | 'row_alt_start' => '<tr>',
|
| 428 | 'row_alt_end' => '</tr>',
|
| 429 | 'cell_alt_start' => '<td>',
|
| 430 | 'cell_alt_end' => '</td>',
|
| 431 |
|
| 432 | 'table_close' => '</table>'
|
| 433 | );
|
| 434 | }
|
| 435 |
|
| 436 |
|
| 437 | }
|
| 438 |
|
admin | 2f8ae02 | 2006-10-09 03:25:43 +0000 | [diff] [blame] | 439 | ?> |