From 2b39d9d1b837c92a0901ea9f0385172b763e18c8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 3 Apr 2012 19:14:23 +0300 Subject: Minor cleanup and style changes to the Table library --- system/libraries/Table.php | 123 +++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 67 deletions(-) (limited to 'system/libraries/Table.php') diff --git a/system/libraries/Table.php b/system/libraries/Table.php index 11a4858a9..992b057ad 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); } } } @@ -262,8 +256,8 @@ class CI_Table { } } - // 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('template['row_'.$name.'start'].$this->newline; @@ -333,27 +327,24 @@ class CI_Table { { if ($key !== 'data') { - $temp = str_replace('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' => '', + return array( + 'table_open' => '
', - 'thead_open' => '', - 'thead_close' => '', + 'thead_open' => '', + 'thead_close' => '', - 'heading_row_start' => '', - 'heading_row_end' => '', - 'heading_cell_start' => '', + 'heading_row_start' => '', + 'heading_row_end' => '', + 'heading_cell_start' => '', - 'tbody_open' => '', - 'tbody_close' => '', + 'tbody_open' => '', + 'tbody_close' => '', - 'row_start' => '', - 'row_end' => '', - 'cell_start' => '', + 'row_start' => '', + 'row_end' => '', + 'cell_start' => '', - 'row_alt_start' => '', - 'row_alt_end' => '', - 'cell_alt_start' => '', + 'row_alt_start' => '', + 'row_alt_end' => '', + 'cell_alt_start' => '', - 'table_close' => '
', - 'heading_cell_end' => '
', + 'heading_cell_end' => '
', - 'cell_end' => '
', + 'cell_end' => '
', - 'cell_alt_end' => '
', + 'cell_alt_end' => '
' - ); + 'table_close' => '' + ); } - } /* End of file Table.php */ -/* Location: ./system/libraries/Table.php */ +/* Location: ./system/libraries/Table.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b