home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm / Renderer / Array.php next >
Encoding:
PHP Script  |  2008-07-02  |  10.1 KB  |  340 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A concrete renderer for HTML_QuickForm, makes an array of form contents
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.01 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_01.txt If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category    HTML
  16.  * @package     HTML_QuickForm
  17.  * @author      Alexey Borzov <avb@php.net>
  18.  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
  19.  * @author      Bertrand Mansion <bmansion@mamasam.com>
  20.  * @author      Thomas Schulz <ths@4bconsult.de>
  21.  * @copyright   2001-2007 The PHP Group
  22.  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
  23.  * @version     CVS: $Id: Array.php,v 1.10 2007/05/29 18:34:36 avb Exp $
  24.  * @link        http://pear.php.net/package/HTML_QuickForm
  25.  */
  26.  
  27. /**
  28.  * An abstract base class for QuickForm renderers
  29.  */
  30. require_once 'HTML/QuickForm/Renderer.php';
  31.  
  32. /**
  33.  * A concrete renderer for HTML_QuickForm, makes an array of form contents
  34.  *
  35.  * Based on old HTML_QuickForm::toArray() code.
  36.  *
  37.  * The form array structure is the following:
  38.  * <pre>
  39.  * array(
  40.  *   'frozen'           => 'whether the form is frozen',
  41.  *   'javascript'       => 'javascript for client-side validation',
  42.  *   'attributes'       => 'attributes for <form> tag',
  43.  *   'requirednote      => 'note about the required elements',
  44.  *   // if we set the option to collect hidden elements
  45.  *   'hidden'           => 'collected html of all hidden elements',
  46.  *   // if there were some validation errors:
  47.  *   'errors' => array(
  48.  *     '1st element name' => 'Error for the 1st element',
  49.  *     ...
  50.  *     'nth element name' => 'Error for the nth element'
  51.  *   ),
  52.  *   // if there are no headers in the form:
  53.  *   'elements' => array(
  54.  *     element_1,
  55.  *     ...
  56.  *     element_N
  57.  *   )
  58.  *   // if there are headers in the form:
  59.  *   'sections' => array(
  60.  *     array(
  61.  *       'header'   => 'Header text for the first header',
  62.  *       'name'     => 'Header name for the first header',
  63.  *       'elements' => array(
  64.  *          element_1,
  65.  *          ...
  66.  *          element_K1
  67.  *       )
  68.  *     ),
  69.  *     ...
  70.  *     array(
  71.  *       'header'   => 'Header text for the Mth header',
  72.  *       'name'     => 'Header name for the Mth header',
  73.  *       'elements' => array(
  74.  *          element_1,
  75.  *          ...
  76.  *          element_KM
  77.  *       )
  78.  *     )
  79.  *   )
  80.  * );
  81.  * </pre>
  82.  *
  83.  * where element_i is an array of the form:
  84.  * <pre>
  85.  * array(
  86.  *   'name'      => 'element name',
  87.  *   'value'     => 'element value',
  88.  *   'type'      => 'type of the element',
  89.  *   'frozen'    => 'whether element is frozen',
  90.  *   'label'     => 'label for the element',
  91.  *   'required'  => 'whether element is required',
  92.  *   'error'     => 'error associated with the element',
  93.  *   'style'     => 'some information about element style (e.g. for Smarty)',
  94.  *   // if element is not a group
  95.  *   'html'      => 'HTML for the element'
  96.  *   // if element is a group
  97.  *   'separator' => 'separator for group elements',
  98.  *   'elements'  => array(
  99.  *     element_1,
  100.  *     ...
  101.  *     element_N
  102.  *   )
  103.  * );
  104.  * </pre>
  105.  *
  106.  * @category    HTML
  107.  * @package     HTML_QuickForm
  108.  * @author      Alexey Borzov <avb@php.net>
  109.  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
  110.  * @author      Bertrand Mansion <bmansion@mamasam.com>
  111.  * @author      Thomas Schulz <ths@4bconsult.de>
  112.  * @version     Release: 3.2.10
  113.  * @since       3.0
  114.  */
  115. class HTML_QuickForm_Renderer_Array extends HTML_QuickForm_Renderer
  116. {
  117.    /**#@+
  118.     * @access private
  119.     */
  120.    /**
  121.     * An array being generated
  122.     * @var array
  123.     */
  124.     var $_ary;
  125.  
  126.    /**
  127.     * Number of sections in the form (i.e. number of headers in it)
  128.     * @var integer
  129.     */
  130.     var $_sectionCount;
  131.  
  132.    /**
  133.     * Current section number
  134.     * @var integer
  135.     */
  136.     var $_currentSection;
  137.  
  138.    /**
  139.     * Array representing current group
  140.     * @var array
  141.     */
  142.     var $_currentGroup = null;
  143.  
  144.    /**
  145.     * Additional style information for different elements
  146.     * @var array
  147.     */
  148.     var $_elementStyles = array();
  149.  
  150.    /**
  151.     * true: collect all hidden elements into string; false: process them as usual form elements
  152.     * @var bool
  153.     */
  154.     var $_collectHidden = false;
  155.  
  156.    /**
  157.     * true:  render an array of labels to many labels, $key 0 named 'label', the rest "label_$key"
  158.     * false: leave labels as defined
  159.     * @var bool
  160.     */
  161.     var $_staticLabels = false;
  162.    /**#@-*/
  163.  
  164.    /**
  165.     * Constructor
  166.     *
  167.     * @param  bool    true: collect all hidden elements into string; false: process them as usual form elements
  168.     * @param  bool    true: render an array of labels to many labels, $key 0 to 'label' and the oterh to "label_$key"
  169.     * @access public
  170.     */
  171.     function HTML_QuickForm_Renderer_Array($collectHidden = false, $staticLabels = false)
  172.     {
  173.         $this->HTML_QuickForm_Renderer();
  174.         $this->_collectHidden = $collectHidden;
  175.         $this->_staticLabels  = $staticLabels;
  176.     } // end constructor
  177.  
  178.  
  179.    /**
  180.     * Returns the resultant array
  181.     *
  182.     * @access public
  183.     * @return array
  184.     */
  185.     function toArray()
  186.     {
  187.         return $this->_ary;
  188.     }
  189.  
  190.  
  191.     function startForm(&$form)
  192.     {
  193.         $this->_ary = array(
  194.             'frozen'            => $form->isFrozen(),
  195.             'javascript'        => $form->getValidationScript(),
  196.             'attributes'        => $form->getAttributes(true),
  197.             'requirednote'      => $form->getRequiredNote(),
  198.             'errors'            => array()
  199.         );
  200.         if ($this->_collectHidden) {
  201.             $this->_ary['hidden'] = '';
  202.         }
  203.         $this->_elementIdx     = 1;
  204.         $this->_currentSection = null;
  205.         $this->_sectionCount   = 0;
  206.     } // end func startForm
  207.  
  208.  
  209.     function renderHeader(&$header)
  210.     {
  211.         $this->_ary['sections'][$this->_sectionCount] = array(
  212.             'header' => $header->toHtml(),
  213.             'name'   => $header->getName()
  214.         );
  215.         $this->_currentSection = $this->_sectionCount++;
  216.     } // end func renderHeader
  217.  
  218.  
  219.     function renderElement(&$element, $required, $error)
  220.     {
  221.         $elAry = $this->_elementToArray($element, $required, $error);
  222.         if (!empty($error)) {
  223.             $this->_ary['errors'][$elAry['name']] = $error;
  224.         }
  225.         $this->_storeArray($elAry);
  226.     } // end func renderElement
  227.  
  228.  
  229.     function renderHidden(&$element)
  230.     {
  231.         if ($this->_collectHidden) {
  232.             $this->_ary['hidden'] .= $element->toHtml() . "\n";
  233.         } else {
  234.             $this->renderElement($element, false, null);
  235.         }
  236.     } // end func renderHidden
  237.  
  238.  
  239.     function startGroup(&$group, $required, $error)
  240.     {
  241.         $this->_currentGroup = $this->_elementToArray($group, $required, $error);
  242.         if (!empty($error)) {
  243.             $this->_ary['errors'][$this->_currentGroup['name']] = $error;
  244.         }
  245.     } // end func startGroup
  246.  
  247.  
  248.     function finishGroup(&$group)
  249.     {
  250.         $this->_storeArray($this->_currentGroup);
  251.         $this->_currentGroup = null;
  252.     } // end func finishGroup
  253.  
  254.  
  255.    /**
  256.     * Creates an array representing an element
  257.     *
  258.     * @access private
  259.     * @param  HTML_QuickForm_element    element being processed
  260.     * @param  bool                      Whether an element is required
  261.     * @param  string                    Error associated with the element
  262.     * @return array
  263.     */
  264.     function _elementToArray(&$element, $required, $error)
  265.     {
  266.         $ret = array(
  267.             'name'      => $element->getName(),
  268.             'value'     => $element->getValue(),
  269.             'type'      => $element->getType(),
  270.             'frozen'    => $element->isFrozen(),
  271.             'required'  => $required,
  272.             'error'     => $error
  273.         );
  274.         // render label(s)
  275.         $labels = $element->getLabel();
  276.         if (is_array($labels) && $this->_staticLabels) {
  277.             foreach($labels as $key => $label) {
  278.                 $key = is_int($key)? $key + 1: $key;
  279.                 if (1 === $key) {
  280.                     $ret['label'] = $label;
  281.                 } else {
  282.                     $ret['label_' . $key] = $label;
  283.                 }
  284.             }
  285.         } else {
  286.             $ret['label'] = $labels;
  287.         }
  288.  
  289.         // set the style for the element
  290.         if (isset($this->_elementStyles[$ret['name']])) {
  291.             $ret['style'] = $this->_elementStyles[$ret['name']];
  292.         }
  293.         if ('group' == $ret['type']) {
  294.             $ret['separator'] = $element->_separator;
  295.             $ret['elements']  = array();
  296.         } else {
  297.             $ret['html']      = $element->toHtml();
  298.         }
  299.         return $ret;
  300.     }
  301.  
  302.  
  303.    /**
  304.     * Stores an array representation of an element in the form array
  305.     *
  306.     * @access private
  307.     * @param array  Array representation of an element
  308.     * @return void
  309.     */
  310.     function _storeArray($elAry)
  311.     {
  312.         // where should we put this element...
  313.         if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) {
  314.             $this->_currentGroup['elements'][] = $elAry;
  315.         } elseif (isset($this->_currentSection)) {
  316.             $this->_ary['sections'][$this->_currentSection]['elements'][] = $elAry;
  317.         } else {
  318.             $this->_ary['elements'][] = $elAry;
  319.         }
  320.     }
  321.  
  322.  
  323.    /**
  324.     * Sets a style to use for element rendering
  325.     *
  326.     * @param mixed      element name or array ('element name' => 'style name')
  327.     * @param string     style name if $elementName is not an array
  328.     * @access public
  329.     * @return void
  330.     */
  331.     function setElementStyle($elementName, $styleName = null)
  332.     {
  333.         if (is_array($elementName)) {
  334.             $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
  335.         } else {
  336.             $this->_elementStyles[$elementName] = $styleName;
  337.         }
  338.     }
  339. }
  340. ?>