1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10
* @license http://codeigniter.com/user_guide/license.html
11
* @link http://codeigniter.com
12
* @since Version 1.3.1
16
// ------------------------------------------------------------------------
19
* HTML Table Generating Class
21
* Lets you create tables manually or from database result objects, or arrays.
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
32
var $heading = array();
33
var $auto_heading = TRUE;
37
var $empty_cells = "";
38
var $function = FALSE;
40
public function __construct()
42
log_message('debug', "Table Class Initialized");
45
// --------------------------------------------------------------------
54
function set_template($template)
56
if ( ! is_array($template))
61
$this->template = $template;
64
// --------------------------------------------------------------------
67
* Set the table heading
69
* Can be passed as an array or discreet params
75
function set_heading()
77
$args = func_get_args();
78
$this->heading = $this->_prep_args($args);
81
// --------------------------------------------------------------------
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.
94
function make_columns($array = array(), $col_limit = 0)
96
if ( ! is_array($array) OR count($array) == 0)
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;
111
while (count($array) > 0)
113
$temp = array_splice($array, 0, $col_limit);
115
if (count($temp) < $col_limit)
117
for ($i = count($temp); $i < $col_limit; $i++)
129
// --------------------------------------------------------------------
134
* Can be passed as an array or discreet params
140
function set_empty($value)
142
$this->empty_cells = $value;
145
// --------------------------------------------------------------------
150
* Can be passed as an array or discreet params
158
$args = func_get_args();
159
$this->rows[] = $this->_prep_args($args);
162
// --------------------------------------------------------------------
167
* Ensures a standard associative array format for all cell data
173
function _prep_args($args)
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])))
180
// args sent as indexed array
181
if ( ! isset($args[0]['data']))
183
foreach ($args[0] as $key => $val)
185
if (is_array($val) && isset($val['data']))
191
$args[$key] = array('data' => $val);
198
foreach ($args as $key => $val)
200
if ( ! is_array($val))
202
$args[$key] = array('data' => $val);
210
// --------------------------------------------------------------------
213
* Add a table caption
219
function set_caption($caption)
221
$this->caption = $caption;
224
// --------------------------------------------------------------------
233
function generate($table_data = NULL)
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))
239
if (is_object($table_data))
241
$this->_set_from_object($table_data);
243
elseif (is_array($table_data))
245
$set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
246
$this->_set_from_array($table_data, $set_heading);
250
// Is there anything to display? No? Smite them!
251
if (count($this->heading) == 0 AND count($this->rows) == 0)
253
return 'Undefined table data';
256
// Compile and validate the template date
257
$this->_compile_template();
259
// set a custom cell manipulation function to a locally scoped variable so its callable
260
$function = $this->function;
264
$out = $this->template['table_open'];
265
$out .= $this->newline;
267
// Add any caption here
270
$out .= $this->newline;
271
$out .= '<caption>' . $this->caption . '</caption>';
272
$out .= $this->newline;
275
// Is there a table heading to display?
276
if (count($this->heading) > 0)
278
$out .= $this->template['thead_open'];
279
$out .= $this->newline;
280
$out .= $this->template['heading_row_start'];
281
$out .= $this->newline;
283
foreach ($this->heading as $heading)
285
$temp = $this->template['heading_cell_start'];
287
foreach ($heading as $key => $val)
291
$temp = str_replace('<th', "<th $key='$val'", $temp);
296
$out .= isset($heading['data']) ? $heading['data'] : '';
297
$out .= $this->template['heading_cell_end'];
300
$out .= $this->template['heading_row_end'];
301
$out .= $this->newline;
302
$out .= $this->template['thead_close'];
303
$out .= $this->newline;
306
// Build the table rows
307
if (count($this->rows) > 0)
309
$out .= $this->template['tbody_open'];
310
$out .= $this->newline;
313
foreach ($this->rows as $row)
315
if ( ! is_array($row))
320
// We use modulus to alternate the row colors
321
$name = (fmod($i++, 2)) ? '' : 'alt_';
323
$out .= $this->template['row_'.$name.'start'];
324
$out .= $this->newline;
326
foreach ($row as $cell)
328
$temp = $this->template['cell_'.$name.'start'];
330
foreach ($cell as $key => $val)
334
$temp = str_replace('<td', "<td $key='$val'", $temp);
338
$cell = isset($cell['data']) ? $cell['data'] : '';
341
if ($cell === "" OR $cell === NULL)
343
$out .= $this->empty_cells;
347
if ($function !== FALSE && is_callable($function))
349
$out .= call_user_func($function, $cell);
357
$out .= $this->template['cell_'.$name.'end'];
360
$out .= $this->template['row_'.$name.'end'];
361
$out .= $this->newline;
364
$out .= $this->template['tbody_close'];
365
$out .= $this->newline;
368
$out .= $this->template['table_close'];
370
// Clear table class properties before generating the table
376
// --------------------------------------------------------------------
379
* Clears the table arrays. Useful if multiple tables are being generated
386
$this->rows = array();
387
$this->heading = array();
388
$this->auto_heading = TRUE;
391
// --------------------------------------------------------------------
394
* Set table data from a database result object
400
function _set_from_object($query)
402
if ( ! is_object($query))
407
// First generate the headings from the table column names
408
if (count($this->heading) == 0)
410
if ( ! method_exists($query, 'list_fields'))
415
$this->heading = $this->_prep_args($query->list_fields());
418
// Next blast through the result array and build out the rows
420
if ($query->num_rows() > 0)
422
foreach ($query->result_array() as $row)
424
$this->rows[] = $this->_prep_args($row);
429
// --------------------------------------------------------------------
432
* Set table data from an array
438
function _set_from_array($data, $set_heading = TRUE)
440
if ( ! is_array($data) OR count($data) == 0)
446
foreach ($data as $row)
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)
451
$this->heading = $this->_prep_args($row);
455
$this->rows[] = $this->_prep_args($row);
462
// --------------------------------------------------------------------
470
function _compile_template()
472
if ($this->template == NULL)
474
$this->template = $this->_default_template();
478
$this->temp = $this->_default_template();
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)
481
if ( ! isset($this->template[$val]))
483
$this->template[$val] = $this->temp[$val];
488
// --------------------------------------------------------------------
496
function _default_template()
499
'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
501
'thead_open' => '<thead>',
502
'thead_close' => '</thead>',
504
'heading_row_start' => '<tr>',
505
'heading_row_end' => '</tr>',
506
'heading_cell_start' => '<th>',
507
'heading_cell_end' => '</th>',
509
'tbody_open' => '<tbody>',
510
'tbody_close' => '</tbody>',
512
'row_start' => '<tr>',
513
'row_end' => '</tr>',
514
'cell_start' => '<td>',
515
'cell_end' => '</td>',
517
'row_alt_start' => '<tr>',
518
'row_alt_end' => '</tr>',
519
'cell_alt_start' => '<td>',
520
'cell_alt_end' => '</td>',
522
'table_close' => '</table>'
530
/* End of file Table.php */
531
/* Location: ./system/libraries/Table.php */
b'\\ No newline at end of file'