cvs: pear /Structures_DataGrid DataGrid.php /Structures_DataGrid/DataGrid Core.php Renderer.php

From: Date: Wed, 14 Dec 2005 22:55:53 +0000
Subject: cvs: pear /Structures_DataGrid DataGrid.php /Structures_DataGrid/DataGrid Core.php Renderer.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-36871@lists.php.net to get a copy of this message
olivierg Wed Dec 14 22:55:53 2005 EDT Removed files: /pear/Structures_DataGrid/DataGrid Core.php Modified files: /pear/Structures_DataGrid DataGrid.php /pear/Structures_DataGrid/DataGrid Renderer.php Log: Merged the Datagrid, Renderer and Core classes (bug #5859)

http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid.php?r1=1.22&r2=1.23&diff_format=u Index: pear/Structures_DataGrid/DataGrid.php diff -u pear/Structures_DataGrid/DataGrid.php:1.22 pear/Structures_DataGrid/DataGrid.php:1.23 --- pear/Structures_DataGrid/DataGrid.php:1.22 Tue Dec 13 12:17:14 2005 +++ pear/Structures_DataGrid/DataGrid.php Wed Dec 14 22:55:53 2005 @@ -16,7 +16,7 @@ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: DataGrid.php,v 1.22 2005/12/13 12:17:14 wiesemann Exp $ +// $Id: DataGrid.php,v 1.23 2005/12/14 22:55:53 olivierg Exp $ require_once 'PEAR.php'; @@ -67,15 +67,71 @@ * ?> * </code> * - * @version $Revision: 1.22 $ + * @version $Revision: 1.23 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ -class Structures_DataGrid extends Structures_DataGrid_Renderer +class Structures_DataGrid { /** + * Renderer driver + * @var object Structures_DataGrid_Renderer_* family + */ + var $renderer; + + /** + * Array of columns. Columns are defined as a DataGridColumn object. + * @var array + */ + var $columnSet = array(); + + /** + * Array of records. Records are defined as a DataGridRecord object. + * @var array + */ + var $recordSet = array(); + + /** + * The Data Source Driver object + * @var object Structures_DataGrid_DataSource + */ + var $_dataSource; + + /** + * An array of fields to sort by. Each field is an array of the field name + * and the direction, either ASC or DESC. + * @var array + */ + var $sortArray; + + /** + * Limit of records to show per page. + * @var string + */ + var $rowLimit; + + /** + * The current page to show. + * @var string + */ + var $page; + + /** + * Whether the page number was provided at instantiation or not + * @var bool + * @access private + */ + var $_forcePage; + + /** + * GET/POST/Cookie parameters prefix + * @var string + */ + var $_requestPrefix = ''; + + /** * Constructor * * Builds the DataGrid class. The Core functionality and Renderer are @@ -92,7 +148,26 @@ function Structures_DataGrid($limit = null, $page = null, $renderer = DATAGRID_RENDER_TABLE) { - parent::Structures_DataGrid_Renderer($renderer, $limit, $page); + //parent::Structures_DataGrid_Renderer($renderer, $limit, $page); + if (PEAR::isError($this->setRenderer($renderer))) { + $this->setRenderer(DATAGRID_RENDER_TABLE); + } + + //parent::Structures_DataGrid_Core($limit, $page); + // Set the defined rowlimit + $this->rowLimit = $limit; + + //Use set page number, otherwise automatically detect the page number + if (!is_null ($page)) { + $this->page = $page; + $this->_forcePage = true; + } else { + $this->page = 1; + $this->_forcePage = false; + } + + // Automatic handling of GET/POST/COOKIE variables + $this->_parseHttpRequest(); } /** @@ -108,6 +183,394 @@ print_r($this); echo '</pre>'; } + + /** + * Render + * + * Renders that output by calling the specified renderer's render method. + * + * @param string $limit The row limit per page. + * @param string $page The current page viewed. + * @access public + */ + function render() + { + return $this->renderer->render(); + } + + /** + * Get Renderer + * + * Retrieves the renderer object as a reference + * + * @access public + */ + function &getRenderer() + { + return $this->renderer; + } + + /** + * Set Renderer + * + * Defines which renderer to be used by the DataGrid + * + * @param string $renderer The defined renderer string + * @param string $path An optional value to change the path of + * the location of the renderer class file. + * Please set in the notation of '/my/path' + * @access public + */ + function setRenderer($renderer, $path = 'Structures/DataGrid/Renderer') + { + $class = 'Structures_DataGrid_Renderer_' . $renderer; + $file = $path . '/' . $renderer . '.php'; + + if (include_once($file)) { + $this->renderer = new $class($this); + } else { + return new PEAR_Error('Invalid renderer'); + } + + return true; + } + + /** + * Set Default Headers + * + * This method handles determining if column headers need to be set. + * + * @access private + */ + function _setDefaultHeaders() + { + if ((!count($this->columnSet)) && (count($this->recordSet))) { + $arrayKeys = array_keys($this->recordSet[0]); + foreach ($arrayKeys as $key) { + $width = ceil(100/count($arrayKeys)); + $column = new Structures_DataGrid_Column($key, $key, $key, + array('width' => + $width.'%')); + $this->addColumn($column); + } + } + } + + /** + * Retrieves the current page number when paging is implemented + * + * @return string the current page number + * @access public + */ + function getCurrentPage() + { + return $this->page; + } + + /** + * Define the current page number. This is used when paging is implemented + * + * @access public + * @param string $page The current page number. + */ + function setCurrentPage($page) + { + $this->page = $page; + } + + /** + * Returns the total number of pages + * (returns 0 if there are no records, returns 1 if there is no row limit) + * + * @return string the total number of pages + * @access public + */ + function getPageCount() + { + if (is_null($this->rowLimit) || $this->getRecordCount() == 0) { + return 1; + } else { + return ceil($this->getRecordCount() / $this->rowLimit); + } + } + + /** + * Returns the total number of records + * + * @return string the total number of records + * @access public + */ + function getRecordCount() + { + if (!is_null ($this->_dataSource)) { + return $this->_dataSource->count(); + } else { + return count($this->recordSet); + } + } + + /** + * Returns the number of the first record of the current page + * (returns 0 if there are no records, returns 1 if there is no row limit) + * + * @return string the number of the first record currently shown + * @access public + */ + function getCurrentRecordNumberStart() + { + if (is_null($this->page)) { + return 1; + } elseif ($this->getRecordCount() == 0) { + return 0; + } else { + return ($this->page - 1) * $this->rowLimit + 1; + } + } + + /** + * Returns the number of the last record of the current page + * + * @return string the number of the last record currently shown + * @access public + */ + function getCurrentRecordNumberEnd() + { + if (is_null($this->rowLimit)) { + return $this->getRecordCount(); + } else { + return + min($this->getCurrentRecordNumberStart() + $this->rowLimit - 1, + $this->getRecordCount()); + } + } + + /** + * If you need to change the request variables, you can define a prefix. + * This is extra useful when using multiple datagrids. + * + * @access public + * @param string $prefix The prefix to use on request variables; + */ + function setRequestPrefix($prefix) + { + $this->_requestPrefix = $prefix; + + // Automatic handling of GET/POST/COOKIE variables + $this->_parseHttpRequest(); + } + + /** + * Adds a DataGrid_Column object to this DataGrid object + * + * @access public + * @param object Structures_DataGrid_Column $column The column + * object to add. This object should be a + * Structures_DataGrid_Column object. + * @return bool True if successful, otherwise false. + */ + function addColumn($column) + { + if (is_a($column, 'structures_datagrid_column')) { + $this->columnSet = array_merge($this->columnSet, array($column)); + return true; + } else { + return false; + } + } + + /** + * A simple way to add a recod set to the datagrid + * + * @access public + * @param mixed $rs The record set in any of the supported data + * source types + * @param array $options Optional. The options to be used for the + * data source + * @param string $type Optional. The data source type + * @return bool True if successful, otherwise PEAR_Error. + */ + function bind($rs, $options = array(), $type = null) + { + require_once 'Structures/DataGrid/DataSource.php'; + + $source =& Structures_DataGrid_DataSource::create($rs, $options, $type); + if (!PEAR::isError($source)) { + return $this->bindDataSource($source); + } else { + return $source; + } + } + + /** + * Allows binding to a data source driver. + * + * @access public + * @param mixed $source The data source driver object + * @return mixed True if successful, otherwise PEAR_Error + */ + function bindDataSource(&$source) + { + if (is_subclass_of($source, 'structures_datagrid_datasource')) { + $this->_dataSource =& $source; + if (PEAR::isError($result = $this->fetchDataSource())) { + return $result; + } + if ($columnSet = $this->_dataSource->getColumns()) { + $this->columnSet = array_merge($this->columnSet, $columnSet); + } + } else { + return new PEAR_Error('Invalid data source type, ' . + 'must be a valid data source driver class'); + } + + return true; + } + + function fetchDataSource() + { + if ($this->_dataSource != null) { + // Determine Page + $page = $this->page ? $this->page - 1 : 0; + + // Fetch the Data + $recordSet = $this->_dataSource->fetch( + ($page*$this->rowLimit), + $this->rowLimit, $this->sortArray[0], + $this->sortArray[1]); + + if (PEAR::isError($recordSet)) { + return $recordSet; + } else { + $this->recordSet = array_merge($this->recordSet, $recordSet); + /* + if (count($columnSet = $this->_dataSource->getColumns())) { + $this->columnSet = $columnSet; + } + */ + } + } + } + + + /** + * Adds a DataGrid_Record object to this DataGrid object + * + * @access public + * @param object Structures_DataGrid_Record $record The record + * object to add. This object must be a Structures_DataGrid_Record + * object. + * @return bool True if successful, otherwise false. + */ + function addRecord($record) + { + if (is_a($record, 'structures_datagrid_record')) { + $this->recordSet = array_merge($this->recordSet, + array($record->getRecord())); + return true; + } else { + return new PEAR_Error('Not a valid DataGrid Record'); + } + } + + /** + * Drops a DataGrid_Record object from this DataGrid object + * + * @access public + * @param object Structures_DataGrid_Record $record The record + * object to drop. This object must be a Structures_DataGrid_Record + * object. + * @return void + */ + function dropRecord($record) + { + unset($this->recordSet[$record->getRecord()]); + } + + /** + * Sorts the records by the defined field. + * Do not use this method if data is coming from a database as sorting + * is much faster coming directly from the database itself. + * + * @access public + * @param string $sortBy The field to sort the record set by. + * @param string $direction The sort direction, either ASC or DESC. + * @return void + */ + function sortRecordSet($sortBy, $direction = 'ASC') + { + $this->sortArray = array($sortBy, $direction); + if ($this->_dataSource) { + $this->_dataSource->sort($sortBy, $direction); + } else { + usort($this->recordSet, array($this, '_sort')); + } + } + + function _sort($a, $b, $i = 0) + { + //$bool = strnatcmp($a[$this->sortArray[0]], $b[$this->sortArray[0]]); + $bool = strnatcasecmp($a[$this->sortArray[0]], $b[$this->sortArray[0]]); + + if ($this->sortArray[1] == 'DESC') { + $bool = $bool * -1; + } + + return $bool; + } + + /** + * Parse HTTP Request parameters + * + * @access private + * @return array Associative array of parsed arguments, each of these + * defaulting to null if not found. + */ + function _parseHttpRequest() + { + $prefix = $this->_requestPrefix; + + // Determine page, sort and direction values + + if (!$this->_forcePage) { + if (isset($_REQUEST[$prefix . 'page'])) { + // Use POST, GET, or COOKIE value in respective order + if (isset($_POST[$prefix . 'page'])) { + $this->page = $_POST[$prefix . 'page']; + } elseif (isset($_GET[$prefix . 'page'])) { + $this->page = $_GET[$prefix . 'page']; + } elseif (isset($_COOKIE[$prefix . 'page'])) { + $this->page = $_COOKIE[$prefix . 'page']; + } + } else { + $this->page = 1; + } + if (!is_numeric ($this->page)) { + $this->page = 1; + } + } + + if (isset($_REQUEST[$prefix . 'orderBy'])) { + // Use POST, GET, or COOKIE value in respective order + if (isset($_POST[$prefix . 'orderBy'])) { + $this->sortArray[0] = $_POST[$prefix . 'orderBy']; + } elseif (isset($_GET[$prefix . 'orderBy'])) { + $this->sortArray[0] = $_GET[$prefix . 'orderBy']; + } elseif (isset($_COOKIE[$prefix . 'orderBy'])) { + $this->sortArray[0] = $_COOKIE[$prefix . 'orderBy']; + } + } + + if (isset($_REQUEST[$prefix . 'direction'])) { + // Use POST, GET, or COOKIE value in respective order + if (isset($_POST[$prefix . 'direction'])) { + $this->sortArray[1] = $_POST[$prefix . 'direction']; + } elseif (isset($_GET[$prefix . 'direction'])) { + $this->sortArray[1] = $_GET[$prefix . 'direction']; + } elseif (isset($_COOKIE[$prefix . 'direction'])) { + $this->sortArray[1] = $_COOKIE[$prefix . 'direction']; + } + } + } } ?> http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer.php?r1=1.26&r2=1.27&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer.php diff -u pear/Structures_DataGrid/DataGrid/Renderer.php:1.26 pear/Structures_DataGrid/DataGrid/Renderer.php:1.27 --- pear/Structures_DataGrid/DataGrid/Renderer.php:1.26 Fri Oct 28 13:54:49 2005 +++ pear/Structures_DataGrid/DataGrid/Renderer.php Wed Dec 14 22:55:53 2005 @@ -13,128 +13,39 @@ // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ -// | Author: Andrew Nagy <asnagy@webitecture.org> | +// | Authors: Andrew Nagy <asnagy@webitecture.org> | +// | Olivier Guilyardi <olivier@samalyse.com> | // +----------------------------------------------------------------------+ // -// $Id: Renderer.php,v 1.26 2005/10/28 13:54:49 olivierg Exp $ +// $Id: Renderer.php,v 1.27 2005/12/14 22:55:53 olivierg Exp $ require_once 'Structures/DataGrid/Core.php'; /** * Structures_DataGrid_Renderer Class * - * This class handles managing the output for the DataGrid. - * By default, the output is handled by the HTML_Table renderer. + * Base class of all Renderer drivers * - * @version $Revision: 1.26 $ + * @version $Revision: 1.27 $ * @author Andrew S. Nagy <asnagy@webitecture.org> + * @author Olivier Guilyardi <olivier@samalyse.com> * @access public * @package Structures_DataGrid * @category Structures */ -class Structures_DataGrid_Renderer extends Structures_DataGrid_Core +class Structures_DataGrid_Renderer { - /** - * Renderer driver - * @var object Structures_DataGrid_Renderer_* family - */ - var $renderer; - - /** - * Constructor - * - * Determins the appropriate renderer to use. Uses the HTML_Table renderer - * as the default. - * - * @param string $renderer The renderer to use. - * @param int $limit The row limit per page - * @param int $page The page to show (starting from 1). - * Note : if you specify this, the "page" GET - * variable will be ignored. - * @access public - */ - function Structures_DataGrid_Renderer($renderer = DATAGRID_RENDER_TABLE, - $limit = null, $page = null) - { - if (PEAR::isError($this->setRenderer($renderer))) { - $this->setRenderer(DATAGRID_RENDER_TABLE); - } - - parent::Structures_DataGrid_Core($limit, $page); - } - - /** - * Render - * - * Renders that output by calling the specified renderer's render method. - * - * @param string $limit The row limit per page. - * @param string $page The current page viewed. - * @access public - */ - function render() - { - return $this->renderer->render(); - } - - /** - * Get Renderer - * - * Retrieves the renderer object as a reference - * - * @access public - */ - function &getRenderer() + function header($labels) { - return $this->renderer; } - - /** - * Set Renderer - * - * Defines which renderer to be used by the DataGrid - * - * @param string $renderer The defined renderer string - * @param string $path An optional value to change the path of - * the location of the renderer class file. - * Please set in the notation of '/my/path' - * @access public - */ - function setRenderer($renderer, $path = 'Structures/DataGrid/Renderer') + + function row($content) { - $class = 'Structures_DataGrid_Renderer_' . $renderer; - $file = $path . '/' . $renderer . '.php'; - - if (include_once($file)) { - $this->renderer = new $class($this); - } else { - return new PEAR_Error('Invalid renderer'); - } - - return true; } - - /** - * Set Default Headers - * - * This method handles determining if column headers need to be set. - * - * @access private - */ - function _setDefaultHeaders() + + function footer($labels) { - if ((!count($this->columnSet)) && (count($this->recordSet))) { - $arrayKeys = array_keys($this->recordSet[0]); - foreach ($arrayKeys as $key) { - $width = ceil(100/count($arrayKeys)); - $column = new Structures_DataGrid_Column($key, $key, $key, - array('width' => - $width.'%')); - $this->addColumn($column); - } - } } - } ?>
« previous php.pear.cvs (#36871) next »