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.9 2004/10/15 20:00:48 ths 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.  *       'name'     => 'Header name for the first header',
  56.  *       'elements' => array(
  57.  *          element_1,
  58.  *          ...
  59.  *          element_K1
  60.  *       )
  61.  *     ),
  62.  *     ...
  63.  *     array(
  64.  *       'header'   => 'Header text for the Mth header',
  65.  *       'name'     => 'Header name for the Mth header',
  66.  *       'elements' => array(
  67.  *          element_1,
  68.  *          ...
  69.  *          element_KM
  70.  *       )
  71.  *     )
  72.  *   )
  73.  * );
  74.  *
  75.  * where element_i is an array of the form:
  76.  * array(
  77.  *   'name'      => 'element name',
  78.  *   'value'     => 'element value',
  79.  *   'type'      => 'type of the element',
  80.  *   'frozen'    => 'whether element is frozen',
  81.  *   'label'     => 'label for the element',
  82.  *   'required'  => 'whether element is required',
  83.  *   'error'     => 'error associated with the element',
  84.  *   'style'     => 'some information about element style (e.g. for Smarty)',
  85.  *   // if element is not a group
  86.  *   'html'      => 'HTML for the element'
  87.  *   // if element is a group
  88.  *   'separator' => 'separator for group elements',
  89.  *   'elements'  => array(
  90.  *     element_1,
  91.  *     ...
  92.  *     element_N
  93.  *   )
  94.  * );
  95.  *
  96.  * @access public
  97.  */
  98. {
  99.    /**
  100.     * An array being generated
  101.     * @var array 
  102.     */
  103.     var $_ary;
  104.  
  105.    /**
  106.     * Number of sections in the form (i.e. number of headers in it)
  107.     * @var integer 
  108.     */
  109.     var $_sectionCount;
  110.  
  111.    /**
  112.     * Current section number
  113.     * @var integer 
  114.     */
  115.     var $_currentSection;
  116.  
  117.    /**
  118.     * Array representing current group
  119.     * @var array 
  120.     */
  121.     var $_currentGroup = null;
  122.  
  123.    /**
  124.     * Additional style information for different elements
  125.     * @var array 
  126.     */
  127.     var $_elementStyles = array();
  128.  
  129.    /**
  130.     * true: collect all hidden elements into string; false: process them as usual form elements
  131.     * @var bool 
  132.     */
  133.     var $_collectHidden = false;
  134.  
  135.    /**
  136.     * true:  render an array of labels to many labels, $key 0 named 'label', the rest "label_$key"
  137.     * false: leave labels as defined
  138.     * @var bool 
  139.     */
  140.     var $staticLabels = false;
  141.  
  142.    /**
  143.     * Constructor
  144.     *
  145.     * @param  bool    true: collect all hidden elements into string; false: process them as usual form elements
  146.     * @param  bool    true: render an array of labels to many labels, $key 0 to 'label' and the oterh to "label_$key"
  147.     * @access public
  148.     */
  149.     function HTML_QuickForm_Renderer_Array($collectHidden = false$staticLabels = false)
  150.     {
  151.         $this->HTML_QuickForm_Renderer();
  152.         $this->_collectHidden $collectHidden;
  153.         $this->_staticLabels  $staticLabels;
  154.     // end constructor
  155.  
  156.  
  157.    /**
  158.     * Returns the resultant array
  159.     *
  160.     * @access public
  161.     * @return array 
  162.     */
  163.     function toArray()
  164.     {
  165.         return $this->_ary;
  166.     }
  167.  
  168.  
  169.     function startForm(&$form)
  170.     {
  171.         $this->_ary = array(
  172.             'frozen'            => $form->isFrozen(),
  173.             'javascript'        => $form->getValidationScript(),
  174.             'attributes'        => $form->getAttributes(true),
  175.             'requirednote'      => $form->getRequiredNote(),
  176.             'errors'            => array()
  177.         );
  178.         if ($this->_collectHidden{
  179.             $this->_ary['hidden''';
  180.         }
  181.         $this->_elementIdx     = 1;
  182.         $this->_currentSection = null;
  183.         $this->_sectionCount   = 0;
  184.     // end func startForm
  185.  
  186.  
  187.     function renderHeader(&$header)
  188.     {
  189.         $this->_ary['sections'][$this->_sectionCount= array(
  190.             'header' => $header->toHtml(),
  191.             'name'   => $header->getName()
  192.         );
  193.         $this->_currentSection $this->_sectionCount++;
  194.     // end func renderHeader
  195.  
  196.  
  197.     function renderElement(&$element$required$error)
  198.     {
  199.         $elAry $this->_elementToArray($element$required$error);
  200.         if (!empty($error)) {
  201.             $this->_ary['errors'][$elAry['name']] $error;
  202.         }
  203.         $this->_storeArray($elAry);
  204.     // end func renderElement
  205.  
  206.  
  207.     function renderHidden(&$element)
  208.     {
  209.         if ($this->_collectHidden{
  210.             $this->_ary['hidden'.= $element->toHtml("\n";
  211.         else {
  212.             $this->renderElement($elementfalsenull);
  213.         }
  214.     // end func renderHidden
  215.  
  216.  
  217.     function startGroup(&$group$required$error)
  218.     {
  219.         $this->_currentGroup $this->_elementToArray($group$required$error);
  220.         if (!empty($error)) {
  221.             $this->_ary['errors'][$this->_currentGroup['name']] $error;
  222.         }
  223.     // end func startGroup
  224.  
  225.  
  226.     function finishGroup(&$group)
  227.     {
  228.         $this->_storeArray($this->_currentGroup);
  229.         $this->_currentGroup = null;
  230.     // end func finishGroup
  231.  
  232.  
  233.    /**
  234.     * Creates an array representing an element
  235.     *
  236.     * @access private
  237.     * @param  object    An HTML_QuickForm_element object
  238.     * @param  bool      Whether an element is required
  239.     * @param  string    Error associated with the element
  240.     * @return array 
  241.     */
  242.     function _elementToArray(&$element$required$error)
  243.     {
  244.         $ret = array(
  245.             'name'      => $element->getName(),
  246.             'value'     => $element->getValue(),
  247.             'type'      => $element->getType(),
  248.             'frozen'    => $element->isFrozen(),
  249.             'required'  => $required,
  250.             'error'     => $error
  251.         );
  252.         // render label(s)
  253.         $labels $element->getLabel();
  254.         if (is_array($labels&& $this->_staticLabels{
  255.             foreach($labels as $key => $label{
  256.                 $key is_int($key)$key + 1: $key;
  257.                 if (1 === $key{
  258.                     $ret['label'$label;
  259.                 else {
  260.                     $ret['label_' $key$label;
  261.                 }
  262.             }
  263.         else {
  264.             $ret['label'$labels;
  265.         }
  266.  
  267.         // set the style for the element
  268.         if (isset($this->_elementStyles[$ret['name']])) {
  269.             $ret['style'$this->_elementStyles[$ret['name']];
  270.         }
  271.         if ('group' == $ret['type']{
  272.             $ret['separator'$element->_separator;
  273.             $ret['elements']  = array();
  274.         else {
  275.             $ret['html']      $element->toHtml();
  276.         }
  277.         return $ret;
  278.     }
  279.  
  280.  
  281.    /**
  282.     * Stores an array representation of an element in the form array
  283.     *
  284.     * @access private
  285.     * @param array  Array representation of an element
  286.     * @return void 
  287.     */
  288.     function _storeArray($elAry)
  289.     {
  290.         // where should we put this element...
  291.         if (is_array($this->_currentGroup&& ('group' != $elAry['type'])) {
  292.             $this->_currentGroup['elements'][$elAry;
  293.         elseif (isset($this->_currentSection)) {
  294.             $this->_ary['sections'][$this->_currentSection]['elements'][$elAry;
  295.         else {
  296.             $this->_ary['elements'][$elAry;
  297.         }
  298.     }
  299.  
  300.  
  301.    /**
  302.     * Sets a style to use for element rendering
  303.     *
  304.     * @param mixed      element name or array ('element name' => 'style name')
  305.     * @param string     style name if $elementName is not an array
  306.     * @access public
  307.     * @return void 
  308.     */
  309.     function setElementStyle($elementName$styleName = null)
  310.     {
  311.         if (is_array($elementName)) {
  312.             $this->_elementStyles array_merge($this->_elementStyles$elementName);
  313.         else {
  314.             $this->_elementStyles[$elementName$styleName;
  315.         }
  316.     }
  317. }
  318. ?>

Documentation generated on Mon, 11 Mar 2019 14:16:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.