cvs: pear /Structures_DataGrid DataGrid.php package.xml /Structures_DataGrid/DataGrid Column.php Core.php Record.php Renderer.php
/Structures_DataGrid/DataGrid/Record DB.php DataObject.php /Structures_DataGrid/DataGrid/Renderer HTMLTable.php Smarty.php XLS.php XML.php
| From: | Andrew Nagy | Date: | Wed, 17 Mar 2004 16:08:32 +0000 |
| Subject: | cvs: pear /Structures_DataGrid DataGrid.php package.xml /Structures_DataGrid/DataGrid Column.php Core.php Record.php Renderer.php /Structures_DataGrid/DataGrid/Record DB.php DataObject.php /Structures_DataGrid/DataGrid/Renderer HTMLTable.php Smarty.php XLS.php XML.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-17484@lists.php.net to get a copy of this message | ||
asnagy Wed Mar 17 11:08:32 2004 EDT
Added files:
/pear/Structures_DataGrid DataGrid.php package.xml
/pear/Structures_DataGrid/DataGrid Column.php Core.php Record.php
Renderer.php
/pear/Structures_DataGrid/DataGrid/Record DB.php DataObject.php
/pear/Structures_DataGrid/DataGrid/Renderer HTMLTable.php
Smarty.php XLS.php
XML.php
Log:
Initial Checkin to PHP CVS Tree
http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid.php +++ pear/Structures_DataGrid/DataGrid.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: DataGrid.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ require_once ('PEAR.php'); require_once ('DataGrid/Renderer.php'); define ('DATAGRID_RENDER_TABLE', 'HTMLTable'); define ('DATAGRID_RENDER_SMARTY', 'Smarty'); define ('DATAGRID_RENDER_XML', 'XML'); define ('DATAGRID_RENDER_XLS', 'XLS'); define ('DATAGRID_RECORD_DATAOBJECT', 'DataObject'); define ('DATAGRID_RECORD_DBRESULT', 'DBResult'); define ('DATAGRID_SORT_ASC', 'ASC'); define ('DATAGRID_SORT_DESC', 'DESC'); /** * Structures_DataGrid Class * * A PHP class to implement the functionality provided by the .NET Framework's * DataGrid control. This class can produce a data driven list in many formats * based on a defined record set. Commonly, this is used for outputting an HTML * table based on a record set from a database or an XML document. It allows * for the output to be published in many ways, including an HTML table, * an HTML Template, an Excel spreadsheet, an XML document. The data can * be sorted and paged, each cell can have custom output, and the table can be * custom designed with alternating color rows. * * Quick Example: * <?php * require('DataGrid.php'); * $dg = new Structures_DataGrid(); * $result = mysql_query('SELECT * FROM users'); * while ($rs = mysql_fetch_assoc($result)) { * $dataSet[] = $rs; * } * $dg->bind($dataSet); * $dg->render(); * ?> * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid extends Structures_DataGrid_Renderer { /** * Constructor * * Acts somewhat as a factory and instantiates the Renderer and the Core * * @param string $limit The row limit per page. * @param string $page The current page viewed. * @param string $renderer The renderer to use. * @return void * @access public */ function Structures_DataGrid($limit = null, $page = 1, $renderer = DATAGRID_RENDER_TABLE) { Structures_DataGrid_Renderer::Structures_DataGrid_Renderer($renderer); Structures_DataGrid_Core::Structures_DataGrid_Core($limit, $page); } /** * Method used for debuging purposes only. Displays a dump of the DataGrid * object. * * @access public * @return void */ function dump() { echo '<pre>'; print_r($this); echo '</pre>'; } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/package.xml?r=1.1&p=1 Index: pear/Structures_DataGrid/package.xml +++ pear/Structures_DataGrid/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> <package version="1.0"> <name>Structures_DataGrid</name> <license>PHP License</license> <summary> A package to create a grid like structure based on a record set of data that will output in many formats including an HTML Table. </summary> <description> This package offers a toolkit to render out a datagrid in HTML format as well as many other formats such as an XML Document, an Excel Spreadsheet, a Smarty Template and more. It also offers paging and sorting functionallity to limit the data that is presented. This concept is based on the .NET Framework DataGrid </description> <maintainers> <maintainer> <user>asnagy</user> <name>Andrew S. Nagy</name> <email>asnagy@webitecture.org</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.4.1</version> <date>2004-03-16</date> <state>alpha</state> <notes> Implemented name change from "DataGrid" to "Structures_DataGrid" Removed html link elements from column class as to not be hard coded for html output </notes> <filelist> <dir name="/" baseinstalldir=""> <file role="php">DataGrid.php</file> <dir name="DataGrid"> <file role="php">Core.php</file> <file role="php">Column.php</file> <file role="php">Record.php</file> <file role="php">Renderer.php</file> <dir name="Renderer"> <file role="php">HTMLTable.php</file> <file role="php">Smarty.php</file> <file role="php">XML.php</file> <file role="php">XLS.php</file> </dir> <dir name="Record"> <file role="php">DB.php</file> <file role="php">DataObject.php</file> </dir> <dir name="Docs"> <dir name="Examples"> <file role="php">example.php</file> <file role="php">example-smarty.php</file> <file role="php">example.tpl</file> <file role="php">example-xml.php</file> <file role="php">example-xls.php</file> </dir> </dir> </dir> </dir> </filelist> </release> <deps> <dep type="pkg" rel="ge" version="1.2" optional="No">PEAR</dep> <dep type="pkg" rel="ge" version="1.5" optional="Yes">HTML_Table</dep> <dep type="pkg" rel="ge" version="0.6" optional="Yes">Spreadsheet_Excel_Writer</dep> <dep type="pkg" rel="ge" version="0.5.2" optional="Yes">XML_Util</dep> </deps> <changelog> <release> <version>0.4.1</version> <date>2004-03-16</date> <state>alpha</state> <notes> Implemented name change from "DataGrid" to "Structures_DataGrid". Also removed the link components from the DataGrid_Column. </notes> </release> <release> <version>0.4</version> <date>2004-03-10</date> <state>alpha</state> <notes> This is the first public release. </notes> </release> </changelog> </package> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Column.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Column.php +++ pear/Structures_DataGrid/DataGrid/Column.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Column.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ /** * Structures_DataGrid_Column Class * * This class represents a single column for the DataGrid. * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Column { /** * The name of the column * @var array */ var $columnName; /** * The name of the field to map to * @var array */ var $fieldName; /** * The field name to order by. Optional * @var array */ var $orderBy; /** * The attributes of the td tag. Optional * @var array */ var $attribs; /** * The URL for the link. Optional * @var array var $link; * The field to be used as the id for the link * @var array var $linkField; */ /** * The value to be used if a cell contains a null value * @var array */ var $autoFill; /** * A function to be called for each cell to modify the output * @var array */ var $formatter; /** * Constructor * * Creates default table style settings * * @access public */ // function DataGrid_Column($columnName, $fieldName, $orderBy = null, // $attribs = array(), $link = null, // $linkField = null, $autoFill = null, // $formatter = null) function Structures_DataGrid_Column($columnName, $fieldName, $orderBy = null, $attribs = array(), $autoFill = null, $formatter = null) { $this->columnName = $columnName; $this->fieldName = $fieldName; $this->orderBy = $orderBy; $this->attribs = $attribs; //$this->link = $link; //$this->linkField = $linkField; $this->autoFill = $autoFill; $this->formatter = $formatter; } /** * Set Auto Fill * * Defines a value to be printed if a cell in the column is null. * * @access public */ function setAutoFill($str) { $this->autoFill = $str; } /** * Set Formatter * * Defines the function and paramters to be called by the formatter method. * * @access public */ function setFormatter($str) { $this->formatter = $str; } /** * Formatter * * Calls a predefined function to develop custom output for the column. It * can accepts paramaters so that each cell in the column can be unique * based on the record. * * @access public * @todo add the record array to the formatter automatically */ function formatter($record) { // Define any parameters if ($size = strpos($this->formatter, '(')) { // Retrieve the name of the function $formatter = substr($this->formatter, 0, $size); // Build the list of parameters $length = strlen($this->formatter) - $size - 2; $parameters = substr($this->formatter, $size + 1, $length); $parameters = split(',', $parameters); // Process the parameters $params = array(); $param['record'] = $record; // Automatically pass the record array in foreach($parameters as $param) { $param = str_replace('$', '', $param); if (strpos($param, '=') != false) { $vars = split('=', $param); $params[$vars[0]] = $vars[1]; } else { $params[$param] = $result; } } } else { $formatter = $this->formatter; $params = null; } // Call the formatter if (is_callable($formatter)) { $result = call_user_func_array($formatter, $params); } else { $result = new PEAR_Error('Unable to process formatter'); } return $result; } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Core.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Core.php +++ pear/Structures_DataGrid/DataGrid/Core.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Core.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ require_once ('DataGrid/Column.php'); require_once ('DataGrid/Record.php'); /** * Structures_DataGrid_Core Class * * The Core class implements the Core functionality of the DataGrid. * It offers the paging and sorting methods as well as the record and column * management methods. * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Core { /** * Array of columns. Columns are defined as a DataGridColumn object. * @var array */ var $columnSet = array(); /** * Array of records. Records are defined as a DataGridRecord object. * @var array */ var $recordSet = array(); /** * An array of fields to sort by. Each field is an array of the field name * and the direction, either ASC or DESC. * @var array */ var $sortArray; /** * Limit of records to show per page. * @var string */ var $rowlimit; /** * The current page to show. * @var string */ var $page; /** * The array of available pages. * @var array */ var $pageList = array(); /** * Constructor * * Creates default table style settings * * @param string $limit The row limit per page. * @param string $page The current page viewed. * @access public */ function Structures_DataGrid_Core($limit = null, $page = 1) { $this->rowLimit = $limit; $this->page = $page; } /** * Retrieves the current page number when paging is implemented * * @return string the current page number * @access public */ function getCurrentPage() { return $this->page; } /** * Define the current page number. This is used when paging is implemented * * @access public * @param string $page The current page number. */ function setCurrentPage($page) { $this->page = $page; } /** * Adds a DataGridColumn object to this DataGrid object * * @access public * @param object $column The column object to add. This object should be * a HTML_DataGridColumn object. * @return bool True if successful, otherwise false. */ function addColumn($column) { if (get_class($column) == 'structures_datagrid_column') { $this->columnSet = array_merge($this->columnSet, array($column)); return true; } else { return false; } } /** * A simple way to add an associative array record set to the data grid. * * @access public * @param array $rs The associative array recordset * @return bool True if successful, otherwise false. */ function bind($rs) { if (is_array($rs)) { $this->recordSet = $rs; return true; } else { return new PEAR_Error('Recordset must be an associative array'); } } /** * Adds a DataGrid_Record object to this DataGrid object * * @access public * @param object $record The record object to add. This object must be * a HTML_DataGridRecord object. * @return bool True if successful, otherwise false. */ function addRecord($record) { if ((get_class($record) == 'structures_datagrid_record') || (get_parent_class($record) == 'structures_datagrid_record')) { $this->recordSet = array_merge($this->recordSet, array($record->getRecord())); return true; } else { return new PEAR_Error('Not a valid DataGrid Record'); } } /** * Drops a DataGridRecord object from this DataGrid object * * @access public * @param object DataGrid_Record $record The record object to drop. * This object must be a * Structures_DataGridRecord * object. * @return void */ function dropRecord($record) { unset($this->recordSet[$record->getRecord()]); } /** * Sorts the records by the defined field. * Do not use this method if data is coming from a database as sorting * is much faster coming directly from the database itself. * * @access public * @param string $sortBy The field to sort the record set by. * @param string $direction The sort direction, either ASC or DESC. * @return void */ function sortRecordSet($sortBy, $direction = 'ASC') { // Define sorting values $this->sortArray = array($sortBy, $direction); // Sort the recordset usort($this->recordSet, array($this, '_sort')); } function _sort($a, $b, $i = 0) { $bool = strnatcmp($a[$this->sortArray[0]], $b[$this->sortArray[0]]); if ($this->sortArray[1] == DATAGRID_SORT_DESC) { $bool = $bool * -1; } return $bool; } /** * Handles building the paging of the DataGrid * * @access private * @return void */ function buildPaging() { if (($this->rowLimit > 0) && (count($this->recordSet) > $this->rowLimit)) { $pageCount = ceil(count(($this->recordSet))/($this->rowLimit)); for ($i = 1; $i <= $pageCount; $i++) { if ($this->page == $i) { $this->pageList[$i] = null; } else { $this->pageList[$i] = "page=$i"; } } } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Record.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Record.php +++ pear/Structures_DataGrid/DataGrid/Record.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Record.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ /** * Structures_DataGrid_Record Class * * This class represents one record for the DataGrid. All data is stored * as an Associative Array with the keys as the Column Name and the value * as the cell value. Other data types can be added by the extended classes, * such as a DB_Result or a DB_DataObject. * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Record { var $_record = array(); /** * Constructor * * Builds the record. Accepts data as either an Array or a DB_DataObject. * * @access public * @todo Allow more data types. */ function Structures_DataGrid_Record($data = null) { if ($data != null) { $this->setRecord($data); } } /** * Get Value * * Retrieves the value for the column name specified * * @access public * @param string $key The name of the column to retrieve the value for. * @return void */ function getValue($key) { return $this->_record[$key]; } /** * Set Value * * Sets the value for the column name specified * * @access public * @param string $key The name of the column to work on. * @param string $data The data to set the specific cell to. * @return void */ function setValue($key, $data) { $this->_record[$key] = $data; } /** * Get Record * * Retrieves the current record as an array. * * @access public * @return array The current record as an array. */ function getRecord() { return $this->_record; } /** * Set Record * * Sets the current record. The record must be defined as an array. * * @access public * @return mixed Returns true if successful, otherwise a * PEAR_Error object is returned. */ function setRecord($data) { if (is_array($data)) { $this->_record = $data; return true; } else { return new PEAR_Error('Invalid data type. Must be an array'); } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer.php +++ pear/Structures_DataGrid/DataGrid/Renderer.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Renderer.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ require_once ('DataGrid/Core.php'); /** * Structures_DataGrid_Renderer Class * * This class handles mangeing the output for the DataGrid. * By default, the output is handled by the HTML Table renderer. * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer extends Structures_DataGrid_Core { var $renderer; /** * Constructor * * Determins the appropriate renderer to use. Uses the HTML_Table renderer * as the default. * * @param string $renderer The renderer to use. * @access public */ function Structures_DataGrid_Renderer($renderer = DATAGRID_RENDER_TABLE) { if (PEAR::isError($this->setRenderer($renderer))) { $this->setRenderer(DATAGRID_RENDER_TABLE); } } /** * Render * * This calls the spcified renderer's render method. * * @param string $limit The row limit per page. * @param string $page The current page viewed. * @access public */ function render() { // Check to see if column headers exist, if not create them $this->_setDefaultHeaders(); // Render out the data $this->renderer->render(&$this); } function &getRenderer() { return $this->renderer; } function setRenderer($renderer) { $class = 'Structures_DataGrid_Renderer_' . $renderer; $file = 'DataGrid/Renderer/' . $renderer . '.php'; if (@include_once($file)) { $this->renderer = new $class(); } else { return new PEAR_Error('Invalid renderer'); } return true; } /** * Set Default Headers * * This method handles determining if column headers need to be set. * * @access private */ function _setDefaultHeaders() { if ((!count($this->columnSet)) && (count($this->recordSet))) { $arrayKeys = array_keys($this->recordSet[0]); foreach ($arrayKeys as $key) { $width = ceil(100/count($arrayKeys)); $column = new Structures_DataGrid_Column($key, $key, $key, array('width' => $width.'%')); $this->addColumn($column); } } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Record/DB.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Record/DB.php +++ pear/Structures_DataGrid/DataGrid/Record/DB.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: DB.php,v 1.1 2004/03/17 16:08:31 asnagy Exp $ require_once('DataGrid/Record.php'); /** * Structures_DataGrid_Record_DB Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Record_DB extends Structures_DataGrid_Record { /** * Constructor * * Builds the record. Accepts data as an DB_Result object. * * @access public * @todo Allow more data types. */ function Structures_DataGrid_Record_DB($data = null) { $this->setRecord($data); } function setRecord($data) { if (get_class($data) == 'db_result') { parent::setRecord($data->fetchRow(DB_FETCHMODE_ASSOC)); } else { return new PEAR_Error('Invalid data type. Data must be a DB_Result record'); } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Record/DataObject.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Record/DataObject.php +++ pear/Structures_DataGrid/DataGrid/Record/DataObject.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: DataObject.php,v 1.1 2004/03/17 16:08:31 asnagy Exp $ require_once('DataGrid/Record.php'); /** * Structures_DataGrid_Record_DataObject Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Record_DataObject extends Structures_DataGrid_Record { /** * Constructor * * Builds the record if sepcified. Accepts data as either an Array or a DB_DataObject. * * @access public * @todo Allow more data types. */ function Structures_DataGrid_Record_DataObject($data = null) { $this->setRecord($data); } function setRecord($data) { if (get_parent_class($data) == 'db_dataobject') { parent::setRecord($data->toArray()); } else { return new PEAR_Error('Invalid data type. Data must be a DB_DataObject record'); } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php +++ pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: HTMLTable.php,v 1.1 2004/03/17 16:08:32 asnagy Exp $ require_once ('HTML/Table.php'); /** * Structures_DataGrid_Renderer_HTMLTable Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer_HTMLTable { /** * The table header background color * @var string */ var $headerBgColor; /** * The dark row background color * @var string */ var $rowDarkBgColor; /** * The light row background color * @var string */ var $rowLightBgColor; /** * An associative array containing each attribute of the table * @var array */ var $attrs; /** * A boolean value to determine if empty rows should be printed. * @var array */ var $allowEmptyRows; /** * An associative array containing the attributes for empty rows * @var array */ var $emptyRowAttributes = array(); /** * The structures_datagrid object * @var object Structures_DataGrid */ var $_dg; /** * The html_table object * @var object HTML_Table */ var $_table; /** * Constructor * * Build default values * * @access public */ function Structures_DataGrid_Renderer_HTMLTable() { $this->headerBgColor = '#FFFFFF'; $this->rowDarkBgColor = '#FFFFFF'; $this->rowLightBgColor = '#FFFFFF'; $this->attrs = array('cellpadding' => 4, 'cellspacing' => 0, 'border' => 0); $this->_table = new HTML_Table($this->attrs); } /** * Set a table attribute * * @access public * @param string $name The CSS class to use for the table. */ function setTableAttribute($attr, $value) { $this->attrs[$attr] = $value; $this->_table->_attributes = $this->attrs; } /** * Define the table's header bgcolor * * @access public * @param string $bgColor The header bgcolor to use for the table. */ function setTableHeaderBgColor($bgColor) { $this->headerBgColor = $bgColor; } /** * Define the table's row dark color * * @access public * @param string $bgColor The color to use for the dark table row. */ function setTableRowDarkBgColor($bgColor) { $this->rowDarkBgColor = $bgColor; } /** * Define the table's row light color * * @access public * @param string $bgColor The color to use for the light table row. */ function setTableRowLightBgColor($bgColor) { $this->rowLightBgColor = $bgColor; } /** * Define the table's autofill value. This value appears only in an empty table cell. * * @access public * @param string $value The value to use for empty cells. */ function setAutoFill($value) { $this->table->setAutoFill($value); } /** * Generates the HTML for the DataGrid * * @access public * @return void */ function render(&$dg) { $this->_dg = &$dg; // Define Table Header $this->_buildHTMLTableHeader(); // Build Table Data $this->_buildHTMLTableBody(); // Define Alternating Row attributes $lightRow = array('bgcolor' => $this->rowLightBgColor); $darkRow = array('bgcolor' => $this->rowDarkBgColor); $this->_table->altRowAttributes(1, $lightRow, $darkRow); // Print the table echo $this->_table->toHTML(); } /** * Handles building the header of the DataGrid * * @access private * @return void */ function _buildHTMLTableHeader() { $cnt = 0; foreach ($this->_dg->columnSet as $column) { //Define Content if ($column->orderBy != null) { if (stristr($_SERVER['PHP_SELF'], '?')) { $url = $_SERVER['PHP_SELF'] . '&orderBy='; } else { $url = $_SERVER['PHP_SELF'] . '?orderBy='; } if ($this->_dg->sortArray[1] == 'ASC') { $direction = '&direction=DESC'; } else { $direction = '&direction=ASC'; } $str = '<a href="' . $url . $column->orderBy . $direction . '"><b>' . $column->columnName . '</b></a>'; } else { $str = '<b>' . $column->columnName . '</b>'; } // Print Content to HTML_Table $this->_table->setHeaderContents(0, $cnt, $str); $this->_table->setCellAttributes(0, $cnt, $column->attribs); $cnt++; } // Define Table Header attributes $attr = array('bgcolor' => $this->headerBgColor); $this->_table->setRowAttributes(0, $attr); } /** * Handles building the body of the DataGrid * * @access private * @return void */ function _buildHTMLTableBody() { if (count($this->_dg->recordSet)) { // Determine looping values if ($this->_dg->page > 1) { $begin = ($this->_dg->page - 1) * $this->_dg->rowLimit; $limit = $this->_dg->page * $this->_dg->rowLimit; } else { $begin = 0; if ($this->_dg->rowLimit == null) { $limit = count($this->_dg->recordSet); } else { $limit = $this->_dg->rowLimit; } } // Begin loop for ($i = $begin; $i < $limit; $i++) { $row = $this->_dg->recordSet[$i]; if ($row != null) { // Print Row $cnt = 0; foreach ($this->_dg->columnSet as $column) { $rowCnt = ($i-$begin)+1; // Build Content if ($column->formatter != null) { $content = $column->formatter($row); } elseif ($column->fieldName == null) { if ($column->autoFill != null) { $content = $column->autoFill; } else { $content = $column->columnName; } } else { $content = $row[$column->fieldName]; } // Print Content to HTML_Table $this->_table->setCellContents($rowCnt, $cnt, $content); $this->_table->setCellAttributes($rowCnt, $cnt, $column->attribs); $cnt++; } } else { // Determine if empty row should be printed if ($this->allowEmptyRows) { $rowCnt++; $this->_table->setRowAttributes($rowCnt, $this->emptyRowAttributes, false); $this->_table->setCellContents($rowCnt, 0, ' '); } } } } } /** * Handles the printing of the page list for the DataGrid in HTML. * * @access public * @param string $seperator The string to use to seperate each page link * @return void * @todo Investigate HTML::Pager */ function printPaging($seperator = '|') { // Generate Paging $this->_dg->buildPaging(); // Create base url for link if (isset($_SERVER['QUERY_STRING'])) { $url = $_SERVER['PHP_SELF'] . '?'; $queryString = explode('&', $_SERVER['QUERY_STRING']); foreach ($queryString as $value) { if (!strstr($value, 'page') && ($value != '')) { $url .= $value . '&'; } } $url .= 'page='; } else { $url = $_SERVER['PHP_SELF'] . '?page='; } // Print back link if ($this->_dg->page > 1) { echo '<a href="' . $url . ($this->_dg->page - 1) . '"><<</a> '; } $cnt = 0; foreach ($this->_dg->pageList as $page => $link) { if ($link != '') { echo '<a href="' . $url . $page . "\"> $page </a>"; } else { echo "<b> $page </b>"; } if ($cnt+1 < count($this->_dg->pageList)) { echo " $seperator "; } $cnt++; } if ($this->_dg->page < count($this->_dg->pageList)) { echo ' <a href="' . $url . ($this->_dg->page + 1) . '">>></a>'; } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php +++ pear/Structures_DataGrid/DataGrid/Renderer/Smarty.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Smarty.php,v 1.1 2004/03/17 16:08:32 asnagy Exp $ require_once ('Smarty/Smarty.class.php'); /** * Structures_DataGrid_Renderer_Smarty Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer_Smarty { var $_dg; var $_smarty; var $_tpl; /** * Constructor * * Build default values * * @access public */ function Structures_DataGrid_Renderer_Smarty() { $this->_smarty = new Smarty(); $this->_smarty->template_dir = dirname($_SERVER['SCRIPT_FILENAME']); $this->_smarty->compile_dir = dirname($_SERVER['SCRIPT_FILENAME']) . '/compile'; } function setSmarty(&$smarty) { $this->_smarty = &$smarty; } function setTemplate($tpl) { if (is_file($this->_smarty->template_dir . '/' . $tpl)) { $this->_tpl = $tpl; } else { return new PEAR_Error('Error: Unable to find ' . $this->smarty->template_dir . '/' . $tpl); } } function render(&$dg) { $this->_dg = &$dg; if ($this->_tpl != '') { $this->_smarty->assign('recordSet', $this->_dg->recordSet); $this->_smarty->assign('columnSet', $this->_dg->columnSet); $this->_smarty->assign('recordLimit', $this->_dg->rowLimit); $this->_smarty->assign('pageList', $this->_dg->pageList); $this->_smarty->assign('currentPage', $this->_dg->page); $this->_smarty->display($this->_tpl); } else { return new PEAR_Error('Error: No template defined'); } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer/XLS.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer/XLS.php +++ pear/Structures_DataGrid/DataGrid/Renderer/XLS.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: XLS.php,v 1.1 2004/03/17 16:08:32 asnagy Exp $ require_once 'Spreadsheet/Excel/Writer.php'; /** * Structures_DataGrid_Renderer_XLS Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer_XLS { var $_dg; var $_workbook; var $_worksheet; var $_filename = 'spreadsheet.xls'; /** * Constructor * * Build default values * * @access public */ function Structures_DataGrid_Renderer_XLS() { $this->_workbook = new Spreadsheet_Excel_Writer(); $this->_workbook->send($this->_filename); $this->_worksheet =& $this->_workbook->addWorksheet(); } function render(&$dg) { $this->_dg = &$dg; $this->_buildHeader(); $this->_buildBody(); $this->_workbook->close(); } /** * Handles building the header of the DataGrid * * @access private * @return void */ function _buildHeader() { $cnt = 0; foreach ($this->_dg->columnSet as $column) { //Define Content $str = $column->columnName; $this->_worksheet->write(0, $cnt, $str); $cnt++; } } /** * Handles building the body of the DataGrid * * @access private * @return void */ function _buildBody() { if (count($this->_dg->recordSet)) { // Determine looping values if ($this->_dg->page > 1) { $begin = ($this->_dg->page - 1) * $this->_dg->rowLimit; $limit = $this->_dg->page * $this->_dg->rowLimit; } else { $begin = 0; if ($this->_dg->rowLimit == null) { $limit = count($this->_dg->recordSet); } else { $limit = $this->_dg->rowLimit; } } for ($i = $begin; $i < $limit; $i++) { $row = $this->_dg->recordSet[$i]; $cnt = 0; foreach ($this->_dg->columnSet as $column) { $rowCnt = ($i-$begin) + 1; // Build Content if ($column->formatter != null) { $content = $column->formatter($row); } elseif ($column->fieldName == null) { if ($column->autoFill != null) { $content = $column->autoFill; } else { $content = $column->columnName; } } else { $content = $row[$column->fieldName]; } $this->_worksheet->write($rowCnt, $cnt, $str); $cnt++; } } } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer/XML.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer/XML.php +++ pear/Structures_DataGrid/DataGrid/Renderer/XML.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: XML.php,v 1.1 2004/03/17 16:08:32 asnagy Exp $ require ('XML/Util.php'); /** * Structures_DataGrid_Renderer_XML Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer_XML { var $_dg; /** * Constructor * * Build default values * * @access public */ function Structures_DataGrid_Renderer_XML() { } function render(&$dg) { $this->_dg = &$dg; header('Content-type: text/xml'); echo XML_Util::getXMLDeclaration() . "\n"; echo "<DataGrid>\n"; foreach ($this->_dg->recordSet as $row) { echo " <Row>\n"; foreach ($this->_dg->columnSet as $column) { // Build Content if ($column->formatter != null) { $content = $column->formatter($row); } elseif ($column->fieldName == null) { if ($column->autoFill != null) { $content = $column->autoFill; } else { $content = $column->columnName; } } else { $content = $row[$column->fieldName]; } echo ' ' . XML_Util::createTag($column->columnName, null, $content) . "\n"; } echo " </Row>\n"; } echo "</DataGrid>\n"; } } ?>
http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid.php +++ pear/Structures_DataGrid/DataGrid.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: DataGrid.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ require_once ('PEAR.php'); require_once ('DataGrid/Renderer.php'); define ('DATAGRID_RENDER_TABLE', 'HTMLTable'); define ('DATAGRID_RENDER_SMARTY', 'Smarty'); define ('DATAGRID_RENDER_XML', 'XML'); define ('DATAGRID_RENDER_XLS', 'XLS'); define ('DATAGRID_RECORD_DATAOBJECT', 'DataObject'); define ('DATAGRID_RECORD_DBRESULT', 'DBResult'); define ('DATAGRID_SORT_ASC', 'ASC'); define ('DATAGRID_SORT_DESC', 'DESC'); /** * Structures_DataGrid Class * * A PHP class to implement the functionality provided by the .NET Framework's * DataGrid control. This class can produce a data driven list in many formats * based on a defined record set. Commonly, this is used for outputting an HTML * table based on a record set from a database or an XML document. It allows * for the output to be published in many ways, including an HTML table, * an HTML Template, an Excel spreadsheet, an XML document. The data can * be sorted and paged, each cell can have custom output, and the table can be * custom designed with alternating color rows. * * Quick Example: * <?php * require('DataGrid.php'); * $dg = new Structures_DataGrid(); * $result = mysql_query('SELECT * FROM users'); * while ($rs = mysql_fetch_assoc($result)) { * $dataSet[] = $rs; * } * $dg->bind($dataSet); * $dg->render(); * ?> * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid extends Structures_DataGrid_Renderer { /** * Constructor * * Acts somewhat as a factory and instantiates the Renderer and the Core * * @param string $limit The row limit per page. * @param string $page The current page viewed. * @param string $renderer The renderer to use. * @return void * @access public */ function Structures_DataGrid($limit = null, $page = 1, $renderer = DATAGRID_RENDER_TABLE) { Structures_DataGrid_Renderer::Structures_DataGrid_Renderer($renderer); Structures_DataGrid_Core::Structures_DataGrid_Core($limit, $page); } /** * Method used for debuging purposes only. Displays a dump of the DataGrid * object. * * @access public * @return void */ function dump() { echo '<pre>'; print_r($this); echo '</pre>'; } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/package.xml?r=1.1&p=1 Index: pear/Structures_DataGrid/package.xml +++ pear/Structures_DataGrid/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0"> <package version="1.0"> <name>Structures_DataGrid</name> <license>PHP License</license> <summary> A package to create a grid like structure based on a record set of data that will output in many formats including an HTML Table. </summary> <description> This package offers a toolkit to render out a datagrid in HTML format as well as many other formats such as an XML Document, an Excel Spreadsheet, a Smarty Template and more. It also offers paging and sorting functionallity to limit the data that is presented. This concept is based on the .NET Framework DataGrid </description> <maintainers> <maintainer> <user>asnagy</user> <name>Andrew S. Nagy</name> <email>asnagy@webitecture.org</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.4.1</version> <date>2004-03-16</date> <state>alpha</state> <notes> Implemented name change from "DataGrid" to "Structures_DataGrid" Removed html link elements from column class as to not be hard coded for html output </notes> <filelist> <dir name="/" baseinstalldir=""> <file role="php">DataGrid.php</file> <dir name="DataGrid"> <file role="php">Core.php</file> <file role="php">Column.php</file> <file role="php">Record.php</file> <file role="php">Renderer.php</file> <dir name="Renderer"> <file role="php">HTMLTable.php</file> <file role="php">Smarty.php</file> <file role="php">XML.php</file> <file role="php">XLS.php</file> </dir> <dir name="Record"> <file role="php">DB.php</file> <file role="php">DataObject.php</file> </dir> <dir name="Docs"> <dir name="Examples"> <file role="php">example.php</file> <file role="php">example-smarty.php</file> <file role="php">example.tpl</file> <file role="php">example-xml.php</file> <file role="php">example-xls.php</file> </dir> </dir> </dir> </dir> </filelist> </release> <deps> <dep type="pkg" rel="ge" version="1.2" optional="No">PEAR</dep> <dep type="pkg" rel="ge" version="1.5" optional="Yes">HTML_Table</dep> <dep type="pkg" rel="ge" version="0.6" optional="Yes">Spreadsheet_Excel_Writer</dep> <dep type="pkg" rel="ge" version="0.5.2" optional="Yes">XML_Util</dep> </deps> <changelog> <release> <version>0.4.1</version> <date>2004-03-16</date> <state>alpha</state> <notes> Implemented name change from "DataGrid" to "Structures_DataGrid". Also removed the link components from the DataGrid_Column. </notes> </release> <release> <version>0.4</version> <date>2004-03-10</date> <state>alpha</state> <notes> This is the first public release. </notes> </release> </changelog> </package> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Column.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Column.php +++ pear/Structures_DataGrid/DataGrid/Column.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Column.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ /** * Structures_DataGrid_Column Class * * This class represents a single column for the DataGrid. * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Column { /** * The name of the column * @var array */ var $columnName; /** * The name of the field to map to * @var array */ var $fieldName; /** * The field name to order by. Optional * @var array */ var $orderBy; /** * The attributes of the td tag. Optional * @var array */ var $attribs; /** * The URL for the link. Optional * @var array var $link; * The field to be used as the id for the link * @var array var $linkField; */ /** * The value to be used if a cell contains a null value * @var array */ var $autoFill; /** * A function to be called for each cell to modify the output * @var array */ var $formatter; /** * Constructor * * Creates default table style settings * * @access public */ // function DataGrid_Column($columnName, $fieldName, $orderBy = null, // $attribs = array(), $link = null, // $linkField = null, $autoFill = null, // $formatter = null) function Structures_DataGrid_Column($columnName, $fieldName, $orderBy = null, $attribs = array(), $autoFill = null, $formatter = null) { $this->columnName = $columnName; $this->fieldName = $fieldName; $this->orderBy = $orderBy; $this->attribs = $attribs; //$this->link = $link; //$this->linkField = $linkField; $this->autoFill = $autoFill; $this->formatter = $formatter; } /** * Set Auto Fill * * Defines a value to be printed if a cell in the column is null. * * @access public */ function setAutoFill($str) { $this->autoFill = $str; } /** * Set Formatter * * Defines the function and paramters to be called by the formatter method. * * @access public */ function setFormatter($str) { $this->formatter = $str; } /** * Formatter * * Calls a predefined function to develop custom output for the column. It * can accepts paramaters so that each cell in the column can be unique * based on the record. * * @access public * @todo add the record array to the formatter automatically */ function formatter($record) { // Define any parameters if ($size = strpos($this->formatter, '(')) { // Retrieve the name of the function $formatter = substr($this->formatter, 0, $size); // Build the list of parameters $length = strlen($this->formatter) - $size - 2; $parameters = substr($this->formatter, $size + 1, $length); $parameters = split(',', $parameters); // Process the parameters $params = array(); $param['record'] = $record; // Automatically pass the record array in foreach($parameters as $param) { $param = str_replace('$', '', $param); if (strpos($param, '=') != false) { $vars = split('=', $param); $params[$vars[0]] = $vars[1]; } else { $params[$param] = $result; } } } else { $formatter = $this->formatter; $params = null; } // Call the formatter if (is_callable($formatter)) { $result = call_user_func_array($formatter, $params); } else { $result = new PEAR_Error('Unable to process formatter'); } return $result; } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Core.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Core.php +++ pear/Structures_DataGrid/DataGrid/Core.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Core.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ require_once ('DataGrid/Column.php'); require_once ('DataGrid/Record.php'); /** * Structures_DataGrid_Core Class * * The Core class implements the Core functionality of the DataGrid. * It offers the paging and sorting methods as well as the record and column * management methods. * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Core { /** * Array of columns. Columns are defined as a DataGridColumn object. * @var array */ var $columnSet = array(); /** * Array of records. Records are defined as a DataGridRecord object. * @var array */ var $recordSet = array(); /** * An array of fields to sort by. Each field is an array of the field name * and the direction, either ASC or DESC. * @var array */ var $sortArray; /** * Limit of records to show per page. * @var string */ var $rowlimit; /** * The current page to show. * @var string */ var $page; /** * The array of available pages. * @var array */ var $pageList = array(); /** * Constructor * * Creates default table style settings * * @param string $limit The row limit per page. * @param string $page The current page viewed. * @access public */ function Structures_DataGrid_Core($limit = null, $page = 1) { $this->rowLimit = $limit; $this->page = $page; } /** * Retrieves the current page number when paging is implemented * * @return string the current page number * @access public */ function getCurrentPage() { return $this->page; } /** * Define the current page number. This is used when paging is implemented * * @access public * @param string $page The current page number. */ function setCurrentPage($page) { $this->page = $page; } /** * Adds a DataGridColumn object to this DataGrid object * * @access public * @param object $column The column object to add. This object should be * a HTML_DataGridColumn object. * @return bool True if successful, otherwise false. */ function addColumn($column) { if (get_class($column) == 'structures_datagrid_column') { $this->columnSet = array_merge($this->columnSet, array($column)); return true; } else { return false; } } /** * A simple way to add an associative array record set to the data grid. * * @access public * @param array $rs The associative array recordset * @return bool True if successful, otherwise false. */ function bind($rs) { if (is_array($rs)) { $this->recordSet = $rs; return true; } else { return new PEAR_Error('Recordset must be an associative array'); } } /** * Adds a DataGrid_Record object to this DataGrid object * * @access public * @param object $record The record object to add. This object must be * a HTML_DataGridRecord object. * @return bool True if successful, otherwise false. */ function addRecord($record) { if ((get_class($record) == 'structures_datagrid_record') || (get_parent_class($record) == 'structures_datagrid_record')) { $this->recordSet = array_merge($this->recordSet, array($record->getRecord())); return true; } else { return new PEAR_Error('Not a valid DataGrid Record'); } } /** * Drops a DataGridRecord object from this DataGrid object * * @access public * @param object DataGrid_Record $record The record object to drop. * This object must be a * Structures_DataGridRecord * object. * @return void */ function dropRecord($record) { unset($this->recordSet[$record->getRecord()]); } /** * Sorts the records by the defined field. * Do not use this method if data is coming from a database as sorting * is much faster coming directly from the database itself. * * @access public * @param string $sortBy The field to sort the record set by. * @param string $direction The sort direction, either ASC or DESC. * @return void */ function sortRecordSet($sortBy, $direction = 'ASC') { // Define sorting values $this->sortArray = array($sortBy, $direction); // Sort the recordset usort($this->recordSet, array($this, '_sort')); } function _sort($a, $b, $i = 0) { $bool = strnatcmp($a[$this->sortArray[0]], $b[$this->sortArray[0]]); if ($this->sortArray[1] == DATAGRID_SORT_DESC) { $bool = $bool * -1; } return $bool; } /** * Handles building the paging of the DataGrid * * @access private * @return void */ function buildPaging() { if (($this->rowLimit > 0) && (count($this->recordSet) > $this->rowLimit)) { $pageCount = ceil(count(($this->recordSet))/($this->rowLimit)); for ($i = 1; $i <= $pageCount; $i++) { if ($this->page == $i) { $this->pageList[$i] = null; } else { $this->pageList[$i] = "page=$i"; } } } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Record.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Record.php +++ pear/Structures_DataGrid/DataGrid/Record.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Record.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ /** * Structures_DataGrid_Record Class * * This class represents one record for the DataGrid. All data is stored * as an Associative Array with the keys as the Column Name and the value * as the cell value. Other data types can be added by the extended classes, * such as a DB_Result or a DB_DataObject. * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Record { var $_record = array(); /** * Constructor * * Builds the record. Accepts data as either an Array or a DB_DataObject. * * @access public * @todo Allow more data types. */ function Structures_DataGrid_Record($data = null) { if ($data != null) { $this->setRecord($data); } } /** * Get Value * * Retrieves the value for the column name specified * * @access public * @param string $key The name of the column to retrieve the value for. * @return void */ function getValue($key) { return $this->_record[$key]; } /** * Set Value * * Sets the value for the column name specified * * @access public * @param string $key The name of the column to work on. * @param string $data The data to set the specific cell to. * @return void */ function setValue($key, $data) { $this->_record[$key] = $data; } /** * Get Record * * Retrieves the current record as an array. * * @access public * @return array The current record as an array. */ function getRecord() { return $this->_record; } /** * Set Record * * Sets the current record. The record must be defined as an array. * * @access public * @return mixed Returns true if successful, otherwise a * PEAR_Error object is returned. */ function setRecord($data) { if (is_array($data)) { $this->_record = $data; return true; } else { return new PEAR_Error('Invalid data type. Must be an array'); } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer.php +++ pear/Structures_DataGrid/DataGrid/Renderer.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Renderer.php,v 1.1 2004/03/17 16:08:30 asnagy Exp $ require_once ('DataGrid/Core.php'); /** * Structures_DataGrid_Renderer Class * * This class handles mangeing the output for the DataGrid. * By default, the output is handled by the HTML Table renderer. * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer extends Structures_DataGrid_Core { var $renderer; /** * Constructor * * Determins the appropriate renderer to use. Uses the HTML_Table renderer * as the default. * * @param string $renderer The renderer to use. * @access public */ function Structures_DataGrid_Renderer($renderer = DATAGRID_RENDER_TABLE) { if (PEAR::isError($this->setRenderer($renderer))) { $this->setRenderer(DATAGRID_RENDER_TABLE); } } /** * Render * * This calls the spcified renderer's render method. * * @param string $limit The row limit per page. * @param string $page The current page viewed. * @access public */ function render() { // Check to see if column headers exist, if not create them $this->_setDefaultHeaders(); // Render out the data $this->renderer->render(&$this); } function &getRenderer() { return $this->renderer; } function setRenderer($renderer) { $class = 'Structures_DataGrid_Renderer_' . $renderer; $file = 'DataGrid/Renderer/' . $renderer . '.php'; if (@include_once($file)) { $this->renderer = new $class(); } else { return new PEAR_Error('Invalid renderer'); } return true; } /** * Set Default Headers * * This method handles determining if column headers need to be set. * * @access private */ function _setDefaultHeaders() { if ((!count($this->columnSet)) && (count($this->recordSet))) { $arrayKeys = array_keys($this->recordSet[0]); foreach ($arrayKeys as $key) { $width = ceil(100/count($arrayKeys)); $column = new Structures_DataGrid_Column($key, $key, $key, array('width' => $width.'%')); $this->addColumn($column); } } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Record/DB.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Record/DB.php +++ pear/Structures_DataGrid/DataGrid/Record/DB.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: DB.php,v 1.1 2004/03/17 16:08:31 asnagy Exp $ require_once('DataGrid/Record.php'); /** * Structures_DataGrid_Record_DB Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Record_DB extends Structures_DataGrid_Record { /** * Constructor * * Builds the record. Accepts data as an DB_Result object. * * @access public * @todo Allow more data types. */ function Structures_DataGrid_Record_DB($data = null) { $this->setRecord($data); } function setRecord($data) { if (get_class($data) == 'db_result') { parent::setRecord($data->fetchRow(DB_FETCHMODE_ASSOC)); } else { return new PEAR_Error('Invalid data type. Data must be a DB_Result record'); } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Record/DataObject.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Record/DataObject.php +++ pear/Structures_DataGrid/DataGrid/Record/DataObject.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: DataObject.php,v 1.1 2004/03/17 16:08:31 asnagy Exp $ require_once('DataGrid/Record.php'); /** * Structures_DataGrid_Record_DataObject Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Record_DataObject extends Structures_DataGrid_Record { /** * Constructor * * Builds the record if sepcified. Accepts data as either an Array or a DB_DataObject. * * @access public * @todo Allow more data types. */ function Structures_DataGrid_Record_DataObject($data = null) { $this->setRecord($data); } function setRecord($data) { if (get_parent_class($data) == 'db_dataobject') { parent::setRecord($data->toArray()); } else { return new PEAR_Error('Invalid data type. Data must be a DB_DataObject record'); } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.php +++ pear/Structures_DataGrid/DataGrid/Renderer/HTMLTable.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: HTMLTable.php,v 1.1 2004/03/17 16:08:32 asnagy Exp $ require_once ('HTML/Table.php'); /** * Structures_DataGrid_Renderer_HTMLTable Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer_HTMLTable { /** * The table header background color * @var string */ var $headerBgColor; /** * The dark row background color * @var string */ var $rowDarkBgColor; /** * The light row background color * @var string */ var $rowLightBgColor; /** * An associative array containing each attribute of the table * @var array */ var $attrs; /** * A boolean value to determine if empty rows should be printed. * @var array */ var $allowEmptyRows; /** * An associative array containing the attributes for empty rows * @var array */ var $emptyRowAttributes = array(); /** * The structures_datagrid object * @var object Structures_DataGrid */ var $_dg; /** * The html_table object * @var object HTML_Table */ var $_table; /** * Constructor * * Build default values * * @access public */ function Structures_DataGrid_Renderer_HTMLTable() { $this->headerBgColor = '#FFFFFF'; $this->rowDarkBgColor = '#FFFFFF'; $this->rowLightBgColor = '#FFFFFF'; $this->attrs = array('cellpadding' => 4, 'cellspacing' => 0, 'border' => 0); $this->_table = new HTML_Table($this->attrs); } /** * Set a table attribute * * @access public * @param string $name The CSS class to use for the table. */ function setTableAttribute($attr, $value) { $this->attrs[$attr] = $value; $this->_table->_attributes = $this->attrs; } /** * Define the table's header bgcolor * * @access public * @param string $bgColor The header bgcolor to use for the table. */ function setTableHeaderBgColor($bgColor) { $this->headerBgColor = $bgColor; } /** * Define the table's row dark color * * @access public * @param string $bgColor The color to use for the dark table row. */ function setTableRowDarkBgColor($bgColor) { $this->rowDarkBgColor = $bgColor; } /** * Define the table's row light color * * @access public * @param string $bgColor The color to use for the light table row. */ function setTableRowLightBgColor($bgColor) { $this->rowLightBgColor = $bgColor; } /** * Define the table's autofill value. This value appears only in an empty table cell. * * @access public * @param string $value The value to use for empty cells. */ function setAutoFill($value) { $this->table->setAutoFill($value); } /** * Generates the HTML for the DataGrid * * @access public * @return void */ function render(&$dg) { $this->_dg = &$dg; // Define Table Header $this->_buildHTMLTableHeader(); // Build Table Data $this->_buildHTMLTableBody(); // Define Alternating Row attributes $lightRow = array('bgcolor' => $this->rowLightBgColor); $darkRow = array('bgcolor' => $this->rowDarkBgColor); $this->_table->altRowAttributes(1, $lightRow, $darkRow); // Print the table echo $this->_table->toHTML(); } /** * Handles building the header of the DataGrid * * @access private * @return void */ function _buildHTMLTableHeader() { $cnt = 0; foreach ($this->_dg->columnSet as $column) { //Define Content if ($column->orderBy != null) { if (stristr($_SERVER['PHP_SELF'], '?')) { $url = $_SERVER['PHP_SELF'] . '&orderBy='; } else { $url = $_SERVER['PHP_SELF'] . '?orderBy='; } if ($this->_dg->sortArray[1] == 'ASC') { $direction = '&direction=DESC'; } else { $direction = '&direction=ASC'; } $str = '<a href="' . $url . $column->orderBy . $direction . '"><b>' . $column->columnName . '</b></a>'; } else { $str = '<b>' . $column->columnName . '</b>'; } // Print Content to HTML_Table $this->_table->setHeaderContents(0, $cnt, $str); $this->_table->setCellAttributes(0, $cnt, $column->attribs); $cnt++; } // Define Table Header attributes $attr = array('bgcolor' => $this->headerBgColor); $this->_table->setRowAttributes(0, $attr); } /** * Handles building the body of the DataGrid * * @access private * @return void */ function _buildHTMLTableBody() { if (count($this->_dg->recordSet)) { // Determine looping values if ($this->_dg->page > 1) { $begin = ($this->_dg->page - 1) * $this->_dg->rowLimit; $limit = $this->_dg->page * $this->_dg->rowLimit; } else { $begin = 0; if ($this->_dg->rowLimit == null) { $limit = count($this->_dg->recordSet); } else { $limit = $this->_dg->rowLimit; } } // Begin loop for ($i = $begin; $i < $limit; $i++) { $row = $this->_dg->recordSet[$i]; if ($row != null) { // Print Row $cnt = 0; foreach ($this->_dg->columnSet as $column) { $rowCnt = ($i-$begin)+1; // Build Content if ($column->formatter != null) { $content = $column->formatter($row); } elseif ($column->fieldName == null) { if ($column->autoFill != null) { $content = $column->autoFill; } else { $content = $column->columnName; } } else { $content = $row[$column->fieldName]; } // Print Content to HTML_Table $this->_table->setCellContents($rowCnt, $cnt, $content); $this->_table->setCellAttributes($rowCnt, $cnt, $column->attribs); $cnt++; } } else { // Determine if empty row should be printed if ($this->allowEmptyRows) { $rowCnt++; $this->_table->setRowAttributes($rowCnt, $this->emptyRowAttributes, false); $this->_table->setCellContents($rowCnt, 0, ' '); } } } } } /** * Handles the printing of the page list for the DataGrid in HTML. * * @access public * @param string $seperator The string to use to seperate each page link * @return void * @todo Investigate HTML::Pager */ function printPaging($seperator = '|') { // Generate Paging $this->_dg->buildPaging(); // Create base url for link if (isset($_SERVER['QUERY_STRING'])) { $url = $_SERVER['PHP_SELF'] . '?'; $queryString = explode('&', $_SERVER['QUERY_STRING']); foreach ($queryString as $value) { if (!strstr($value, 'page') && ($value != '')) { $url .= $value . '&'; } } $url .= 'page='; } else { $url = $_SERVER['PHP_SELF'] . '?page='; } // Print back link if ($this->_dg->page > 1) { echo '<a href="' . $url . ($this->_dg->page - 1) . '"><<</a> '; } $cnt = 0; foreach ($this->_dg->pageList as $page => $link) { if ($link != '') { echo '<a href="' . $url . $page . "\"> $page </a>"; } else { echo "<b> $page </b>"; } if ($cnt+1 < count($this->_dg->pageList)) { echo " $seperator "; } $cnt++; } if ($this->_dg->page < count($this->_dg->pageList)) { echo ' <a href="' . $url . ($this->_dg->page + 1) . '">>></a>'; } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer/Smarty.php +++ pear/Structures_DataGrid/DataGrid/Renderer/Smarty.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: Smarty.php,v 1.1 2004/03/17 16:08:32 asnagy Exp $ require_once ('Smarty/Smarty.class.php'); /** * Structures_DataGrid_Renderer_Smarty Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer_Smarty { var $_dg; var $_smarty; var $_tpl; /** * Constructor * * Build default values * * @access public */ function Structures_DataGrid_Renderer_Smarty() { $this->_smarty = new Smarty(); $this->_smarty->template_dir = dirname($_SERVER['SCRIPT_FILENAME']); $this->_smarty->compile_dir = dirname($_SERVER['SCRIPT_FILENAME']) . '/compile'; } function setSmarty(&$smarty) { $this->_smarty = &$smarty; } function setTemplate($tpl) { if (is_file($this->_smarty->template_dir . '/' . $tpl)) { $this->_tpl = $tpl; } else { return new PEAR_Error('Error: Unable to find ' . $this->smarty->template_dir . '/' . $tpl); } } function render(&$dg) { $this->_dg = &$dg; if ($this->_tpl != '') { $this->_smarty->assign('recordSet', $this->_dg->recordSet); $this->_smarty->assign('columnSet', $this->_dg->columnSet); $this->_smarty->assign('recordLimit', $this->_dg->rowLimit); $this->_smarty->assign('pageList', $this->_dg->pageList); $this->_smarty->assign('currentPage', $this->_dg->page); $this->_smarty->display($this->_tpl); } else { return new PEAR_Error('Error: No template defined'); } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer/XLS.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer/XLS.php +++ pear/Structures_DataGrid/DataGrid/Renderer/XLS.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: XLS.php,v 1.1 2004/03/17 16:08:32 asnagy Exp $ require_once 'Spreadsheet/Excel/Writer.php'; /** * Structures_DataGrid_Renderer_XLS Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer_XLS { var $_dg; var $_workbook; var $_worksheet; var $_filename = 'spreadsheet.xls'; /** * Constructor * * Build default values * * @access public */ function Structures_DataGrid_Renderer_XLS() { $this->_workbook = new Spreadsheet_Excel_Writer(); $this->_workbook->send($this->_filename); $this->_worksheet =& $this->_workbook->addWorksheet(); } function render(&$dg) { $this->_dg = &$dg; $this->_buildHeader(); $this->_buildBody(); $this->_workbook->close(); } /** * Handles building the header of the DataGrid * * @access private * @return void */ function _buildHeader() { $cnt = 0; foreach ($this->_dg->columnSet as $column) { //Define Content $str = $column->columnName; $this->_worksheet->write(0, $cnt, $str); $cnt++; } } /** * Handles building the body of the DataGrid * * @access private * @return void */ function _buildBody() { if (count($this->_dg->recordSet)) { // Determine looping values if ($this->_dg->page > 1) { $begin = ($this->_dg->page - 1) * $this->_dg->rowLimit; $limit = $this->_dg->page * $this->_dg->rowLimit; } else { $begin = 0; if ($this->_dg->rowLimit == null) { $limit = count($this->_dg->recordSet); } else { $limit = $this->_dg->rowLimit; } } for ($i = $begin; $i < $limit; $i++) { $row = $this->_dg->recordSet[$i]; $cnt = 0; foreach ($this->_dg->columnSet as $column) { $rowCnt = ($i-$begin) + 1; // Build Content if ($column->formatter != null) { $content = $column->formatter($row); } elseif ($column->fieldName == null) { if ($column->autoFill != null) { $content = $column->autoFill; } else { $content = $column->columnName; } } else { $content = $row[$column->fieldName]; } $this->_worksheet->write($rowCnt, $cnt, $str); $cnt++; } } } } } ?> http://cvs.php.net/co.php/pear/Structures_DataGrid/DataGrid/Renderer/XML.php?r=1.1&p=1 Index: pear/Structures_DataGrid/DataGrid/Renderer/XML.php +++ pear/Structures_DataGrid/DataGrid/Renderer/XML.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. | // +----------------------------------------------------------------------+ // | Author: Andrew Nagy <asnagy@webitecture.org> | // +----------------------------------------------------------------------+ // // $Id: XML.php,v 1.1 2004/03/17 16:08:32 asnagy Exp $ require ('XML/Util.php'); /** * Structures_DataGrid_Renderer_XML Class * * @version $Revision: 1.1 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid * @category Structures */ class Structures_DataGrid_Renderer_XML { var $_dg; /** * Constructor * * Build default values * * @access public */ function Structures_DataGrid_Renderer_XML() { } function render(&$dg) { $this->_dg = &$dg; header('Content-type: text/xml'); echo XML_Util::getXMLDeclaration() . "\n"; echo "<DataGrid>\n"; foreach ($this->_dg->recordSet as $row) { echo " <Row>\n"; foreach ($this->_dg->columnSet as $column) { // Build Content if ($column->formatter != null) { $content = $column->formatter($row); } elseif ($column->fieldName == null) { if ($column->autoFill != null) { $content = $column->autoFill; } else { $content = $column->columnName; } } else { $content = $row[$column->fieldName]; } echo ' ' . XML_Util::createTag($column->columnName, null, $content) . "\n"; } echo " </Row>\n"; } echo "</DataGrid>\n"; } } ?>