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

Source for file Array.php

Documentation is available at Array.php

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

Documentation generated on Mon, 11 Mar 2019 13:52:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.