2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
   5  * A renderer that makes it quick and easy to create customized forms.
 
   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      Jason Rust <jrust@rustyparts.com>
 
  18  * @copyright   2001-2011 The PHP Group
 
  19  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
 
  21  * @link        http://pear.php.net/package/HTML_QuickForm
 
  25  * A concrete renderer for HTML_QuickForm, based on QuickForm 2.x built-in one
 
  27 require_once 'HTML/QuickForm/Renderer/Default.php';
 
  30  * A renderer that makes it quick and easy to create customized forms.
 
  32  * This renderer has three main distinctives: an easy way to create
 
  33  * custom-looking forms, the ability to separate the creation of form
 
  34  * elements from their display, and being able to use QuickForm in
 
  35  * widget-based template systems.  See the online docs for more info.
 
  36  * For a usage example see: docs/renderers/QuickHtml_example.php
 
  39  * @package     HTML_QuickForm
 
  40  * @author      Jason Rust <jrust@rustyparts.com>
 
  41  * @version     Release: 3.2.16
 
  44 class HTML_QuickForm_Renderer_QuickHtml extends HTML_QuickForm_Renderer_Default {
 
  48      * The array of rendered elements
 
  51     var $renderedElements = array();
 
  62     function HTML_QuickForm_Renderer_QuickHtml()
 
  64         $this->HTML_QuickForm_Renderer_Default();
 
  65         // The default templates aren't used for this renderer
 
  66         $this->clearAllTemplates();
 
  73      * returns the HTML generated for the form
 
  75      * @param string $data (optional) Any extra data to put before the end of the form
 
  80     function toHtml($data = '')
 
  82         // Render any elements that haven't been rendered explicitly by elementToHtml()
 
  83         foreach (array_keys($this->renderedElements) as $key) {
 
  84             if (!$this->renderedElements[$key]['rendered']) {
 
  85                 $this->renderedElements[$key]['rendered'] = true;
 
  86                 $data .= $this->renderedElements[$key]['html'] . "\n";
 
  90         // Insert the extra data and form elements at the end of the form
 
  91         $this->_html = str_replace('</form>', $data . "\n</form>", $this->_html);
 
  96     // {{{ elementToHtml()
 
  99      * Gets the html for an element and marks it as rendered.
 
 101      * @param string $elementName The element name
 
 102      * @param string $elementValue (optional) The value of the element.  This is only useful
 
 103      *               for elements that have the same name (i.e. radio and checkbox), but
 
 107      * @return string The html for the QuickForm element
 
 108      * @throws HTML_QuickForm_Error
 
 110     function elementToHtml($elementName, $elementValue = null)
 
 113         // Find the key for the element
 
 114         foreach ($this->renderedElements as $key => $data) {
 
 115             if ($data['name'] == $elementName && 
 
 116                 // See if the value must match as well
 
 117                 (is_null($elementValue) ||
 
 118                  $data['value'] == $elementValue)) {
 
 124         if (is_null($elementKey)) {
 
 125             $msg = is_null($elementValue) ? "Element $elementName does not exist." : 
 
 126                 "Element $elementName with value of $elementValue does not exist.";
 
 127             return PEAR::raiseError(null, QUICKFORM_UNREGISTERED_ELEMENT, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true);
 
 129             if ($this->renderedElements[$elementKey]['rendered']) {
 
 130                 $msg = is_null($elementValue) ? "Element $elementName has already been rendered." : 
 
 131                     "Element $elementName with value of $elementValue has already been rendered.";
 
 132                 return PEAR::raiseError(null, QUICKFORM_ERROR, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true);
 
 134                 $this->renderedElements[$elementKey]['rendered'] = true;
 
 135                 return $this->renderedElements[$elementKey]['html'];
 
 138     } // end func elementToHtml
 
 141     // {{{ renderElement()
 
 144      * Gets the html for an element and adds it to the array by calling
 
 145      * parent::renderElement()
 
 147      * @param HTML_QuickForm_element    form element being visited
 
 148      * @param bool                      Whether an element is required
 
 149      * @param string                    An error message associated with an element
 
 152      * @return mixed HTML string of element if $immediateRender is set, else we just add the
 
 153      *               html to the global _html string 
 
 155     function renderElement(&$element, $required, $error)
 
 158         parent::renderElement($element, $required, $error);
 
 159         if (!$this->_inGroup) {
 
 160             $this->renderedElements[] = array(
 
 161                     'name' => $element->getName(), 
 
 162                     'value' => $element->getValue(), 
 
 163                     'html' => $this->_html, 
 
 164                     'rendered' => false);
 
 167     } // end func renderElement
 
 170     // {{{ renderHidden()
 
 173      * Gets the html for a hidden element and adds it to the array.
 
 175      * @param HTML_QuickForm_element    hidden form element being visited
 
 179     function renderHidden(&$element)
 
 181         $this->renderedElements[] = array(
 
 182                 'name' => $element->getName(), 
 
 183                 'value' => $element->getValue(), 
 
 184                 'html' => $element->toHtml(), 
 
 185                 'rendered' => false);
 
 186     } // end func renderHidden
 
 192      * Gets the html for the group element and adds it to the array by calling
 
 193      * parent::finishGroup()
 
 195      * @param    HTML_QuickForm_group   group being visited
 
 199     function finishGroup(&$group)
 
 202         parent::finishGroup($group);
 
 203         $this->renderedElements[] = array(
 
 204                 'name' => $group->getName(), 
 
 205                 'value' => $group->getValue(), 
 
 206                 'html' => $this->_html, 
 
 207                 'rendered' => false);
 
 209     } // end func finishGroup
 
 212 } // end class HTML_QuickForm_Renderer_QuickHtml