summaryrefslogtreecommitdiffstats
path: root/system/libraries/Table.php
diff options
context:
space:
mode:
authorAndrey Andreev <narf@bofh.bg>2012-04-06 20:49:57 +0200
committerAndrey Andreev <narf@bofh.bg>2012-04-06 20:49:57 +0200
commit89ba83c811338134030ef051626e38e3876bc762 (patch)
tree24e3b8b6f738c9a7443e88312c808787dacb14fa /system/libraries/Table.php
parent11c111c3ff128791f571ac2519bb796baecf7226 (diff)
parentea09a8a5552f2aacdeab0c88a605fe44047ebd0a (diff)
Merge upstream branch
Diffstat (limited to 'system/libraries/Table.php')
-rw-r--r--system/libraries/Table.php125
1 files changed, 57 insertions, 68 deletions
diff --git a/system/libraries/Table.php b/system/libraries/Table.php
index 11a4858a9..3777d29ff 100644
--- a/system/libraries/Table.php
+++ b/system/libraries/Table.php
@@ -38,14 +38,14 @@
*/
class CI_Table {
- public $rows = array();
- public $heading = array();
- public $auto_heading = TRUE;
- public $caption = NULL;
- public $template = NULL;
- public $newline = "\n";
- public $empty_cells = '';
- public $function = FALSE;
+ public $rows = array();
+ public $heading = array();
+ public $auto_heading = TRUE;
+ public $caption = NULL;
+ public $template = NULL;
+ public $newline = "\n";
+ public $empty_cells = '';
+ public $function = FALSE;
/**
* Set the template from the table config file if it exists
@@ -55,13 +55,13 @@ class CI_Table {
*/
public function __construct($config = array())
{
- log_message('debug', 'Table Class Initialized');
-
// initialize config
foreach ($config as $key => $val)
{
$this->template[$key] = $val;
}
+
+ log_message('debug', 'Table Class Initialized');
}
// --------------------------------------------------------------------
@@ -70,7 +70,7 @@ class CI_Table {
* Set the template
*
* @param array
- * @return void
+ * @return bool
*/
public function set_template($template)
{
@@ -80,6 +80,7 @@ class CI_Table {
}
$this->template = $template;
+ return TRUE;
}
// --------------------------------------------------------------------
@@ -101,9 +102,9 @@ class CI_Table {
// --------------------------------------------------------------------
/**
- * Set columns. Takes a one-dimensional array as input and creates
+ * Set columns. Takes a one-dimensional array as input and creates
* a multi-dimensional array with a depth equal to the number of
- * columns. This allows a single array with many elements to be
+ * columns. This allows a single array with many elements to be
* displayed in a table that has a fixed column count.
*
* @param array
@@ -184,29 +185,22 @@ class CI_Table {
*
* Ensures a standard associative array format for all cell data
*
- * @param type
- * @return type
+ * @param array
+ * @return array
*/
protected 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])))
+ if (isset($args[0]) && 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);
- }
+ $args[$key] = (is_array($val) && isset($val['data'])) ? $val : array('data' => $val);
}
}
}
@@ -257,13 +251,13 @@ class CI_Table {
}
elseif (is_array($table_data))
{
- $set_heading = (count($this->heading) === 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
+ $set_heading = (count($this->heading) !== 0 OR $this->auto_heading != FALSE);
$this->_set_from_array($table_data, $set_heading);
}
}
- // Is there anything to display? No? Smite them!
- if (count($this->heading) === 0 AND count($this->rows) === 0)
+ // Is there anything to display? No? Smite them!
+ if (count($this->heading) === 0 && count($this->rows) === 0)
{
return 'Undefined table data';
}
@@ -297,7 +291,7 @@ class CI_Table {
{
if ($key != 'data')
{
- $temp = str_replace('<th', "<th $key='$val'", $temp);
+ $temp = str_replace('<th', '<th '.$key.'="'.$val.'"', $temp);
}
}
@@ -321,7 +315,7 @@ class CI_Table {
}
// We use modulus to alternate the row colors
- $name = (fmod($i++, 2)) ? '' : 'alt_';
+ $name = fmod($i++, 2) ? '' : 'alt_';
$out .= $this->template['row_'.$name.'start'].$this->newline;
@@ -333,27 +327,24 @@ class CI_Table {
{
if ($key !== 'data')
{
- $temp = str_replace('<td', "<td $key='$val'", $temp);
+ $temp = str_replace('<td', '<td '.$key.'="'.$val.'"', $temp);
}
}
$cell = isset($cell['data']) ? $cell['data'] : '';
$out .= $temp;
- if ($cell === "" OR $cell === NULL)
+ if ($cell === '' OR $cell === NULL)
{
$out .= $this->empty_cells;
}
+ elseif ($function !== FALSE && is_callable($function))
+ {
+ $out .= call_user_func($function, $cell);
+ }
else
{
- if ($function !== FALSE && is_callable($function))
- {
- $out .= call_user_func($function, $cell);
- }
- else
- {
- $out .= $cell;
- }
+ $out .= $cell;
}
$out .= $this->template['cell_'.$name.'end'];
@@ -382,9 +373,9 @@ class CI_Table {
*/
public function clear()
{
- $this->rows = array();
- $this->heading = array();
- $this->auto_heading = TRUE;
+ $this->rows = array();
+ $this->heading = array();
+ $this->auto_heading = TRUE;
}
// --------------------------------------------------------------------
@@ -399,7 +390,7 @@ class CI_Table {
{
if ( ! is_object($query))
{
- return FALSE;
+ return;
}
// First generate the headings from the table column names
@@ -407,14 +398,13 @@ class CI_Table {
{
if ( ! is_callable(array($query, 'list_fields')))
{
- return FALSE;
+ return;
}
$this->heading = $this->_prep_args($query->list_fields());
}
// Next blast through the result array and build out the rows
-
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
@@ -443,7 +433,7 @@ class CI_Table {
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 AND count($data) > 1 AND count($this->heading) === 0 AND $set_heading == TRUE)
+ if ($i++ === 0 && count($data) > 1 && count($this->heading) === 0 && $set_heading == TRUE)
{
$this->heading = $this->_prep_args($row);
}
@@ -488,36 +478,35 @@ class CI_Table {
*/
protected function _default_template()
{
- return array (
- 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
+ return array(
+ 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
- 'thead_open' => '<thead>',
- 'thead_close' => '</thead>',
+ 'thead_open' => '<thead>',
+ 'thead_close' => '</thead>',
- 'heading_row_start' => '<tr>',
- 'heading_row_end' => '</tr>',
- 'heading_cell_start' => '<th>',
- 'heading_cell_end' => '</th>',
+ 'heading_row_start' => '<tr>',
+ 'heading_row_end' => '</tr>',
+ 'heading_cell_start' => '<th>',
+ 'heading_cell_end' => '</th>',
- 'tbody_open' => '<tbody>',
- 'tbody_close' => '</tbody>',
+ 'tbody_open' => '<tbody>',
+ 'tbody_close' => '</tbody>',
- 'row_start' => '<tr>',
- 'row_end' => '</tr>',
- 'cell_start' => '<td>',
- 'cell_end' => '</td>',
+ 'row_start' => '<tr>',
+ 'row_end' => '</tr>',
+ 'cell_start' => '<td>',
+ 'cell_end' => '</td>',
- 'row_alt_start' => '<tr>',
- 'row_alt_end' => '</tr>',
- 'cell_alt_start' => '<td>',
- 'cell_alt_end' => '</td>',
+ 'row_alt_start' => '<tr>',
+ 'row_alt_end' => '</tr>',
+ 'cell_alt_start' => '<td>',
+ 'cell_alt_end' => '</td>',
- 'table_close' => '</table>'
- );
+ 'table_close' => '</table>'
+ );
}
-
}
/* End of file Table.php */
-/* Location: ./system/libraries/Table.php */
+/* Location: ./system/libraries/Table.php */ \ No newline at end of file