PageRenderTime 32ms CodeModel.GetById 3ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/php-pear-HTML-Table-1.8.3/HTML_Table-1.8.3/Table.php

#
PHP | 1046 lines | 504 code | 52 blank | 490 comment | 98 complexity | e300df2ee732d977337dfe3b846769fd MD5 | raw file
   1<?php
   2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3
   4/**
   5 * PEAR::HTML_Table makes the design of HTML tables easy, flexible, reusable and
   6 * efficient.
   7 *
   8 * The PEAR::HTML_Table package provides methods for easy and efficient design
   9 * of HTML tables.
  10 * - Lots of customization options.
  11 * - Tables can be modified at any time.
  12 * - The logic is the same as standard HTML editors.
  13 * - Handles col and rowspans.
  14 * - PHP code is shorter, easier to read and to maintain.
  15 * - Tables options can be reused.
  16 *
  17 * For auto filling of data and such then check out
  18 * http://pear.php.net/package/HTML_Table_Matrix
  19 *
  20 * PHP versions 4 and 5
  21 *
  22 * LICENSE:
  23 *
  24 * Copyright (c) 2005-2007, Adam Daniel <adaniel1@eesus.jnj.com>,
  25 *                          Bertrand Mansion <bmansion@mamasam.com>,
  26 *                          Mark Wiesemann <wiesemann@php.net>
  27 * All rights reserved.
  28 *
  29 * Redistribution and use in source and binary forms, with or without
  30 * modification, are permitted provided that the following conditions
  31 * are met:
  32 *
  33 *    * Redistributions of source code must retain the above copyright
  34 *      notice, this list of conditions and the following disclaimer.
  35 *    * Redistributions in binary form must reproduce the above copyright
  36 *      notice, this list of conditions and the following disclaimer in the
  37 *      documentation and/or other materials provided with the distribution.
  38 *    * The names of the authors may not be used to endorse or promote products
  39 *      derived from this software without specific prior written permission.
  40 *
  41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  42 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  43 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  45 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  46 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  47 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  48 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  49 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  50 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  51 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52
  53 *
  54 * @category   HTML
  55 * @package    HTML_Table
  56 * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  57 * @author     Bertrand Mansion <bmansion@mamasam.com>
  58 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
  59 * @version    CVS: $Id: Table.php 297540 2010-04-05 19:58:39Z wiesemann $
  60 * @link       http://pear.php.net/package/HTML_Table
  61 */
  62
  63
  64/**
  65* Requires PEAR, HTML_Common and HTML_Table_Storage
  66*/
  67require_once 'PEAR.php';
  68require_once 'HTML/Common.php';
  69require_once 'HTML/Table/Storage.php';
  70
  71/**
  72 * PEAR::HTML_Table makes the design of HTML tables easy, flexible, reusable and efficient.
  73 *
  74 * The PEAR::HTML_Table package provides methods for easy and efficient design
  75 * of HTML tables.
  76 * - Lots of customization options.
  77 * - Tables can be modified at any time.
  78 * - The logic is the same as standard HTML editors.
  79 * - Handles col and rowspans.
  80 * - PHP code is shorter, easier to read and to maintain.
  81 * - Tables options can be reused.
  82 *
  83 * For auto filling of data and such then check out
  84 * http://pear.php.net/package/HTML_Table_Matrix
  85 *
  86 * @category   HTML
  87 * @package    HTML_Table
  88 * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  89 * @author     Bertrand Mansion <bmansion@mamasam.com>
  90 * @copyright  2005-2006 The PHP Group
  91 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
  92 * @version    Release: @package_version@
  93 * @link       http://pear.php.net/package/HTML_Table
  94 */
  95class HTML_Table extends HTML_Common {
  96
  97    /**
  98     * Value to insert into empty cells. This is used as a default for
  99     * newly-created tbodies.
 100     * @var    string
 101     * @access private
 102     */
 103    var $_autoFill = '&nbsp;';
 104
 105    /**
 106     * Automatically adds a new row, column, or body if a given row, column, or
 107     * body index does not exist.
 108     * This is used as a default for newly-created tbodies.
 109     * @var    bool
 110     * @access private
 111     */
 112    var $_autoGrow = true;
 113
 114    /**
 115     * Array containing the table caption
 116     * @var     array
 117     * @access  private
 118     */
 119    var $_caption = array();
 120
 121    /**
 122     * Array containing the table column group specifications
 123     *
 124     * @var     array
 125     * @author  Laurent Laville (pear at laurent-laville dot org)
 126     * @access  private
 127     */
 128    var $_colgroup = array();
 129
 130    /**
 131     * HTML_Table_Storage object for the (t)head of the table
 132     * @var    object
 133     * @access private
 134     */
 135    var $_thead = null;
 136
 137    /**
 138     * HTML_Table_Storage object for the (t)foot of the table
 139     * @var    object
 140     * @access private
 141     */
 142    var $_tfoot = null;
 143
 144    /**
 145     * HTML_Table_Storage object for the (t)body of the table
 146     * @var    object
 147     * @access private
 148     */
 149    var $_tbodies = array();
 150
 151    /**
 152     * Number of bodies in the table
 153     * @var    int
 154     * @access private
 155     */
 156    var $_tbodyCount = 0;
 157
 158    /**
 159     * Whether to use <thead>, <tfoot> and <tbody> or not
 160     * @var    bool
 161     * @access private
 162     */
 163    var $_useTGroups = false;
 164
 165    /**
 166     * Class constructor
 167     * @param    array    $attributes        Associative array of table tag
 168     *                                       attributes
 169     * @param    int      $tabOffset         Tab offset of the table
 170     * @param    bool     $useTGroups        Whether to use <thead>, <tfoot> and
 171     *                                       <tbody> or not
 172     * @access   public
 173     */
 174    function HTML_Table($attributes = null, $tabOffset = 0, $useTGroups = false)
 175    {
 176        HTML_Common::HTML_Common($attributes, (int)$tabOffset);
 177        $this->_useTGroups = (boolean)$useTGroups;
 178        $this->addBody();
 179        if ($this->_useTGroups) {
 180            $this->_thead = new HTML_Table_Storage($tabOffset, $this->_useTGroups);
 181            $this->_tfoot = new HTML_Table_Storage($tabOffset, $this->_useTGroups);
 182        }
 183    }
 184
 185    /**
 186     * Returns the API version
 187     * @access  public
 188     * @return  double
 189     * @deprecated
 190     */
 191    function apiVersion()
 192    {
 193        return 1.7;
 194    }
 195
 196    /**
 197     * Returns the HTML_Table_Storage object for <thead>
 198     * @access  public
 199     * @return  object
 200     */
 201    function &getHeader()
 202    {
 203        if (is_null($this->_thead)) {
 204            $this->_useTGroups = true;
 205            $this->_thead = new HTML_Table_Storage($this->_tabOffset,
 206                                                    $this->_useTGroups);
 207            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 208                $this->_tbodies[$i]->setUseTGroups(true);
 209            }
 210        }
 211        return $this->_thead;
 212    }
 213
 214    /**
 215     * Returns the HTML_Table_Storage object for <tfoot>
 216     * @access  public
 217     * @return  object
 218     */
 219    function &getFooter()
 220    {
 221        if (is_null($this->_tfoot)) {
 222            $this->_useTGroups = true;
 223            $this->_tfoot = new HTML_Table_Storage($this->_tabOffset,
 224                                                    $this->_useTGroups);
 225            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 226                $this->_tbodies[$i]->setUseTGroups(true);
 227            }
 228        }
 229        return $this->_tfoot;
 230    }
 231
 232    /**
 233     * Returns the HTML_Table_Storage object for the specified <tbody>
 234     * (or the whole table if <t{head|foot|body}> is not used)
 235     * @param   int       $body              (optional) The index of the body to
 236     *                                       return.
 237     * @access  public
 238     * @return  object
 239     * @throws  PEAR_Error
 240     */
 241    function &getBody($body = 0)
 242    {
 243        $ret = $this->_adjustTbodyCount($body, 'getBody');
 244        if (PEAR::isError($ret)) {
 245            return $ret;
 246        }
 247        return $this->_tbodies[$body];
 248    }
 249
 250    /**
 251     * Adds a table body and returns the body identifier
 252     * @param   mixed        $attributes     (optional) Associative array or
 253     *                                       string of table body attributes
 254     * @access  public
 255     * @return  int
 256     */
 257    function addBody($attributes = null)
 258    {
 259        if (!$this->_useTGroups && $this->_tbodyCount > 0) {
 260            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 261                $this->_tbodies[$i]->setUseTGroups(true);
 262            }
 263            $this->_useTGroups = true;
 264        }
 265
 266        $body = $this->_tbodyCount++;
 267        $this->_tbodies[$body] = new HTML_Table_Storage($this->_tabOffset,
 268                                                         $this->_useTGroups);
 269        $this->_tbodies[$body]->setAutoFill($this->_autoFill);
 270        $this->_tbodies[$body]->setAttributes($attributes);
 271        return $body;
 272    }
 273
 274    /**
 275     * Adjusts the number of bodies
 276     * @param   int          $body           Body index
 277     * @param   string       $method         Name of calling method
 278     * @access  private
 279     * @throws  PEAR_Error
 280     */
 281    function _adjustTbodyCount($body, $method)
 282    {
 283        if ($this->_autoGrow) {
 284            while ($this->_tbodyCount <= (int)$body) {
 285                $this->addBody();
 286            }
 287        } else {
 288            return PEAR::raiseError('Invalid body reference[' .
 289                $body . '] in HTML_Table::' . $method);
 290        }
 291    }
 292
 293    /**
 294     * Sets the table caption
 295     * @param   string    $caption
 296     * @param   mixed     $attributes        Associative array or string of
 297     *                                       table row attributes
 298     * @access  public
 299     */
 300    function setCaption($caption, $attributes = null)
 301    {
 302        $attributes = $this->_parseAttributes($attributes);
 303        $this->_caption = array('attr' => $attributes, 'contents' => $caption);
 304    }
 305
 306    /**
 307     * Sets the table columns group specifications, or removes existing ones.
 308     *
 309     * @param   mixed     $colgroup        (optional) Columns attributes
 310     * @param   mixed     $attributes      (optional) Associative array or string
 311     *                                                  of table row attributes
 312     * @author  Laurent Laville (pear at laurent-laville dot org)
 313     * @access  public
 314     */
 315    function setColGroup($colgroup = null, $attributes = null)
 316    {
 317        if (isset($colgroup)) {
 318            $attributes = $this->_parseAttributes($attributes);
 319            $this->_colgroup[] = array('attr' => $attributes,
 320                                       'contents' => $colgroup);
 321        } else {
 322            $this->_colgroup = array();
 323        }
 324    }
 325
 326    /**
 327     * Sets the autoFill value
 328     * @param   mixed   $fill          Whether autoFill should be enabled or not
 329     * @param   int     $body          (optional) The index of the body to set.
 330     *                                 Pass null to set for all bodies.
 331     * @access  public
 332     * @throws  PEAR_Error
 333     */
 334    function setAutoFill($fill, $body = null)
 335    {
 336        if (!is_null($body)) {
 337            $ret = $this->_adjustTbodyCount($body, 'setAutoFill');
 338            if (PEAR::isError($ret)) {
 339                return $ret;
 340            }
 341            $this->_tbodies[$body]->setAutoFill($fill);
 342        } else {
 343            $this->_autoFill = $fill;
 344            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 345                $this->_tbodies[$i]->setAutoFill($fill);
 346            }
 347        }
 348    }
 349
 350    /**
 351     * Returns the autoFill value
 352     * @param    int         $body   (optional) The index of the body to get.
 353     *                               Pass null to get the default for new bodies.
 354     * @access   public
 355     * @return   mixed
 356     * @throws   PEAR_Error
 357     */
 358    function getAutoFill($body = null)
 359    {
 360        if (!is_null($body)) {
 361            $ret = $this->_adjustTbodyCount($body, 'getAutoFill');
 362            if (PEAR::isError($ret)) {
 363                return $ret;
 364            }
 365            return $this->_tbodies[$body]->getAutoFill();
 366        } else {
 367            return $this->_autoFill;
 368        }
 369    }
 370
 371    /**
 372     * Sets the autoGrow value
 373     * @param    bool     $grow        Whether autoGrow should be enabled or not
 374     * @param    int      $body        (optional) The index of the body to set.
 375     *                                 Pass null to set for all bodies.
 376     * @access   public
 377     * @throws   PEAR_Error
 378     */
 379    function setAutoGrow($grow, $body = null)
 380    {
 381        if (!is_null($body)) {
 382            $ret = $this->_adjustTbodyCount($body, 'setAutoGrow');
 383            if (PEAR::isError($ret)) {
 384                return $ret;
 385            }
 386            $this->_tbodies[$body]->setAutoGrow($grow);
 387        } else {
 388            $this->_autoGrow = $grow;
 389            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 390                $this->_tbodies[$i]->setAutoGrow($grow);
 391            }
 392        }
 393    }
 394
 395    /**
 396     * Returns the autoGrow value
 397     * @param    int     $body       (optional) The index of the body to get.
 398     *                               Pass null to get the default for new bodies.
 399     * @access   public
 400     * @return   mixed
 401     * @throws   PEAR_Error
 402     */
 403    function getAutoGrow($body = null)
 404    {
 405        if (!is_null($body)) {
 406            $ret = $this->_adjustTbodyCount($body, 'getAutoGrow');
 407            if (PEAR::isError($ret)) {
 408                return $ret;
 409            }
 410            return $this->_tbodies[$body]->getAutoGrow();
 411        } else {
 412            return $this->_autoGrow;
 413        }
 414    }
 415
 416    /**
 417     * Sets the number of rows in the table body
 418     * @param    int       $rows       The number of rows
 419     * @param    int       $body       (optional) The index of the body to set.
 420     * @access   public
 421     * @throws   PEAR_Error
 422     */
 423    function setRowCount($rows, $body = 0)
 424    {
 425        $ret = $this->_adjustTbodyCount($body, 'setRowCount');
 426        if (PEAR::isError($ret)) {
 427            return $ret;
 428        }
 429        $this->_tbodies[$body]->setRowCount($rows);
 430    }
 431
 432    /**
 433     * Sets the number of columns in the table
 434     * @param    int         $cols      The number of columns
 435     * @param    int         $body      (optional) The index of the body to set.
 436     * @access   public
 437     * @throws   PEAR_Error
 438     */
 439    function setColCount($cols, $body = 0)
 440    {
 441        $ret = $this->_adjustTbodyCount($body, 'setColCount');
 442        if (PEAR::isError($ret)) {
 443            return $ret;
 444        }
 445        $this->_tbodies[$body]->setColCount($cols);
 446    }
 447
 448    /**
 449     * Returns the number of rows in the table
 450     * @param    int    $body           (optional) The index of the body to get.
 451     *                                  Pass null to get the total number of
 452     *                                  rows in all bodies.
 453     * @access   public
 454     * @return   int
 455     * @throws   PEAR_Error
 456     */
 457    function getRowCount($body = null)
 458    {
 459        if (!is_null($body)) {
 460            $ret = $this->_adjustTbodyCount($body, 'getRowCount');
 461            if (PEAR::isError($ret)) {
 462                return $ret;
 463            }
 464            return $this->_tbodies[$body]->getRowCount();
 465        } else {
 466            $rowCount = 0;
 467            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 468                $rowCount += $this->_tbodies[$i]->getRowCount();
 469            }
 470            return $rowCount;
 471        }
 472    }
 473
 474    /**
 475     * Gets the number of columns in the table
 476     *
 477     * If a row index is specified, the count will not take
 478     * the spanned cells into account in the return value.
 479     *
 480     * @param    int      $row          Row index to serve for cols count
 481     * @param    int      $body         (optional) The index of the body to get.
 482     * @access   public
 483     * @return   int
 484     * @throws   PEAR_Error
 485     */
 486    function getColCount($row = null, $body = 0)
 487    {
 488        $ret = $this->_adjustTbodyCount($body, 'getColCount');
 489        if (PEAR::isError($ret)) {
 490            return $ret;
 491        }
 492        return $this->_tbodies[$body]->getColCount($row);
 493    }
 494
 495    /**
 496     * Sets a rows type 'TH' or 'TD'
 497     * @param    int         $row    Row index
 498     * @param    string      $type   'TH' or 'TD'
 499     * @param    int         $body   (optional) The index of the body to set.
 500     * @access   public
 501     * @throws   PEAR_Error
 502     */
 503    function setRowType($row, $type, $body = 0)
 504    {
 505        $ret = $this->_adjustTbodyCount($body, 'setRowType');
 506        if (PEAR::isError($ret)) {
 507            return $ret;
 508        }
 509        $this->_tbodies[$body]->setRowType($row, $type);
 510    }
 511
 512    /**
 513     * Sets a columns type 'TH' or 'TD'
 514     * @param    int         $col    Column index
 515     * @param    string      $type   'TH' or 'TD'
 516     * @param    int         $body   (optional) The index of the body to set.
 517     *                               Pass null to set for all bodies.
 518     * @access   public
 519     * @throws   PEAR_Error
 520     */
 521    function setColType($col, $type, $body = null)
 522    {
 523        if (!is_null($body)) {
 524            $ret = $this->_adjustTbodyCount($body, 'setColType');
 525            if (PEAR::isError($ret)) {
 526                return $ret;
 527            }
 528            $this->_tbodies[$body]->setColType($col, $type);
 529        } else {
 530            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 531                $this->_tbodies[$i]->setColType($col, $type);
 532            }
 533        }
 534    }
 535
 536    /**
 537     * Sets the cell attributes for an existing cell.
 538     *
 539     * If the given indices do not exist and autoGrow is true then the given
 540     * row and/or col is automatically added.  If autoGrow is false then an
 541     * error is returned.
 542     * @param    int     $row          Row index
 543     * @param    int     $col          Column index
 544     * @param    mixed   $attributes   Associative array or string of
 545     *                                 table row attributes
 546     * @param    int     $body         (optional) The index of the body to set.
 547     * @access   public
 548     * @throws   PEAR_Error
 549     */
 550    function setCellAttributes($row, $col, $attributes, $body = 0)
 551    {
 552        $ret = $this->_adjustTbodyCount($body, 'setCellAttributes');
 553        if (PEAR::isError($ret)) {
 554            return $ret;
 555        }
 556        $ret = $this->_tbodies[$body]->setCellAttributes($row, $col, $attributes);
 557        if (PEAR::isError($ret)) {
 558            return $ret;
 559        }
 560    }
 561
 562    /**
 563     * Updates the cell attributes passed but leaves other existing attributes
 564     * intact
 565     * @param    int      $row          Row index
 566     * @param    int      $col          Column index
 567     * @param    mixed    $attributes   Associative array or string of table row
 568     *                                  attributes
 569     * @param    int      $body         (optional) The index of the body to set.
 570     * @access   public
 571     * @throws   PEAR_Error
 572     */
 573    function updateCellAttributes($row, $col, $attributes, $body = 0)
 574    {
 575        $ret = $this->_adjustTbodyCount($body, 'updateCellAttributes');
 576        if (PEAR::isError($ret)) {
 577            return $ret;
 578        }
 579        $ret = $this->_tbodies[$body]->updateCellAttributes($row, $col, $attributes);
 580        if (PEAR::isError($ret)) {
 581            return $ret;
 582        }
 583    }
 584
 585    /**
 586     * Returns the attributes for a given cell
 587     * @param    int         $row        Row index
 588     * @param    int         $col        Column index
 589     * @param    int         $body       (optional) The index of the body to get.
 590     * @return   array
 591     * @access   public
 592     * @throws   PEAR_Error
 593     */
 594    function getCellAttributes($row, $col, $body = 0)
 595    {
 596        $ret = $this->_adjustTbodyCount($body, 'getCellAttributes');
 597        if (PEAR::isError($ret)) {
 598            return $ret;
 599        }
 600        return $this->_tbodies[$body]->getCellAttributes($row, $col);
 601    }
 602
 603    /**
 604     * Sets the cell contents for an existing cell
 605     *
 606     * If the given indices do not exist and autoGrow is true then the given
 607     * row and/or col is automatically added.  If autoGrow is false then an
 608     * error is returned.
 609     * @param    int      $row         Row index
 610     * @param    int      $col         Column index
 611     * @param    mixed    $contents    May contain html or any object with a
 612     *                                 toHTML() method; it is an array (with
 613     *                                 strings and/or objects), $col will be
 614     *                                 used as start offset and the array
 615     *                                 elements will be set to this and the
 616     *                                 following columns in $row
 617     * @param    string   $type        (optional) Cell type either 'TH' or 'TD'
 618     * @param    int      $body        (optional) The index of the body to set.
 619     * @access   public
 620     * @throws   PEAR_Error
 621     */
 622    function setCellContents($row, $col, $contents, $type = 'TD', $body = 0)
 623    {
 624        $ret = $this->_adjustTbodyCount($body, 'setCellContents');
 625        if (PEAR::isError($ret)) {
 626            return $ret;
 627        }
 628        $ret = $this->_tbodies[$body]->setCellContents($row, $col, $contents, $type);
 629        if (PEAR::isError($ret)) {
 630            return $ret;
 631        }
 632    }
 633
 634    /**
 635     * Returns the cell contents for an existing cell
 636     * @param    int        $row    Row index
 637     * @param    int        $col    Column index
 638     * @param    int        $body   (optional) The index of the body to get.
 639     * @access   public
 640     * @return   mixed
 641     * @throws   PEAR_Error
 642     */
 643    function getCellContents($row, $col, $body = 0)
 644    {
 645        $ret = $this->_adjustTbodyCount($body, 'getCellContents');
 646        if (PEAR::isError($ret)) {
 647            return $ret;
 648        }
 649        return $this->_tbodies[$body]->getCellContents($row, $col);
 650    }
 651
 652    /**
 653     * Sets the contents of a header cell
 654     * @param    int      $row
 655     * @param    int      $col
 656     * @param    mixed    $contents
 657     * @param    mixed    $attributes   Associative array or string of
 658     *                                  table row attributes
 659     * @param    int      $body         (optional) The index of the body to set.
 660     * @access   public
 661     * @throws   PEAR_Error
 662     */
 663    function setHeaderContents($row, $col, $contents, $attributes = null,
 664        $body = 0)
 665    {
 666        $ret = $this->_adjustTbodyCount($body, 'setHeaderContents');
 667        if (PEAR::isError($ret)) {
 668            return $ret;
 669        }
 670        $this->_tbodies[$body]->setHeaderContents($row, $col, $contents, $attributes);
 671    }
 672
 673    /**
 674     * Adds a table row and returns the row identifier
 675     * @param    array     $contents     (optional) Must be a indexed array of
 676     *                                   valid cell contents
 677     * @param    mixed     $attributes   (optional) Associative array or string
 678     *                                   of table row attributes. This can also
 679     *                                   be an array of attributes, in which
 680     *                                   case the attributes will be repeated
 681     *                                   in a loop.
 682     * @param    string    $type         (optional) Cell type either 'th' or 'td'
 683     * @param    bool      $inTR         false if attributes are to be applied
 684     *                                   in TD tags; true if attributes are to
 685     *                                  ┬┤be applied in TR tag
 686     * @param    int       $body         (optional) The index of the body to use.
 687     * @return   int
 688     * @access   public
 689     * @throws   PEAR_Error
 690     */
 691    function addRow($contents = null, $attributes = null, $type = 'td',
 692        $inTR = false, $body = 0)
 693    {
 694        $ret = $this->_adjustTbodyCount($body, 'addRow');
 695        if (PEAR::isError($ret)) {
 696            return $ret;
 697        }
 698        $ret = $this->_tbodies[$body]->addRow($contents, $attributes, $type, $inTR);
 699        return $ret;
 700    }
 701
 702    /**
 703     * Sets the row attributes for an existing row
 704     * @param    int      $row          Row index
 705     * @param    mixed    $attributes   Associative array or string of table row
 706     *                                  attributes. This can also be an array of
 707     *                                  attributes, in which case the attributes
 708     *                                  will be repeated in a loop.
 709     * @param    bool     $inTR         false if attributes are to be applied in
 710     *                                  TD tags; true if attributes are to be
 711     *                                  applied in TR tag
 712     * @param    int      $body         (optional) The index of the body to set.
 713     * @access   public
 714     * @throws   PEAR_Error
 715     */
 716    function setRowAttributes($row, $attributes, $inTR = false, $body = 0)
 717    {
 718        $ret = $this->_adjustTbodyCount($body, 'setRowAttributes');
 719        if (PEAR::isError($ret)) {
 720            return $ret;
 721        }
 722        $ret = $this->_tbodies[$body]->setRowAttributes($row, $attributes, $inTR);
 723        if (PEAR::isError($ret)) {
 724            return $ret;
 725        }
 726    }
 727
 728    /**
 729     * Updates the row attributes for an existing row
 730     * @param    int      $row          Row index
 731     * @param    mixed    $attributes   Associative array or string of table row
 732     *                                  attributes
 733     * @param    bool     $inTR         false if attributes are to be applied in
 734     *                                  TD tags; true if attributes are to be
 735     *                                  applied in TR tag
 736     * @param    int      $body         (optional) The index of the body to set.
 737     * @access   public
 738     * @throws   PEAR_Error
 739     */
 740    function updateRowAttributes($row, $attributes = null, $inTR = false,
 741        $body = 0)
 742    {
 743        $ret = $this->_adjustTbodyCount($body, 'updateRowAttributes');
 744        if (PEAR::isError($ret)) {
 745            return $ret;
 746        }
 747        $ret = $this->_tbodies[$body]->updateRowAttributes($row, $attributes, $inTR);
 748        if (PEAR::isError($ret)) {
 749            return $ret;
 750        }
 751    }
 752
 753    /**
 754     * Returns the attributes for a given row as contained in the TR tag
 755     * @param    int      $row       Row index
 756     * @param    int      $body      (optional) The index of the body to get.
 757     * @return   array
 758     * @access   public
 759     * @throws   PEAR_Error
 760     */
 761    function getRowAttributes($row, $body = 0)
 762    {
 763        $ret = $this->_adjustTbodyCount($body, 'getRowAttributes');
 764        if (PEAR::isError($ret)) {
 765            return $ret;
 766        }
 767        return $this->_tbodies[$body]->getRowAttributes($row);
 768    }
 769
 770    /**
 771     * Alternates the row attributes starting at $start
 772     * @param   int     $start            Row index of row in which alternating
 773     *                                    begins
 774     * @param   mixed   $attributes1      Associative array or string of table
 775     *                                    row attributes
 776     * @param   mixed   $attributes2      Associative array or string of table
 777     *                                    row attributes
 778     * @param   bool    $inTR             false if attributes are to be applied
 779     *                                    in TD tags; true if attributes are to
 780     *                                    be applied in TR tag
 781     * @param   int     $firstAttributes  (optional) Which attributes should be
 782     *                                    applied to the first row, 1 or 2.
 783     * @param   int     $body             (optional) The index of the body to set.
 784     *                                    Pass null to set for all bodies.
 785     * @access  public
 786     * @throws  PEAR_Error
 787     */
 788    function altRowAttributes($start, $attributes1, $attributes2, $inTR = false,
 789        $firstAttributes = 1, $body = null)
 790    {
 791        if (!is_null($body)) {
 792            $ret = $this->_adjustTbodyCount($body, 'altRowAttributes');
 793            if (PEAR::isError($ret)) {
 794                return $ret;
 795            }
 796            $this->_tbodies[$body]->altRowAttributes($start, $attributes1,
 797                $attributes2, $inTR, $firstAttributes);
 798        } else {
 799            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 800                $this->_tbodies[$i]->altRowAttributes($start, $attributes1,
 801                    $attributes2, $inTR, $firstAttributes);
 802                // if the tbody's row count is odd, toggle $firstAttributes to
 803                // prevent the next tbody's first row from having the same
 804                // attributes as this tbody's last row.
 805                if ($this->_tbodies[$i]->getRowCount() % 2) {
 806                    $firstAttributes ^= 3;
 807                }
 808            }
 809        }
 810    }
 811
 812    /**
 813     * Adds a table column and returns the column identifier
 814     * @param    array     $contents     (optional) Must be a indexed array of
 815     *                                   valid cell contents
 816     * @param    mixed     $attributes   (optional) Associative array or string
 817     *                                   of table row attributes
 818     * @param    string    $type         (optional) Cell type either 'th' or 'td'
 819     * @param    int       $body         (optional) The index of the body to use.
 820     * @return   int
 821     * @access   public
 822     * @throws   PEAR_Error
 823     */
 824    function addCol($contents = null, $attributes = null, $type = 'td', $body = 0)
 825    {
 826        $ret = $this->_adjustTbodyCount($body, 'addCol');
 827        if (PEAR::isError($ret)) {
 828            return $ret;
 829        }
 830        return $this->_tbodies[$body]->addCol($contents, $attributes, $type);
 831    }
 832
 833    /**
 834     * Sets the column attributes for an existing column
 835     * @param    int       $col          Column index
 836     * @param    mixed     $attributes   (optional) Associative array or string
 837     *                                   of table row attributes
 838     * @param    int       $body         (optional) The index of the body to set.
 839     *                                   Pass null to set for all bodies.
 840     * @access   public
 841     * @throws   PEAR_Error
 842     */
 843    function setColAttributes($col, $attributes = null, $body = null)
 844    {
 845        if (!is_null($body)) {
 846            $ret = $this->_adjustTbodyCount($body, 'setColAttributes');
 847            if (PEAR::isError($ret)) {
 848                return $ret;
 849            }
 850            $this->_tbodies[$body]->setColAttributes($col, $attributes);
 851        } else {
 852            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 853                $this->_tbodies[$i]->setColAttributes($col, $attributes);
 854            }
 855        }
 856    }
 857
 858    /**
 859     * Updates the column attributes for an existing column
 860     * @param    int       $col          Column index
 861     * @param    mixed     $attributes   (optional) Associative array or
 862     *                                   string of table row attributes
 863     * @param    int       $body         (optional) The index of the body to set.
 864     *                                   Pass null to set for all bodies.
 865     * @access   public
 866     * @throws   PEAR_Error
 867     */
 868    function updateColAttributes($col, $attributes = null, $body = null)
 869    {
 870        if (!is_null($body)) {
 871            $ret = $this->_adjustTbodyCount($body, 'updateColAttributes');
 872            if (PEAR::isError($ret)) {
 873                return $ret;
 874            }
 875            $this->_tbodies[$body]->updateColAttributes($col, $attributes);
 876        } else {
 877            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 878                $this->_tbodies[$i]->updateColAttributes($col, $attributes);
 879            }
 880        }
 881    }
 882
 883    /**
 884     * Sets the attributes for all cells
 885     * @param    mixed    $attributes    (optional) Associative array or
 886     *                                   string of table row attributes
 887     * @param    int      $body          (optional) The index of the body to set.
 888     *                                   Pass null to set for all bodies.
 889     * @access   public
 890     * @throws   PEAR_Error
 891     */
 892    function setAllAttributes($attributes = null, $body = null)
 893    {
 894        if (!is_null($body)) {
 895            $ret = $this->_adjustTbodyCount($body, 'setAllAttributes');
 896            if (PEAR::isError($ret)) {
 897                return $ret;
 898            }
 899            $this->_tbodies[$body]->setAllAttributes($attributes);
 900        } else {
 901            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 902                $this->_tbodies[$i]->setAllAttributes($attributes);
 903            }
 904        }
 905    }
 906
 907    /**
 908     * Updates the attributes for all cells
 909     * @param    mixed    $attributes   (optional) Associative array or string
 910     *                                  of table row attributes
 911     * @param    int      $body         (optional) The index of the body to set.
 912     *                                  Pass null to set for all bodies.
 913     * @access   public
 914     * @throws   PEAR_Error
 915     */
 916    function updateAllAttributes($attributes = null, $body = null)
 917    {
 918        if (!is_null($body)) {
 919            $ret = $this->_adjustTbodyCount($body, 'updateAllAttributes');
 920            if (PEAR::isError($ret)) {
 921                return $ret;
 922            }
 923            $this->_tbodies[$body]->updateAllAttributes($attributes);
 924        } else {
 925            for ($i = 0; $i < $this->_tbodyCount; $i++) {
 926                $this->_tbodies[$i]->updateAllAttributes($attributes);
 927            }
 928        }
 929    }
 930
 931    /**
 932     * Returns the table structure as HTML
 933     * @access  public
 934     * @return  string
 935     */
 936    function toHtml()
 937    {
 938        $strHtml = '';
 939        $tabs = $this->_getTabs();
 940        $tab = $this->_getTab();
 941        $lnEnd = $this->_getLineEnd();
 942        $tBodyColCounts = array();
 943        for ($i = 0; $i < $this->_tbodyCount; $i++) {
 944            $tBodyColCounts[] = $this->_tbodies[$i]->getColCount();
 945        }
 946        $tBodyMaxColCount = 0;
 947        if (count($tBodyColCounts) > 0) {
 948            $tBodyMaxColCount = max($tBodyColCounts);
 949        }
 950        if ($this->_comment) {
 951            $strHtml .= $tabs . "<!-- $this->_comment -->" . $lnEnd;
 952        }
 953        if ($this->getRowCount() > 0 && $tBodyMaxColCount > 0) {
 954            $strHtml .=
 955                $tabs . '<table' . $this->_getAttrString($this->_attributes) . '>' . $lnEnd;
 956            if (!empty($this->_caption)) {
 957                $attr = $this->_caption['attr'];
 958                $contents = $this->_caption['contents'];
 959                $strHtml .= $tabs . $tab . '<caption' . $this->_getAttrString($attr) . '>';
 960                if (is_array($contents)) {
 961                    $contents = implode(', ', $contents);
 962                }
 963                $strHtml .= $contents;
 964                $strHtml .= '</caption>' . $lnEnd;
 965            }
 966            if (!empty($this->_colgroup)) {
 967                foreach ($this->_colgroup as $g => $col) {
 968                    $attr = $this->_colgroup[$g]['attr'];
 969                    $contents = $this->_colgroup[$g]['contents'];
 970                    $strHtml .= $tabs . $tab . '<colgroup' . $this->_getAttrString($attr) . '>';
 971                    if (!empty($contents)) {
 972                        $strHtml .= $lnEnd;
 973                        if (!is_array($contents)) {
 974                            $contents = array($contents);
 975                        }
 976                        foreach ($contents as $a => $colAttr) {
 977                            $attr = $this->_parseAttributes($colAttr);
 978                            $strHtml .= $tabs . $tab . $tab . '<col' . $this->_getAttrString($attr) . ' />' . $lnEnd;
 979                        }
 980                        $strHtml .= $tabs . $tab;
 981                    }
 982                    $strHtml .= '</colgroup>' . $lnEnd;
 983                }
 984            }
 985            if ($this->_useTGroups) {
 986                $tHeadColCount = 0;
 987                if ($this->_thead !== null) {
 988                    $tHeadColCount = $this->_thead->getColCount();
 989                }
 990                $tFootColCount = 0;
 991                if ($this->_tfoot !== null) {
 992                    $tFootColCount = $this->_tfoot->getColCount();
 993                }
 994                $maxColCount = max($tHeadColCount, $tFootColCount, $tBodyMaxColCount);
 995                if ($this->_thead !== null) {
 996                    $this->_thead->setColCount($maxColCount);
 997                    if ($this->_thead->getRowCount() > 0) {
 998                        $strHtml .= $tabs . $tab . '<thead' .
 999                                    $this->_getAttrString($this->_thead->_attributes) .
1000                                    '>' . $lnEnd;
1001                        $strHtml .= $this->_thead->toHtml($tabs, $tab);
1002                        $strHtml .= $tabs . $tab . '</thead>' . $lnEnd;
1003                    }
1004                }
1005                if ($this->_tfoot !== null) {
1006                    $this->_tfoot->setColCount($maxColCount);
1007                    if ($this->_tfoot->getRowCount() > 0) {
1008                        $strHtml .= $tabs . $tab . '<tfoot' .
1009                                    $this->_getAttrString($this->_tfoot->_attributes) .
1010                                    '>' . $lnEnd;
1011                        $strHtml .= $this->_tfoot->toHtml($tabs, $tab);
1012                        $strHtml .= $tabs . $tab . '</tfoot>' . $lnEnd;
1013                    }
1014                }
1015                for ($i = 0; $i < $this->_tbodyCount; $i++) {
1016                    $this->_tbodies[$i]->setColCount($maxColCount);
1017                    if ($this->_tbodies[$i]->getRowCount() > 0) {
1018                        $strHtml .= $tabs . $tab . '<tbody' .
1019                                    $this->_getAttrString($this->_tbodies[$i]->_attributes) .
1020                                    '>' . $lnEnd;
1021                        $strHtml .= $this->_tbodies[$i]->toHtml($tabs, $tab);
1022                        $strHtml .= $tabs . $tab . '</tbody>' . $lnEnd;
1023                    }
1024                }
1025            } else {
1026                for ($i = 0; $i < $this->_tbodyCount; $i++) {
1027                    $strHtml .= $this->_tbodies[$i]->toHtml($tabs, $tab);
1028                }
1029            }
1030            $strHtml .= $tabs . '</table>' . $lnEnd;
1031        }
1032        return $strHtml;
1033    }
1034
1035    /**
1036     * Returns the table structure as HTML
1037     * @access  public
1038     * @return  string
1039     */
1040    function __toString()
1041    {
1042        return $this->toHtml();
1043    }
1044}
1045
1046?>