cvs: pear /Structures_DataGrid DataGrid.php /Structures_DataGrid/DataGrid/DataSource Common.php DataObject.php
/Structures_DataGrid/DataGrid/Renderer Common.php HTMLTable.php Smarty.php XUL.php
| From: | Olivier Guilyardi | Date: | Thu, 16 Mar 2006 01:05:52 +0000 |
| Subject: | cvs: pear /Structures_DataGrid DataGrid.php /Structures_DataGrid/DataGrid/DataSource Common.php DataObject.php /Structures_DataGrid/DataGrid/Renderer Common.php HTMLTable.php Smarty.php XUL.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-39117@lists.php.net to get a copy of this message | ||
olivierg Thu Mar 16 01:05:52 2006 UTC
Modified files:
/pear/Structures_DataGrid DataGrid.php
/pear/Structures_DataGrid/DataGrid/DataSource Common.php
DataObject.php
/pear/Structures_DataGrid/DataGrid/Renderer Common.php
HTMLTable.php
Smarty.php XUL.php
Log:
Refactoring and more....
- The renderer driver interface is now more flexible, and allow to write very
simple drivers. Any of the fill(), render() and getOutput() ways of
rendering can be supported or ignored by the driver.
- The sorting HTTP query building process has been abstracted in the
Renderer_Common class
- The XUL renderer has been refactored and (quite) tested
- Some work on the Smarty renderer (not tested)
- Lots of docblocks update
- Cosmetic changes in the DataObject common class
http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid.php?r1=1.44&r2=1.45&diff_format=u Index: pear/Structures_DataGrid/DataGrid.php diff -u pear/Structures_DataGrid/DataGrid.php:1.44 pear/Structures_DataGrid/DataGrid.php:1.45 --- pear/Structures_DataGrid/DataGrid.php:1.44 Wed Mar 8 20:13:09 2006 +++ pear/Structures_DataGrid/DataGrid.php Thu Mar 16 01:05:52 2006 @@ -18,7 +18,7 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: DataGrid.php,v 1.44 2006/03/08 20:13:09 wiesemann Exp $ +// $Id: DataGrid.php,v 1.45 2006/03/16 01:05:52 olivierg Exp $ require_once 'PEAR.php'; @@ -35,6 +35,9 @@ define('DATAGRID_RENDER_DEFAULT', DATAGRID_RENDER_TABLE); +// PEAR_Error code for unsupported features +define('DATAGRID_ERROR_UNSUPPORTED', 1); + // Data Source Drivers define('DATAGRID_SOURCE_ARRAY', 'Array'); define('DATAGRID_SOURCE_DATAOBJECT','DataObject'); @@ -72,7 +75,7 @@ * ?> * </code> * - * @version $Revision: 1.44 $ + * @version $Revision: 1.45 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -90,6 +93,13 @@ var $_renderer; /** + * Renderer driver type + * @var object Structures_DataGrid_Renderer_* family + * @access private + */ + var $_rendererType = null; + + /** * Array of columns. Columns are defined as a DataGridColumn object. * @var array * @access private @@ -345,19 +355,34 @@ * @param string $limit The row limit per page. * @param string $page The current page viewed. * @access public + * @return mixed True or PEAR_Error */ function render() { isset($this->_renderer) or $this->setRenderer(DATAGRID_RENDER_DEFAULT); + $this->_renderer->isBuilt() or $this->build(); - $this->_renderer->render(); + $test = $this->_renderer->render(); + + if (PEAR::isError($test)) { + if ($test->getCode() == DATAGRID_ERROR_UNSUPPORTED) { + $type = is_null($this->_rendererType) + ? get_class($this->_renderer) + : $this->_rendererType; + return new PEAR_Error("The $type driver does not support the ". + "render() method. Try using fill()."); + } else { + return $test; + } + } } /** - * Return the datagrid output as a string + * Return the datagrid output * * @access public - * @return string The datagrid output (HTML, CSV, etc...) + * @return mixed The datagrid output (Usually a string : HTML, CSV, etc...) + * or a PEAR_Error */ function getOutput() { @@ -392,6 +417,7 @@ { $renderer =& $this->rendererFactory($type, $options); if (!PEAR::isError($renderer)) { + $this->_rendererType = $type; return $this->attachRenderer($renderer); } else { return $renderer; @@ -456,7 +482,16 @@ } } - $this->_renderer->setContainer($container); + $test = $this->_renderer->setContainer($container); + if (PEAR::isError($test)) { + if ($test->getCode() == DATAGRID_ERROR_UNSUPPORTED) { + return new PEAR_Error("The $type driver does not support the " . + "fill() method. Try using render()."); + } else { + return $test; + } + } + $this->_renderer->build(); return true; http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/DataSource/Common.php?r1=1.7&r2=1.8&diff_format=u Index: pear/Structures_DataGrid/DataGrid/DataSource/Common.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/Common.php:1.7 pear/Structures_DataGrid/DataGrid/DataSource/Common.php:1.8 --- pear/Structures_DataGrid/DataGrid/DataSource/Common.php:1.7 Thu Mar 2 01:43:52 2006 +++ pear/Structures_DataGrid/DataGrid/DataSource/Common.php Thu Mar 16 01:05:52 2006 @@ -17,7 +17,7 @@ // | Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: Common.php,v 1.7 2006/03/02 01:43:52 olivierg Exp $ +// $Id: Common.php,v 1.8 2006/03/16 01:05:52 olivierg Exp $ /** @@ -157,7 +157,7 @@ * array(feature => true|false, ...) * @return void */ - function _addFeatures($features) + function _setFeatures($features) { $this->_features = array_merge($this->_features, $features); } http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php?r1=1.25&r2=1.26&diff_format=u Index: pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.25 pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.26 --- pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.25 Thu Mar 2 00:52:36 2006 +++ pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php Thu Mar 16 01:05:52 2006 @@ -17,7 +17,7 @@ // | Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: DataObject.php,v 1.25 2006/03/02 00:52:36 olivierg Exp $ +// $Id: DataObject.php,v 1.26 2006/03/16 01:05:52 olivierg Exp $ require_once 'Structures/DataGrid/DataSource/Common.php'; @@ -38,7 +38,7 @@ * same kind of information as the "fields" options. If the "fields" * option is set, this one will not be used. Default : "fb_fieldsToRender". * - * @version $Revision: 1.25 $ + * @version $Revision: 1.26 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @author Andrew Nagy <asnagy@webitecture.org> * @access public @@ -84,7 +84,7 @@ 'sort_property' => 'fb_linkOrderFields', 'formbuilder_integration' => false)); - $this->_addFeatures(array('multiSort' => true)); + $this->_setFeatures(array('multiSort' => true)); } /** http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/Common.php?r1=1.21&r2=1.22&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/Common.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.21 pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.22 --- pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.21 Fri Mar 3 22:43:00 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/Common.php Thu Mar 16 01:05:52 2006 @@ -17,11 +17,11 @@ // | Olivier Guilyardi <olivier@samalyse.com> | // +----------------------------------------------------------------------+ // -// $Id: Common.php,v 1.21 2006/03/03 22:43:00 olivierg Exp $ +// $Id: Common.php,v 1.22 2006/03/16 01:05:52 olivierg Exp $ + +require_once 'PHP/Compat/Function/http_build_query.php'; /** - * Structures_DataGrid_Renderer_Common Class - * * Base class of all Renderer drivers * * Recognized options : @@ -41,26 +41,25 @@ * - encoding : the content encoding. If the mbstring extension is * present the default value is set from * mb_internal_encoding(), otherwise it is ISO-8859-1 + * - extraVars : variables to be added to the generated HTTP queries + * - excludeVars : variables to be removed to the generated HTTP queries * * --- DRIVER INTERFACE --- * - * Methods that drivers MUST implement : - * - init() - * - buildBody() - * - flatten() - * - * Methods that drivers MAY implement : + * Methods (none required) : * - Constructor + * - setContainer() + * - getContainer() + * - init() * - defaultCellFormatter() * - buildHeader() + * - buildBody() * - buildFooter() * - finalize() + * - flatten() * - render() * - * Read/Write property : - * - $_container - * - * Read-Only properties + * Properties (read-only) : * - $_columns * - $_records * - $_columnsNum @@ -78,28 +77,16 @@ * - fillWithEmptyRows * - numberAlign * - * @version $Revision: 1.21 $ + * @version $Revision: 1.22 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @access public * @package Structures_DataGrid * @category Structures + * @abstract */ class Structures_DataGrid_Renderer_Common { /** - * Rendering container - * - * This variable can be of any type. Its type is specific to a given - * driver. - * - * Drivers can read and write to this property. - * - * @var mixed - * @access protected - */ - var $_container = null; - - /** * Columns' fields names and labels * * Drivers can read the content of this property but must not change it. @@ -185,7 +172,7 @@ */ var $_pageLimit = null; - /** + /** * GET/POST/Cookie parameters prefix * * Drivers can read the content of this property but must not change it. @@ -238,7 +225,16 @@ var $_isBuilt = false; /** - * Instantiate the driver and set default options + * Cache for the GET parameters that are common to all sorting http queries + * + * @var array + * @access private + * @see Structures_DataGrid_Renderer_Common::buildSortingHttpQuery() + */ + var $_sortingHttpQueryCommon = null; + + /** + * Instantiate the driver and set default options and features * * Drivers may overload this method in order to change/add default options. * @@ -249,13 +245,15 @@ { $this->_options = array( - /* Protected options, that the drivers should handle */ + /* Options that the drivers may/should handle */ 'encoding' => function_exists('mb_internal_encoding') ? mb_internal_encoding() : 'ISO-8859-1', 'fillWithEmptyRows' => false, 'numberAlign' => true, - - /* Private options, that must not be accessed by drivers */ + 'extraVars' => array(), + 'excludeVars' => array(), + + /* Options that must not be accessed by drivers */ 'buildHeader' => true, 'buildFooter' => true, 'defaultCellValue' => null, @@ -263,6 +261,7 @@ 'disableColumnSorting' => array(), ); + } /** @@ -295,28 +294,6 @@ } /** - * Attach a container object - * - * @param object Container of the class supported by the driver - * @access public - */ - function setContainer(&$container) - { - $this->_container =& $container; - } - - /** - * Return the container used by the driver - * - * @return object Container of the class supported by the driver - * @access public - */ - function &getContainer() - { - return $this->_container; - } - - /** * Provide columns and records data * * @param array $columns Array of Structures_DataGrid_Column objects @@ -356,13 +333,45 @@ } /** + * Attach a container object + * + * Drivers that provide support for the Structures_DataGrid::fill() method + * must implement this method. + * + * @abstract + * @param object Container of the class supported by the driver + * @access public + * @return mixed True or PEAR_Error + */ + function setContainer(&$container) + { + return $this->_noSupport(__FUNCTION__); + } + + /** + * Return the container used by the driver + * + * Drivers should implement this method when they have some kind of support + * for rendering containers. + * + * @abstract + * @return object Container of the class supported by the driver + * or PEAR_Error + * @access public + */ + function &getContainer() + { + return $this->_noSupport(__FUNCTION__); + } + + /** * Create or/and prepare the container * - * Drivers are required to implement this method. + * Drivers may optionally implement this method. * * This method is responsible for creating the container if it has not * already been provided by the user with the setContainer() method. - * It is where preliminary container setup also happens. + * It is where preliminary container should also be done. * * @abstract * @access protected @@ -426,7 +435,7 @@ /** * Retrieve output from the container object * - * Drivers are required to implement this method. + * Drivers may optionally implement this method. * * This method is meant to retrieve final output from the container. * @@ -556,9 +565,12 @@ */ function getOutput() { - $this->_isBuilt or $this->build(); - - return $this->flatten(); + if ($this->_isImplemented('flatten')) { + $this->_isBuilt or $this->build(); + return $this->flatten(); + } else { + return $this->_noSupport(__FUNCTION__); + } } /** @@ -568,13 +580,46 @@ * writing to the standard output (like calling header(), etc...). * * @access public + * @return mixed True or a PEAR_Error */ function render() { - echo $this->getOutput(); + if ($this->_isImplemented('flatten')) { + $this->_isBuilt or $this->build(); + echo $this->flatten(); + } else { + $this->build(); + } } /** + * Return an error related to an unsupported public method + * + * When a given public method is not implemented/supported by the driver + * it must return a PEAR_Error object with code DATAGRID_ERROR_UNSUPPORTED. + * This is a helper method for generating such PEAR_Error objects. + * + * Example: + * + * <code> + * function anUnsupportedMethod() + * { + * return $this->_noSupport(__FUNCTION__); + * } + * </code> + * + * @param string $method The name of the unsupported method + * @return object PEAR_Error with code DATAGRID_ERROR_UNSUPPORTED + * @access protected + */ + function _noSupport($method) + { + return new PEAR_Error("The renderer driver class \"" .get_class($this). + "\" does not support the $method() method", + DATAGRID_ERROR_UNSUPPORTED); + } + + /** * 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 * @@ -591,14 +636,15 @@ /* What are we supposed to do with $status = true ? */ } - /** + /** * Set the HTTP Request prefix * * @param string $prefix The prefix string * @return void * @access public */ - function setRequestPrefix($prefix) { + function setRequestPrefix($prefix) + { $this->_requestPrefix = $prefix; } @@ -636,6 +682,107 @@ { return $this->_isBuilt; } + + /** + * Build an HTTP query for sorting a given column + * + * This is a handy method that most drivers can use in order to build + * the HTTP queries that are used to sort columns. + * + * It takes the global "extraVars", "excludeVars" options as well as the + * $_requestPrefix property into account and can also convert the ampersand + * to XML/HTML entities according to the "encoding" option. + * + * @param $field Sort field name + * @param $direciton Sort direction + * @param $convertAmpersand Whether to convert ampersands to XML/HTML + * compliant entities + * @param $extraParameters Optional extra HTTP parameters + * @return string Query string of the + * @access protected + * + */ + function _buildSortingHttpQuery($field, $direction, $convertAmpersand = false, + $extraParameters = array()) + { + $prefix = $this->_requestPrefix; + + if (is_null($this->_sortingHttpQueryCommon)) { + // Build and cache the list of common get parameters + $this->_sortingHttpQueryCommon = $this->_options['extraVars']; + $ignore = $this->_options['excludeVars']; + $ignore[] = $prefix . 'orderBy'; + $ignore[] = $prefix . 'direction'; + foreach ($extraParameters as $var => $value) { + $ignore[] = $prefix . $var; + } + foreach ($_GET as $key => $val) { + if (!in_array ($key, $ignore)) { + $this->_sortingHttpQueryCommon[$key] = $val; + } + } + } + + // Build list of GET variables + $get = array(); + $get[$prefix . 'orderBy'] = $field; + $get[$prefix . 'direction'] = $direction; + foreach ($extraParameters as $var => $value) { + $get[$prefix . $var] = $value; + } + + // Merge common and column-specific GET variables + $get = array_merge($this->_sortingHttpQueryCommon, $get); + + // Build query + if ($convertAmpersand and ini_get('arg_separator.output') == '&') { + $query = htmlentities(http_build_query($get),ENT_QUOTES, + $this->_options['encoding']); + } else { + $query = http_build_query($get); + } + + return $query; + } + + + /** + * Detect whether a method is implemented in the driver 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 + * @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) + { + if ($class == 'structures_datagrid_renderer_common') { + 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); + } + } + } } ?> http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php?r1=1.91&r2=1.92&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.91 pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.92 --- pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.91 Fri Mar 3 22:49:03 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php Thu Mar 16 01:05:52 2006 @@ -18,11 +18,10 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: HTMLTable.php,v 1.91 2006/03/03 22:49:03 olivierg Exp $ +// $Id: HTMLTable.php,v 1.92 2006/03/16 01:05:52 olivierg Exp $ require_once 'Structures/DataGrid/Renderer/Common.php'; require_once 'HTML/Table.php'; -require_once 'PHP/Compat/Function/http_build_query.php'; /** * Structures_DataGrid_Renderer_HTMLTable Class @@ -43,18 +42,16 @@ * Can be text or HTML to define an image. * - sortIconDESC : The icon to define that sorting is currently Descending. * Can be text or HTML to define an image. - * - extraVars : variables to be added to the generated links - * - excludeVars : variables to be removed to the generated links * - columnAttributes : attributes for the header row. This is an array of the * form: array(attribute => value, ...) * - headerAttributes : column cells attributes. This is an array of the form : * array(fieldName => array(attribute => value, ...) ... ) * - convertEntities : whether or not to convert html entities. Default: true * This calls htmlspecialchars(). - * - sortingResetsPaging: whether to reset paging on sorting request + * - sortingResetsPaging: whether sorting HTTP queries reset paging * (default : true) * - * @version $Revision: 1.91 $ + * @version $Revision: 1.92 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -64,7 +61,13 @@ */ class Structures_DataGrid_Renderer_HTMLTable extends Structures_DataGrid_Renderer_Common { - + /** + * Rendering container + * @var object HTML_Table object + * @access protected + */ + var $_table; + /** * The html_table_storage object for the table header * @var object HTML_Table_Storage @@ -101,8 +104,6 @@ 'selfPath' => $_SERVER['PHP_SELF'], 'sortIconASC' => '', 'sortIconDESC' => '', - 'extraVars' => array(), - 'excludeVars' => array(), 'columnAttributes' => array(), 'headerAttributes' => array(), 'convertEntities' => true, @@ -112,18 +113,45 @@ } /** + * Attach an already instantiated HTML_Table object + * + * @var object HTML_Table object + * @return mixed True or PEAR_Error + * @access public + */ + function setContainer(&$table) + { + $this->_table =& $table; + return true; + } + + /** + * Return the currently used HTML_Table object + * + * @return object HTML_Table (reference to) or PEAR_Error + * @access public + */ + function &getContainer() + { + if (!isset($this->_table)) { + $this->init(); + } + return $this->_table; + } + + /** * 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); + if (!isset($this->_table)) { + $this->_table = new HTML_Table(null, null, true); } - $this->_tableHeader =& $this->_container->getHeader(); - $this->_tableBody =& $this->_container->getBody(); + $this->_tableHeader =& $this->_table->getHeader(); + $this->_tableBody =& $this->_table->getBody(); } /** @@ -136,10 +164,10 @@ */ function setTableAttribute($attr, $value) { - if (is_null($this->_container)) { + if (is_null($this->_table)) { $this->init(); } - $this->_container->updateAttributes(array($attr => $value)); + $this->_table->updateAttributes(array($attr => $value)); } /** @@ -188,7 +216,7 @@ */ function setAutoFill($value) { - if (is_null($this->_container)) { + if (is_null($this->_table)) { $this->init(); } $this->_tableBody->setAutoFill($value); @@ -222,6 +250,7 @@ /** * Determines whether or not to use the Header * + * @deprecated Use the "buildHeader" option instead * @access public * @param bool $bool value to determine to use the header or not. */ @@ -235,7 +264,8 @@ * * This method adds the provided variables to the paging and sorting * links. The variable values are automatically url encoded. - * + * + * @deprecated Use the "extraVars" option instead * @param array $vars Array of the form (key => value, ...) * @access public * @return void @@ -252,6 +282,7 @@ * links. This is helpful when using variables that determine what page to * show such as an 'action' variable, etc. * + * @deprecated Use the "excludeVars" option instead * @param array $vars An array of variables to remove * @access public * @return void @@ -282,7 +313,7 @@ */ function &getTable() { - return $this->_container; + return $this->_table; } /** @@ -294,19 +325,6 @@ */ function buildHeader() { - $prefix = $this->_requestPrefix; - - // Build the list of common get parameters - $common = $this->_options['extraVars']; - $ignore = $this->_options['excludeVars']; - $ignore[] = $prefix . 'orderBy'; - $ignore[] = $prefix . 'page'; - $ignore[] = $prefix . 'direction'; - foreach ($_GET as $key => $val) { - if (!in_array ($key, $ignore)) { - $common[$key] = $val; - } - } $row = $this->_tableHeader->getRowCount(); for ($col = 0; $col < $this->_columnsNum; $col++) { @@ -332,26 +350,13 @@ $direction = 'ASC'; } - // Build list of GET variables - $get = array(); - $get[$prefix . 'orderBy'] = $field; - $get[$prefix . 'direction'] = $direction; - $get[$prefix . 'page'] - = $this->_options['sortingResetsPaging'] ? 1 : $this->_page; + // Build HTTP query + $extra = array ('page' => $this->_options['sortingResetsPaging'] + ? 1 : $this->_page); + $query = $this->_buildSortingHttpQuery($field, $direction, true, $extra); // Build Link URL - $url = $this->_options['selfPath'] . '?'; - - // Merge common and column-specific GET variables - if (ini_get('arg_separator.output') == '&') { - $url .= htmlentities( - http_build_query(array_merge($common, $get)), - ENT_QUOTES, - $this->_options['encoding']); - } else { - $url .= http_build_query(array_merge($common, $get)); - } - + $url = $this->_options['selfPath'] . '?' . $query; // Build HTML Link $str = "<a href=\"$url\">$label $icon</a>"; @@ -450,7 +455,7 @@ */ function flatten() { - return $this->_container->toHTML(); + return $this->_table->toHTML(); } /** @@ -513,11 +518,11 @@ */ function _buildPaging($options) { - $defaults = array('totalItems' => $this->_totalRecordsNum, - 'perPage' => is_null($this->_pageLimit) - ? $this->_totalRecordsNum - : $this->_pageLimit, - 'urlVar' => $this->_requestPrefix . 'page', + $defaults = array('totalItems' => $this->_totalRecordsNum, + 'perPage' => is_null($this->_pageLimit) + ? $this->_totalRecordsNum + : $this->_pageLimit, + 'urlVar' => $this->_requestPrefix . 'page', 'currentPage' => $this->_page); $options = array_merge($defaults, $options); $this->_pager =& Pager::factory($options); http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php?r1=1.13&r2=1.14&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php:1.13 pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php:1.14 --- pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php:1.13 Sat Feb 25 09:58:45 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php Thu Mar 16 01:05:52 2006 @@ -18,51 +18,55 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Smarty.php,v 1.13 2006/02/25 09:58:45 wiesemann Exp $ +// $Id: Smarty.php,v 1.14 2006/03/16 01:05:52 olivierg Exp $ -require_once 'Structures/DataGrid/Renderer/Common.php'; -require_once 'Smarty/Smarty.class.php'; +require_once 'Structures/DataGrid/Renderer/HTMLTable.php'; +// require_once 'Smarty/Smarty.class.php'; /** * Structures_DataGrid_Renderer_Smarty Class * - * @version $Revision: 1.13 $ + * @version $Revision: 1.14 $ * @author Andrew S. Nagy <asnagy@webitecture.org> + * @author Olivier Guilyardi <olivier@samalyse.com> * @access public * @package Structures_DataGrid * @category Structures */ -class Structures_DataGrid_Renderer_Smarty extends Structures_DataGrid_Renderer_Common +class Structures_DataGrid_Renderer_Smarty extends Structures_DataGrid_Renderer_HTMLTable { // FIXME: test this renderer // FIXME: implement paging feature request (write a common paging class for // HTMLTable and Smarty renderer) /** - * Name of the template file - * - * @var string - * @access private + * Smarty container + * @var object $_smarty; */ - var $_tpl; - + var $_smarty; + /** - * Constructor - * - * Build default values - * - * @access public + * Attach an already instantiated Smarty object + * + * @param object $smarty Smarty container + * @return mixed True or PEAR_Error */ - function Structures_DataGrid_Renderer_Smarty() + function setContainer(&$smarty) { - parent::Structures_DataGrid_Renderer_Common(); - $this->_addDefaultOptions( - array( - // FIXME: maybe add an option for the template filename - ) - ); + $this->_smarty =& $smarty; + return true; } - + + /** + * Return the currently used Smarty object + * @return object Smarty or PEAR_Error object + */ + function &getContainer() + { + isset($this->_smarty) or $this->init(); + return &$this->_smarty; + } + /** * Initialize the Smarty instance if it is not already existing * @@ -70,41 +74,23 @@ */ function init() { - if (is_null($this->_container)) { - $this->_container = new Smarty(); - $this->_container->template_dir = dirname($_SERVER['SCRIPT_FILENAME']); - $this->_container->compile_dir = dirname($_SERVER['SCRIPT_FILENAME']) . '/compile'; + if (!isset($this->_smarty)) { + $this->_smarty = new Smarty(); } } /** * Attach a Smarty instance - * (deprecated, use $renderer->setContainer() or $dg->fill() instead) - * + * + * @deprecated Use setContainer() instead * @param object Smarty instance * @access public */ function setSmarty(&$smarty) { - $this->_container = &$smarty; + return $this->setContainer($smarty); } - /** - * Set the Smarty template - * - * @param string Filename of the template - * @access public - */ - function setTemplate($tpl) - { - // FIXME: make $_tpl an option (?) - if (is_file($this->_container->template_dir . '/' . $tpl)) { - $this->_tpl = $tpl; - } else { - return new PEAR_Error('Error: Unable to find ' . - $this->_container->template_dir . '/' . $tpl); - } - } /** * Handles building the body of the table @@ -115,46 +101,39 @@ function buildBody() { if ($this->_tpl != '') { - $this->_container->assign('recordSet', $this->_records); - $this->_container->assign('columnSet', $this->_columns); - $this->_container->assign('recordLimit', $this->_pageLimit); - $this->_container->assign('currentPage', $this->_page); + $this->_smarty->assign('recordSet', $this->_records); + $this->_smarty->assign('columnSet', $this->_columns); + $this->_smarty->assign('recordLimit', $this->_pageLimit); + $this->_smarty->assign('currentPage', $this->_page); } } /** * Gets the Smarty object * - * OBSOLETE - * + * @deprecated Use getContainer() instead * @access public - * @return object Console_Table The Console_Table object for the DataGrid + * @return object Smarty container (reference) */ - function getSmarty() + function &getSmarty() { - return $this->_container; + return $this->getContainer(); } + /** - * Retrieve output from the container object - * - * @return mixed Output - * @access protected + * Discard the unsupported render() method + * + * This Smarty driver does not support the render() method. + * It is required to use the setContainer() (or + * Structures_DataGrid::fill()) method in order to do anything + * with this driver. + * */ - function flatten() + function render() { - $smarty = $this->getSmarty(); - - // FIXME: this error shouldn't occur - // FIXME: test $this->_tpl here (if it's not set, display() may result - // in an error [to be checked!]) - if (PEAR::isError($smarty)) { - return $smarty; - } else { - $smarty->display($this->_tpl); - } + return $this->_noSupport(__FUNCTION__); } - } ?> http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/XUL.php?r1=1.18&r2=1.19&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/XUL.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/XUL.php:1.18 pear/Structures_DataGrid/DataGrid/Renderer/XUL.php:1.19 --- pear/Structures_DataGrid/DataGrid/Renderer/XUL.php:1.18 Thu Mar 2 00:42:16 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/XUL.php Thu Mar 16 01:05:52 2006 @@ -18,7 +18,7 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: XUL.php,v 1.18 2006/03/02 00:42:16 olivierg Exp $ +// $Id: XUL.php,v 1.19 2006/03/16 01:05:52 olivierg Exp $ require_once 'Structures/DataGrid/Renderer/Common.php'; //require_once 'XML/XUL.php'; @@ -31,16 +31,37 @@ * For additional information on the XUL Listbox, refer to this url: * http://www.xulplanet.com/references/elemref/ref_listbox.html * + * This driver does not support the setContainer() method, and can not + * be used in conjunction with the Structures_DataGrid::fill() method. + * + * The generated XUL is buffered so you can use the + * Structures_DataGrid::getOutput() method as an alternative to render() + * + * You have to setup your XUL document, just as you would with an HTML + * document. This driver will only generated the <listbox> element and + * content. + * + * Basic example : + * <code> + * <?php + * header('Content-type: application/vnd.mozilla.xul+xml'); + * echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; + * echo "<?xml-stylesheet href=\"myStyle.css\" type=\"text/css\"?>\n"; + * echo "<window title=\"MyDataGrid\" + * xmlns=\"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul\">\n"; + * // Instantiate your datagrid and setup its datasource, then call : + * $datagrid->setRenderer(DATAGRID_RENDER_XUL); + * $datagrid->render(); + * echo "</window>\n"; + * ?> + * </code> + * * Recognized options: * - * - title: (string) The title of the datagrid - * (default: 'DataGrid') - * - css: (array) An array of css URL's - * (default: 'chrome://global/skin/') - * - selfPath: (string) The complete path for sorting and paging links - * (default: $_SERVER['PHP_SELF']) + * - selfPath: (string) The complete path for sorting and paging links + * (default: $_SERVER['PHP_SELF']) * - * @version $Revision: 1.18 $ + * @version $Revision: 1.19 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -52,11 +73,11 @@ class Structures_DataGrid_Renderer_XUL extends Structures_DataGrid_Renderer_Common { /** - * Whether the container is user-provided or not - * @var bool + * The generated XUL data + * @var string * @access protected */ - var $_isCustomContainer; + var $_xul = ''; /** * Constructor @@ -70,8 +91,6 @@ parent::Structures_DataGrid_Renderer_Common(); $this->_addDefaultOptions( array( - 'title' => 'DataGrid', - 'css' => array('chrome://global/skin/'), 'selfPath' => $_SERVER['PHP_SELF'] ) ); @@ -89,52 +108,7 @@ */ function init() { - if (is_null($this->_container)) { - - $this->_isCustomContainer = false; - - // Define XML - $this->_container = XML_Util::getXMLDeclaration() . "\n"; - - // Define Stylesheets - foreach ($this->_options['css'] as $css) { - $this->_container .= "<?xml-stylesheet href=\"$css\" " . - "type=\"text/css\"?>\n"; - } - - // Define Window Element - $this->_container .= - "<window title=\"{$this->_options['title']}\" " . - "xmlns=\"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul\">\n"; - - // Define Listbox Element - $this->_container .= "<listbox rows=\"" . $this->_pageLimit . "\">\n"; - - } else { - $this->_isCustomContainer = true; - } - } - - /** - * Sets the datagrid title - * - * @access public - * @param string $title The title of the datagrid - */ - function setTitle($title) - { - $this->_options['title'] = $title; - } - - /** - * Adds a stylesheet to the list of stylesheets - * - * @access public - * @param string $url The url of the stylesheet - */ - function addStyleSheet($url) - { - array_push($this->_options['css'], $url); + $this->_xul = "<listbox rows=\"" . $this->_pageLimit . "\">\n"; } /** @@ -145,7 +119,7 @@ */ function buildHeader() { - $this->_container .= " <listhead>\n"; + $this->_xul .= " <listhead>\n"; for ($col = 0; $col < $this->_columnsNum; $col++) { $field = $this->_columns[$col]['field']; $label = $this->_columns[$col]['label']; @@ -176,9 +150,9 @@ } $onCommand = - "oncommand=\"location.href='{$this->_options['selfPath']}". - "?{$this->_requestPrefix}orderBy=$field". - "&{$this->_requestPrefix}direction=$dirArg';\""; + "oncommand=\"location.href='{$this->_options['selfPath']}?" + . $this->_buildSortingHttpQuery($field, $dirArg, true) + . "'\""; $sortDirection = "sortDirection=\"$dirCur\""; } else { $onCommand = ''; @@ -186,10 +160,10 @@ } $label = XML_Util::replaceEntities($label); - $this->_container .= ' <listheader label="' . $label . '" ' . + $this->_xul .= ' <listheader label="' . $label . '" ' . "$sortDirection $onCommand />\n"; } - $this->_container .= " </listhead>\n"; + $this->_xul .= " </listhead>\n"; } /** @@ -201,31 +175,28 @@ function buildBody() { for ($row = 0; $row < $this->_recordsNum; $row++) { - $this->_container .= " <listitem>\n"; + $this->_xul .= " <listitem>\n"; for ($col = 0; $col < $this->_columnsNum; $col++) { $value = $this->_records[$row][$col]; // FIXME: 'ä' is displayed as '?' ==> encoding is required! - $this->_container .= ' ' . + $this->_xul .= ' ' . XML_Util::createTag('listcell', array('label' => $value)) . "\n"; } - $this->_container .= " </listitem>\n"; + $this->_xul .= " </listitem>\n"; } } /** - * Close the XUL listbox and window, if needed + * Close the XUL listbox * * @access protected */ function finalize() { - if (!$this->_isCustomContainer) { - $this->_container .= "</listbox>\n"; - $this->_container .= "</window>\n"; - } + $this->_xul .= "</listbox>\n"; } /** @@ -247,18 +218,7 @@ */ function flatten() { - return $this->_container; - } - - /** - * Render to the standard output - * - * @access public - */ - function render() - { - header('Content-type: application/vnd.mozilla.xul+xml'); - parent::render(); + return $this->_xul; } }
http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid.php?r1=1.44&r2=1.45&diff_format=u Index: pear/Structures_DataGrid/DataGrid.php diff -u pear/Structures_DataGrid/DataGrid.php:1.44 pear/Structures_DataGrid/DataGrid.php:1.45 --- pear/Structures_DataGrid/DataGrid.php:1.44 Wed Mar 8 20:13:09 2006 +++ pear/Structures_DataGrid/DataGrid.php Thu Mar 16 01:05:52 2006 @@ -18,7 +18,7 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: DataGrid.php,v 1.44 2006/03/08 20:13:09 wiesemann Exp $ +// $Id: DataGrid.php,v 1.45 2006/03/16 01:05:52 olivierg Exp $ require_once 'PEAR.php'; @@ -35,6 +35,9 @@ define('DATAGRID_RENDER_DEFAULT', DATAGRID_RENDER_TABLE); +// PEAR_Error code for unsupported features +define('DATAGRID_ERROR_UNSUPPORTED', 1); + // Data Source Drivers define('DATAGRID_SOURCE_ARRAY', 'Array'); define('DATAGRID_SOURCE_DATAOBJECT','DataObject'); @@ -72,7 +75,7 @@ * ?> * </code> * - * @version $Revision: 1.44 $ + * @version $Revision: 1.45 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -90,6 +93,13 @@ var $_renderer; /** + * Renderer driver type + * @var object Structures_DataGrid_Renderer_* family + * @access private + */ + var $_rendererType = null; + + /** * Array of columns. Columns are defined as a DataGridColumn object. * @var array * @access private @@ -345,19 +355,34 @@ * @param string $limit The row limit per page. * @param string $page The current page viewed. * @access public + * @return mixed True or PEAR_Error */ function render() { isset($this->_renderer) or $this->setRenderer(DATAGRID_RENDER_DEFAULT); + $this->_renderer->isBuilt() or $this->build(); - $this->_renderer->render(); + $test = $this->_renderer->render(); + + if (PEAR::isError($test)) { + if ($test->getCode() == DATAGRID_ERROR_UNSUPPORTED) { + $type = is_null($this->_rendererType) + ? get_class($this->_renderer) + : $this->_rendererType; + return new PEAR_Error("The $type driver does not support the ". + "render() method. Try using fill()."); + } else { + return $test; + } + } } /** - * Return the datagrid output as a string + * Return the datagrid output * * @access public - * @return string The datagrid output (HTML, CSV, etc...) + * @return mixed The datagrid output (Usually a string : HTML, CSV, etc...) + * or a PEAR_Error */ function getOutput() { @@ -392,6 +417,7 @@ { $renderer =& $this->rendererFactory($type, $options); if (!PEAR::isError($renderer)) { + $this->_rendererType = $type; return $this->attachRenderer($renderer); } else { return $renderer; @@ -456,7 +482,16 @@ } } - $this->_renderer->setContainer($container); + $test = $this->_renderer->setContainer($container); + if (PEAR::isError($test)) { + if ($test->getCode() == DATAGRID_ERROR_UNSUPPORTED) { + return new PEAR_Error("The $type driver does not support the " . + "fill() method. Try using render()."); + } else { + return $test; + } + } + $this->_renderer->build(); return true; http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/DataSource/Common.php?r1=1.7&r2=1.8&diff_format=u Index: pear/Structures_DataGrid/DataGrid/DataSource/Common.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/Common.php:1.7 pear/Structures_DataGrid/DataGrid/DataSource/Common.php:1.8 --- pear/Structures_DataGrid/DataGrid/DataSource/Common.php:1.7 Thu Mar 2 01:43:52 2006 +++ pear/Structures_DataGrid/DataGrid/DataSource/Common.php Thu Mar 16 01:05:52 2006 @@ -17,7 +17,7 @@ // | Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: Common.php,v 1.7 2006/03/02 01:43:52 olivierg Exp $ +// $Id: Common.php,v 1.8 2006/03/16 01:05:52 olivierg Exp $ /** @@ -157,7 +157,7 @@ * array(feature => true|false, ...) * @return void */ - function _addFeatures($features) + function _setFeatures($features) { $this->_features = array_merge($this->_features, $features); } http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php?r1=1.25&r2=1.26&diff_format=u Index: pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.25 pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.26 --- pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.25 Thu Mar 2 00:52:36 2006 +++ pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php Thu Mar 16 01:05:52 2006 @@ -17,7 +17,7 @@ // | Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: DataObject.php,v 1.25 2006/03/02 00:52:36 olivierg Exp $ +// $Id: DataObject.php,v 1.26 2006/03/16 01:05:52 olivierg Exp $ require_once 'Structures/DataGrid/DataSource/Common.php'; @@ -38,7 +38,7 @@ * same kind of information as the "fields" options. If the "fields" * option is set, this one will not be used. Default : "fb_fieldsToRender". * - * @version $Revision: 1.25 $ + * @version $Revision: 1.26 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @author Andrew Nagy <asnagy@webitecture.org> * @access public @@ -84,7 +84,7 @@ 'sort_property' => 'fb_linkOrderFields', 'formbuilder_integration' => false)); - $this->_addFeatures(array('multiSort' => true)); + $this->_setFeatures(array('multiSort' => true)); } /** http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/Common.php?r1=1.21&r2=1.22&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/Common.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.21 pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.22 --- pear/Structures_DataGrid/DataGrid/Renderer/Common.php:1.21 Fri Mar 3 22:43:00 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/Common.php Thu Mar 16 01:05:52 2006 @@ -17,11 +17,11 @@ // | Olivier Guilyardi <olivier@samalyse.com> | // +----------------------------------------------------------------------+ // -// $Id: Common.php,v 1.21 2006/03/03 22:43:00 olivierg Exp $ +// $Id: Common.php,v 1.22 2006/03/16 01:05:52 olivierg Exp $ + +require_once 'PHP/Compat/Function/http_build_query.php'; /** - * Structures_DataGrid_Renderer_Common Class - * * Base class of all Renderer drivers * * Recognized options : @@ -41,26 +41,25 @@ * - encoding : the content encoding. If the mbstring extension is * present the default value is set from * mb_internal_encoding(), otherwise it is ISO-8859-1 + * - extraVars : variables to be added to the generated HTTP queries + * - excludeVars : variables to be removed to the generated HTTP queries * * --- DRIVER INTERFACE --- * - * Methods that drivers MUST implement : - * - init() - * - buildBody() - * - flatten() - * - * Methods that drivers MAY implement : + * Methods (none required) : * - Constructor + * - setContainer() + * - getContainer() + * - init() * - defaultCellFormatter() * - buildHeader() + * - buildBody() * - buildFooter() * - finalize() + * - flatten() * - render() * - * Read/Write property : - * - $_container - * - * Read-Only properties + * Properties (read-only) : * - $_columns * - $_records * - $_columnsNum @@ -78,28 +77,16 @@ * - fillWithEmptyRows * - numberAlign * - * @version $Revision: 1.21 $ + * @version $Revision: 1.22 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @access public * @package Structures_DataGrid * @category Structures + * @abstract */ class Structures_DataGrid_Renderer_Common { /** - * Rendering container - * - * This variable can be of any type. Its type is specific to a given - * driver. - * - * Drivers can read and write to this property. - * - * @var mixed - * @access protected - */ - var $_container = null; - - /** * Columns' fields names and labels * * Drivers can read the content of this property but must not change it. @@ -185,7 +172,7 @@ */ var $_pageLimit = null; - /** + /** * GET/POST/Cookie parameters prefix * * Drivers can read the content of this property but must not change it. @@ -238,7 +225,16 @@ var $_isBuilt = false; /** - * Instantiate the driver and set default options + * Cache for the GET parameters that are common to all sorting http queries + * + * @var array + * @access private + * @see Structures_DataGrid_Renderer_Common::buildSortingHttpQuery() + */ + var $_sortingHttpQueryCommon = null; + + /** + * Instantiate the driver and set default options and features * * Drivers may overload this method in order to change/add default options. * @@ -249,13 +245,15 @@ { $this->_options = array( - /* Protected options, that the drivers should handle */ + /* Options that the drivers may/should handle */ 'encoding' => function_exists('mb_internal_encoding') ? mb_internal_encoding() : 'ISO-8859-1', 'fillWithEmptyRows' => false, 'numberAlign' => true, - - /* Private options, that must not be accessed by drivers */ + 'extraVars' => array(), + 'excludeVars' => array(), + + /* Options that must not be accessed by drivers */ 'buildHeader' => true, 'buildFooter' => true, 'defaultCellValue' => null, @@ -263,6 +261,7 @@ 'disableColumnSorting' => array(), ); + } /** @@ -295,28 +294,6 @@ } /** - * Attach a container object - * - * @param object Container of the class supported by the driver - * @access public - */ - function setContainer(&$container) - { - $this->_container =& $container; - } - - /** - * Return the container used by the driver - * - * @return object Container of the class supported by the driver - * @access public - */ - function &getContainer() - { - return $this->_container; - } - - /** * Provide columns and records data * * @param array $columns Array of Structures_DataGrid_Column objects @@ -356,13 +333,45 @@ } /** + * Attach a container object + * + * Drivers that provide support for the Structures_DataGrid::fill() method + * must implement this method. + * + * @abstract + * @param object Container of the class supported by the driver + * @access public + * @return mixed True or PEAR_Error + */ + function setContainer(&$container) + { + return $this->_noSupport(__FUNCTION__); + } + + /** + * Return the container used by the driver + * + * Drivers should implement this method when they have some kind of support + * for rendering containers. + * + * @abstract + * @return object Container of the class supported by the driver + * or PEAR_Error + * @access public + */ + function &getContainer() + { + return $this->_noSupport(__FUNCTION__); + } + + /** * Create or/and prepare the container * - * Drivers are required to implement this method. + * Drivers may optionally implement this method. * * This method is responsible for creating the container if it has not * already been provided by the user with the setContainer() method. - * It is where preliminary container setup also happens. + * It is where preliminary container should also be done. * * @abstract * @access protected @@ -426,7 +435,7 @@ /** * Retrieve output from the container object * - * Drivers are required to implement this method. + * Drivers may optionally implement this method. * * This method is meant to retrieve final output from the container. * @@ -556,9 +565,12 @@ */ function getOutput() { - $this->_isBuilt or $this->build(); - - return $this->flatten(); + if ($this->_isImplemented('flatten')) { + $this->_isBuilt or $this->build(); + return $this->flatten(); + } else { + return $this->_noSupport(__FUNCTION__); + } } /** @@ -568,13 +580,46 @@ * writing to the standard output (like calling header(), etc...). * * @access public + * @return mixed True or a PEAR_Error */ function render() { - echo $this->getOutput(); + if ($this->_isImplemented('flatten')) { + $this->_isBuilt or $this->build(); + echo $this->flatten(); + } else { + $this->build(); + } } /** + * Return an error related to an unsupported public method + * + * When a given public method is not implemented/supported by the driver + * it must return a PEAR_Error object with code DATAGRID_ERROR_UNSUPPORTED. + * This is a helper method for generating such PEAR_Error objects. + * + * Example: + * + * <code> + * function anUnsupportedMethod() + * { + * return $this->_noSupport(__FUNCTION__); + * } + * </code> + * + * @param string $method The name of the unsupported method + * @return object PEAR_Error with code DATAGRID_ERROR_UNSUPPORTED + * @access protected + */ + function _noSupport($method) + { + return new PEAR_Error("The renderer driver class \"" .get_class($this). + "\" does not support the $method() method", + DATAGRID_ERROR_UNSUPPORTED); + } + + /** * 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 * @@ -591,14 +636,15 @@ /* What are we supposed to do with $status = true ? */ } - /** + /** * Set the HTTP Request prefix * * @param string $prefix The prefix string * @return void * @access public */ - function setRequestPrefix($prefix) { + function setRequestPrefix($prefix) + { $this->_requestPrefix = $prefix; } @@ -636,6 +682,107 @@ { return $this->_isBuilt; } + + /** + * Build an HTTP query for sorting a given column + * + * This is a handy method that most drivers can use in order to build + * the HTTP queries that are used to sort columns. + * + * It takes the global "extraVars", "excludeVars" options as well as the + * $_requestPrefix property into account and can also convert the ampersand + * to XML/HTML entities according to the "encoding" option. + * + * @param $field Sort field name + * @param $direciton Sort direction + * @param $convertAmpersand Whether to convert ampersands to XML/HTML + * compliant entities + * @param $extraParameters Optional extra HTTP parameters + * @return string Query string of the + * @access protected + * + */ + function _buildSortingHttpQuery($field, $direction, $convertAmpersand = false, + $extraParameters = array()) + { + $prefix = $this->_requestPrefix; + + if (is_null($this->_sortingHttpQueryCommon)) { + // Build and cache the list of common get parameters + $this->_sortingHttpQueryCommon = $this->_options['extraVars']; + $ignore = $this->_options['excludeVars']; + $ignore[] = $prefix . 'orderBy'; + $ignore[] = $prefix . 'direction'; + foreach ($extraParameters as $var => $value) { + $ignore[] = $prefix . $var; + } + foreach ($_GET as $key => $val) { + if (!in_array ($key, $ignore)) { + $this->_sortingHttpQueryCommon[$key] = $val; + } + } + } + + // Build list of GET variables + $get = array(); + $get[$prefix . 'orderBy'] = $field; + $get[$prefix . 'direction'] = $direction; + foreach ($extraParameters as $var => $value) { + $get[$prefix . $var] = $value; + } + + // Merge common and column-specific GET variables + $get = array_merge($this->_sortingHttpQueryCommon, $get); + + // Build query + if ($convertAmpersand and ini_get('arg_separator.output') == '&') { + $query = htmlentities(http_build_query($get),ENT_QUOTES, + $this->_options['encoding']); + } else { + $query = http_build_query($get); + } + + return $query; + } + + + /** + * Detect whether a method is implemented in the driver 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 + * @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) + { + if ($class == 'structures_datagrid_renderer_common') { + 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); + } + } + } } ?> http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php?r1=1.91&r2=1.92&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.91 pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.92 --- pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php:1.91 Fri Mar 3 22:49:03 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php Thu Mar 16 01:05:52 2006 @@ -18,11 +18,10 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: HTMLTable.php,v 1.91 2006/03/03 22:49:03 olivierg Exp $ +// $Id: HTMLTable.php,v 1.92 2006/03/16 01:05:52 olivierg Exp $ require_once 'Structures/DataGrid/Renderer/Common.php'; require_once 'HTML/Table.php'; -require_once 'PHP/Compat/Function/http_build_query.php'; /** * Structures_DataGrid_Renderer_HTMLTable Class @@ -43,18 +42,16 @@ * Can be text or HTML to define an image. * - sortIconDESC : The icon to define that sorting is currently Descending. * Can be text or HTML to define an image. - * - extraVars : variables to be added to the generated links - * - excludeVars : variables to be removed to the generated links * - columnAttributes : attributes for the header row. This is an array of the * form: array(attribute => value, ...) * - headerAttributes : column cells attributes. This is an array of the form : * array(fieldName => array(attribute => value, ...) ... ) * - convertEntities : whether or not to convert html entities. Default: true * This calls htmlspecialchars(). - * - sortingResetsPaging: whether to reset paging on sorting request + * - sortingResetsPaging: whether sorting HTTP queries reset paging * (default : true) * - * @version $Revision: 1.91 $ + * @version $Revision: 1.92 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -64,7 +61,13 @@ */ class Structures_DataGrid_Renderer_HTMLTable extends Structures_DataGrid_Renderer_Common { - + /** + * Rendering container + * @var object HTML_Table object + * @access protected + */ + var $_table; + /** * The html_table_storage object for the table header * @var object HTML_Table_Storage @@ -101,8 +104,6 @@ 'selfPath' => $_SERVER['PHP_SELF'], 'sortIconASC' => '', 'sortIconDESC' => '', - 'extraVars' => array(), - 'excludeVars' => array(), 'columnAttributes' => array(), 'headerAttributes' => array(), 'convertEntities' => true, @@ -112,18 +113,45 @@ } /** + * Attach an already instantiated HTML_Table object + * + * @var object HTML_Table object + * @return mixed True or PEAR_Error + * @access public + */ + function setContainer(&$table) + { + $this->_table =& $table; + return true; + } + + /** + * Return the currently used HTML_Table object + * + * @return object HTML_Table (reference to) or PEAR_Error + * @access public + */ + function &getContainer() + { + if (!isset($this->_table)) { + $this->init(); + } + return $this->_table; + } + + /** * 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); + if (!isset($this->_table)) { + $this->_table = new HTML_Table(null, null, true); } - $this->_tableHeader =& $this->_container->getHeader(); - $this->_tableBody =& $this->_container->getBody(); + $this->_tableHeader =& $this->_table->getHeader(); + $this->_tableBody =& $this->_table->getBody(); } /** @@ -136,10 +164,10 @@ */ function setTableAttribute($attr, $value) { - if (is_null($this->_container)) { + if (is_null($this->_table)) { $this->init(); } - $this->_container->updateAttributes(array($attr => $value)); + $this->_table->updateAttributes(array($attr => $value)); } /** @@ -188,7 +216,7 @@ */ function setAutoFill($value) { - if (is_null($this->_container)) { + if (is_null($this->_table)) { $this->init(); } $this->_tableBody->setAutoFill($value); @@ -222,6 +250,7 @@ /** * Determines whether or not to use the Header * + * @deprecated Use the "buildHeader" option instead * @access public * @param bool $bool value to determine to use the header or not. */ @@ -235,7 +264,8 @@ * * This method adds the provided variables to the paging and sorting * links. The variable values are automatically url encoded. - * + * + * @deprecated Use the "extraVars" option instead * @param array $vars Array of the form (key => value, ...) * @access public * @return void @@ -252,6 +282,7 @@ * links. This is helpful when using variables that determine what page to * show such as an 'action' variable, etc. * + * @deprecated Use the "excludeVars" option instead * @param array $vars An array of variables to remove * @access public * @return void @@ -282,7 +313,7 @@ */ function &getTable() { - return $this->_container; + return $this->_table; } /** @@ -294,19 +325,6 @@ */ function buildHeader() { - $prefix = $this->_requestPrefix; - - // Build the list of common get parameters - $common = $this->_options['extraVars']; - $ignore = $this->_options['excludeVars']; - $ignore[] = $prefix . 'orderBy'; - $ignore[] = $prefix . 'page'; - $ignore[] = $prefix . 'direction'; - foreach ($_GET as $key => $val) { - if (!in_array ($key, $ignore)) { - $common[$key] = $val; - } - } $row = $this->_tableHeader->getRowCount(); for ($col = 0; $col < $this->_columnsNum; $col++) { @@ -332,26 +350,13 @@ $direction = 'ASC'; } - // Build list of GET variables - $get = array(); - $get[$prefix . 'orderBy'] = $field; - $get[$prefix . 'direction'] = $direction; - $get[$prefix . 'page'] - = $this->_options['sortingResetsPaging'] ? 1 : $this->_page; + // Build HTTP query + $extra = array ('page' => $this->_options['sortingResetsPaging'] + ? 1 : $this->_page); + $query = $this->_buildSortingHttpQuery($field, $direction, true, $extra); // Build Link URL - $url = $this->_options['selfPath'] . '?'; - - // Merge common and column-specific GET variables - if (ini_get('arg_separator.output') == '&') { - $url .= htmlentities( - http_build_query(array_merge($common, $get)), - ENT_QUOTES, - $this->_options['encoding']); - } else { - $url .= http_build_query(array_merge($common, $get)); - } - + $url = $this->_options['selfPath'] . '?' . $query; // Build HTML Link $str = "<a href=\"$url\">$label $icon</a>"; @@ -450,7 +455,7 @@ */ function flatten() { - return $this->_container->toHTML(); + return $this->_table->toHTML(); } /** @@ -513,11 +518,11 @@ */ function _buildPaging($options) { - $defaults = array('totalItems' => $this->_totalRecordsNum, - 'perPage' => is_null($this->_pageLimit) - ? $this->_totalRecordsNum - : $this->_pageLimit, - 'urlVar' => $this->_requestPrefix . 'page', + $defaults = array('totalItems' => $this->_totalRecordsNum, + 'perPage' => is_null($this->_pageLimit) + ? $this->_totalRecordsNum + : $this->_pageLimit, + 'urlVar' => $this->_requestPrefix . 'page', 'currentPage' => $this->_page); $options = array_merge($defaults, $options); $this->_pager =& Pager::factory($options); http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php?r1=1.13&r2=1.14&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php:1.13 pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php:1.14 --- pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php:1.13 Sat Feb 25 09:58:45 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php Thu Mar 16 01:05:52 2006 @@ -18,51 +18,55 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Smarty.php,v 1.13 2006/02/25 09:58:45 wiesemann Exp $ +// $Id: Smarty.php,v 1.14 2006/03/16 01:05:52 olivierg Exp $ -require_once 'Structures/DataGrid/Renderer/Common.php'; -require_once 'Smarty/Smarty.class.php'; +require_once 'Structures/DataGrid/Renderer/HTMLTable.php'; +// require_once 'Smarty/Smarty.class.php'; /** * Structures_DataGrid_Renderer_Smarty Class * - * @version $Revision: 1.13 $ + * @version $Revision: 1.14 $ * @author Andrew S. Nagy <asnagy@webitecture.org> + * @author Olivier Guilyardi <olivier@samalyse.com> * @access public * @package Structures_DataGrid * @category Structures */ -class Structures_DataGrid_Renderer_Smarty extends Structures_DataGrid_Renderer_Common +class Structures_DataGrid_Renderer_Smarty extends Structures_DataGrid_Renderer_HTMLTable { // FIXME: test this renderer // FIXME: implement paging feature request (write a common paging class for // HTMLTable and Smarty renderer) /** - * Name of the template file - * - * @var string - * @access private + * Smarty container + * @var object $_smarty; */ - var $_tpl; - + var $_smarty; + /** - * Constructor - * - * Build default values - * - * @access public + * Attach an already instantiated Smarty object + * + * @param object $smarty Smarty container + * @return mixed True or PEAR_Error */ - function Structures_DataGrid_Renderer_Smarty() + function setContainer(&$smarty) { - parent::Structures_DataGrid_Renderer_Common(); - $this->_addDefaultOptions( - array( - // FIXME: maybe add an option for the template filename - ) - ); + $this->_smarty =& $smarty; + return true; } - + + /** + * Return the currently used Smarty object + * @return object Smarty or PEAR_Error object + */ + function &getContainer() + { + isset($this->_smarty) or $this->init(); + return &$this->_smarty; + } + /** * Initialize the Smarty instance if it is not already existing * @@ -70,41 +74,23 @@ */ function init() { - if (is_null($this->_container)) { - $this->_container = new Smarty(); - $this->_container->template_dir = dirname($_SERVER['SCRIPT_FILENAME']); - $this->_container->compile_dir = dirname($_SERVER['SCRIPT_FILENAME']) . '/compile'; + if (!isset($this->_smarty)) { + $this->_smarty = new Smarty(); } } /** * Attach a Smarty instance - * (deprecated, use $renderer->setContainer() or $dg->fill() instead) - * + * + * @deprecated Use setContainer() instead * @param object Smarty instance * @access public */ function setSmarty(&$smarty) { - $this->_container = &$smarty; + return $this->setContainer($smarty); } - /** - * Set the Smarty template - * - * @param string Filename of the template - * @access public - */ - function setTemplate($tpl) - { - // FIXME: make $_tpl an option (?) - if (is_file($this->_container->template_dir . '/' . $tpl)) { - $this->_tpl = $tpl; - } else { - return new PEAR_Error('Error: Unable to find ' . - $this->_container->template_dir . '/' . $tpl); - } - } /** * Handles building the body of the table @@ -115,46 +101,39 @@ function buildBody() { if ($this->_tpl != '') { - $this->_container->assign('recordSet', $this->_records); - $this->_container->assign('columnSet', $this->_columns); - $this->_container->assign('recordLimit', $this->_pageLimit); - $this->_container->assign('currentPage', $this->_page); + $this->_smarty->assign('recordSet', $this->_records); + $this->_smarty->assign('columnSet', $this->_columns); + $this->_smarty->assign('recordLimit', $this->_pageLimit); + $this->_smarty->assign('currentPage', $this->_page); } } /** * Gets the Smarty object * - * OBSOLETE - * + * @deprecated Use getContainer() instead * @access public - * @return object Console_Table The Console_Table object for the DataGrid + * @return object Smarty container (reference) */ - function getSmarty() + function &getSmarty() { - return $this->_container; + return $this->getContainer(); } + /** - * Retrieve output from the container object - * - * @return mixed Output - * @access protected + * Discard the unsupported render() method + * + * This Smarty driver does not support the render() method. + * It is required to use the setContainer() (or + * Structures_DataGrid::fill()) method in order to do anything + * with this driver. + * */ - function flatten() + function render() { - $smarty = $this->getSmarty(); - - // FIXME: this error shouldn't occur - // FIXME: test $this->_tpl here (if it's not set, display() may result - // in an error [to be checked!]) - if (PEAR::isError($smarty)) { - return $smarty; - } else { - $smarty->display($this->_tpl); - } + return $this->_noSupport(__FUNCTION__); } - } ?> http://cvs.php.net/viewcvs.cgi/pear/Structures_DataGrid/DataGrid/Renderer/XUL.php?r1=1.18&r2=1.19&diff_format=u Index: pear/Structures_DataGrid/DataGrid/Renderer/XUL.php diff -u pear/Structures_DataGrid/DataGrid/Renderer/XUL.php:1.18 pear/Structures_DataGrid/DataGrid/Renderer/XUL.php:1.19 --- pear/Structures_DataGrid/DataGrid/Renderer/XUL.php:1.18 Thu Mar 2 00:42:16 2006 +++ pear/Structures_DataGrid/DataGrid/Renderer/XUL.php Thu Mar 16 01:05:52 2006 @@ -18,7 +18,7 @@ // | Mark Wiesemann <wiesemann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: XUL.php,v 1.18 2006/03/02 00:42:16 olivierg Exp $ +// $Id: XUL.php,v 1.19 2006/03/16 01:05:52 olivierg Exp $ require_once 'Structures/DataGrid/Renderer/Common.php'; //require_once 'XML/XUL.php'; @@ -31,16 +31,37 @@ * For additional information on the XUL Listbox, refer to this url: * http://www.xulplanet.com/references/elemref/ref_listbox.html * + * This driver does not support the setContainer() method, and can not + * be used in conjunction with the Structures_DataGrid::fill() method. + * + * The generated XUL is buffered so you can use the + * Structures_DataGrid::getOutput() method as an alternative to render() + * + * You have to setup your XUL document, just as you would with an HTML + * document. This driver will only generated the <listbox> element and + * content. + * + * Basic example : + * <code> + * <?php + * header('Content-type: application/vnd.mozilla.xul+xml'); + * echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; + * echo "<?xml-stylesheet href=\"myStyle.css\" type=\"text/css\"?>\n"; + * echo "<window title=\"MyDataGrid\" + * xmlns=\"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul\">\n"; + * // Instantiate your datagrid and setup its datasource, then call : + * $datagrid->setRenderer(DATAGRID_RENDER_XUL); + * $datagrid->render(); + * echo "</window>\n"; + * ?> + * </code> + * * Recognized options: * - * - title: (string) The title of the datagrid - * (default: 'DataGrid') - * - css: (array) An array of css URL's - * (default: 'chrome://global/skin/') - * - selfPath: (string) The complete path for sorting and paging links - * (default: $_SERVER['PHP_SELF']) + * - selfPath: (string) The complete path for sorting and paging links + * (default: $_SERVER['PHP_SELF']) * - * @version $Revision: 1.18 $ + * @version $Revision: 1.19 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @author Olivier Guilyardi <olivier@samalyse.com> * @author Mark Wiesemann <wiesemann@php.net> @@ -52,11 +73,11 @@ class Structures_DataGrid_Renderer_XUL extends Structures_DataGrid_Renderer_Common { /** - * Whether the container is user-provided or not - * @var bool + * The generated XUL data + * @var string * @access protected */ - var $_isCustomContainer; + var $_xul = ''; /** * Constructor @@ -70,8 +91,6 @@ parent::Structures_DataGrid_Renderer_Common(); $this->_addDefaultOptions( array( - 'title' => 'DataGrid', - 'css' => array('chrome://global/skin/'), 'selfPath' => $_SERVER['PHP_SELF'] ) ); @@ -89,52 +108,7 @@ */ function init() { - if (is_null($this->_container)) { - - $this->_isCustomContainer = false; - - // Define XML - $this->_container = XML_Util::getXMLDeclaration() . "\n"; - - // Define Stylesheets - foreach ($this->_options['css'] as $css) { - $this->_container .= "<?xml-stylesheet href=\"$css\" " . - "type=\"text/css\"?>\n"; - } - - // Define Window Element - $this->_container .= - "<window title=\"{$this->_options['title']}\" " . - "xmlns=\"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul\">\n"; - - // Define Listbox Element - $this->_container .= "<listbox rows=\"" . $this->_pageLimit . "\">\n"; - - } else { - $this->_isCustomContainer = true; - } - } - - /** - * Sets the datagrid title - * - * @access public - * @param string $title The title of the datagrid - */ - function setTitle($title) - { - $this->_options['title'] = $title; - } - - /** - * Adds a stylesheet to the list of stylesheets - * - * @access public - * @param string $url The url of the stylesheet - */ - function addStyleSheet($url) - { - array_push($this->_options['css'], $url); + $this->_xul = "<listbox rows=\"" . $this->_pageLimit . "\">\n"; } /** @@ -145,7 +119,7 @@ */ function buildHeader() { - $this->_container .= " <listhead>\n"; + $this->_xul .= " <listhead>\n"; for ($col = 0; $col < $this->_columnsNum; $col++) { $field = $this->_columns[$col]['field']; $label = $this->_columns[$col]['label']; @@ -176,9 +150,9 @@ } $onCommand = - "oncommand=\"location.href='{$this->_options['selfPath']}". - "?{$this->_requestPrefix}orderBy=$field". - "&{$this->_requestPrefix}direction=$dirArg';\""; + "oncommand=\"location.href='{$this->_options['selfPath']}?" + . $this->_buildSortingHttpQuery($field, $dirArg, true) + . "'\""; $sortDirection = "sortDirection=\"$dirCur\""; } else { $onCommand = ''; @@ -186,10 +160,10 @@ } $label = XML_Util::replaceEntities($label); - $this->_container .= ' <listheader label="' . $label . '" ' . + $this->_xul .= ' <listheader label="' . $label . '" ' . "$sortDirection $onCommand />\n"; } - $this->_container .= " </listhead>\n"; + $this->_xul .= " </listhead>\n"; } /** @@ -201,31 +175,28 @@ function buildBody() { for ($row = 0; $row < $this->_recordsNum; $row++) { - $this->_container .= " <listitem>\n"; + $this->_xul .= " <listitem>\n"; for ($col = 0; $col < $this->_columnsNum; $col++) { $value = $this->_records[$row][$col]; // FIXME: 'ä' is displayed as '?' ==> encoding is required! - $this->_container .= ' ' . + $this->_xul .= ' ' . XML_Util::createTag('listcell', array('label' => $value)) . "\n"; } - $this->_container .= " </listitem>\n"; + $this->_xul .= " </listitem>\n"; } } /** - * Close the XUL listbox and window, if needed + * Close the XUL listbox * * @access protected */ function finalize() { - if (!$this->_isCustomContainer) { - $this->_container .= "</listbox>\n"; - $this->_container .= "</window>\n"; - } + $this->_xul .= "</listbox>\n"; } /** @@ -247,18 +218,7 @@ */ function flatten() { - return $this->_container; - } - - /** - * Render to the standard output - * - * @access public - */ - function render() - { - header('Content-type: application/vnd.mozilla.xul+xml'); - parent::render(); + return $this->_xul; } }