home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / HTML / Table.php < prev    next >
Encoding:
PHP Script  |  2005-12-02  |  19.7 KB  |  608 lines

  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 efficient.
  6.  *
  7.  * The PEAR::HTML_Table package provides methods for easy and efficient design of HTML tables.
  8.  * - Lots of customization options.
  9.  * - Tables can be modified at any time.
  10.  * - The logic is the same as standard HTML editors.
  11.  * - Handles col and rowspans.
  12.  * - PHP code is shorter, easier to read and to maintain.
  13.  * - Tables options can be reused.
  14.  *
  15.  * For auto filling of data and such then check out http://pear.php.net/package/HTML_Table_Matrix
  16.  *
  17.  * PHP versions 4
  18.  *
  19.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  20.  * that is available through the world-wide-web at the following URI:
  21.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  22.  * the PHP License and are unable to obtain it through the web, please
  23.  * send a note to license@php.net so we can mail you a copy immediately.
  24.  *
  25.  * @category   HTML
  26.  * @package    HTML_Table
  27.  * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  28.  * @author     Bertrand Mansion <bmansion@mamasam.com>
  29.  * @copyright  2005 The PHP Group
  30.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  31.  * @version    CVS: $Id: Table.php,v 1.29 2005/10/25 12:41:19 wiesemann Exp $
  32.  * @link       http://pear.php.net/package/HTML_Table
  33.  */
  34.  
  35.  
  36. /**
  37. * Requires PEAR, HTML_Common and HTML_Table_Storage
  38. */
  39. require_once 'PEAR.php';
  40. require_once 'HTML/Common.php';
  41. require_once 'HTML/Table/Storage.php';
  42.  
  43. /**
  44.  * PEAR::HTML_Table makes the design of HTML tables easy, flexible, reusable and efficient.
  45.  *
  46.  * The PEAR::HTML_Table package provides methods for easy and efficient design of HTML tables.
  47.  * - Lots of customization options.
  48.  * - Tables can be modified at any time.
  49.  * - The logic is the same as standard HTML editors.
  50.  * - Handles col and rowspans.
  51.  * - PHP code is shorter, easier to read and to maintain.
  52.  * - Tables options can be reused.
  53.  *
  54.  * For auto filling of data and such then check out http://pear.php.net/package/HTML_Table_Matrix
  55.  *
  56.  * @category   HTML
  57.  * @package    HTML_Table
  58.  * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  59.  * @author     Bertrand Mansion <bmansion@mamasam.com>
  60.  * @copyright  2005 The PHP Group
  61.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  62.  * @version    Release: @package_version@
  63.  * @link       http://pear.php.net/package/HTML_Table
  64.  */
  65. class HTML_Table extends HTML_Common {
  66.  
  67.     /**
  68.      * Value to insert into empty cells
  69.      * @var    string
  70.      * @access private
  71.      */
  72.     var $_autoFill = ' ';
  73.  
  74.     /**
  75.      * Array containing the table caption
  76.      * @var     array
  77.      * @access  private
  78.      */
  79.     var $_caption = array();
  80.  
  81.     /**
  82.      * HTML_Table_Storage object for the (t)head of the table
  83.      * @var    object
  84.      * @access private
  85.      */
  86.     var $_thead = null;
  87.  
  88.     /**
  89.      * HTML_Table_Storage object for the (t)foot of the table
  90.      * @var    object
  91.      * @access private
  92.      */
  93.     var $_tfoot = null;
  94.  
  95.     /**
  96.      * HTML_Table_Storage object for the (t)body of the table
  97.      * @var    object
  98.      * @access private
  99.      */
  100.     var $_tbody = null;
  101.  
  102.     /**
  103.      * Whether to use <thead>, <tfoot> and <tbody> or not
  104.      * @var    bool
  105.      * @access private
  106.      */
  107.     var $_useTGroups = false;
  108.  
  109.     /**
  110.      * Class constructor
  111.      * @param    array    $attributes        Associative array of table tag attributes
  112.      * @param    int      $tabOffset         Tab offset of the table
  113.      * @param    bool     $useTGroups        Whether to use <thead>, <tfoot> and
  114.      *                                       <tbody> or not
  115.      * @access   public
  116.      */
  117.     function HTML_Table($attributes = null, $tabOffset = 0, $useTGroups = false)
  118.     {
  119.         $commonVersion = 1.7;
  120.         if (HTML_Common::apiVersion() < $commonVersion) {
  121.             return PEAR::raiseError('HTML_Table version ' . $this->apiVersion() . ' requires ' .
  122.                 "HTML_Common version $commonVersion or greater.", 0, PEAR_ERROR_TRIGGER);
  123.         }
  124.         HTML_Common::HTML_Common($attributes, (int)$tabOffset);
  125.         $this->_useTGroups = (boolean)$useTGroups;
  126.         $this->_tbody =& new HTML_Table_Storage($attributes, $tabOffset, $this->_useTGroups);
  127.         if ($this->_useTGroups) {
  128.             $this->_thead =& new HTML_Table_Storage($attributes, $tabOffset, $this->_useTGroups);
  129.             $this->_tfoot =& new HTML_Table_Storage($attributes, $tabOffset, $this->_useTGroups);
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Returns the API version
  135.      * @access  public
  136.      * @return  double
  137.      */
  138.     function apiVersion()
  139.     {
  140.         return 1.7;
  141.     }
  142.  
  143.     /**
  144.      * Returns the HTML_Table_Storage object for <thead>
  145.      * @access  public
  146.      * @return  object
  147.      */
  148.     function &getHeader()
  149.     {
  150.         if (is_null($this->_thead)) {
  151.             $this->_useTGroups = true;
  152.             $this->_thead =& new HTML_Table_Storage($this->_attributes,
  153.                 $this->_tabOffset, $this->_useTGroups);
  154.             $this->_tbody->setUseTGroups(true);
  155.         }
  156.         return $this->_thead;
  157.     }
  158.  
  159.     /**
  160.      * Returns the HTML_Table_Storage object for <tfoot>
  161.      * @access  public
  162.      * @return  object
  163.      */
  164.     function &getFooter()
  165.     {
  166.         if (is_null($this->_tfoot)) {
  167.             $this->_useTGroups = true;
  168.             $this->_tfoot =& new HTML_Table_Storage($this->_attributes,
  169.                 $this->_tabOffset, $this->_useTGroups);
  170.             $this->_tbody->setUseTGroups(true);
  171.         }
  172.         return $this->_tfoot;
  173.     }
  174.  
  175.     /**
  176.      * Returns the HTML_Table_Storage object for <tbody>
  177.      * (or the whole table if <t{head|foot|body> is not used)
  178.      * @access  public
  179.      * @return  object
  180.      */
  181.     function &getBody()
  182.     {
  183.         return $this->_tbody;
  184.     }
  185.  
  186.     /**
  187.      * Sets the table caption
  188.      * @param   string    $caption
  189.      * @param   mixed     $attributes        Associative array or string of table row attributes
  190.      * @access  public
  191.      */
  192.     function setCaption($caption, $attributes = null)
  193.     {
  194.         $attributes = $this->_parseAttributes($attributes);
  195.         $this->_caption = array('attr' => $attributes, 'contents' => $caption);
  196.     }
  197.  
  198.     /**
  199.      * Sets the autoFill value
  200.      * @param   mixed   $fill
  201.      * @access  public
  202.      */
  203.     function setAutoFill($fill)
  204.     {
  205.         $this->_tbody->setAutoFill($fill);
  206.     }
  207.  
  208.     /**
  209.      * Returns the autoFill value
  210.      * @access   public
  211.      * @return   mixed
  212.      */
  213.     function getAutoFill()
  214.     {
  215.         return $this->_tbody->getAutoFill();
  216.     }
  217.  
  218.     /**
  219.      * Sets the autoGrow value
  220.      * @param    bool   $fill
  221.      * @access   public
  222.      */
  223.     function setAutoGrow($grow)
  224.     {
  225.         $this->_tbody->setAutoGrow($grow);
  226.     }
  227.  
  228.     /**
  229.      * Returns the autoGrow value
  230.      * @access   public
  231.      * @return   mixed
  232.      */
  233.     function getAutoGrow()
  234.     {
  235.         return $this->_tbody->getAutoGrow();
  236.     }
  237.  
  238.     /**
  239.      * Sets the number of rows in the table
  240.      * @param    int     $rows
  241.      * @access   public
  242.      */
  243.     function setRowCount($rows)
  244.     {
  245.         $this->_tbody->setRowCount($rows);
  246.     }
  247.  
  248.     /**
  249.      * Sets the number of columns in the table
  250.      * @param    int     $cols
  251.      * @access   public
  252.      */
  253.     function setColCount($cols)
  254.     {
  255.         $this->_tbody->setColCount($cols);
  256.     }
  257.  
  258.     /**
  259.      * Returns the number of rows in the table
  260.      * @access   public
  261.      * @return   int
  262.      */
  263.     function getRowCount()
  264.     {
  265.         return $this->_tbody->getRowCount();
  266.     }
  267.  
  268.     /**
  269.      * Gets the number of columns in the table
  270.      *
  271.      * If a row index is specified, the count will not take
  272.      * the spanned cells into account in the return value.
  273.      *
  274.      * @param    int    Row index to serve for cols count
  275.      * @access   public
  276.      * @return   int
  277.      */
  278.     function getColCount($row = null)
  279.     {
  280.         return $this->_tbody->getColCount($row);
  281.     }
  282.  
  283.     /**
  284.      * Sets a rows type 'TH' or 'TD'
  285.      * @param    int         $row    Row index
  286.      * @param    string      $type   'TH' or 'TD'
  287.      * @access   public
  288.      */
  289.  
  290.     function setRowType($row, $type)
  291.     {
  292.         $this->_tbody->setRowType($row, $type);
  293.     }
  294.  
  295.     /**
  296.      * Sets a columns type 'TH' or 'TD'
  297.      * @param    int         $col    Column index
  298.      * @param    string      $type   'TH' or 'TD'
  299.      * @access   public
  300.      */
  301.     function setColType($col, $type)
  302.     {
  303.         $this->_tbody->setColType($col, $type);
  304.     }
  305.  
  306.     /**
  307.      * Sets the cell attributes for an existing cell.
  308.      *
  309.      * If the given indices do not exist and autoGrow is true then the given
  310.      * row and/or col is automatically added.  If autoGrow is false then an
  311.      * error is returned.
  312.      * @param    int        $row         Row index
  313.      * @param    int        $col         Column index
  314.      * @param    mixed      $attributes  Associative array or string of table row attributes
  315.      * @access   public
  316.      * @throws   PEAR_Error
  317.      */
  318.     function setCellAttributes($row, $col, $attributes)
  319.     {
  320.         $ret = $this->_tbody->setCellAttributes($row, $col, $attributes);
  321.         if (PEAR::isError($ret)) {
  322.             return $ret;
  323.         }
  324.     }
  325.  
  326.     /**
  327.      * Updates the cell attributes passed but leaves other existing attributes in tact
  328.      * @param    int     $row         Row index
  329.      * @param    int     $col         Column index
  330.      * @param    mixed   $attributes  Associative array or string of table row attributes
  331.      * @access   public
  332.      */
  333.     function updateCellAttributes($row, $col, $attributes)
  334.     {
  335.         $ret = $this->_tbody->updateCellAttributes($row, $col, $attributes);
  336.         if (PEAR::isError($ret)) {
  337.             return $ret;
  338.         }
  339.     }
  340.  
  341.     /**
  342.      * Returns the attributes for a given cell
  343.      * @param    int     $row         Row index
  344.      * @param    int     $col         Column index
  345.      * @return   array
  346.      * @access   public
  347.      */
  348.     function getCellAttributes($row, $col)
  349.     {
  350.         return $this->_tbody->getCellAttributes($row, $col);
  351.     }
  352.  
  353.     /**
  354.      * Sets the cell contents for an existing cell
  355.      *
  356.      * If the given indices do not exist and autoGrow is true then the given
  357.      * row and/or col is automatically added.  If autoGrow is false then an
  358.      * error is returned.
  359.      * @param    int      $row        Row index
  360.      * @param    int      $col        Column index
  361.      * @param    mixed    $contents   May contain html or any object with a toHTML method;
  362.      *                                if it is an array (with strings and/or objects), $col
  363.      *                                will be used as start offset and the array elements
  364.      *                                will be set to this and the following columns in $row
  365.      * @param    string   $type       (optional) Cell type either 'TH' or 'TD'
  366.      * @access   public
  367.      * @throws   PEAR_Error
  368.      */
  369.     function setCellContents($row, $col, $contents, $type = 'TD')
  370.     {
  371.         $ret = $this->_tbody->setCellContents($row, $col, $contents, $type);
  372.         if (PEAR::isError($ret)) {
  373.             return $ret;
  374.         }
  375.     }
  376.  
  377.     /**
  378.      * Returns the cell contents for an existing cell
  379.      * @param    int        $row    Row index
  380.      * @param    int        $col    Column index
  381.      * @access   public
  382.      * @return   mixed
  383.      */
  384.     function getCellContents($row, $col)
  385.     {
  386.         return $this->_tbody->getCellContents($row, $col);
  387.     }
  388.  
  389.     /**
  390.      * Sets the contents of a header cell
  391.      * @param    int     $row
  392.      * @param    int     $col
  393.      * @param    mixed   $contents
  394.      * @param    mixed  $attributes Associative array or string of table row attributes
  395.      * @access   public
  396.      */
  397.     function setHeaderContents($row, $col, $contents, $attributes = null)
  398.     {
  399.         $this->_tbody->setHeaderContents($row, $col, $contents, $attributes);
  400.     }
  401.  
  402.     /**
  403.      * Adds a table row and returns the row identifier
  404.      * @param    array    $contents   (optional) Must be a indexed array of valid cell contents
  405.      * @param    mixed    $attributes (optional) Associative array or string of table row attributes
  406.      *                                This can also be an array of attributes, in which case the attributes
  407.      *                                will be repeated in a loop.
  408.      * @param    string   $type       (optional) Cell type either 'th' or 'td'
  409.      * @param    bool     $inTR           false if attributes are to be applied in TD tags
  410.      *                                    true if attributes are to be applied in TR tag
  411.      * @return   int
  412.      * @access   public
  413.      */
  414.     function addRow($contents = null, $attributes = null, $type = 'td', $inTR = false)
  415.     {
  416.         $ret = $this->_tbody->addRow($contents, $attributes, $type, $inTR);
  417.         return $ret;
  418.     }
  419.  
  420.     /**
  421.      * Sets the row attributes for an existing row
  422.      * @param    int      $row            Row index
  423.      * @param    mixed    $attributes     Associative array or string of table row attributes
  424.      *                                    This can also be an array of attributes, in which case the attributes
  425.      *                                    will be repeated in a loop.
  426.      * @param    bool     $inTR           false if attributes are to be applied in TD tags
  427.      *                                    true if attributes are to be applied in TR tag
  428.      * @access   public
  429.      * @throws   PEAR_Error
  430.      */
  431.     function setRowAttributes($row, $attributes, $inTR = false)
  432.     {
  433.         $ret = $this->_tbody->setRowAttributes($row, $attributes, $inTR);
  434.         if (PEAR::isError($ret)) {
  435.             return $ret;
  436.         }
  437.     }
  438.  
  439.     /**
  440.      * Updates the row attributes for an existing row
  441.      * @param    int      $row            Row index
  442.      * @param    mixed    $attributes     Associative array or string of table row attributes
  443.      * @param    bool     $inTR           false if attributes are to be applied in TD tags
  444.      *                                    true if attributes are to be applied in TR tag
  445.      * @access   public
  446.      * @throws   PEAR_Error
  447.      */
  448.     function updateRowAttributes($row, $attributes = null, $inTR = false)
  449.     {
  450.         $ret = $this->_tbody->updateRowAttributes($row, $attributes, $inTR);
  451.         if (PEAR::isError($ret)) {
  452.             return $ret;
  453.         }
  454.     }
  455.  
  456.     /**
  457.      * Returns the attributes for a given row as contained in the TR tag
  458.      * @param    int     $row         Row index
  459.      * @return   array
  460.      * @access   public
  461.      */
  462.     function getRowAttributes($row)
  463.     {
  464.         return $this->_tbody->getRowAttributes($row);
  465.     }
  466.  
  467.     /**
  468.      * Alternates the row attributes starting at $start
  469.      * @param    int      $start          Row index of row in which alternating begins
  470.      * @param    mixed    $attributes1    Associative array or string of table row attributes
  471.      * @param    mixed    $attributes2    Associative array or string of table row attributes
  472.      * @param    bool     $inTR           false if attributes are to be applied in TD tags
  473.      *                                    true if attributes are to be applied in TR tag
  474.      * @access   public
  475.      */
  476.     function altRowAttributes($start, $attributes1, $attributes2, $inTR = false)
  477.     {
  478.         $this->_tbody->altRowAttributes($start, $attributes1, $attributes2, $inTR);
  479.     }
  480.  
  481.     /**
  482.      * Adds a table column and returns the column identifier
  483.      * @param    array    $contents   (optional) Must be a indexed array of valid cell contents
  484.      * @param    mixed    $attributes (optional) Associative array or string of table row attributes
  485.      * @param    string   $type       (optional) Cell type either 'th' or 'td'
  486.      * @return   int
  487.      * @access   public
  488.      */
  489.     function addCol($contents = null, $attributes = null, $type = 'td')
  490.     {
  491.         return $this->_tbody->addCol($contents, $attributes, $type);
  492.     }
  493.  
  494.     /**
  495.      * Sets the column attributes for an existing column
  496.      * @param    int      $col            Column index
  497.      * @param    mixed    $attributes     (optional) Associative array or string of table row attributes
  498.      * @access   public
  499.      */
  500.     function setColAttributes($col, $attributes = null)
  501.     {
  502.         $this->_tbody->setColAttributes($col, $attributes);
  503.     }
  504.  
  505.     /**
  506.      * Updates the column attributes for an existing column
  507.      * @param    int      $col            Column index
  508.      * @param    mixed    $attributes     (optional) Associative array or string of table row attributes
  509.      * @access   public
  510.      */
  511.     function updateColAttributes($col, $attributes = null)
  512.     {
  513.         $this->_tbody->updateColAttributes($col, $attributes);
  514.     }
  515.  
  516.     /**
  517.      * Sets the attributes for all cells
  518.      * @param    mixed    $attributes        (optional) Associative array or string of table row attributes
  519.      * @access   public
  520.      */
  521.     function setAllAttributes($attributes = null)
  522.     {
  523.         $this->_tbody->setAllAttributes($attributes);
  524.     }
  525.  
  526.     /**
  527.      * Updates the attributes for all cells
  528.      * @param    mixed    $attributes        (optional) Associative array or string of table row attributes
  529.      * @access   public
  530.      */
  531.     function updateAllAttributes($attributes = null)
  532.     {
  533.         $this->_tbody->updateAllAttributes($attributes);
  534.     }
  535.  
  536.     /**
  537.      * Returns the table structure as HTML
  538.      * @access  public
  539.      * @return  string
  540.      */
  541.     function toHtml()
  542.     {
  543.         $strHtml = '';
  544.         $tabs = $this->_getTabs();
  545.         $tab = $this->_getTab();
  546.         $lnEnd = $this->_getLineEnd();
  547.         if ($this->_comment) {
  548.             $strHtml .= $tabs . "<!-- $this->_comment -->" . $lnEnd;
  549.         }
  550.         $strHtml .=
  551.             $tabs . '<table' . $this->_getAttrString($this->_attributes) . '>' . $lnEnd;
  552.         if (!empty($this->_caption)) {
  553.             $attr = $this->_caption['attr'];
  554.             $contents = $this->_caption['contents'];
  555.             $strHtml .= $tabs . $tab . '<caption' . $this->_getAttrString($attr) . '>';
  556.             if (is_array($contents)) {
  557.                 $contents = implode(', ', $contents);
  558.             }
  559.             $strHtml .= $contents;
  560.             $strHtml .= '</caption>' . $lnEnd;
  561.         }
  562.         if ($this->_useTGroups) {
  563.             $tHeadColCount = 0;
  564.             if ($this->_thead !== null) {
  565.                 $tHeadColCount = $this->_thead->getColCount();
  566.             }
  567.             $tFootColCount = 0;
  568.             if ($this->_tfoot !== null) {
  569.                 $tFootColCount = $this->_tfoot->getColCount();
  570.             }
  571.             $tBodyColCount = 0;
  572.             if ($this->_tbody !== null) {
  573.                 $tBodyColCount = $this->_tbody->getColCount();
  574.             }
  575.             $maxColCount = max($tHeadColCount, $tFootColCount, $tBodyColCount);
  576.             if ($this->_thead !== null) {
  577.                 $this->_thead->setColCount($maxColCount);
  578.                 if ($this->_thead->getRowCount() > 0) {
  579.                     $strHtml .= $tabs . $tab . '<thead>' . $lnEnd;
  580.                     $strHtml .= $this->_thead->toHtml();
  581.                     $strHtml .= $tabs . $tab . '</thead>' . $lnEnd;
  582.                 }
  583.             }
  584.             if ($this->_tfoot !== null) {
  585.                 $this->_tfoot->setColCount($maxColCount);
  586.                 if ($this->_tfoot->getRowCount() > 0) {
  587.                     $strHtml .= $tabs . $tab . '<tfoot>' . $lnEnd;
  588.                     $strHtml .= $this->_tfoot->toHtml();
  589.                     $strHtml .= $tabs . $tab . '</tfoot>' . $lnEnd;
  590.                 }
  591.             }
  592.             if ($this->_tbody !== null) {
  593.                 $this->_tbody->setColCount($maxColCount);
  594.                 if ($this->_tbody->getRowCount() > 0) {
  595.                     $strHtml .= $tabs . $tab . '<tbody>' . $lnEnd;
  596.                     $strHtml .= $this->_tbody->toHtml();
  597.                     $strHtml .= $tabs . $tab . '</tbody>' . $lnEnd;
  598.                 }
  599.             }
  600.         } else {
  601.             $strHtml .= $this->_tbody->toHtml();
  602.         }
  603.         $strHtml .= $tabs . '</table>' . $lnEnd;
  604.         return $strHtml;
  605.     }
  606.  
  607. }
  608. ?>