home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / QuickHtml.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  7.3 KB  |  204 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: 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. class HTML_QuickForm_Renderer_QuickHtml extends HTML_QuickForm_Renderer_Default {
  36.     // {{{ properties
  37.  
  38.     /**
  39.      * The array of rendered elements
  40.      * @var array
  41.      */
  42.     var $renderedElements = array();
  43.  
  44.     // }}}
  45.     // {{{ constructor
  46.     
  47.     /**
  48.      * Constructor
  49.      *
  50.      * @access public
  51.      * @return void
  52.      */
  53.     function HTML_QuickForm_Renderer_QuickHtml()
  54.     {
  55.         $this->HTML_QuickForm_Renderer_Default();
  56.         // The default templates aren't used for this renderer
  57.         $this->clearAllTemplates();
  58.     } // end constructor
  59.  
  60.     // }}}
  61.     // {{{ toHtml()
  62.  
  63.     /**
  64.      * returns the HTML generated for the form
  65.      *
  66.      * @param string $data (optional) Any extra data to put before the end of the form
  67.      *
  68.      * @access public
  69.      * @return string
  70.      */
  71.     function toHtml($data = '')
  72.     {
  73.         // Render any elements that haven't been rendered explicitly by elementToHtml()
  74.         foreach (array_keys($this->renderedElements) as $key) {
  75.             if (!$this->renderedElements[$key]['rendered']) {
  76.                 $this->renderedElements[$key]['rendered'] = true;
  77.                 $data .= $this->renderedElements[$key]['html'] . "\n";
  78.             }
  79.         }
  80.  
  81.         // Insert the extra data and form elements at the end of the form
  82.         $this->_html = str_replace('</form>', $data . "\n</form>", $this->_html);
  83.         return $this->_html;
  84.     } // end func toHtml
  85.  
  86.     // }}}
  87.     // {{{ elementToHtml()
  88.  
  89.     /**
  90.      * Gets the html for an element and marks it as rendered.
  91.      *
  92.      * @param string $elementName The element name
  93.      * @param string $elementValue (optional) The value of the element.  This is only useful
  94.      *               for elements that have the same name (i.e. radio and checkbox), but
  95.      *               different values
  96.      *
  97.      * @access public
  98.      * @return string The html for the QuickForm element
  99.      */
  100.     function elementToHtml($elementName, $elementValue = null)
  101.     {
  102.         $elementKey = null;
  103.         // Find the key for the element
  104.         foreach ($this->renderedElements as $key => $data) {
  105.             if ($data['name'] == $elementName && 
  106.                 // See if the value must match as well
  107.                 (is_null($elementValue) ||
  108.                  $data['value'] == $elementValue)) {
  109.                 $elementKey = $key;
  110.                 break;
  111.             }
  112.         }
  113.  
  114.         if (is_null($elementKey)) {
  115.             $msg = is_null($elementValue) ? "Element $elementName does not exist." : 
  116.                 "Element $elementName with value of $elementValue does not exist.";
  117.             return PEAR::raiseError(null, QUICKFORM_UNREGISTERED_ELEMENT, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true);
  118.         } else {
  119.             if ($this->renderedElements[$elementKey]['rendered']) {
  120.                 $msg = is_null($elementValue) ? "Element $elementName has already been rendered." : 
  121.                     "Element $elementName with value of $elementValue has already been rendered.";
  122.                 return PEAR::raiseError(null, QUICKFORM_ERROR, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true);
  123.             } else {
  124.                 $this->renderedElements[$elementKey]['rendered'] = true;
  125.                 return $this->renderedElements[$elementKey]['html'];
  126.             }
  127.         }
  128.     } // end func elementToHtml
  129.  
  130.     // }}}
  131.     // {{{ renderElement()
  132.  
  133.     /**
  134.      * Gets the html for an element and adds it to the array by calling
  135.      * parent::renderElement()
  136.      *
  137.      * @param object     An HTML_QuickForm_element object
  138.      * @param bool       Whether an element is required
  139.      * @param string     An error message associated with an element
  140.      *
  141.      * @access public
  142.      * @return mixed HTML string of element if $immediateRender is set, else we just add the
  143.      *               html to the global _html string 
  144.      */
  145.     function renderElement(&$element, $required, $error)
  146.     {
  147.         $this->_html = '';
  148.         parent::renderElement($element, $required, $error);
  149.         if (!$this->_inGroup) {
  150.             $this->renderedElements[] = array(
  151.                     'name' => $element->getName(), 
  152.                     'value' => $element->getValue(), 
  153.                     'html' => $this->_html, 
  154.                     'rendered' => false);
  155.         }
  156.         $this->_html = '';
  157.     } // end func renderElement
  158.  
  159.     // }}}
  160.     // {{{ renderHidden()
  161.  
  162.     /**
  163.      * Gets the html for a hidden element and adds it to the array.
  164.      * 
  165.      * @param object     An HTML_QuickForm_hidden object being visited
  166.      * @access public
  167.      * @return void
  168.      */
  169.     function renderHidden(&$element)
  170.     {
  171.         $this->renderedElements[] = array(
  172.                 'name' => $element->getName(), 
  173.                 'value' => $element->getValue(), 
  174.                 'html' => $element->toHtml(), 
  175.                 'rendered' => false);
  176.     } // end func renderHidden
  177.     
  178.     // }}}
  179.     // {{{ finishGroup()
  180.  
  181.     /**
  182.      * Gets the html for the group element and adds it to the array by calling
  183.      * parent::finishGroup()
  184.      *
  185.      * @param    object      An HTML_QuickForm_group object being visited
  186.      * @access   public
  187.      * @return   void
  188.      */
  189.     function finishGroup(&$group)
  190.     {
  191.         $this->_html = '';
  192.         parent::finishGroup($group);
  193.         $this->renderedElements[] = array(
  194.                 'name' => $group->getName(), 
  195.                 'value' => $group->getValue(), 
  196.                 'html' => $this->_html, 
  197.                 'rendered' => false);
  198.         $this->_html = '';
  199.     } // end func finishGroup
  200.  
  201.     // }}}
  202. } // end class HTML_QuickForm_Renderer_QuickHtml
  203. ?>
  204.