home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm / Renderer / QuickHtml.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  7.4 KB  |  214 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A renderer that makes it quick and easy to create customized forms.
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  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.
  14.  *
  15.  * @category    HTML
  16.  * @package     HTML_QuickForm
  17.  * @author      Jason Rust <jrust@rustyparts.com>
  18.  * @copyright   2001-2007 The PHP Group
  19.  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
  20.  * @version     CVS: $Id: QuickHtml.php,v 1.2 2007/05/29 18:34:36 avb Exp $
  21.  * @link        http://pear.php.net/package/HTML_QuickForm
  22.  */
  23.  
  24. /**
  25.  * A concrete renderer for HTML_QuickForm, based on QuickForm 2.x built-in one
  26.  */ 
  27. require_once 'HTML/QuickForm/Renderer/Default.php';
  28.  
  29. /**
  30.  * A renderer that makes it quick and easy to create customized forms.
  31.  *
  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
  37.  * 
  38.  * @category    HTML
  39.  * @package     HTML_QuickForm
  40.  * @author      Jason Rust <jrust@rustyparts.com>
  41.  * @version     Release: 3.2.10
  42.  * @since       3.1.1
  43.  */
  44. class HTML_QuickForm_Renderer_QuickHtml extends HTML_QuickForm_Renderer_Default {
  45.     // {{{ properties
  46.  
  47.     /**
  48.      * The array of rendered elements
  49.      * @var array
  50.      */
  51.     var $renderedElements = array();
  52.  
  53.     // }}}
  54.     // {{{ constructor
  55.     
  56.     /**
  57.      * Constructor
  58.      *
  59.      * @access public
  60.      * @return void
  61.      */
  62.     function HTML_QuickForm_Renderer_QuickHtml()
  63.     {
  64.         $this->HTML_QuickForm_Renderer_Default();
  65.         // The default templates aren't used for this renderer
  66.         $this->clearAllTemplates();
  67.     } // end constructor
  68.  
  69.     // }}}
  70.     // {{{ toHtml()
  71.  
  72.     /**
  73.      * returns the HTML generated for the form
  74.      *
  75.      * @param string $data (optional) Any extra data to put before the end of the form
  76.      *
  77.      * @access public
  78.      * @return string
  79.      */
  80.     function toHtml($data = '')
  81.     {
  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";
  87.             }
  88.         }
  89.  
  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);
  92.         return $this->_html;
  93.     } // end func toHtml
  94.  
  95.     // }}}
  96.     // {{{ elementToHtml()
  97.  
  98.     /**
  99.      * Gets the html for an element and marks it as rendered.
  100.      *
  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
  104.      *               different values
  105.      *
  106.      * @access public
  107.      * @return string The html for the QuickForm element
  108.      * @throws HTML_QuickForm_Error
  109.      */
  110.     function elementToHtml($elementName, $elementValue = null)
  111.     {
  112.         $elementKey = 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)) {
  119.                 $elementKey = $key;
  120.                 break;
  121.             }
  122.         }
  123.  
  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);
  128.         } else {
  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);
  133.             } else {
  134.                 $this->renderedElements[$elementKey]['rendered'] = true;
  135.                 return $this->renderedElements[$elementKey]['html'];
  136.             }
  137.         }
  138.     } // end func elementToHtml
  139.  
  140.     // }}}
  141.     // {{{ renderElement()
  142.  
  143.     /**
  144.      * Gets the html for an element and adds it to the array by calling
  145.      * parent::renderElement()
  146.      *
  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
  150.      *
  151.      * @access public
  152.      * @return mixed HTML string of element if $immediateRender is set, else we just add the
  153.      *               html to the global _html string 
  154.      */
  155.     function renderElement(&$element, $required, $error)
  156.     {
  157.         $this->_html = '';
  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);
  165.         }
  166.         $this->_html = '';
  167.     } // end func renderElement
  168.  
  169.     // }}}
  170.     // {{{ renderHidden()
  171.  
  172.     /**
  173.      * Gets the html for a hidden element and adds it to the array.
  174.      * 
  175.      * @param HTML_QuickForm_element    hidden form element being visited
  176.      * @access public
  177.      * @return void
  178.      */
  179.     function renderHidden(&$element)
  180.     {
  181.         $this->renderedElements[] = array(
  182.                 'name' => $element->getName(), 
  183.                 'value' => $element->getValue(), 
  184.                 'html' => $element->toHtml(), 
  185.                 'rendered' => false);
  186.     } // end func renderHidden
  187.     
  188.     // }}}
  189.     // {{{ finishGroup()
  190.  
  191.     /**
  192.      * Gets the html for the group element and adds it to the array by calling
  193.      * parent::finishGroup()
  194.      *
  195.      * @param    HTML_QuickForm_group   group being visited
  196.      * @access   public
  197.      * @return   void
  198.      */
  199.     function finishGroup(&$group)
  200.     {
  201.         $this->_html = '';
  202.         parent::finishGroup($group);
  203.         $this->renderedElements[] = array(
  204.                 'name' => $group->getName(), 
  205.                 'value' => $group->getValue(), 
  206.                 'html' => $this->_html, 
  207.                 'rendered' => false);
  208.         $this->_html = '';
  209.     } // end func finishGroup
  210.  
  211.     // }}}
  212. } // end class HTML_QuickForm_Renderer_QuickHtml
  213. ?>
  214.