[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/admin/inc/ -> class_table.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id$
  10   */
  11  
  12  /**
  13   * Generate a data grid/table.
  14   */
  15  class DefaultTable
  16  {
  17      /**
  18       * @var array Array of cells for the current row.
  19       */
  20      private $_cells = array();
  21  
  22      /**
  23       * @var array Array of rows for the current table.
  24       */
  25      private $_rows = array();
  26  
  27      /**
  28       * @var array Array of headers for the current table.
  29       */
  30      private $_headers = array();
  31  
  32      /**
  33       * Construct an individual cell for this table.
  34       *
  35       * @param string The HTML content for this cell.
  36       * @param array Array of extra information about this cell (class, id, colspan, rowspan, width)
  37       */
  38  	function construct_cell($data, $extra=array())
  39      {
  40          foreach(array('class', 'style', 'id') as $field)
  41          {
  42              // Common-used fields
  43              if(!isset($extra[$field]))
  44              {
  45                  $extra[$field] = '';
  46              }
  47          }
  48  
  49          $this->_cells[] = array("data" => $data, "extra" => $extra);
  50      }
  51  
  52      /**
  53       * Construct a row from the earlier defined constructed cells for the table.
  54       *
  55       * @param array Array of extra information about this row (class, id)
  56       */
  57  	function construct_row($extra = array())
  58      {
  59          $i = 1;
  60          $cells = '';
  61  
  62          foreach(array('class', 'style', 'id', 'rowspan', 'width') as $field)
  63          {
  64              // Common-used fields
  65              if(!isset($extra[$field]))
  66              {
  67                  $extra[$field] = '';
  68              }
  69          }
  70  
  71          // We construct individual cells here
  72          foreach($this->_cells as $key => $cell)
  73          {
  74              $cells .= "\t\t\t<td";
  75              if($key == 0)
  76              {
  77                  $cell['extra']['class'] .= " first";
  78              }
  79              elseif(!isset($this->_cells[$key+1]))
  80              {
  81                  $cell['extra']['class'] .= " last";
  82              }
  83              if($i == 2)
  84              {
  85                  $cell['extra']['class'] .= " alt_col";
  86                  $i = 0;
  87              }
  88              $i++;
  89              if(isset($cell['extra']['class']))            
  90              {
  91                  $cells .= " class=\"".trim($cell['extra']['class'])."\"";
  92              }
  93              if(isset($cell['extra']['style']))            
  94              {
  95                  $cells .= " style=\"".$cell['extra']['style']."\"";
  96              }
  97              if(isset($cell['extra']['id']))
  98              {
  99                  $cells .= $cell['extra']['id'];
 100              }
 101              if(isset($cell['extra']['colspan']) && $cell['extra']['colspan'] > 1)
 102              {
 103                  $cells .= " colspan=\"".$cell['extra']['colspan']."\"";
 104              }
 105              if(isset($cell['extra']['rowspan']) && $cell['extra']['rowspan'] > 1)
 106              {
 107                  $cells .= " rowspan=\"".$cell['extra']['rowspan']."\"";
 108              }
 109              if(isset($cell['extra']['width']))            
 110              {
 111                  $cells .= " width=\"".$cell['extra']['width']."\"";
 112              }
 113              $cells .= ">";
 114              $cells .= $cell['data'];
 115              $cells .= "</td>\n";
 116          }
 117          $data['cells'] = $cells;
 118          $data['extra'] = $extra;
 119          $this->_rows[] = $data;
 120          
 121          $this->_cells = array();
 122      }
 123  
 124      /**
 125       * Construct a header cell for this table.
 126       *
 127       * @param string The HTML content for this header cell.
 128       * @param array Array of extra information for this header cell (class, style, colspan, width)
 129       */
 130  	function construct_header($data, $extra=array())
 131      {
 132          foreach(array('class', 'style', 'rowspan', 'width') as $field)
 133          {
 134              // Common-used fields
 135              if(!isset($extra[$field]))
 136              {
 137                  $extra[$field] = '';
 138              }
 139          }
 140  
 141          $this->_headers[] = array("data" => $data, "extra" => $extra);
 142      }
 143  
 144      /**
 145       * return the cells of a row for the table based row.
 146       *
 147       * @param string The id of the row you want to give it.
 148       * @param boolean Whether or not to return or echo the resultant contents.
 149       * @return string The output of the row cells (optional).
 150       */
 151  	function output_row_cells($row_id, $return=false)
 152      {
 153          $row = $this->_rows[$row_id]['cells'];
 154          
 155          if(!$return)
 156          {
 157              echo $row;
 158          }
 159          else
 160          {
 161              return $row;
 162          }
 163      }
 164  
 165      /**
 166       * Count the number of rows in the table. Useful for displaying a 'no rows' message.
 167       *
 168       * @return int The number of rows in the table.
 169       */
 170  	function num_rows()
 171      {
 172          return count($this->_rows);
 173      }
 174  
 175      /**
 176       * Output this table to the browser.
 177       *
 178       * @param string The heading for this table.
 179       * @param int The border width for this table.
 180       * @param string The class for this table.
 181       * @param boolean Whether or not to return or echo the resultant contents.
 182       * @return string The output of the row cells (optional).
 183       */
 184  	function output($heading="", $border=1, $class="general", $return=false)
 185      {
 186          if($return == true)
 187          {
 188              return $this->construct_html($heading, $border, $class);
 189          }
 190          else
 191          {
 192              echo $this->construct_html($heading, $border, $class);
 193          }
 194      }
 195  
 196      /**
 197       * Fetch the built HTML for this table.
 198       *
 199       * @param string The heading for this table.
 200       * @param int The border width for this table.
 201       * @param string The class for this table.
 202       * @param string The id for this table.
 203       * @return string The built HTML.
 204       */
 205  	function construct_html($heading="", $border=1, $class=null, $table_id="")
 206      {
 207          $table = '';
 208          if($border == 1)
 209          {
 210              $table .= "<div class=\"border_wrapper\">\n";
 211              if($heading != "")
 212              {
 213                  $table .= "    <div class=\"title\">".$heading."</div>\n";
 214              }
 215          }
 216          $table .= "<table";
 217          if(!is_null($class))
 218          {
 219              if(!$class)
 220              {
 221                  $class = "general";
 222              }
 223              $table .= " class=\"".$class."\"";
 224          }
 225          if($table_id != "")
 226          {
 227              $table .= " id=\"".$table_id."\"";
 228          }
 229          $table .= " cellspacing=\"0\">\n";
 230          if($this->_headers)
 231          {
 232              $table .= "\t<thead>\n";
 233              $table .= "\t\t<tr>\n";
 234              foreach($this->_headers as $key => $data)
 235              {
 236                  $table .= "\t\t\t<th";
 237                  if($key == 0)
 238                  {
 239                      $data['extra']['class'] .= " first";
 240                  }
 241                  elseif(!isset($this->_headers[$key+1]))
 242                  {
 243                      $data['extra']['class'] .= " last";
 244                  }
 245                  if(isset($data['extra']['class']))
 246                  {
 247                      $table .= " class=\"".$data['extra']['class']."\"";
 248                  }
 249                  if(isset($data['extra']['style']))
 250                  {
 251                      $table .= " style=\"".$data['extra']['style']."\"";
 252                  }
 253                  if(isset($data['extra']['width']))
 254                  {
 255                      $table .= " width=\"".$data['extra']['width']."\"";
 256                  }
 257                  if(isset($data['extra']['colspan']) && $data['extra']['colspan'] > 1)
 258                  {
 259                      $table .= " colspan=\"".$data['extra']['colspan']."\"";
 260                  }
 261                  $table .= ">".$data['data']."</th>\n";
 262              }
 263              $table .= "\t\t</tr>\n";
 264              $table .= "\t</thead>\n";
 265          }
 266          $table .= "\t<tbody>\n";
 267          $i = 1;
 268          foreach($this->_rows as $key => $table_row)
 269          {
 270              $table .= "\t\t<tr";
 271              if($table_row['extra']['id'])
 272              {
 273                  $table .= " id=\"{$table_row['extra']['id']}\"";
 274              }
 275              if($key == 0)
 276              {
 277                  $table_row['extra']['class'] .= " first";
 278              }
 279              else if(!isset($this->_rows[$key+1]))
 280              {
 281                  $table_row['extra']['class'] .= " last";
 282              }
 283              if($i == 2 && !isset($table_row['extra']['no_alt_row']))
 284              {
 285                  $table_row['extra']['class'] .= " alt_row";
 286                  $i = 0;
 287              }
 288              $i++;
 289              if($table_row['extra']['class'])
 290              {
 291                  $table .= " class=\"".trim($table_row['extra']['class'])."\"";
 292              }
 293              $table .= ">\n";
 294              $table .= $table_row['cells'];
 295              $table .= "\t\t</tr>\n";
 296          }
 297          $table .= "\t</tbody>\n";
 298          $table .= "</table>\n";
 299          // Clean up
 300          $this->_cells = $this->_rows = $this->_headers = array();
 301          if($border == 1)
 302          {
 303              $table .= "</div>";
 304          }
 305          return $table;
 306      }
 307  }
 308  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1