Source for file Column.php
Documentation is available at Column.php
* Structures_DataGrid_Column Class
* Copyright (c) 1997-2007, Andrew Nagy <asnagy@webitecture.org>,
* Olivier Guilyardi <olivier@samalyse.com>,
* Mark Wiesemann <wiesemann@php.net>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* CSV file id: $Id: Column.php,v 1.54 2007/10/04 14:38:39 olivierg Exp $
* @version $Revision: 1.54 $
* @package Structures_DataGrid
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* Structures_DataGrid_Column Class
* This class represents a single column for the DataGrid.
* @version $Revision: 1.54 $
* @author Andrew S. Nagy <asnagy@webitecture.org>
* @author Olivier Guilyardi <olivier@samalyse.com>
* @author Mark Wiesemann <wiesemann@php.net>
* @package Structures_DataGrid
* The unique id of the column
* The name (label) of the column
* The name of the field to map to
* The field name to order by. Optional
* The default direction to order this column by
var $defaultDirection = 'ASC';
* The attributes to use for the cell. Optional
* The value to be used if a cell is empty
* A callback function to be called for each cell to modify the output
* User defined parameters passed to the formatter callback function
* Creates default table style settings
* @param string $label The label of the column to be printed
* @param string $field The name of the field for the column
* @param string $orderBy The field or expression to order the
* @param array $attributes The attributes for the XML or HTML
* TD tag; form: array(name => value, ...)
* @param string $autoFillValue The value to use for the autoFill
* @param mixed $formatter Formatter callback. See setFormatter()
* @param array $formatterArgs Associative array of arguments
* passed as second argument to the
* @see http://www.php.net/manual/en/language.pseudo-types.php
* @see Structures_DataGrid::addColumn()
$formatterArgs = array ())
* The label is the text rendered into the column header.
* The label is the text rendered into the column header.
* @param string $str Column label
* Get name of the field for the column to be mapped to
* Returns the name of the field for the column to be mapped to
* Set name of the field for the column to be mapped to
* Defines the name of the field for the column to be mapped to
* @param string $str The name of the field for the column to
* Get the field name to order the data by
* @return string field name
* Set the field name to order the data by
* @param string $str field name
* Return the default direction to order this column by
* @return string "ASC" or "DESC"
return $this->defaultDirection;
* Set the default direction to order this column by
* @param string $str "ASC" or "DESC"
$this->defaultDirection = $str;
* Get the column XML/HTML attributes
* Return the attributes applied to all cells in this column.
* This only makes sense for HTML or XML rendering
* @return array Attributes; form: array(name => value, ...)
* Set the column XML/HTML attributes
* Set the attributes to be applied to all cells in this column.
* This only makes sense for HTML or XML rendering
* @param array $attributes form: array(name => value, ...)
* Returns the value to be printed if a cell in the column is null.
* Defines a value to be printed if a cell in the column is null.
* @param string $str The value to use for the autoFill
* Define a formatting callback function with optional arguments for
* The callback function receives the following array as its first argument:
* 'record' => associative array of all fields values for this record,
* 'fieldName' => the field name of this column,
* 'columnName' => the label (header) of this column,
* 'orderBy' => the field name to sort this column by,
* 'attribs' => this column's attributes,
* 'currRow' => zero-based row index,
* 'currCol' => zero-based column index,
* If you pass the optional $arguments parameter to setFormatter(), the callback
* function will receive it as its second argument.
* @param mixed $formatter Callback PHP pseudo-type (Array or String)
* @param array $arguments Associative array of parameters passed to
* as second argument to the callback function
* @return mixed PEAR_Error on failure
* @see http://www.php.net/manual/en/language.pseudo-types.php
$this->formatterArgs = $arguments;
$formatter[1 ] = $this->_parseCallbackString ($formatter[1 ],
$formatter = $this->_parseCallbackString ($formatter,
$this->formatter = $formatter;
return PEAR ::raiseError ('Column formatter is not a valid callback');
* EXPERIMENTAL: the behaviour of this method may change in future releases.
* This method allows to associate an "automatic" predefined formatter
* to the column, for common needs as formatting dates, numbers, ...
* The currently supported predefined formatters are :
* - dateFromTimestamp: format a UNIX timestamp according to the
* date()-like format string passed as second argument
* - dateFromMysql : format a MySQL DATE, DATETIME, or TIMESTAMP MySQL
* according to the date() like format string passed as second argument
* - number: format a number, according to the same optional 2nd, 3rd and
* 4th arguments that the number_format() PHP function accepts.
* - printf: format using the printf expression passed as 2nd argument.
* - printfURL: url-encode and format using the printf expression passed
* @example format.php Common formats
* @param mixed $type,... Predefined formatter name, followed by
* formatter-specific parameters
* @param array $data Datagrid and record data
* @param data $params Formatter-specific parameters
function _autoFormatter ($data, $params)
$value = $data['record'][$data['fieldName']];
case 'dateFromTimestamp':
return date($format, $value);
'*([0-9]+):([0-9]+):([0-9]+)$/', $value, $r)) {
$time = mktime($r[4 ], $r[5 ], $r[6 ], $r[2 ], $r[3 ], $r[1 ]);
return date($format, $time);
} elseif (preg_match('/^([0-9]+)-([0-9]+)-([0-9]+)$/', $value, $r)){
$time = mktime(0 , 0 , 0 , $r[2 ], $r[3 ], $r[1 ]);
return date($format, $time);
return "Unrecognized date format";
switch (count($params)) {
return "Wrong parameter count for the 'number' format";
return sprintf($params[1 ], $value);
* Parse a callback function string
* This method parses a string of the type "myFunction($param1=foo,...)",
* return the isolated function name ("myFunction") and fills $paramList
* with the extracted parameters (array('param1' => foo, ...))
* @param string $callback Callback function string
* @param array $paramList Reference to an array of parameters
* @return string Function name
function _parseCallbackString ($callback, &$paramList)
if ($size = strpos($callback, '(')) {
$orig_callback = $callback;
// Retrieve the name of the function to call
$callback = substr($callback, 0 , $size);
if (strstr($callback, '->')) {
$callback = explode('->', $callback);
} elseif (strstr($callback, '::')) {
$callback = explode('::', $callback);
// Build the list of parameters
$length = strlen($orig_callback) - $size - 2;
$parameters = substr($orig_callback, $size + 1 , $length);
$parameters = ($parameters === '') ? array () : split(',', $parameters);
// Process the parameters
foreach($parameters as $param) {
if (strpos($param, '=') != false ) {
$vars = split('=', $param);
$paramList[trim($vars[0 ])] = trim($vars[1 ]);
$paramList[$param] = $ $param;
* This method is not meant to be called by user-space code.
* Calls a predefined function to develop custom output for the column. The
* defined function can accept parameters so that each cell in the column
* can be unique based on the record. The function will also automatically
* receive the record array as a parameter. All parameters passed into the
* function will be in one array.
// Define the parameter list
$paramList['record'] = $record;
$paramList['orderBy'] = $this->orderBy;
$paramList['attribs'] = $this->attribs;
$paramList['currRow'] = $row;
$paramList['currCol'] = $col;
if (isset ($GLOBALS['_STRUCTURES_DATAGRID']['column_formatter_BC'])) {
$paramList = array_merge($this->formatterArgs, $paramList);
if ($this->formatterArgs) {
/* vim: set expandtab tabstop=4 shiftwidth=4: */
Documentation generated on Tue, 18 Dec 2007 11:30:07 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|