HTML_Table_Matrix
[ class tree: HTML_Table_Matrix ] [ index: HTML_Table_Matrix ] [ all elements ]

Source for file Matrix.php

Documentation is available at Matrix.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Ian Eure <ieure@php.net>                                    |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Matrix.php,v 1.1.1.1 2004/04/19 23:47:14 ieure Exp $
  20.  
  21. require_once 'PEAR.php';
  22. require_once 'HTML/Table.php';
  23. require_once 'HTML/Table/Matrix/Filler.php';
  24.  
  25. /**
  26.  * Fills a HTML table with data.
  27.  *
  28.  * Simple usage:
  29.  *
  30.  * // This is the data to put in the table.
  31.  * $data = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
  32.  *               'nine', 'ten');
  33.  * $m = &new HTML_Table_Matrix;
  34.  * $m->setData($data);
  35.  * // Pick a filler class. We use the stock left-to-right-top-to-bottom here.
  36.  * $f = &HTML_Table_Matrix_Filler::factory('LRTB');
  37.  * $m->accept($f);
  38.  * // Make the table 2 rows deep by 5 cols wide
  39.  * $m->setTableSize(2, 5);
  40.  * // Output the table.
  41.  * print $m->toHtml();
  42.  * 
  43.  *
  44.  * @package HTML_Table_Matrix
  45.  * @author Ian Eure <ieure@php.net>
  46.  * @version 1.0.2
  47.  * @link http://atomized.org/PEAR/
  48.  */
  49. class HTML_Table_Matrix extends HTML_Table {
  50.  
  51.     // {{{ properties
  52.     /**
  53.      * The filler
  54.      *
  55.      * @type object
  56.      * @access private
  57.      * @see accept()
  58.      */
  59.      var $_filler '';
  60.  
  61.     /**
  62.      * The row to start filling at. Useful if you want to put other stuff in
  63.      * the table.
  64.      *
  65.      * @access private
  66.      * @var int 
  67.      * @see setFillStart()
  68.      */
  69.     var $_fillStartRow = 0;
  70.  
  71.     /**
  72.      * The column to start filling at. Useful if you want to put other stuff in
  73.      * the table.
  74.      *
  75.      * @access private
  76.      * @var int 
  77.      * @see setFillStart()
  78.      */
  79.     var $_fillStartCol = 0;
  80.  
  81.     /**
  82.      * The number of rows in the table. 0 = Undefined.
  83.      *
  84.      * @access private
  85.      * @var int 
  86.      * @see setTableSize()
  87.      */
  88.     var $_rows = 0;
  89.  
  90.     /**
  91.      * The number of columns in the table. 0 = Undefined.
  92.      *
  93.      * @access private
  94.      * @var int 
  95.      * @see setTableSize()
  96.      */
  97.     var $_cols = 10;
  98.  
  99.     /**
  100.      * Has the table been filled?
  101.      *
  102.      * @access private
  103.      * @var boolean 
  104.      */
  105.     var $_isFilled = FALSE;
  106.  
  107.     /**
  108.      * Data to fill table with
  109.      *
  110.      * @access private
  111.      * @var array 
  112.      * @see setData()
  113.      */
  114.     var $_data = array();
  115.     // }}}
  116.  
  117.  
  118.     // {{{ setData()
  119.     /**
  120.      * Sets data to fill table with.
  121.      *
  122.      * @return void 
  123.      * @param array $data 1-dimensional array of matrix data
  124.      */
  125.     function setData(&$data)
  126.     {
  127.         $this->_data $data;
  128.     }
  129.     // }}}
  130.  
  131.     // {{{ setFillStart()
  132.     /**
  133.      * Set the row & column to start filling at.
  134.      *
  135.      * Defaults to (0,0), which is the upper-left corner of the table. Setting
  136.      * this to a larger value will leave other cells empty, e.g. if you want to
  137.      * add a header or other information in the table in addition to the matrix
  138.      * data.
  139.      *
  140.      * @param int $row Row to start filling at
  141.      * @param int $col Column to start filling at
  142.      * @return void 
  143.      */
  144.     function setFillStart($row$col)
  145.     {
  146.         $this->_fillStartRow $row;
  147.         $this->_fillStartCol $col;
  148.     }
  149.     // }}}
  150.  
  151.     // {{{ setTableSize()
  152.     /**
  153.      * Set the size of the resulting table.
  154.      *
  155.      * The table will be forced to this size, regardless of whether or not
  156.      * there is enough (or too much) data to fill it up. If the table size
  157.      * (rows * cols) is smaller than the amount of data given to us, only
  158.      * (rows * cols) items are laid out.
  159.      *
  160.      * @param int $rows Number of rows, or zero to auto-size.
  161.      * @param int $cols Number of columns, or zero to auto-size.
  162.      * @return void 
  163.      */
  164.     function setTableSize($rows = 0$cols = 0)
  165.     {
  166.         $this->_rows $rows;
  167.         $this->_cols $cols;
  168.     }
  169.     // }}}
  170.  
  171.     /**
  172.      * Return the total table size (w * h)
  173.      *
  174.      * @return int Table size
  175.      * @access protected
  176.      */
  177.     function _getTableSize()
  178.     {
  179.         if ($this->_cols == 0 || $this->_rows == 0{
  180.             return count($this->_data);
  181.         }
  182.         return $this->_rows $this->_cols;
  183.     }
  184.     
  185.     /**
  186.      * Accept a Filler
  187.      */
  188.     function accept(&$filler)
  189.     {
  190.         if (!HTML_Table_Matrix_Filler::isValid($filler)) {
  191.             return PEAR::raiseError("Provided filler is of the wrong class.");
  192.         }
  193.         $this->_filler $filler;
  194.         return true;
  195.     }
  196.  
  197.     // {{{ _calculateSize()
  198.     /**
  199.      * Calculates the size of the table based on the data provided.
  200.      *
  201.      * @access private
  202.      * @return void 
  203.      * @see setData()
  204.      */
  205.     function _calculateSize()
  206.     {
  207.         reset($this->_data);
  208.         $n count($this->_data);
  209.  
  210.         if (!$this->_rows && $this->_cols{
  211.             $this->_rows ceil($n $this->_cols);
  212.         else if (!$this->_cols && $this->_rows{
  213.             $this->_cols ceil($n $this->_rows);
  214.         }
  215.     }
  216.     // }}}
  217.  
  218.     // {{{ fillTable()
  219.     /**
  220.     * Fills table with provided data. RL & BT modes are not implemented yet.
  221.     *
  222.     * This function does the actual laying out of the data into the table.
  223.     * It isn't necessary to call this unless you want to add or change something
  224.     * in the table, as toHtml() calls this automatically if the table has not
  225.     * yet been filled with data.
  226.     *
  227.     * @return mixed boolean true on success, PEAR_Error otherwise
  228.     * @see setData()
  229.     */
  230.     function fillTable()
  231.     {
  232.         if (!HTML_Table_Matrix_Filler::isValid($this->_filler)) {
  233.             return PEAR::raiseError("No Filler has been set.");
  234.         }
  235.  
  236.         $this->_calculateSize();
  237.         reset($this->_data);
  238.         $size $this->_getTableSize();
  239.         for($i = 0; $i $size$i++{
  240.             list($row$col$this->_filler->next($index);
  241.             $this->_fillCell($row$col);
  242.             $index++;
  243.         }
  244.  
  245.         $this->_isFilled = TRUE;
  246.         return true;
  247.     }
  248.     // }}}
  249.  
  250.     // {{{ _fillCell()
  251.     /**
  252.      * Fills a cell with data.
  253.      *
  254.      * Note: this depends on the array pointer of $_data pointing at the
  255.      * right item. Possibly not be the best way to handle this.
  256.      *
  257.      * @access private
  258.      * @param int $row Row of cell to fill.
  259.      * @param int $col Column of cell to fill.
  260.      */
  261.     function _fillCell($row$col)
  262.     {
  263.         list($null$dataeach($this->_data);
  264.         $this->setCellContents($row$col$data);
  265.     }
  266.     // }}}
  267.  
  268.  
  269.     // {{{ toHtml()
  270.     /**
  271.      * Returns HTML table. Calls fillTable() if the table has not already
  272.      * been filled.
  273.      *
  274.      * @return string HTML Table
  275.      * @see HTML_Table::toHtml()
  276.      */
  277.     function toHtml()
  278.     {
  279.         if (!$this->_isFilled{
  280.             $this->fillTable();
  281.         }
  282.  
  283.         return(parent::toHtml());
  284.     }
  285.     // }}}
  286. }
  287. ?>

Documentation generated on Mon, 11 Mar 2019 13:51:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.