home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Table / Matrix.php next >
Encoding:
PHP Script  |  2008-07-02  |  7.5 KB  |  288 lines

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