home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Array.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  8.8 KB  |  292 lines

  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. class HTML_QuickForm_Renderer_Array extends HTML_QuickForm_Renderer
  97. {
  98.    /**
  99.     * An array being generated
  100.     * @var array
  101.     */
  102.     var $_ary;
  103.  
  104.    /**
  105.     * Number of sections in the form (i.e. number of headers in it)
  106.     * @var integer
  107.     */
  108.     var $_sectionCount;
  109.  
  110.    /**
  111.     * Current section number
  112.     * @var integer
  113.     */
  114.     var $_currentSection;
  115.  
  116.    /**
  117.     * Array representing current group
  118.     * @var array
  119.     */
  120.     var $_currentGroup = null;
  121.  
  122.    /**
  123.     * Additional style information for different elements  
  124.     * @var array
  125.     */
  126.     var $_elementStyles = array();
  127.  
  128.    /**
  129.     * true: collect all hidden elements into string; false: process them as usual form elements
  130.     * @var bool
  131.     */
  132.     var $_collectHidden = false;
  133.  
  134.    /**
  135.     * Constructor
  136.     *
  137.     * @param  bool    true: collect all hidden elements into string; false: process them as usual form elements
  138.     * @access public
  139.     */
  140.     function HTML_QuickForm_Renderer_Array($collectHidden = false)
  141.     {
  142.         $this->HTML_QuickForm_Renderer();
  143.         $this->_collectHidden = $collectHidden;
  144.     } // end constructor
  145.  
  146.  
  147.    /**
  148.     * Returns the resultant array
  149.     * 
  150.     * @access public
  151.     * @return array
  152.     */
  153.     function toArray()
  154.     {
  155.         return $this->_ary;
  156.     }
  157.  
  158.  
  159.     function startForm(&$form)
  160.     {
  161.         $this->_ary = array(
  162.             'frozen'            => $form->isFrozen(),
  163.             'javascript'        => $form->getValidationScript(),
  164.             'attributes'        => $form->getAttributes(true),
  165.             'requirednote'      => $form->getRequiredNote(),
  166.             'errors'            => array()
  167.         );
  168.         if ($this->_collectHidden) {
  169.             $this->_ary['hidden'] = '';
  170.         }
  171.         $this->_elementIdx     = 1;
  172.         $this->_currentSection = null;
  173.         $this->_sectionCount   = 0;
  174.     } // end func startForm
  175.  
  176.  
  177.     function renderHeader(&$header)
  178.     {
  179.         $this->_ary['sections'][$this->_sectionCount] = array('header' => $header->toHtml());
  180.         $this->_currentSection = $this->_sectionCount++;
  181.     } // end func renderHeader
  182.  
  183.  
  184.     function renderElement(&$element, $required, $error)
  185.     {
  186.         $elAry = $this->_elementToArray($element, $required, $error);
  187.         if (!empty($error)) {
  188.             $this->_ary['errors'][$elAry['name']] = $error;
  189.         }
  190.         $this->_storeArray($elAry);
  191.     } // end func renderElement
  192.  
  193.  
  194.     function renderHidden(&$element)
  195.     {
  196.         if ($this->_collectHidden) {
  197.             $this->_ary['hidden'] .= $element->toHtml() . "\n";
  198.         } else {
  199.             $this->renderElement($element, false, null);
  200.         }
  201.     } // end func renderHidden
  202.  
  203.  
  204.     function startGroup(&$group, $required, $error)
  205.     {
  206.         $this->_currentGroup = $this->_elementToArray($group, $required, $error);
  207.         if (!empty($error)) {
  208.             $this->_ary['errors'][$this->_currentGroup['name']] = $error;
  209.         }
  210.     } // end func startGroup
  211.  
  212.  
  213.     function finishGroup(&$group)
  214.     {
  215.         $this->_storeArray($this->_currentGroup);
  216.         $this->_currentGroup = null;
  217.     } // end func finishGroup
  218.  
  219.  
  220.    /**
  221.     * Creates an array representing an element
  222.     * 
  223.     * @access private
  224.     * @param  object    An HTML_QuickForm_element object
  225.     * @param  bool      Whether an element is required
  226.     * @param  string    Error associated with the element
  227.     * @return array
  228.     */
  229.     function _elementToArray(&$element, $required, $error)
  230.     {
  231.         $ret = array(
  232.             'name'      => $element->getName(),
  233.             'value'     => $element->getValue(),
  234.             'type'      => $element->getType(),
  235.             'frozen'    => $element->isFrozen(),
  236.             'label'     => $element->getLabel(),
  237.             'required'  => $required,
  238.             'error'     => $error
  239.         );
  240.         // set the style for the element
  241.         if (isset($this->_elementStyles[$ret['name']])) {
  242.             $ret['style'] = $this->_elementStyles[$ret['name']];
  243.         }
  244.         if ('group' == $ret['type']) {
  245.             $ret['separator'] = $element->_separator;
  246.             $ret['elements']  = array();
  247.         } else {
  248.             $ret['html']      = $element->toHtml();
  249.         }
  250.         return $ret;
  251.     }
  252.  
  253.  
  254.    /**
  255.     * Stores an array representation of an element in the form array
  256.     * 
  257.     * @access private
  258.     * @param array  Array representation of an element
  259.     * @return void
  260.     */
  261.     function _storeArray($elAry)
  262.     {
  263.         // where should we put this element...
  264.         if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) {
  265.             $this->_currentGroup['elements'][] = $elAry;
  266.         } elseif (isset($this->_currentSection)) {
  267.             $this->_ary['sections'][$this->_currentSection]['elements'][] = $elAry;
  268.         } else {
  269.             $this->_ary['elements'][] = $elAry;
  270.         }
  271.     }
  272.  
  273.  
  274.    /**
  275.     * Sets a style to use for element rendering
  276.     * 
  277.     * @param mixed      element name or array ('element name' => 'style name')
  278.     * @param string     style name if $elementName is not an array
  279.     * @access public
  280.     * @return void
  281.     */
  282.     function setElementStyle($elementName, $styleName = null)
  283.     {
  284.         if (is_array($elementName)) {
  285.             $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
  286.         } else {
  287.             $this->_elementStyles[$elementName] = $styleName;
  288.         }
  289.     }
  290. }
  291. ?>
  292.