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

From: Date: Thu, 16 Mar 2006 12:23:57 +0000
Subject: cvs: pear /Structures_DataGrid/DataGrid/Renderer Common.php HTMLTable.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-39120@lists.php.net to get a copy of this message
olivierg Thu Mar 16 12:23:57 2006 UTC Modified files: /pear/Structures_DataGrid/DataGrid/Renderer Common.php HTMLTable.php Log: - Created easy to implement buildRow() and buildEmptyRow() methods - Made buildHeader() more obvious to implemented by passing it a $columns argument - Merged _isImplemented() and _isImplementedRecursive()

http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/Common.php?r1=1.22&r2=1.23&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/Common.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.22 pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.23 --- pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.22 Thu Mar 16 01:05:52 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/Common.php Thu Mar 16 12:23:57 2006 @@ -17,7 +17,7 @@ // | Olivier Guilyardi <olivier@samalyse.com> | // +----------------------------------------------------------------------+ // -// $Id: Common.php,v 1.22 2006/03/16 01:05:52 olivierg Exp $ +// $Id: Common.php,v 1.23 2006/03/16 12:23:57 olivierg Exp $ require_once 'PHP/Compat/Function/http_build_query.php'; @@ -77,7 +77,7 @@ * - fillWithEmptyRows * - numberAlign * - * @version $Revision: 1.22 $ + * @version $Revision: 1.23 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @access public * @package Structures_DataGrid @@ -386,27 +386,75 @@ * Drivers may optionally implement this method. * * @abstract + * + * @param array $columns Columns' fields names and labels (This is a + * convenient reference to the $_columns protected + * property) * @access protected * @return void */ - function buildHeader() + function buildHeader(&$columns) { } /** * Build the body * - * Drivers are required to implement this method. + * Drivers may overload() this method, if buildRow() and buildEmptyRow() + * are not flexible enough. * - * @abstract * @access protected * @return void */ function buildBody() { + for ($row = 0; $row < $this->_recordsNum; $row++) { + $this->buildRow($row,$this->_records[$row]); + } + + if ($this->_options['fillWithEmptyRows'] && !is_null($this->_pageLimit)) { + for ($row = $this->_recordsNum; $row < $this->_pageLimit; $row++) { + $this->buildEmptyRow($row); + } + } } /** + * Build a body row + * + * This is a very simple method for drivers to build a row. + * For more flexibility, drivers should overload buildBody() + * + * @param int $index Row index (zero-based) + * @param array $data Record data. + * Structure: array(0 => <value0>, 1 => <value1>, ...) + * @return void + * @access protected + * @abstract + */ + function buildRow($index,$data) + { + } + + /** + * Build an empty row + * + * Drivers must overload this method if they need to do something with + * empty rows that remain at the end of the body. + * + * This method will only be called if the "fillWithEmptyRows" option is + * enabled. + * + * @param int $index Row index (zero-based) + * @return void + * @access protected + * @abstract + */ + function buildEmptyRow($index) + { + } + + /** * Build the footer * * Drivers may optionally implement this method. @@ -540,7 +588,7 @@ } if ($this->_options['buildHeader']) { - $this->buildHeader(); + $this->buildHeader($this->_columns); } $this->buildBody(); @@ -744,45 +792,38 @@ return $query; } - /** - * Detect whether a method is implemented in the driver class + * Detect whether a method is implemented a child class * * This method is able to detect if a given method is implemented in the * driver (child class) even if it is declared in the root class. * * @param $method Method name - * @return bool + * @param $class The class to scan (Internal use) + * @return bool * @access protected */ - function _isImplemented($method) - { - return $this->_isImplementedRecursive($method,get_class($this)); - } - - /** - * Helper method for _isImplemented() - * - * @param $method Method name - * @param $class The class to scan - * @return boold - * @access private - */ - function _isImplementedRecursive($method, $class) + function _isImplemented($method, $class = null) { - if ($class == 'structures_datagrid_renderer_common') { + if (is_null($class)) { + $class = get_class($this); + } + + $parent = get_parent_class ($class); + + if (!$parent) { return false; } else { $methods = get_class_methods($class); if (in_array ($method, $methods)) { return true; } else { - $parent = get_parent_class ($class); - return $this->_isImplementedRecursive($method, $parent); + return $this->_isImplemented($method, $parent); } } } + } ?> http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php?r1=1.92&r2=1.93&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.92 pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.93 --- pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.92 Thu Mar 16 01:05:52 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php Thu Mar 16 12:23:57 2006 @@ -18,7 +18,7 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: HTMLTable.php,v 1.92 2006/03/16 01:05:52 olivierg Exp $ +// $Id: HTMLTable.php,v 1.93 2006/03/16 12:23:57 olivierg Exp $ require_once 'Structures/DataGrid/Renderer/Common.php'; require_once 'HTML/Table.php'; @@ -51,7 +51,7 @@ * - sortingResetsPaging: whether sorting HTTP queries reset paging * (default : true) * - * @version $Revision: 1.92 $ + * @version $Revision: 1.93 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -87,6 +87,12 @@ var $_pager; /** + * The body row index to start rendering at + * @var int + */ + var $_bodyStartRow; + + /** * Constructor * * Build default values @@ -133,9 +139,7 @@ */ function &getContainer() { - if (!isset($this->_table)) { - $this->init(); - } + isset($this->_table) or $this->init(); return $this->_table; } @@ -152,6 +156,20 @@ $this->_tableHeader =& $this->_table->getHeader(); $this->_tableBody =& $this->_table->getBody(); + + $this->_bodyStartRow = $this->_tableBody->getRowCount(); + + // Define alternating row attributes + if ($this->_options['evenRowAttributes'] + or $this->_options['oddRowAttributes']) { + + $this->_tableBody->altRowAttributes( + 0, + $this->_options['evenRowAttributes'], + $this->_options['oddRowAttributes'], + true + ); + } } /** @@ -319,17 +337,18 @@ /** * Handles building the header of the DataGrid * + * @param array $columns Columns' fields names and labels * @access protected * @return void * @see http://www.php.net/manual/en/function.http-build-query.php */ - function buildHeader() + function buildHeader(&$columns) { $row = $this->_tableHeader->getRowCount(); - for ($col = 0; $col < $this->_columnsNum; $col++) { - $field = $this->_columns[$col]['field']; - $label = $this->_columns[$col]['label']; + foreach ($columns as $col => $spec) { + $field = $spec['field']; + $label = $spec['label']; // Define Content if (in_array ($field, $this->_sortableFields)) { @@ -376,63 +395,59 @@ } /** - * Handles building the body of the DataGrid + /** + * Build a body row * - * @access protected - * @return void + * @param int $index Row index (zero-based) + * @param array $data Record data. + * Structure: array(0 => <value0>, 1 => <value1>, ...) + * @return void + * @access protected */ - function buildBody() + function buildRow($index,$data) { - $startRow = $this->_tableBody->getRowCount(); - for ($row = 0; $row < $this->_recordsNum; $row++) { - $recordRow = $row + $startRow; - 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['numberAlign'] and is_numeric($value)) - ? array('align' => 'right') - : array(); - - // merge auto-aligned and column attributes - if (isset($this->_options['columnAttributes'][$field])) { - $attributes = array_merge($attributes, - $this->_options['columnAttributes'][$field]); - } + $outputRow = $this->_bodyStartRow + $index; + foreach ($data as $col => $value) { + $field = $this->_columns[$col]['field']; - // Set Content in HTML_Table - $this->_tableBody->setCellContents($recordRow, $col, $value); - if ($attributes) { - $this->_tableBody->setCellAttributes($recordRow, $col, $attributes); - } + // Right-align the content if it is numeric + $attributes = ($this->_options['numberAlign'] and is_numeric($value)) + ? array('align' => 'right') + : array(); + + // merge auto-aligned and column attributes + if (isset($this->_options['columnAttributes'][$field])) { + $attributes = array_merge($attributes, + $this->_options['columnAttributes'][$field]); } - } - // output empty rows - if ($this->_options['fillWithEmptyRows'] && !is_null($this->_pageLimit)) { - for ($row = $this->_recordsNum; $row < $this->_pageLimit; $row++) { - $recordRow = $row + $startRow; - for ($col = 0; $col < $this->_columnsNum; $col++) { - $this->_tableBody->setCellAttributes($recordRow, $col, $this->_options['emptyRowAttributes']); - $this->_tableBody->setCellContents($recordRow, $col, '&nbsp;'); - } + // Set Content in HTML_Table + $this->_tableBody->setCellContents($outputRow, $col, $value); + if ($attributes) { + $this->_tableBody->setCellAttributes($outputRow, $col, $attributes); } } - - // Define alternating row attributes - if ($this->_options['evenRowAttributes'] - or $this->_options['oddRowAttributes']) { - - $this->_tableBody->altRowAttributes( - 0, - $this->_options['evenRowAttributes'], - $this->_options['oddRowAttributes'], - true - ); + } + + /** + * Build an empty row + * + * This method will only be called if the "fillWithEmptyRows" option is + * enabled. + * + * @param int $index Row index (zero-based) + * @return void + * @abstract + */ + function buildEmptyRow($index) + { + $outputRow = $this->_bodyStartRow + $index; + for ($col = 0; $col < $this->_columnsNum; $col++) { + $this->_tableBody->setCellAttributes($outputRow, $col, $this->_options['emptyRowAttributes']); + $this->_tableBody->setCellContents($outputRow, $col, '&nbsp;'); } } - + /** * Default formatter for all cells *
« previous php.pear.cvs (#39120) next »