cvs: pear /Structures_DataGrid/DataGrid Core.php DataSource.php Source.php /Structures_DataGrid/DataGrid/DataSource Array.php DataObject.php
/Structures_DataGrid/DataGrid/Docs/Examples example.php

From: Date: Wed, 15 Dec 2004 16:15:54 +0000
Subject: cvs: pear /Structures_DataGrid/DataGrid Core.php DataSource.php Source.php /Structures_DataGrid/DataGrid/DataSource Array.php DataObject.php
/Structures_DataGrid/DataGrid/Docs/Examples example.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-27448@lists.php.net to get a copy of this message
asnagy Wed Dec 15 11:15:54 2004 EDT Added files: /pear/Structures_DataGrid/DataGrid DataSource.php Removed files: /pear/Structures_DataGrid/DataGrid Source.php Modified files: /pear/Structures_DataGrid/DataGrid Core.php /pear/Structures_DataGrid/DataGrid/DataSource Array.php DataObject.php /pear/Structures_DataGrid/DataGrid/Docs/Examples example.php Log: Renamed Source.php to DataSource.php to follow naming conventions

http://cvs.php.net/diff.php/pear/Structures_DataGrid/DataGrid/Core.php?r1=1.22&r2=1.23&ty=u Index: pear/Structures_DataGrid/DataGrid/Core.php diff -u pear/Structures_DataGrid/DataGrid/Core.php:1.22 pear/Structures_DataGrid/DataGrid/Core.php:1.23 --- pear/Structures_DataGrid/DataGrid/Core.php:1.22 Fri Dec 10 18:31:55 2004 +++ pear/Structures_DataGrid/DataGrid/Core.php Wed Dec 15 11:15:53 2004 @@ -16,7 +16,7 @@ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: Core.php,v 1.22 2004/12/10 23:31:55 asnagy Exp $ +// $Id: Core.php,v 1.23 2004/12/15 16:15:53 asnagy Exp $ require_once 'Structures/DataGrid/Column.php'; require_once 'Structures/DataGrid/Record.php'; @@ -28,7 +28,7 @@ * It offers the paging and sorting methods as well as the record and column * management methods. * - * @version $Revision: 1.22 $ + * @version $Revision: 1.23 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid @@ -145,7 +145,7 @@ */ function bind($rs) { - require_once 'Structures/DataGrid/Source.php'; + require_once 'Structures/DataGrid/DataSource.php'; $source =& Structures_DataGrid_DataSource::create($rs); if (!PEAR::isError($source)) { http://cvs.php.net/diff.php/pear/Structures_DataGrid/DataGrid/DataSource/Array.php?r1=1.8&r2=1.9&ty=u Index: pear/Structures_DataGrid/DataGrid/DataSource/Array.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.8 pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.9 --- pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.8 Fri Dec 10 17:04:22 2004 +++ pear/Structures_DataGrid/DataGrid/DataSource/Array.php Wed Dec 15 11:15:54 2004 @@ -17,16 +17,16 @@ // | Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: Array.php,v 1.8 2004/12/10 22:04:22 asnagy Exp $ +// $Id: Array.php,v 1.9 2004/12/15 16:15:54 asnagy Exp $ -require_once 'Structures/DataGrid/Source.php'; +require_once 'Structures/DataGrid/DataSource.php'; /** * Array Data Source Driver * * This class is a data source driver for a 2D Array * - * @version $Revision: 1.8 $ + * @version $Revision: 1.9 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @author Andrew Nagy <asnagy@webitecture.org> * @access public http://cvs.php.net/diff.php/pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php?r1=1.11&r2=1.12&ty=u Index: pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.11 pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.12 --- pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.11 Mon Dec 13 13:06:43 2004 +++ pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php Wed Dec 15 11:15:54 2004 @@ -17,9 +17,9 @@ // | Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // -// $Id: DataObject.php,v 1.11 2004/12/13 18:06:43 asnagy Exp $ +// $Id: DataObject.php,v 1.12 2004/12/15 16:15:54 asnagy Exp $ -require_once 'Structures/DataGrid/Source.php'; +require_once 'Structures/DataGrid/DataSource.php'; /** * PEAR::DB_DataObject Data Source Driver @@ -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.11 $ + * @version $Revision: 1.12 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @author Andrew Nagy <asnagy@webitecture.org> * @access public @@ -141,7 +141,7 @@ */ function &fetch($offset=0, $len=null, $sortField=null, $sortDir='ASC') { - // Check to see if QUery has already been submitted + // Check to see if Query has already been submitted if ($this->_dataobject->_DB_resultid != '') { $this->_rowNum = $this->_dataobject->N; } else { http://cvs.php.net/diff.php/pear/Structures_DataGrid/DataGrid/Docs/Examples/example.php?r1=1.7&r2=1.8&ty=u Index: pear/Structures_DataGrid/DataGrid/Docs/Examples/example.php diff -u pear/Structures_DataGrid/DataGrid/Docs/Examples/example.php:1.7 pear/Structures_DataGrid/DataGrid/Docs/Examples/example.php:1.8 --- pear/Structures_DataGrid/DataGrid/Docs/Examples/example.php:1.7 Fri Oct 22 11:36:57 2004 +++ pear/Structures_DataGrid/DataGrid/Docs/Examples/example.php Wed Dec 15 11:15:54 2004 @@ -9,7 +9,6 @@ <?php require_once('Structures/DataGrid.php'); -require_once('Structures/DataGrid/Source.php'); class Printer { function printLink($params) @@ -91,6 +90,7 @@ /* Bind a datasource object Option #2 +require_once('Structures/DataGrid/DataSource.php'); $source = Structures_DataGrid_DataSource::create($rs); $dg->bindDataSource($source); */ http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/DataSource.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/DataSource.php +++ pear/Structures_DataGrid/DataGrid/DataSource.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Olivier Guilyardi <olivier@samalyse.com> | // | Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: DataSource.php,v 1.1 2004/12/15 16:15:53 asnagy Exp $ // Data Source Drivers define('DATAGRID_SOURCE_ARRAY', 'Array'); define('DATAGRID_SOURCE_DATAOBJECT','DataObject'); define('DATAGRID_SOURCE_DB', 'DB'); define('DATAGRID_SOURCE_XML', 'XML'); define('DATAGRID_SOURCE_RSS', 'RSS'); define('DATAGRID_SOURCE_CSV', 'CSV'); /** * Base abstract class for data source drivers * * <b>Recognized options (valid for all drivers) :</b> * * <b>"generate_columns" : </b> Generate Structures_DataGrid_Column objects * with labels. (default : true) * * <b>"labels" : </b> How to translate the field names to column labels. * Only used when "generate_columns" is true. Default : array(). * This is an associative array of the form : * <code> * array ("fieldName" => "fieldLabel", ...) * </code> * * <b>"fields" : </b> Which fields should be rendered (Only used when * "generate_columns" is true. The default is an empty array : all of * the DataObject's fields will be rendered. * This is an array of the form : * <code> * array ("fieldName1", "fieldName2", ...) * </code> * * Users may want to see the create() factory method * * Developers : * * <b>HOWTO develop a new source driver</b> * * Subclass this Structures_DataGrid_DataSource class : * <code> * class Structures_DataGrid_DataSource_Foo extends Structures_DataGrid_DataSource * </code> * * In the constructor, initialize default options . These defaults will be * used to validate user provided options, so you need to set all possible * ones * <code> * function Structures_DataGrid_DataSource_Foo() * { * parent::Structures_DataGrid_DataSource(); // required * $this->_addDefaultOptions(array( 'bar' => true)); * } * </code> * * Expose the fetch(), count() and bind() methods, overloading * the provided skeleton. See the corresponding prototypes * for more information on how to do this. * * Eventually, use the dump() debugging method to test your brand new * driver * * @author Olivier Guilyardi <olivier@samalyse.com> * @author Andrew Nagy <asnagy@webitecture.org> * @package Structures_DataGrid * @category Structures * @version $Revision $ */ class Structures_DataGrid_DataSource { /** * Common and driver-specific options * * @var array * @access protected * @see Structures_DataGrid_DataSource::_setOption() * @see Structures_DataGrid_DataSource::addDefaultOptions() */ var $_options = array(); /** * Constructor * */ function Structures_DataGrid_DataSource() { $this->_options = array('generate_columns' => null, 'labels' => array(), 'fields' => array()); } /** * Adds some default options. * * This method is meant to be called by drivers. It allows adding some * default options. Additionally to setting default values the options * names (keys) are used by setOptions() to validate its input. * * @access protected * @param array $options An associative array of the from: * array(optionName => optionValue, ...) * @return void * @see Structures_DataGrid_DataSource::_setOption */ function _addDefaultOptions($options) { $this->_options = array_merge($this->_options, $options); } /** * Driver Factory * * A clever method which loads and instantiate data source drivers. * * Can be called in various ways : * * Detect the source type and load the appropriate driver with default * options : * <code> * $driver =& Structures_DataGrid_DataSource::create($source); * </code> * * Detect the source type and load the appropriate driver with custom * options : * <code> * $driver =& Structures_DataGrid_DataSource::create($source, $options); * </code> * * Load a driver for an explicit type (faster, bypasses detection routine) : * <code> * $driver =& Structures_DataGrid_DataSource::create($source, $type, $options); * </code> * * @access public * @param mixed $source The data source respective to the driver * @param array $options An associative array of the form : * array(optionName => optionValue, ...) * @param string $type The data source type constant (of the form * DATAGRID_DATASOURCE_*) * @uses Structures_DataGrid_DataSource::_detectSourceType() * @return mixed Returns the source driver object or * PEAR_Error on failure * @static */ function &create($source, $options=array(), $type=null) { if (is_null($type) && !($type = Structures_DataGrid_DataSource::_detectSourceType($source))) { return new PEAR_Error('Unable to determine the data source type. '. 'You may want to explicitly specify it.'); } if (!@include_once "Structures/DataGrid/DataSource/$type.php") { return new PEAR_Error("No such data source driver: '$type'"); } $classname = "Structures_DataGrid_DataSource_$type"; $driver = new $classname(); $driver->bind($source, $options); return $driver; } /** * Set options * * @param mixed $options An associative array of the form : * array("option_name" => "option_value",...) * @access protected */ function setOptions($options) { $this->_options = array_merge($this->_options, $options); } /** * Generate columns if options are properly set * * Note : must be called after fetch() * * @access public * @return array Array of Column objects. Empty array if irrelevant. */ function getColumns() { $columns = array(); if ($this->_options['generate_columns'] and $fieldList = $this->_options['fields']) { include_once('Structures/DataGrid/Column.php'); foreach ($fieldList as $field) { $label = strtr($field, $this->_options['labels']); $col = new Structures_DataGrid_Column($label, $field, $field); $columns[] = $col; } } return $columns; } // Begin driver method prototypes DocBook template /**#@+ * * This method is public, but please note that it is not intended to be called by * user-space code. It is meant to be called by the main Structures_DataGrid * container. * * It is intended to be overloaded by drivers. */ /** * Fetching method prototype * * When overloaded, should either return a PEAR_Error or a <b>reference</b> * to an array of the form : * array("Columns" => $columns, "Records" => $records) * where $columns is an array of Structures_DataGrid_Column objects and * $records an assoc array of rows * * @param integer $offset Limit offset (starting from 0) * @param integer $len Limit length * @param string $sortField Field to sort by * @param string $sortDir Sort direction : 'ASC' or 'DESC' * @return object PEAR_Error An error with message * 'No data source driver loaded' * @access public */ function &fetch($offset=0, $len=null, $sortField=null, $sortDir='ASC') { $err = new PEAR_Error("No data source driver loaded"); return $err; } /** * Counting method prototype * * Note : must be called before fetch() * * When overloaded, should either return a numeric value indicating the * total number of rows in the data source, or a PEAR_Error object * * @return object PEAR_Error An error with message * 'No data source driver loaded' * @access public */ function count() { return PEAR_Error("No data source driver loaded"); } /** * Sorting method prototype * * Note : must be called before fetch() * * @return object PEAR_Error An error with message * 'No data source driver loaded' * @access public */ function sort() { return PEAR_Error("No data source driver loaded"); } /** * Datasource binding method prototype * * When overloaded, should either return true or a PEAR_Error object * * @return object PEAR_Error An error with message * 'No data source driver loaded' * @access public */ function bind() { return PEAR_Error("No data source driver loaded"); } /**#@-*/ // End DocBook template /** * Dump the data as returned by fetch(). * * This method is meant for debugging purposes. It returns what fetch() * would return to its DataGrid host as a nicely formatted console-style * table. * * @param integer $offset Limit offset (starting from 0) * @param integer $len Limit length * @param string $sortField Field to sort by * @param string $sortDir Sort direction : 'ASC' or 'DESC' * @return string The table string, ready to be printed * @uses Structures_DataGrid_DataSource::fetch() * @access public */ function dump($offset=0, $len=null, $sortField=null, $sortDir='ASC') { $records =& $this->fetch($offset, $len, $sortField, $sortDir); $columns = $this->getColumns(); if (!$columns and !$records) { return "<Empty set>\n"; } include_once 'Console/Table.php'; $table = new Console_Table(); $headers = array(); if ($columns) { foreach ($columns as $col) { $headers[] = is_null($col->fieldName) ? $col->columnName : "{$col->columnName} ({$col->fieldName})"; } } else { $headers = array_keys($records[0]); } $table->setHeaders($headers); foreach ($records as $rec) { $table->addRow($rec); } return $table->getTable(); } /** * Detect source type * * @param mixed $source Some kind of source * @return string The type constant of this source or null if * it couldn't be detected * @access private */ function _detectSourceType($source) { switch($source) { // DB_DataObject case (is_subclass_of($source, 'db_dataobject')): return DATAGRID_SOURCE_DATAOBJECT; break; // DB_Result case (strtolower(get_class($source)) == 'db_result'): return DATAGRID_SOURCE_DB; break; // Array case (is_array($source)): return DATAGRID_SOURCE_ARRAY; break; // RSS case (is_string($source) and stristr('<rdf:RDF', $source)): return DATAGRID_SOURCE_RSS; break; // XML case (is_string($source) and ereg('^ *<\?xml', $source)): return DATAGRID_SOURCE_XML; break; // CSV //case (is_string($source)): // return DATAGRID_SOURCE_CSV; // break; default: return null; break; } } } ?>
« previous php.pear.cvs (#27448) next »