cvs: pear /Structures_DataGrid DataGrid.php /Structures_DataGrid/DataGrid Column.php /Structures_DataGrid/DataGrid/Renderer Common.php HTMLTable.php

From: Date: Fri, 24 Feb 2006 13:40:29 +0000
Subject: cvs: pear /Structures_DataGrid DataGrid.php /Structures_DataGrid/DataGrid Column.php /Structures_DataGrid/DataGrid/Renderer Common.php HTMLTable.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-38567@lists.php.net to get a copy of this message
wiesemann Fri Feb 24 13:40:29 2006 UTC Modified files: /pear/Structures_DataGrid DataGrid.php /pear/Structures_DataGrid/DataGrid Column.php /pear/Structures_DataGrid/DataGrid/Renderer Common.php HTMLTable.php Log: - code clean-up, CS and WS corrections - small phpdoc fixes and additions

http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid.php?r1=1.38&r2=1.39&diff_format=u Index: pear/Structures_DataGrid/DataGrid.php diff -u pear/Structures_DataGrid/DataGrid.php:1.38 pear/Structures_DataGrid/DataGrid.php:1.39 --- pear/Structures_DataGrid/DataGrid.php:1.38 Fri Feb 24 10:01:18 2006 +++ pear/Structures_DataGrid/DataGrid.php Fri Feb 24 13:40:28 2006 @@ -18,22 +18,22 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: DataGrid.php,v 1.38 2006/02/24 10:01:18 wiesemann Exp $ +// $Id: DataGrid.php,v 1.39 2006/02/24 13:40:28 wiesemann Exp $ require_once 'PEAR.php'; require_once 'Structures/DataGrid/Column.php'; // Renderer Drivers -define ('DATAGRID_RENDER_TABLE', 'HTMLTable'); -define ('DATAGRID_RENDER_SMARTY', 'Smarty'); -define ('DATAGRID_RENDER_XML', 'XML'); -define ('DATAGRID_RENDER_XLS', 'XLS'); -define ('DATAGRID_RENDER_XUL', 'XUL'); -define ('DATAGRID_RENDER_CSV', 'CSV'); -define ('DATAGRID_RENDER_CONSOLE', 'Console'); +define('DATAGRID_RENDER_TABLE', 'HTMLTable'); +define('DATAGRID_RENDER_SMARTY', 'Smarty'); +define('DATAGRID_RENDER_XML', 'XML'); +define('DATAGRID_RENDER_XLS', 'XLS'); +define('DATAGRID_RENDER_XUL', 'XUL'); +define('DATAGRID_RENDER_CSV', 'CSV'); +define('DATAGRID_RENDER_CONSOLE', 'Console'); -define ('DATAGRID_RENDER_DEFAULT', DATAGRID_RENDER_TABLE); +define('DATAGRID_RENDER_DEFAULT', DATAGRID_RENDER_TABLE); // Data Source Drivers define('DATAGRID_SOURCE_ARRAY', 'Array'); @@ -72,7 +72,7 @@ * ?> * </code> * - * @version $Revision: 1.38 $ + * @version $Revision: 1.39 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -82,7 +82,7 @@ */ class Structures_DataGrid { - /* + /** * Renderer driver * @var object Structures_DataGrid_Renderer_* family * @access private @@ -177,10 +177,6 @@ // Automatic handling of GET/POST/COOKIE variables $this->_parseHttpRequest(); - - /* - $this->setRenderer($rendererType); - */ } /** @@ -318,21 +314,13 @@ */ function &rendererFactory($type, $options = array()) { - /* - if (is_null($type) && - !($type = Structures_DataGrid::_detectRendererType($renderingContainer))) { - return new PEAR_Error('Unable to determine the renderer type. '. - 'You may want to explicitly specify it.'); - } - */ - // FIXME: any security risk here (like including /etc/passwd) ? $className = "Structures_DataGrid_Renderer_$type"; if (PEAR::isError($driver =& $this->loadDriver($className))) { return $driver; } - + if ($options) { $driver->setOptions($options); } @@ -340,14 +328,14 @@ if ($this->_requestPrefix) { $driver->setRequestPrefix($this->_requestPrefix); } - + if (isset($this->sortArray)) { $driver->setCurrentSorting($this->sortArray[0], $this->sortArray[1]); } - + return $driver; } - + /** * Render the datagrid * @@ -374,7 +362,7 @@ $this->_renderer->isBuilt() or $this->build(); return $this->_renderer->getOutput(); } - + /** * Get Renderer * @@ -428,10 +416,10 @@ return new PEAR_Error('Invalid renderer type, ' . 'must be a valid renderer driver class'); } - + return true; } - + /** * Fill a rendering container with data * @@ -454,17 +442,17 @@ 'specify its type explicitly.'); } } - + if (PEAR::isError($test = $this->setRenderer($type, $options))) { return $test; } - + $this->_renderer->setContainer($container); $this->_renderer->build(); return true; } - + /** * Set Default Headers * @@ -482,7 +470,7 @@ } } } - + /** * Retrieves the current page number when paging is implemented * @@ -504,7 +492,7 @@ { $this->page = $page; } - + /** * Returns the total number of pages * (returns 0 if there are no records, returns 1 if there is no row limit) @@ -540,13 +528,13 @@ */ function getRecordCount() { - if (!is_null ($this->_dataSource)) { + 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) @@ -580,8 +568,8 @@ 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. @@ -597,8 +585,8 @@ if (isset($this->_renderer)) { $this->_renderer->setRequestPrefix($prefix); } - } - + } + /** * Adds a DataGrid_Column object to this DataGrid object * @@ -685,7 +673,7 @@ return new PEAR_Error('Invalid data source type, ' . 'must be a valid data source driver class'); } - + return true; } @@ -700,13 +688,13 @@ if ($this->_dataSource != null) { // Determine Page $page = $this->page ? $this->page - 1 : 0; - + // Fetch the Data $recordSet = $this->_dataSource->fetch( - ($page*$this->rowLimit), + ($page * $this->rowLimit), $this->rowLimit, $this->sortArray[0], $this->sortArray[1]); - + if (PEAR::isError($recordSet)) { return $recordSet; } else { @@ -714,11 +702,10 @@ return true; } } else { - return new PEAR_Error ("Can not fetch data: no datasource driver loaded."); + return new PEAR_Error("Cannot fetch data: no datasource driver loaded."); } } - - + /** * Adds a DataGrid_Record object to this DataGrid object * @@ -772,10 +759,17 @@ usort($this->recordSet, array($this, '_sort')); } } - - function _sort($a, $b, $i = 0) + + /** + * Callback method used in sortRecordSet() + * + * @access private + * @param string $a First value + * @param string $direction Second value + * @return boolean + */ + function _sort($a, $b) { - //$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') { @@ -784,7 +778,7 @@ return $bool; } - + /** * Parse HTTP Request parameters * @@ -795,9 +789,9 @@ 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 @@ -815,7 +809,7 @@ $this->page = 1; } } - + if (isset($_REQUEST[$prefix . 'orderBy'])) { // Use POST, GET, or COOKIE value in respective order if (isset($_POST[$prefix . 'orderBy'])) { @@ -826,7 +820,7 @@ $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'])) { @@ -942,7 +936,7 @@ $this->_renderer->build(); return true; } else { - return new PEAR_Error ("Cannot build the datagrid: no datasource driver loaded"); + return new PEAR_Error("Cannot build the datagrid: no datasource driver loaded"); } } @@ -954,8 +948,7 @@ */ function __get($var) { - if ($var == 'renderer') - { + if ($var == 'renderer') { isset($this->_renderer) or $this->setRenderer(DATAGRID_RENDER_DEFAULT); return $this->_renderer; } http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Column.php?r1=1.22&r2=1.23&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Column.php diff -u pear/Structures_DataGrid/DataGrid/Column.php:1.22 pear/Structures_DataGrid/DataGrid/Column.php:1.23 --- pear/Structures_DataGrid/DataGrid/Column.php:1.22 Mon Jan 30 14:25:01 2006 +++ pear/Structures_DataGrid/DataGrid/Column.php Fri Feb 24 13:40:29 2006 @@ -16,14 +16,14 @@ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: Column.php,v 1.22 2006/01/30 14:25:01 wiesemann Exp $ +// $Id: Column.php,v 1.23 2006/02/24 13:40:29 wiesemann Exp $ /** * Structures_DataGrid_Column Class * * This class represents a single column for the DataGrid. * - * @version $Revision: 1.22 $ + * @version $Revision: 1.23 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid @@ -102,7 +102,7 @@ } /** - * Set Auto Fill Value + * Set auto fill value * * Defines a value to be printed if a cell in the column is null. * @@ -157,7 +157,7 @@ */ function formatter($record) { - // Define the Parameter list + // Define the parameter list $paramList = array(); $paramList['record'] = $record; $paramList['fieldName'] = $this->fieldName; @@ -215,7 +215,6 @@ if (is_callable($formatter)) { $result = call_user_func($formatter, $paramList); } else { - //$result = new PEAR_Error('Unable to process formatter'); $result = false; PEAR::raiseError('Unable to process formatter', '1', PEAR_ERROR_TRIGGER); http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/Common.php?r1=1.14&r2=1.15&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/Common.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.14 pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.15 --- pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.14 Thu Feb 23 14:09:36 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/Common.php Fri Feb 24 13:40:29 2006 @@ -17,7 +17,7 @@ // | Olivier Guilyardi <olivier@samalyse.com> | // +----------------------------------------------------------------------+ // -// $Id: Common.php,v 1.14 2006/02/23 14:09:36 olivierg Exp $ +// $Id: Common.php,v 1.15 2006/02/24 13:40:29 wiesemann Exp $ /** * Structures_DataGrid_Renderer_Common Class @@ -56,7 +56,7 @@ * added * * - * @version $Revision: 1.14 $ + * @version $Revision: 1.15 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @access public * @package Structures_DataGrid @@ -77,7 +77,7 @@ * @access protected */ var $_records = array(); - + /** * Container filled with data by the driver * @var array @@ -98,7 +98,7 @@ * @access protected */ var $_currentSortDirection = null; - + /** * Columns objects * @@ -117,7 +117,7 @@ * @access protected */ var $_columnsNum; - + /** * Number of records in the current page * @var int @@ -154,12 +154,12 @@ var $_requestPrefix = ''; /** - * Wether the datagrid has been built or not + * Whether the datagrid has been built or not * @var bool * @access private */ var $_isBuilt = false; - + /** * Common and driver-specific options * @@ -170,6 +170,13 @@ */ var $_options = array(); + /** + * Constructor + * + * Build default values + * + * @access public + */ function Structures_DataGrid_Renderer_Common() { $this->_options = array( @@ -183,7 +190,7 @@ 'disableColumnSorting' => array(), ); } - + /** * Adds some default options. * @@ -235,7 +242,7 @@ { return $this->_container; } - + /** * Provide columns and records data * @@ -261,7 +268,7 @@ $this->_currentSortField = $field; $this->_currentSortDirection = $direction; } - + /** * Specify page and row limits * @@ -275,7 +282,7 @@ $this->_pageLimit = $rowsPerPage; $this->_totalRecordsNum = $totalRowNum; } - + /** * Create or/and prepare the container * @@ -291,7 +298,6 @@ function init() { } - /** * Build the header @@ -318,7 +324,7 @@ function buildBody() { } - + /** * Build the footer * @@ -372,7 +378,7 @@ { return $value; } - + /** * Build the grid * @@ -413,16 +419,16 @@ $this->_columns[$index] = compact('field','label'); } - + $this->_columnsNum = count($this->_columns); $this->_recordsNum = count($this->_records); $this->init(); - + if (is_null($this->_pageLimit)) { $this->_pageLimit = $this->_recordsNum; } - + for ($rec = 0; $rec < $this->_recordsNum; $rec++) { $content = array(); foreach ($this->_columnObjects as $column) { @@ -430,13 +436,13 @@ } $this->_records[$rec] = $content; } - + if ($this->_options['buildHeader']) { $this->buildHeader(); } - + $this->buildBody(); - + if ($this->_options['buildFooter']) { $this->buildFooter(); } @@ -446,6 +452,12 @@ $this->_isBuilt = true; } + /** + * Returns the output from the renderer (e.g. HTML table, XLS object, ...) + * + * @access public + * @return mixed The output from the renderer + */ function getOutput() { $this->_isBuilt or $this->build(); @@ -453,11 +465,19 @@ return $this->flatten(); } + /** + * Renders the output from the render (e.g. echoes an HTML table, displays + * an Excel spreadsheet, ...) + * + * This is quite an obsolete method... + * + * @access public + */ function render() { echo $this->getOutput(); } - + /** * Sets the rendered status. This can be used to "flush the cache" in case * you need to render the datagrid twice with the second time having changes @@ -465,7 +485,7 @@ * This is quite an obsolete method... * * @access public - * @params bool $status The rendered status of the DataGrid + * @param bool $status The rendered status of the DataGrid */ function setRendered($status = false) { @@ -474,13 +494,13 @@ } /* What are we supposed to do with $status = true ? */ } - + /** * Set the HTTP Request prefix * * @param string $prefix The prefix string * @return void - * @acess public + * @access public */ function setRequestPrefix($prefix) { $this->_requestPrefix = $prefix; @@ -503,8 +523,7 @@ $value = $this->defaultCellFormatter($record[$column->fieldName]); } - if (empty($value) and !is_null($column->autoFillValue)) - { + if (empty($value) and !is_null($column->autoFillValue)) { $value = $column->autoFillValue; } @@ -514,7 +533,7 @@ /** * Query the grid build status * - * @return bool Wether the grid has already been built or not + * @return bool Whether the grid has already been built or not * @access public */ function isBuilt() http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php?r1=1.78&r2=1.79&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.78 pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.79 --- pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.78 Fri Feb 24 09:58:34 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php Fri Feb 24 13:40:29 2006 @@ -18,7 +18,7 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: HTMLTable.php,v 1.78 2006/02/24 09:58:34 wiesemann Exp $ +// $Id: HTMLTable.php,v 1.79 2006/02/24 13:40:29 wiesemann Exp $ require_once 'Structures/DataGrid/Renderer/Common.php'; require_once 'HTML/Table.php'; @@ -54,7 +54,7 @@ * - encoding : the content encoding. Default: ISO-8859-1 * * - * @version $Revision: 1.78 $ + * @version $Revision: 1.79 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -64,7 +64,7 @@ */ class Structures_DataGrid_Renderer_HTMLTable extends Structures_DataGrid_Renderer_Common { - + /** * The html_table_storage object for the table header * @var object HTML_Table_Storage @@ -76,7 +76,7 @@ * @var object HTML_Table_Storage */ var $_tableBody; - + /** * The HTML::Pager object that controls paging logic. * @var object Pager @@ -88,7 +88,6 @@ * * Build default values * - * @param object Structures_DataGrid $dg The datagrid to render. * @access public */ function Structures_DataGrid_Renderer_HTMLTable() @@ -112,12 +111,16 @@ ); } - + /** + * Initialize HTML_Table instance if it is not already existing + * + * @access protected + */ function init() { if (is_null($this->_container)) { $this->_container = new HTML_Table(null, null, true); - } + } $this->_tableHeader =& $this->_container->getHeader(); $this->_tableBody =& $this->_container->getBody(); @@ -201,11 +204,11 @@ * @access public * @param bool $state Auto-alignment state */ - function setAutoAlign ($state) + function setAutoAlign($state) { $this->_options['autoAlign'] = $state; } - + /** * In order for the DataGrid to render "Empty Rows" to allow for uniformity * across pages with varying results, set this option to true. An example @@ -221,7 +224,7 @@ * array. */ function allowEmptyRows($value, $attributes = array()) - { + { if ($value) { $this->_options['fillWithEmptyRows'] = true; } else { @@ -241,7 +244,7 @@ { $this->_options['buildHeader'] = (bool)$bool; } - + /** * Add custom GET variables to the generated links * @@ -256,7 +259,7 @@ { $this->_options['extraVars'] = $vars; } - + /** * Exclude GET variables from the generated links * @@ -272,7 +275,7 @@ { $this->_options['excludeVars'] = $vars; } - + /** * Generates the HTML for the DataGrid * @@ -283,7 +286,7 @@ { return $this->getOutput(); } - + /** * Gets the HTML_Table object for the DataGrid * @@ -304,7 +307,7 @@ * @return void * @todo Redesign/Rework the header URL building. */ - function buildHeader () + function buildHeader() { $prefix = $this->_requestPrefix; // Build the list of common get parameters @@ -324,7 +327,7 @@ $field = $this->_columns[$col]['field']; $label = $this->_columns[$col]['label']; - //Define Content + // Define Content if (!in_array ($field, $this->_options['disableColumnSorting'])) { // Determine Direction if ($this->_currentSortField == $field && @@ -356,7 +359,7 @@ $str .= ' ' . $this->_options[$iconVar]; } $str .= '</a>'; - + } else { $str = $label; } @@ -385,9 +388,9 @@ for ($col = 0; $col < $this->_columnsNum; $col++) { $value = $this->_records[$row][$col]; $field = $this->_columns[$col]['field']; - - /* Right-align the content if it is numeric */ - $attributes = ($this->_options['autoAlign'] and is_numeric ($value)) + + // Right-align the content if it is numeric + $attributes = ($this->_options['autoAlign'] and is_numeric($value)) ? array('align' => 'right') : array(); @@ -414,8 +417,8 @@ } } } - - // Define Alternating Row attributes + + // Define alternating row attributes if ($this->_options['evenRowAttributes'] or $this->_options['oddRowAttributes']) { @@ -423,20 +426,32 @@ 0, $this->_options['evenRowAttributes'], $this->_options['oddRowAttributes'], - TRUE + true ); } } - - function defaultCellFormatter ($value) + + /** + * Default formatter for all cells + * + * @param string Cell value + * @return string Formatted cell value + * @access protected + */ + function defaultCellFormatter($value) { - return $this->_options['convertEntities'] ? htmlspecialchars($value, ENT_COMPAT, $this->_options['encoding']) : $value; } - function flatten () + /** + * Retrieve output from the container object + * + * @return mixed Output + * @access protected + */ + function flatten() { return $this->_container->toHTML(); } @@ -467,31 +482,30 @@ 'separator' => $separator, 'prevImg' => $prev, 'nextImg' => $next); - + if (is_array($attrs)) { $options = array_merge($options, $attrs); } - + if (isset ($options['extraVars'])) { $options['extraVars'] = array_merge($options['extraVars'], $this->_options['extraVars']); } else { $options['extraVars'] = $this->_options['extraVars']; } - + if (isset ($options['excludeVars'])) { $options['excludeVars'] = array_merge($options['excludeVars'], $this->_options['excludeVars']); } else { $options['excludeVars'] = $this->_options['excludeVars']; } - + $this->_buildPaging($options); // Return paging html return $this->_pager->links; } - /** * Handles generating the paging object @@ -510,7 +524,7 @@ 'currentPage' => $this->_page); $options = array_merge($defaults, $options); $this->_pager =& Pager::factory($options); - } + } } ?>
« previous php.pear.cvs (#38567) next »