diff options
Diffstat (limited to 'system/libraries')
-rw-r--r-- | system/libraries/Table.php | 138 |
1 files changed, 113 insertions, 25 deletions
diff --git a/system/libraries/Table.php b/system/libraries/Table.php index 6d36121f2..1f920ea9e 100644 --- a/system/libraries/Table.php +++ b/system/libraries/Table.php @@ -35,7 +35,7 @@ class CI_Table { var $template = NULL; var $newline = "\n"; var $empty_cells = ""; - + var $function = FALSE; function CI_Table() { @@ -75,7 +75,7 @@ class CI_Table { function set_heading() { $args = func_get_args(); - $this->heading = (is_array($args[0])) ? $args[0] : $args; + $this->heading = $this->_prep_args($args); } // -------------------------------------------------------------------- @@ -110,7 +110,7 @@ class CI_Table { $new = array(); while(count($array) > 0) { - $temp = array_splice($array, 0, $col_limit); + $temp = array_splice($array, 0, $col_limit); if (count($temp) < $col_limit) { @@ -156,11 +156,59 @@ class CI_Table { function add_row() { $args = func_get_args(); - $this->rows[] = (is_array($args[0])) ? $args[0] : $args; + $this->rows[] = $this->_prep_args($args); } // -------------------------------------------------------------------- + + /** + * Prep Args + * + * Ensures a standard associative array format for all cell data + * + * @access public + * @param type + * @return type + */ + function _prep_args($args) + { + // If there is no $args[0], skip this and treat as an associative array + // This can happen if there is only a single key, for example this is passed to table->generate + // array(array('foo'=>'bar')) + if (isset($args[0]) AND (count($args) == 1 && is_array($args[0]))) + { + // args sent as indexed array + if ( ! isset($args[0]['data'])) + { + foreach ($args[0] as $key => $val) + { + if (is_array($val) && isset($val['data'])) + { + $args[$key] = $val; + } + else + { + $args[$key] = array('data' => $val); + } + } + } + } + else + { + foreach ($args as $key => $val) + { + if ( ! is_array($val)) + { + $args[$key] = array('data' => $val); + } + } + } + + return $args; + } + // -------------------------------------------------------------------- + /** * Add a table caption * @@ -208,7 +256,9 @@ class CI_Table { // Compile and validate the template date $this->_compile_template(); - + // set a custom cell manipulation function to a locally scoped variable so its callable + $function = $this->function; + // Build the table! $out = $this->template['table_open']; @@ -225,23 +275,40 @@ class CI_Table { // Is there a table heading to display? if (count($this->heading) > 0) { + $out .= $this->template['thead_open']; + $out .= $this->newline; $out .= $this->template['heading_row_start']; - $out .= $this->newline; + $out .= $this->newline; foreach($this->heading as $heading) { - $out .= $this->template['heading_cell_start']; - $out .= $heading; + $temp = $this->template['heading_cell_start']; + + foreach ($heading as $key => $val) + { + if ($key != 'data') + { + $temp = str_replace('<th', "<th $key='$val'", $temp); + } + } + + $out .= $temp; + $out .= isset($heading['data']) ? $heading['data'] : ''; $out .= $this->template['heading_cell_end']; } $out .= $this->template['heading_row_end']; - $out .= $this->newline; + $out .= $this->newline; + $out .= $this->template['thead_close']; + $out .= $this->newline; } - + // Build the table rows if (count($this->rows) > 0) { + $out .= $this->template['tbody_open']; + $out .= $this->newline; + $i = 1; foreach($this->rows as $row) { @@ -258,15 +325,33 @@ class CI_Table { foreach($row as $cell) { - $out .= $this->template['cell_'.$name.'start']; + $temp = $this->template['cell_'.$name.'start']; + + foreach ($cell as $key => $val) + { + if ($key != 'data') + { + $temp = str_replace('<td', "<td $key='$val'", $temp); + } + } + $cell = isset($cell['data']) ? $cell['data'] : ''; + $out .= $temp; + if ($cell === "" OR $cell === NULL) { $out .= $this->empty_cells; } else { - $out .= $cell; + if ($function !== FALSE && is_callable($function)) + { + $out .= $function($cell); + } + else + { + $out .= $cell; + } } $out .= $this->template['cell_'.$name.'end']; @@ -275,6 +360,9 @@ class CI_Table { $out .= $this->template['row_'.$name.'end']; $out .= $this->newline; } + + $out .= $this->template['tbody_close']; + $out .= $this->newline; } $out .= $this->template['table_close']; @@ -321,7 +409,7 @@ class CI_Table { return FALSE; } - $this->heading = $query->list_fields(); + $this->heading = $this->_prep_args($query->list_fields()); } // Next blast through the result array and build out the rows @@ -330,7 +418,7 @@ class CI_Table { { foreach ($query->result_array() as $row) { - $this->rows[] = $row; + $this->rows[] = $this->_prep_args($row); } } } @@ -353,21 +441,15 @@ class CI_Table { $i = 0; foreach ($data as $row) - { - if ( ! is_array($row)) - { - $this->rows[] = $data; - break; - } - + { // If a heading hasn't already been set we'll use the first row of the array as the heading if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE) { - $this->heading = $row; + $this->heading = $this->_prep_args($row); } else { - $this->rows[] = $row; + $this->rows[] = $this->_prep_args($row); } $i++; @@ -391,7 +473,7 @@ class CI_Table { } $this->temp = $this->_default_template(); - 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) + 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) { if ( ! isset($this->template[$val])) { @@ -412,12 +494,18 @@ class CI_Table { { return array ( 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', - + + 'thead_open' => '<thead>', + 'thead_close' => '</thead>', + 'heading_row_start' => '<tr>', 'heading_row_end' => '</tr>', 'heading_cell_start' => '<th>', 'heading_cell_end' => '</th>', + 'tbody_open' => '<tbody>', + 'tbody_close' => '</tbody>', + 'row_start' => '<tr>', 'row_end' => '</tr>', 'cell_start' => '<td>', |