diff options
author | Andrey Andreev <narf@devilix.net> | 2014-02-11 15:51:43 +0100 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2014-02-11 15:51:43 +0100 |
commit | 05983fcb5b8f04fb895c025e28ef6ffc44a5f602 (patch) | |
tree | b82862de96f675090fb4e88534abc1b9159c99fc /system/libraries/Table.php | |
parent | e52fc267a93fd3627951ddc7c5109b573a871727 (diff) |
A bug fix and optimizations in CI_Table
Diffstat (limited to 'system/libraries/Table.php')
-rw-r--r-- | system/libraries/Table.php | 76 |
1 files changed, 27 insertions, 49 deletions
diff --git a/system/libraries/Table.php b/system/libraries/Table.php index 1d4320855..58016e9b8 100644 --- a/system/libraries/Table.php +++ b/system/libraries/Table.php @@ -294,21 +294,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 +315,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 ($this->function !== FALSE && ! is_callable($this->function)) + { + $this->function = FALSE; + } // Build the table! @@ -326,11 +328,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 +355,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 +391,9 @@ class CI_Table { { $out .= $this->empty_cells; } - elseif ($function !== FALSE && is_callable($function)) + elseif ($this->function !== FALSE) { - $out .= call_user_func($function, $cell); + $out .= call_user_func($this->function, $cell); } else { @@ -435,34 +437,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 +459,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); } } |