diff options
author | Andrew <browner12@gmail.com> | 2014-02-17 19:24:00 +0100 |
---|---|---|
committer | Andrew <browner12@gmail.com> | 2014-02-17 19:24:00 +0100 |
commit | 63cb46b94c4dda33e036643fbca3d94e0ba079f0 (patch) | |
tree | a8bc36fa23e3a7de491223c896d9aadaf4e8c408 /system/libraries/Table.php | |
parent | b6d8b962e44202a74c9b9321a4a53f61a753fccf (diff) | |
parent | ffe8aded4d2210759fce3427ed04893e6c655006 (diff) |
Merge branch 'develop' into patch-1
Diffstat (limited to 'system/libraries/Table.php')
-rw-r--r-- | system/libraries/Table.php | 108 |
1 files changed, 36 insertions, 72 deletions
diff --git a/system/libraries/Table.php b/system/libraries/Table.php index 1d4320855..ff96d9275 100644 --- a/system/libraries/Table.php +++ b/system/libraries/Table.php @@ -18,7 +18,7 @@ * * @package CodeIgniter * @author EllisLab Dev Team - * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) + * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.3.1 @@ -93,7 +93,7 @@ class CI_Table { * * @var function */ - public $function = FALSE; + public $function = NULL; /** * Set the template from the table config file if it exists @@ -143,8 +143,7 @@ class CI_Table { */ public function set_heading($args = array()) { - $args = func_get_args(); - $this->heading = $this->_prep_args($args); + $this->heading = $this->_prep_args(func_get_args()); return $this; } @@ -224,8 +223,7 @@ class CI_Table { */ public function add_row($args = array()) { - $args = func_get_args(); - $this->rows[] = $this->_prep_args($args); + $this->rows[] = $this->_prep_args(func_get_args()); return $this; } @@ -244,26 +242,14 @@ class CI_Table { // 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]) && count($args) === 1 && is_array($args[0])) + if (isset($args[0]) && count($args) === 1 && is_array($args[0]) && ! isset($args[0]['data'])) { - // args sent as indexed array - if ( ! isset($args[0]['data'])) - { - foreach ($args[0] as $key => $val) - { - $args[$key] = (is_array($val) && isset($val['data'])) ? $val : array('data' => $val); - } - } + $args = $args[0]; } - else + + foreach ($args as $key => $val) { - foreach ($args as $key => $val) - { - if ( ! is_array($val)) - { - $args[$key] = array('data' => $val); - } - } + is_array($val) OR $args[$key] = array('data' => $val); } return $args; @@ -294,21 +280,20 @@ class CI_Table { { // The table data can optionally be passed to this function // either as a database result object or an array - if ($table_data !== NULL) + if ( ! empty($table_data)) { - if (is_object($table_data)) + if ($table_data instanceof CI_DB_result) { - $this->_set_from_object($table_data); + $this->_set_from_db_result($table_data); } elseif (is_array($table_data)) { - $set_heading = (count($this->heading) !== 0 OR $this->auto_heading !== FALSE); - $this->_set_from_array($table_data, $set_heading); + $this->_set_from_array($table_data); } } // Is there anything to display? No? Smite them! - if (count($this->heading) === 0 && count($this->rows) === 0) + if (empty($this->heading) && empty($this->rows)) { return 'Undefined table data'; } @@ -316,8 +301,11 @@ 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; + // Validate a possibly existing custom cell manipulation function + if (isset($this->function) && ! is_callable($this->function)) + { + $this->function = NULL; + } // Build the table! @@ -326,11 +314,11 @@ class CI_Table { // Add any caption here if ($this->caption) { - $out .= $this->newline.'<caption>'.$this->caption.'</caption>'.$this->newline; + $out .= '<caption>'.$this->caption.'</caption>'.$this->newline; } // Is there a table heading to display? - if (count($this->heading) > 0) + if ( ! empty($this->heading)) { $out .= $this->template['thead_open'].$this->newline.$this->template['heading_row_start'].$this->newline; @@ -353,7 +341,7 @@ class CI_Table { } // Build the table rows - if (count($this->rows) > 0) + if ( ! empty($this->rows)) { $out .= $this->template['tbody_open'].$this->newline; @@ -389,9 +377,9 @@ class CI_Table { { $out .= $this->empty_cells; } - elseif ($function !== FALSE && is_callable($function)) + elseif (isset($this->function)) { - $out .= call_user_func($function, $cell); + $out .= call_user_func($this->function, $cell); } else { @@ -435,34 +423,20 @@ class CI_Table { /** * Set table data from a database result object * - * @param object + * @param CI_DB_result $db_result Database result object * @return void */ - protected function _set_from_object($query) + protected function _set_from_db_result($object) { - if ( ! is_object($query)) - { - return; - } - // First generate the headings from the table column names - if (count($this->heading) === 0) + if ($this->auto_heading === TRUE && empty($this->heading)) { - if ( ! is_callable(array($query, 'list_fields'))) - { - return; - } - - $this->heading = $this->_prep_args($query->list_fields()); + $this->heading = $this->_prep_args($object->list_fields()); } - // Next blast through the result array and build out the rows - if ($query->num_rows() > 0) + foreach ($object->result_array() as $row) { - foreach ($query->result_array() as $row) - { - $this->rows[] = $this->_prep_args($row); - } + $this->rows[] = $this->_prep_args($row); } } @@ -471,29 +445,19 @@ class CI_Table { /** * Set table data from an array * - * @param array - * @param bool + * @param array $data * @return void */ - protected function _set_from_array($data, $set_heading = TRUE) + protected function _set_from_array($data) { - if ( ! is_array($data) OR count($data) === 0) + if ($this->auto_heading === TRUE && empty($this->heading)) { - return FALSE; + $this->heading = $this->_prep_args(array_shift($data)); } - $i = 0; - foreach ($data as $row) + foreach ($data as &$row) { - // If a heading hasn't already been set we'll use the first row of the array as the heading - if ($i++ === 0 && count($data) > 1 && count($this->heading) === 0 && $set_heading === TRUE) - { - $this->heading = $this->_prep_args($row); - } - else - { - $this->rows[] = $this->_prep_args($row); - } + $this->rows[] = $this->_prep_args($row); } } |