Table.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.3.1
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * HTML Table Generating Class
00020  *
00021  * Lets you create tables manually or from database result objects, or arrays.
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  Libraries
00025  * @category    HTML Tables
00026  * @author              ExpressionEngine Dev Team
00027  * @link                http://codeigniter.com/user_guide/libraries/uri.html
00028  */
00029 class CI_Table {
00030 
00031         var $rows                               = array();
00032         var $heading                    = array();
00033         var $auto_heading               = TRUE; 
00034         var $caption                    = NULL; 
00035         var $template                   = NULL;
00036         var $newline                    = "\n";
00037         var $empty_cells                = "";
00038         
00039         
00040         function CI_Table()
00041         {
00042                 log_message('debug', "Table Class Initialized");
00043         }
00044 
00045         // --------------------------------------------------------------------
00046 
00047         /**
00048          * Set the template
00049          *
00050          * @access      public
00051          * @param       array
00052          * @return      void
00053          */
00054         function set_template($template)
00055         {
00056                 if ( ! is_array($template))
00057                 {
00058                         return FALSE;
00059                 }
00060         
00061                 $this->template = $template;
00062         }
00063 
00064         // --------------------------------------------------------------------
00065 
00066         /**
00067          * Set the table heading
00068          *
00069          * Can be passed as an array or discreet params
00070          *
00071          * @access      public
00072          * @param       mixed
00073          * @return      void
00074          */
00075         function set_heading()
00076         {
00077                 $args = func_get_args();
00078                 $this->heading = (is_array($args[0])) ? $args[0] : $args;
00079         }
00080 
00081         // --------------------------------------------------------------------
00082 
00083         /**
00084          * Set columns.  Takes a one-dimensional array as input and creates
00085          * a multi-dimensional array with a depth equal to the number of
00086          * columns.  This allows a single array with many elements to  be
00087          * displayed in a table that has a fixed column count.
00088          *
00089          * @access      public
00090          * @param       array
00091          * @param       int
00092          * @return      void
00093          */
00094         function make_columns($array = array(), $col_limit = 0)
00095         {
00096                 if ( ! is_array($array) OR count($array) == 0)
00097                 {
00098                         return FALSE;
00099                 }
00100                 
00101                 // Turn off the auto-heading feature since it's doubtful we 
00102                 // will want headings from a one-dimensional array
00103                 $this->auto_heading = FALSE;
00104                 
00105                 if ($col_limit == 0)
00106                 {
00107                         return $array;
00108                 }
00109         
00110                 $new = array();
00111                 while(count($array) > 0)
00112                 {       
00113                         $temp = array_splice($array, 0, $col_limit);
00114                         
00115                         if (count($temp) < $col_limit)
00116                         {
00117                                 for ($i = count($temp); $i < $col_limit; $i++)
00118                                 {
00119                                         $temp[] = '&nbsp;';
00120                                 }
00121                         }
00122                         
00123                         $new[] = $temp;
00124                 }
00125                 
00126                 return $new;
00127         }
00128 
00129         // --------------------------------------------------------------------
00130 
00131         /**
00132          * Set "empty" cells
00133          *
00134          * Can be passed as an array or discreet params
00135          *
00136          * @access      public
00137          * @param       mixed
00138          * @return      void
00139          */
00140         function set_empty($value)
00141         {
00142                 $this->empty_cells = $value;
00143         }
00144         
00145         // --------------------------------------------------------------------
00146 
00147         /**
00148          * Add a table row
00149          *
00150          * Can be passed as an array or discreet params
00151          *
00152          * @access      public
00153          * @param       mixed
00154          * @return      void
00155          */
00156         function add_row()
00157         {
00158                 $args = func_get_args();
00159                 $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
00160         }
00161 
00162         // --------------------------------------------------------------------
00163 
00164         /**
00165          * Add a table caption
00166          *
00167          * @access      public
00168          * @param       string
00169          * @return      void
00170          */
00171         function set_caption($caption)
00172         {
00173                 $this->caption = $caption;
00174         }       
00175 
00176         // --------------------------------------------------------------------
00177 
00178         /**
00179          * Generate the table
00180          *
00181          * @access      public
00182          * @param       mixed
00183          * @return      string
00184          */
00185         function generate($table_data = NULL)
00186         {
00187                 // The table data can optionally be passed to this function
00188                 // either as a database result object or an array
00189                 if ( ! is_null($table_data))
00190                 {
00191                         if (is_object($table_data))
00192                         {
00193                                 $this->_set_from_object($table_data);
00194                         }
00195                         elseif (is_array($table_data))
00196                         {
00197                                 $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
00198                                 $this->_set_from_array($table_data, $set_heading);
00199                         }
00200                 }
00201         
00202                 // Is there anything to display?  No?  Smite them!
00203                 if (count($this->heading) == 0 AND count($this->rows) == 0)
00204                 {
00205                         return 'Undefined table data';
00206                 }
00207         
00208                 // Compile and validate the template date
00209                 $this->_compile_template();
00210         
00211         
00212                 // Build the table!
00213                 
00214                 $out = $this->template['table_open'];
00215                 $out .= $this->newline;         
00216 
00217                 // Add any caption here
00218                 if ($this->caption)
00219                 {
00220                         $out .= $this->newline;
00221                         $out .= '<caption>' . $this->caption . '</caption>';
00222                         $out .= $this->newline;
00223                 }
00224 
00225                 // Is there a table heading to display?
00226                 if (count($this->heading) > 0)
00227                 {
00228                         $out .= $this->template['heading_row_start'];
00229                         $out .= $this->newline;         
00230 
00231                         foreach($this->heading as $heading)
00232                         {
00233                                 $out .= $this->template['heading_cell_start'];
00234                                 $out .= $heading;
00235                                 $out .= $this->template['heading_cell_end'];
00236                         }
00237 
00238                         $out .= $this->template['heading_row_end'];
00239                         $out .= $this->newline;                         
00240                 }
00241 
00242                 // Build the table rows
00243                 if (count($this->rows) > 0)
00244                 {
00245                         $i = 1;
00246                         foreach($this->rows as $row)
00247                         {
00248                                 if ( ! is_array($row))
00249                                 {
00250                                         break;
00251                                 }
00252                         
00253                                 // We use modulus to alternate the row colors
00254                                 $name = (fmod($i++, 2)) ? '' : 'alt_';
00255                         
00256                                 $out .= $this->template['row_'.$name.'start'];
00257                                 $out .= $this->newline;         
00258         
00259                                 foreach($row as $cell)
00260                                 {
00261                                         $out .= $this->template['cell_'.$name.'start'];
00262                                         
00263                                         if ($cell === "")
00264                                         {
00265                                                 $out .= $this->empty_cells;
00266                                         }
00267                                         else
00268                                         {
00269                                                 $out .= $cell;
00270                                         }
00271                                         
00272                                         $out .= $this->template['cell_'.$name.'end'];
00273                                 }
00274         
00275                                 $out .= $this->template['row_'.$name.'end'];
00276                                 $out .= $this->newline; 
00277                         }
00278                 }
00279 
00280                 $out .= $this->template['table_close'];
00281         
00282                 return $out;
00283         }
00284         
00285         // --------------------------------------------------------------------
00286 
00287         /**
00288          * Clears the table arrays.  Useful if multiple tables are being generated
00289          *
00290          * @access      public
00291          * @return      void
00292          */
00293         function clear()
00294         {
00295                 $this->rows                             = array();
00296                 $this->heading                  = array();
00297                 $this->auto_heading             = TRUE; 
00298         }
00299         
00300         // --------------------------------------------------------------------
00301 
00302         /**
00303          * Set table data from a database result object
00304          *
00305          * @access      public
00306          * @param       object
00307          * @return      void
00308          */
00309         function _set_from_object($query)
00310         {
00311                 if ( ! is_object($query))
00312                 {
00313                         return FALSE;
00314                 }
00315                 
00316                 // First generate the headings from the table column names
00317                 if (count($this->heading) == 0)
00318                 {
00319                         if ( ! method_exists($query, 'list_fields'))
00320                         {
00321                                 return FALSE;
00322                         }
00323                         
00324                         $this->heading = $query->list_fields();
00325                 }
00326                                 
00327                 // Next blast through the result array and build out the rows
00328                 
00329                 if ($query->num_rows() > 0)
00330                 {
00331                         foreach ($query->result_array() as $row)
00332                         {
00333                                 $this->rows[] = $row;
00334                         }
00335                 }
00336         }
00337 
00338         // --------------------------------------------------------------------
00339 
00340         /**
00341          * Set table data from an array
00342          *
00343          * @access      public
00344          * @param       array
00345          * @return      void
00346          */
00347         function _set_from_array($data, $set_heading = TRUE)
00348         {
00349                 if ( ! is_array($data) OR count($data) == 0)
00350                 {
00351                         return FALSE;
00352                 }
00353                 
00354                 $i = 0;
00355                 foreach ($data as $row)
00356                 {
00357                         if ( ! is_array($row))
00358                         {
00359                                 $this->rows[] = $data;
00360                                 break;
00361                         }
00362                                                 
00363                         // If a heading hasn't already been set we'll use the first row of the array as the heading
00364                         if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
00365                         {
00366                                 $this->heading = $row;
00367                         }
00368                         else
00369                         {
00370                                 $this->rows[] = $row;
00371                         }
00372                         
00373                         $i++;
00374                 }
00375         }
00376 
00377         // --------------------------------------------------------------------
00378 
00379         /**
00380          * Compile Template
00381          *
00382          * @access      private
00383          * @return      void
00384          */
00385         function _compile_template()
00386         {       
00387                 if ($this->template == NULL)
00388                 {
00389                         $this->template = $this->_default_template();
00390                         return;
00391                 }
00392                 
00393                 $this->temp = $this->_default_template();
00394                 foreach (array('table_open','heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val)
00395                 {
00396                         if ( ! isset($this->template[$val]))
00397                         {
00398                                 $this->template[$val] = $this->temp[$val];
00399                         }
00400                 }       
00401         }
00402         
00403         // --------------------------------------------------------------------
00404 
00405         /**
00406          * Default Template
00407          *
00408          * @access      private
00409          * @return      void
00410          */
00411         function _default_template()
00412         {
00413                 return  array (
00414                                                 'table_open'                    => '<table border="0" cellpadding="4" cellspacing="0">',
00415 
00416                                                 'heading_row_start'     => '<tr>',
00417                                                 'heading_row_end'               => '</tr>',
00418                                                 'heading_cell_start'    => '<th>',
00419                                                 'heading_cell_end'              => '</th>',
00420 
00421                                                 'row_start'                     => '<tr>',
00422                                                 'row_end'                               => '</tr>',
00423                                                 'cell_start'                    => '<td>',
00424                                                 'cell_end'                              => '</td>',
00425 
00426                                                 'row_alt_start'                 => '<tr>',
00427                                                 'row_alt_end'                   => '</tr>',
00428                                                 'cell_alt_start'                => '<td>',
00429                                                 'cell_alt_end'                  => '</td>',
00430 
00431                                                 'table_close'                   => '</table>'
00432                                         );      
00433         }
00434         
00435 
00436 }
00437 
00438 
00439 /* End of file Table.php */
00440 /* Location: ./system/libraries/Table.php */