summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--system/libraries/Table.php76
-rw-r--r--tests/codeigniter/libraries/Table_test.php47
-rw-r--r--user_guide_src/source/changelog.rst1
3 files changed, 55 insertions, 69 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);
}
}
diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php
index ce04b6a6d..4bfbdd623 100644
--- a/tests/codeigniter/libraries/Table_test.php
+++ b/tests/codeigniter/libraries/Table_test.php
@@ -200,16 +200,14 @@ class Table_test extends CI_TestCase {
public function test_set_from_array()
{
- $this->assertFalse($this->table->set_from_array('bogus'));
- $this->assertFalse($this->table->set_from_array(NULL));
-
$data = array(
array('name', 'color', 'number'),
array('Laura', 'Red', '22'),
array('Katie', 'Blue')
);
- $this->table->set_from_array($data, FALSE);
+ $this->table->auto_heading = FALSE;
+ $this->table->set_from_array($data);
$this->assertEmpty($this->table->heading);
$this->table->clear();
@@ -235,22 +233,14 @@ class Table_test extends CI_TestCase {
public function test_set_from_object()
{
- // Make a stub of query instance
- $query = new CI_TestCase();
- $query->list_fields = function(){
- return array('name', 'email');
- };
- $query->result_array = function(){
- return array(
- array('name' => 'John Doe', 'email' => 'john@doe.com'),
- array('name' => 'Foo Bar', 'email' => 'foo@bar.com'),
- );
- };
- $query->num_rows = function(){
- return 2;
- };
-
- $this->table->set_from_object($query);
+ // This needs to be passed by reference to CI_DB_result::__construct()
+ $dummy = new stdClass();
+ $dummy->conn_id = NULL;
+ $dummy->result_id = NULL;
+
+ $db_result = new DB_result_dummy($dummy);
+
+ $this->table->set_from_db_result($db_result);
$expected = array(
array('data' => 'name'),
@@ -290,4 +280,21 @@ class Table_test extends CI_TestCase {
$this->assertTrue(strpos($table, '<td>Small</td>') !== FALSE);
}
+}
+
+// We need this for the _set_from_db_result() test
+class DB_result_dummy extends CI_DB_result
+{
+ public function list_fields()
+ {
+ return array('name', 'email');
+ }
+
+ public function result_array()
+ {
+ return array(
+ array('name' => 'John Doe', 'email' => 'john@doe.com'),
+ array('name' => 'Foo Bar', 'email' => 'foo@bar.com')
+ );
+ }
} \ No newline at end of file
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index e08b7fee0..9e63a6885 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -694,6 +694,7 @@ Bug fixes for 3.0
- Fixed a bug in the :doc:`Session Library <libraries/sessions>` 'cookie' driver where authentication was not performed for encrypted cookies.
- Fixed a bug (#2856) - ODBC method ``affected_rows()`` passed an incorrect value to ``odbc_num_rows()``.
- Fixed a bug (#43) :doc:`Image Manipulation Library <libraries/image_lib>` method ``text_watermark()`` didn't properly determine watermark placement.
+- Fixed a bug where :doc:`HTML Table Library <libraries/table>` ignored its *auto_heading* setting if headings were not already set.
Version 2.1.4
=============