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

Source for file QuickHtml.php

Documentation is available at QuickHtml.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: Jason Rust <jrust@rustyparts.com>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: QuickHtml.php,v 1.1 2003/08/25 16:41:02 jrust Exp $
  20.  
  21. require_once('HTML/QuickForm/Renderer/Default.php');
  22.  
  23. /**
  24.  * A renderer that makes it quick and easy to create customized forms.
  25.  *
  26.  * This renderer has three main distinctives: an easy way to create
  27.  * custom-looking forms, the ability to separate the creation of form
  28.  * elements from their display, and being able to use QuickForm in
  29.  * widget-based template systems.  See the online docs for more info.
  30.  * For a usage example see: docs/renderers/QuickHtml_example.php
  31.  * 
  32.  * @access public
  33.  * @package QuickForm
  34.  */
  35.     // {{{ properties
  36.  
  37.     /**
  38.      * The array of rendered elements
  39.      * @var array 
  40.      */
  41.     var $renderedElements = array();
  42.  
  43.     // }}}
  44.     // {{{ constructor
  45.     
  46.     /**
  47.      * Constructor
  48.      *
  49.      * @access public
  50.      * @return void 
  51.      */
  52.     {
  53.         $this->HTML_QuickForm_Renderer_Default();
  54.         // The default templates aren't used for this renderer
  55.         $this->clearAllTemplates();
  56.     // end constructor
  57.  
  58.     // }}}
  59.     // {{{ toHtml()
  60.  
  61.     /**
  62.      * returns the HTML generated for the form
  63.      *
  64.      * @param string $data (optional) Any extra data to put before the end of the form
  65.      *
  66.      * @access public
  67.      * @return string 
  68.      */
  69.     function toHtml($data '')
  70.     {
  71.         // Render any elements that haven't been rendered explicitly by elementToHtml()
  72.         foreach (array_keys($this->renderedElementsas $key{
  73.             if (!$this->renderedElements[$key]['rendered']{
  74.                 $this->renderedElements[$key]['rendered'= true;
  75.                 $data .= $this->renderedElements[$key]['html'"\n";
  76.             }
  77.         }
  78.  
  79.         // Insert the extra data and form elements at the end of the form
  80.         $this->_html str_replace('</form>'$data "\n</form>"$this->_html);
  81.         return $this->_html;
  82.     // end func toHtml
  83.  
  84.     // }}}
  85.     // {{{ elementToHtml()
  86.  
  87.     /**
  88.      * Gets the html for an element and marks it as rendered.
  89.      *
  90.      * @param string $elementName The element name
  91.      * @param string $elementValue (optional) The value of the element.  This is only useful
  92.      *                for elements that have the same name (i.e. radio and checkbox), but
  93.      *                different values
  94.      *
  95.      * @access public
  96.      * @return string The html for the QuickForm element
  97.      */
  98.     function elementToHtml($elementName$elementValue = null)
  99.     {
  100.         $elementKey = null;
  101.         // Find the key for the element
  102.         foreach ($this->renderedElements as $key => $data{
  103.             if ($data['name'== $elementName && 
  104.                 // See if the value must match as well
  105.                 (is_null($elementValue||
  106.                  $data['value'== $elementValue)) {
  107.                 $elementKey $key;
  108.                 break;
  109.             }
  110.         }
  111.  
  112.         if (is_null($elementKey)) {
  113.             $msg is_null($elementValue? "Element $elementName does not exist." : 
  114.                 "Element $elementName with value of $elementValue does not exist.";
  115.             return PEAR::raiseError(nullQUICKFORM_UNREGISTERED_ELEMENTnullE_USER_WARNING$msg'HTML_QuickForm_Error'true);
  116.         else {
  117.             if ($this->renderedElements[$elementKey]['rendered']{
  118.                 $msg is_null($elementValue? "Element $elementName has already been rendered." : 
  119.                     "Element $elementName with value of $elementValue has already been rendered.";
  120.                 return PEAR::raiseError(nullQUICKFORM_ERRORnullE_USER_WARNING$msg'HTML_QuickForm_Error'true);
  121.             else {
  122.                 $this->renderedElements[$elementKey]['rendered'= true;
  123.                 return $this->renderedElements[$elementKey]['html'];
  124.             }
  125.         }
  126.     // end func elementToHtml
  127.  
  128.     // }}}
  129.     // {{{ renderElement()
  130.  
  131.     /**
  132.      * Gets the html for an element and adds it to the array by calling
  133.      * parent::renderElement()
  134.      *
  135.      * @param object     An HTML_QuickForm_element object
  136.      * @param bool       Whether an element is required
  137.      * @param string     An error message associated with an element
  138.      *
  139.      * @access public
  140.      * @return mixed HTML string of element if $immediateRender is set, else we just add the
  141.      *                html to the global _html string
  142.      */
  143.     function renderElement(&$element$required$error)
  144.     {
  145.         $this->_html '';
  146.         parent::renderElement($element$required$error);
  147.         if (!$this->_inGroup{
  148.             $this->renderedElements[= array(
  149.                     'name' => $element->getName()
  150.                     'value' => $element->getValue()
  151.                     'html' => $this->_html
  152.                     'rendered' => false);
  153.         }
  154.         $this->_html '';
  155.     // end func renderElement
  156.  
  157.     // }}}
  158.     // {{{ renderHidden()
  159.  
  160.     /**
  161.      * Gets the html for a hidden element and adds it to the array.
  162.      * 
  163.      * @param object     An HTML_QuickForm_hidden object being visited
  164.      * @access public
  165.      * @return void 
  166.      */
  167.     function renderHidden(&$element)
  168.     {
  169.         $this->renderedElements[= array(
  170.                 'name' => $element->getName()
  171.                 'value' => $element->getValue()
  172.                 'html' => $element->toHtml()
  173.                 'rendered' => false);
  174.     // end func renderHidden
  175.     
  176.     // }}}
  177.     // {{{ finishGroup()
  178.  
  179.     /**
  180.      * Gets the html for the group element and adds it to the array by calling
  181.      * parent::finishGroup()
  182.      *
  183.      * @param    object      An HTML_QuickForm_group object being visited
  184.      * @access   public
  185.      * @return   void 
  186.      */
  187.     function finishGroup(&$group)
  188.     {
  189.         $this->_html '';
  190.         parent::finishGroup($group);
  191.         $this->renderedElements[= array(
  192.                 'name' => $group->getName()
  193.                 'value' => $group->getValue()
  194.                 'html' => $this->_html
  195.                 'rendered' => false);
  196.         $this->_html '';
  197.     // end func finishGroup
  198.  
  199.     // }}}
  200. // end class HTML_QuickForm_Renderer_QuickHtml
  201. ?>

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