Structures_DataGrid
[ class tree: Structures_DataGrid ] [ index: Structures_DataGrid ] [ all elements ]

Source for file Column.php

Documentation is available at Column.php

  1. <?php
  2. /**
  3.  * Structures_DataGrid_Column Class
  4.  * 
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE:
  8.  * 
  9.  * Copyright (c) 1997-2007, Andrew Nagy <asnagy@webitecture.org>,
  10.  *                          Olivier Guilyardi <olivier@samalyse.com>,
  11.  *                          Mark Wiesemann <wiesemann@php.net>
  12.  * All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted provided that the following conditions
  16.  * are met:
  17.  *
  18.  *    * Redistributions of source code must retain the above copyright
  19.  *      notice, this list of conditions and the following disclaimer.
  20.  *    * Redistributions in binary form must reproduce the above copyright
  21.  *      notice, this list of conditions and the following disclaimer in the
  22.  *      documentation and/or other materials provided with the distribution.
  23.  *    * The names of the authors may not be used to endorse or promote products
  24.  *      derived from this software without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  34.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37.  *
  38.  * CSV file id: $Id: Column.php,v 1.54 2007/10/04 14:38:39 olivierg Exp $
  39.  * 
  40.  * @version  $Revision: 1.54 $
  41.  * @package  Structures_DataGrid
  42.  * @category Structures
  43.  * @license  http://opensource.org/licenses/bsd-license.php New BSD License
  44.  */
  45.  
  46. /**
  47.  * Structures_DataGrid_Column Class
  48.  *
  49.  * This class represents a single column for the DataGrid.
  50.  *
  51.  * @version  $Revision: 1.54 $
  52.  * @author   Andrew S. Nagy <asnagy@webitecture.org>
  53.  * @author   Olivier Guilyardi <olivier@samalyse.com>
  54.  * @author   Mark Wiesemann <wiesemann@php.net>
  55.  * @access   public
  56.  * @package  Structures_DataGrid
  57.  * @category Structures
  58.  */
  59. {
  60.     /**
  61.      * The unique id of the column
  62.      * @var string 
  63.      */
  64.     var $id;
  65.  
  66.     /**
  67.      * The name (label) of the column
  68.      * @var string 
  69.      */
  70.     var $columnName;
  71.  
  72.     /**
  73.      * The name of the field to map to
  74.      * @var string 
  75.      */
  76.     var $fieldName;
  77.  
  78.     /**
  79.      * The field name to order by. Optional
  80.      * @var string 
  81.      */
  82.     var $orderBy;
  83.  
  84.     /**
  85.      * The default direction to order this column by
  86.      * 
  87.      * @var array 
  88.      * @access private
  89.      */
  90.     var $defaultDirection 'ASC';
  91.  
  92.     /**
  93.      * The attributes to use for the cell. Optional
  94.      * @var array 
  95.      */
  96.     var $attribs;
  97.  
  98.     /**
  99.      * The value to be used if a cell is empty
  100.      * @var string 
  101.      */
  102.     var $autoFillValue;
  103.  
  104.     /**
  105.      * A callback function to be called for each cell to modify the output
  106.      * @var     mixed 
  107.      * @access  private
  108.      */
  109.     var $formatter;
  110.     
  111.     /**
  112.      * User defined parameters passed to the formatter callback function
  113.      * @var     array 
  114.      * @access  private
  115.      */
  116.     var $formatterArgs;
  117.  
  118.     /**
  119.      * Constructor
  120.      *
  121.      * Creates default table style settings
  122.      *
  123.      * @param   string      $label          The label of the column to be printed
  124.      * @param   string      $field          The name of the field for the column
  125.      *                                       to be mapped to
  126.      * @param   string      $orderBy        The field or expression to order the
  127.      *                                       data by
  128.      * @param   array       $attributes     The attributes for the XML or HTML
  129.      *                                       TD tag; form: array(name => value, ...)
  130.      * @param   string      $autoFillValue  The value to use for the autoFill
  131.      * @param   mixed       $formatter      Formatter callback. See setFormatter()
  132.      * @param   array       $formatterArgs  Associative array of arguments
  133.      *                                       passed as second argument to the
  134.      *                                       formatter callback
  135.      * @see http://www.php.net/manual/en/language.pseudo-types.php
  136.      * @see Structures_DataGrid::addColumn()
  137.      * @see setFormatter()
  138.      * @access  public
  139.      */
  140.     function Structures_DataGrid_Column($label
  141.                                         $field = null,
  142.                                         $orderBy = null
  143.                                         $attributes = array(),
  144.                                         $autoFillValue = null,
  145.                                         $formatter = null,
  146.                                         $formatterArgs = array())
  147.     {
  148.         $this->id = uniqid('_');
  149.         $this->columnName = $label;
  150.         $this->fieldName = $field;
  151.         $this->orderBy = $orderBy;
  152.         $this->attribs = $attributes;
  153.         $this->autoFillValue = $autoFillValue;
  154.         if (!is_null($formatter)) {
  155.             $this->setFormatter($formatter$formatterArgs);
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Get column label
  161.      *
  162.      * The label is the text rendered into the column header.
  163.      *
  164.      * @return  string 
  165.      * @access  public
  166.      */
  167.     function getLabel()
  168.     {
  169.         return $this->columnName;
  170.     }
  171.  
  172.     /**
  173.      * Set column label
  174.      *
  175.      * The label is the text rendered into the column header.
  176.      *
  177.      * @param   string      $str        Column label
  178.      * @access  public
  179.      */
  180.     function setLabel($str)
  181.     {
  182.         $this->columnName = $str;
  183.     }
  184.  
  185.     /**
  186.      * Get name of the field for the column to be mapped to
  187.      *
  188.      * Returns the name of the field for the column to be mapped to
  189.      *
  190.      * @return  string 
  191.      * @access  public
  192.      */
  193.     function getField()
  194.     {
  195.         return $this->fieldName;
  196.     }
  197.  
  198.     /**
  199.      * Set name of the field for the column to be mapped to
  200.      *
  201.      * Defines the name of the field for the column to be mapped to
  202.      *
  203.      * @param   string      $str        The name of the field for the column to
  204.      *                                   be mapped to
  205.      * @access  public
  206.      */
  207.     function setField($str)
  208.     {
  209.         $this->fieldName = $str;
  210.     }
  211.  
  212.     /**
  213.      * Get the field name to order the data by
  214.      *
  215.      * @return  string field name
  216.      * @access  public
  217.      */
  218.     function getOrderBy()
  219.     {
  220.         return $this->orderBy;
  221.     }
  222.  
  223.     /**
  224.      * Set the field name to order the data by
  225.      *
  226.      * @param   string      $str  field name
  227.      * @access  public
  228.      */
  229.     function setOrderBy($str)
  230.     {
  231.         $this->orderBy = $str;
  232.     }
  233.  
  234.     /**
  235.      * Return the default direction to order this column by
  236.      *
  237.      * @return  string  "ASC" or "DESC"
  238.      * @access  public
  239.      */
  240.     function getDefaultDirection($str)
  241.     {
  242.         return $this->defaultDirection;
  243.     }
  244.  
  245.     /**
  246.      * Set the default direction to order this column by
  247.      *
  248.      * @param   string      $str    "ASC" or "DESC"
  249.      * @access  public
  250.      */
  251.     function setDefaultDirection($str)
  252.     {
  253.         $this->defaultDirection $str;
  254.     }
  255.  
  256.     /**
  257.      * Get the column XML/HTML attributes
  258.      *
  259.      * Return the attributes applied to all cells in this column.
  260.      * This only makes sense for HTML or XML rendering
  261.      *
  262.      * @return  array   Attributes; form: array(name => value, ...)
  263.      * @access  public
  264.      */
  265.     function getAttributes()
  266.     {
  267.         return $this->attribs;
  268.     }
  269.  
  270.     /**
  271.      * Set the column XML/HTML attributes
  272.      *
  273.      * Set the attributes to be applied to all cells in this column.
  274.      * This only makes sense for HTML or XML rendering
  275.      * 
  276.      * @param   array   $attributes form: array(name => value, ...)
  277.      * @access  public
  278.      */
  279.     function setAttributes($attributes)
  280.     {
  281.         $this->attribs = $attributes;
  282.     }
  283.  
  284.     /**
  285.      * Get auto fill value
  286.      *
  287.      * Returns the value to be printed if a cell in the column is null.
  288.      *
  289.      * @return  string 
  290.      * @access  public
  291.      */
  292.     function getAutoFillValue()
  293.     {
  294.         return $this->autoFillValue;
  295.     }
  296.  
  297.     /**
  298.      * Set auto fill value
  299.      *
  300.      * Defines a value to be printed if a cell in the column is null.
  301.      *
  302.      * @param   string      $str        The value to use for the autoFill
  303.      * @access  public
  304.      */
  305.     function setAutoFillValue($str)
  306.     {
  307.         $this->autoFillValue = $str;
  308.     }
  309.  
  310.     /**
  311.      * Set Formatter Callback
  312.      *
  313.      * Define a formatting callback function with optional arguments for
  314.      * this column.
  315.      *
  316.      * The callback function receives the following array as its first argument:
  317.      * <code>
  318.      * array(
  319.      *   'record' => associative array of all fields values for this record,
  320.      *   'fieldName' => the field name of this column,
  321.      *   'columnName' => the label (header) of this column,
  322.      *   'orderBy' => the field name to sort this column by,
  323.      *   'attribs' => this column's attributes,
  324.      *   'currRow' => zero-based row index,
  325.      *   'currCol' => zero-based column index,
  326.      * );
  327.      * </code>
  328.      *
  329.      * If you pass the optional $arguments parameter to setFormatter(), the callback
  330.      * function will receive it as its second argument.
  331.      *
  332.      * @param   mixed   $formatter  Callback PHP pseudo-type (Array or String)
  333.      * @param   array   $arguments  Associative array of parameters passed to
  334.      *                               as second argument to the callback function
  335.      * @return  mixed               PEAR_Error on failure
  336.      * @see http://www.php.net/manual/en/language.pseudo-types.php
  337.      * @access  public
  338.      */
  339.     function setFormatter($formatter$arguments = array())
  340.     {
  341.         $this->formatterArgs $arguments;
  342.         if (is_array($formatter)) {
  343.             $formatter[1$this->_parseCallbackString($formatter[1]
  344.                                                         $this->formatterArgs);
  345.         else {
  346.             $formatter $this->_parseCallbackString($formatter
  347.                                                      $this->formatterArgs);
  348.         }
  349.         if (is_callable ($formatter)) {
  350.             $this->formatter $formatter;
  351.         else {
  352.             return PEAR::raiseError('Column formatter is not a valid callback');
  353.         }
  354.     }
  355.  
  356.     /**
  357.      * Choose a format preset
  358.      *
  359.      * EXPERIMENTAL: the behaviour of this method may change in future releases.
  360.      *
  361.      * This method allows to associate an "automatic" predefined formatter
  362.      * to the column, for common needs as formatting dates, numbers, ...
  363.      *
  364.      * The currently supported predefined formatters are :
  365.      * - dateFromTimestamp: format a UNIX timestamp according to the
  366.      *   date()-like format string passed as second argument
  367.      * - dateFromMysql : format a MySQL DATE, DATETIME, or TIMESTAMP MySQL
  368.      *   according to the date() like format string passed as second argument
  369.      * - number: format a number, according to the same optional 2nd, 3rd and
  370.      *   4th arguments that the number_format() PHP function accepts.
  371.      * - printf: format using the printf expression passed as 2nd argument.
  372.      * - printfURL: url-encode and format using the printf expression passed
  373.      *   as 2nd argument
  374.      *
  375.      * @example format.php         Common formats
  376.      * @param   mixed  $type,...   Predefined formatter name, followed by
  377.      *                              formatter-specific parameters
  378.      * @return  void 
  379.      * @access  public
  380.      */
  381.     function format($type)
  382.     {
  383.         $params func_get_args();
  384.         $this->setFormatter(array(get_class($this)'_autoFormatter')$params);
  385.     }
  386.  
  387.     /**
  388.      * Automatic formatter(s)
  389.      * 
  390.      * @param   array   $data   Datagrid and record data
  391.      * @param   data    $params Formatter-specific parameters
  392.      * @access  private
  393.      * @static
  394.      */
  395.     function _autoFormatter($data$params)
  396.     {
  397.         $value $data['record'][$data['fieldName']];
  398.         $type $params[0];
  399.         
  400.         switch ($type{
  401.             case 'dateFromTimestamp':
  402.                 $format $params[1];
  403.                 return date($format$value);
  404.             case 'dateFromMysql':
  405.                 $format $params[1];
  406.                 if (preg_match('/^([0-9]+)-([0-9]+)-([0-9]+) '.
  407.                                '*([0-9]+):([0-9]+):([0-9]+)$/'$value$r)) {
  408.                     $time mktime($r[4]$r[5]$r[6]$r[2]$r[3]$r[1]);
  409.                     return date($format$time);
  410.                 elseif (preg_match('/^([0-9]+)-([0-9]+)-([0-9]+)$/'$value$r)){
  411.                     $time mktime(000$r[2]$r[3]$r[1]);
  412.                     return date($format$time);
  413.                 else {
  414.                     return "Unrecognized date format";
  415.                 }
  416.             case 'number':
  417.                 switch (count($params)) {
  418.                     case 4: 
  419.                         return number_format($value$params[1]
  420.                                              $params[2]$params[3]);
  421.                     case 3: 
  422.                         return "Wrong parameter count for the 'number' format";
  423.                     case 2: 
  424.                         return number_format($value$params[1]);
  425.                     default:
  426.                         return number_format($value);
  427.                 }
  428.             case 'printfURL':
  429.                 $value urlencode($value);
  430.             case 'printf':
  431.                 return sprintf($params[1]$value);
  432.         }
  433.     }
  434.  
  435.     /**
  436.      * Parse a callback function string
  437.      *
  438.      * This method parses a string of the type "myFunction($param1=foo,...)",
  439.      * return the isolated function name ("myFunction") and fills $paramList
  440.      * with the extracted parameters (array('param1' => foo, ...))
  441.      * 
  442.      * @param   string  $callback   Callback function string
  443.      * @param   array   $paramList  Reference to an array of parameters
  444.      * @return  string              Function name
  445.      * @access  private
  446.      */
  447.     function _parseCallbackString($callback&$paramList)
  448.     {   
  449.         if ($size strpos($callback'(')) {
  450.             $orig_callback $callback;
  451.             // Retrieve the name of the function to call
  452.             $callback substr($callback0$size);
  453.             if (strstr($callback'->')) 
  454.                 $callback explode('->'$callback);
  455.             elseif (strstr($callback'::')) {
  456.                 $callback explode('::'$callback);
  457.             }
  458.  
  459.             // Build the list of parameters
  460.             $length strlen($orig_callback$size - 2;
  461.             $parameters substr($orig_callback$size + 1$length);
  462.             $parameters ($parameters === ''? array(split(','$parameters);
  463.  
  464.             // Process the parameters
  465.             foreach($parameters as $param{
  466.                 if ($param != ''{
  467.                     $param str_replace('$'''$param);
  468.                     if (strpos($param'='!= false{
  469.                         $vars split('='$param);
  470.                         $paramList[trim($vars[0])trim($vars[1]);
  471.                     else {
  472.                         $paramList[$param= $$param;
  473.                     }
  474.                 }
  475.             }
  476.         }
  477.  
  478.         return $callback;
  479.     }
  480.     
  481.     /**
  482.      * Formatter
  483.      *
  484.      * This method is not meant to be called by user-space code.
  485.      * 
  486.      * Calls a predefined function to develop custom output for the column. The
  487.      * defined function can accept parameters so that each cell in the column
  488.      * can be unique based on the record.  The function will also automatically
  489.      * receive the record array as a parameter.  All parameters passed into the
  490.      * function will be in one array.
  491.      *
  492.      * @access  public
  493.      */
  494.     function formatter($record$row$col)
  495.     {
  496.         // Define the parameter list
  497.         $paramList = array();
  498.         $paramList['record'$record;
  499.         $paramList['fieldName'$this->fieldName;
  500.         $paramList['columnName'$this->columnName;
  501.         $paramList['orderBy'$this->orderBy;
  502.         $paramList['attribs'$this->attribs;
  503.         $paramList['currRow'$row;
  504.         $paramList['currCol'$col;
  505.  
  506.         // Call the formatter
  507.         if (isset($GLOBALS['_STRUCTURES_DATAGRID']['column_formatter_BC'])) {
  508.             $paramList array_merge($this->formatterArgs$paramList);
  509.             $formatted call_user_func($this->formatter$paramList);
  510.         else {
  511.             if ($this->formatterArgs{
  512.                 $formatted call_user_func($this->formatter$paramList
  513.                                             $this->formatterArgs);
  514.             else {
  515.                 $formatted call_user_func($this->formatter$paramList);
  516.             }
  517.         }
  518.  
  519.         return $formatted;
  520.     }
  521.  
  522. }
  523.  
  524. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  525. ?>

Documentation generated on Tue, 18 Dec 2007 11:30:07 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.