2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
   5  * A concrete renderer for HTML_QuickForm, makes an array of form contents
 
   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.
 
  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-2011 The PHP Group
 
  22  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
 
  24  * @link        http://pear.php.net/package/HTML_QuickForm
 
  28  * An abstract base class for QuickForm renderers
 
  30 require_once 'HTML/QuickForm/Renderer.php';
 
  33  * A concrete renderer for HTML_QuickForm, makes an array of form contents
 
  35  * Based on old HTML_QuickForm::toArray() code.
 
  37  * The form array structure is the following:
 
  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:
 
  48  *     '1st element name' => 'Error for the 1st element',
 
  50  *     'nth element name' => 'Error for the nth element'
 
  52  *   // if there are no headers in the form:
 
  53  *   'elements' => array(
 
  58  *   // if there are headers in the form:
 
  59  *   'sections' => array(
 
  61  *       'header'   => 'Header text for the first header',
 
  62  *       'name'     => 'Header name for the first header',
 
  63  *       'elements' => array(
 
  71  *       'header'   => 'Header text for the Mth header',
 
  72  *       'name'     => 'Header name for the Mth header',
 
  73  *       'elements' => array(
 
  83  * where element_i is an array of the form:
 
  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(
 
 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.16
 
 115 class HTML_QuickForm_Renderer_Array extends HTML_QuickForm_Renderer
 
 121     * An array being generated
 
 127     * Number of sections in the form (i.e. number of headers in it)
 
 133     * Current section number
 
 136     var $_currentSection;
 
 139     * Array representing current group
 
 142     var $_currentGroup = null;
 
 145     * Additional style information for different elements
 
 148     var $_elementStyles = array();
 
 151     * true: collect all hidden elements into string; false: process them as usual form elements
 
 154     var $_collectHidden = false;
 
 157     * true:  render an array of labels to many labels, $key 0 named 'label', the rest "label_$key"
 
 158     * false: leave labels as defined
 
 161     var $_staticLabels = false;
 
 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"
 
 171     function HTML_QuickForm_Renderer_Array($collectHidden = false, $staticLabels = false)
 
 173         $this->HTML_QuickForm_Renderer();
 
 174         $this->_collectHidden = $collectHidden;
 
 175         $this->_staticLabels  = $staticLabels;
 
 180     * Returns the resultant array
 
 191     function startForm(&$form)
 
 194             'frozen'            => $form->isFrozen(),
 
 195             'javascript'        => $form->getValidationScript(),
 
 196             'attributes'        => $form->getAttributes(true),
 
 197             'requirednote'      => $form->getRequiredNote(),
 
 200         if ($this->_collectHidden) {
 
 201             $this->_ary['hidden'] = '';
 
 203         $this->_elementIdx     = 1;
 
 204         $this->_currentSection = null;
 
 205         $this->_sectionCount   = 0;
 
 206     } // end func startForm
 
 209     function renderHeader(&$header)
 
 211         $this->_ary['sections'][$this->_sectionCount] = array(
 
 212             'header' => $header->toHtml(),
 
 213             'name'   => $header->getName()
 
 215         $this->_currentSection = $this->_sectionCount++;
 
 216     } // end func renderHeader
 
 219     function renderElement(&$element, $required, $error)
 
 221         $elAry = $this->_elementToArray($element, $required, $error);
 
 222         if (!empty($error)) {
 
 223             $this->_ary['errors'][$elAry['name']] = $error;
 
 225         $this->_storeArray($elAry);
 
 226     } // end func renderElement
 
 229     function renderHidden(&$element)
 
 231         if ($this->_collectHidden) {
 
 232             $this->_ary['hidden'] .= $element->toHtml() . "\n";
 
 234             $this->renderElement($element, false, null);
 
 236     } // end func renderHidden
 
 239     function startGroup(&$group, $required, $error)
 
 241         $this->_currentGroup = $this->_elementToArray($group, $required, $error);
 
 242         if (!empty($error)) {
 
 243             $this->_ary['errors'][$this->_currentGroup['name']] = $error;
 
 245     } // end func startGroup
 
 248     function finishGroup(&$group)
 
 250         $this->_storeArray($this->_currentGroup);
 
 251         $this->_currentGroup = null;
 
 252     } // end func finishGroup
 
 256     * Creates an array representing an element
 
 259     * @param  HTML_QuickForm_element    element being processed
 
 260     * @param  bool                      Whether an element is required
 
 261     * @param  string                    Error associated with the element
 
 264     function _elementToArray(&$element, $required, $error)
 
 267             'name'      => $element->getName(),
 
 268             'value'     => $element->getValue(),
 
 269             'type'      => $element->getType(),
 
 270             'frozen'    => $element->isFrozen(),
 
 271             'required'  => $required,
 
 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;
 
 280                     $ret['label'] = $label;
 
 282                     $ret['label_' . $key] = $label;
 
 286             $ret['label'] = $labels;
 
 289         // set the style for the element
 
 290         if (isset($this->_elementStyles[$ret['name']])) {
 
 291             $ret['style'] = $this->_elementStyles[$ret['name']];
 
 293         if ('group' == $ret['type']) {
 
 294             $ret['separator'] = $element->_separator;
 
 295             $ret['elements']  = array();
 
 297             $ret['html']      = $element->toHtml();
 
 304     * Stores an array representation of an element in the form array
 
 307     * @param array  Array representation of an element
 
 310     function _storeArray($elAry)
 
 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;
 
 318             $this->_ary['elements'][] = $elAry;
 
 324     * Sets a style to use for element rendering
 
 326     * @param mixed      element name or array ('element name' => 'style name')
 
 327     * @param string     style name if $elementName is not an array
 
 331     function setElementStyle($elementName, $styleName = null)
 
 333         if (is_array($elementName)) {
 
 334             $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
 
 336             $this->_elementStyles[$elementName] = $styleName;