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 / Object.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  11.5 KB  |  462 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A concrete renderer for HTML_QuickForm, makes an object from form contents
  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      Ron McClain <ron@humaniq.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: Object.php,v 1.5 2007/05/29 18:34:36 avb Exp $
  21.  * @link        http://pear.php.net/package/HTML_QuickForm
  22.  */
  23.  
  24. /**
  25.  * An abstract base class for QuickForm renderers
  26.  */
  27. require_once 'HTML/QuickForm/Renderer.php';
  28.  
  29. /**
  30.  * A concrete renderer for HTML_QuickForm, makes an object from form contents
  31.  *
  32.  * Based on HTML_Quickform_Renderer_Array code
  33.  *
  34.  * @category    HTML
  35.  * @package     HTML_QuickForm
  36.  * @author      Ron McClain <ron@humaniq.com>
  37.  * @version     Release: 3.2.10
  38.  * @since       3.1.1
  39.  */
  40. class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer
  41. {
  42.    /**#@+
  43.     * @access private
  44.     */
  45.     /**
  46.      * The object being generated
  47.      * @var QuickformForm
  48.      */
  49.     var $_obj= null;
  50.  
  51.     /**
  52.      * Number of sections in the form (i.e. number of headers in it)
  53.      * @var integer $_sectionCount
  54.      */
  55.     var $_sectionCount;
  56.  
  57.     /**
  58.     * Current section number
  59.     * @var integer $_currentSection
  60.     */
  61.     var $_currentSection;
  62.  
  63.     /**
  64.     * Object representing current group
  65.     * @var object $_currentGroup
  66.     */
  67.     var $_currentGroup = null;
  68.  
  69.     /**
  70.      * Class of Element Objects
  71.      * @var object $_elementType
  72.      */
  73.     var $_elementType = 'QuickFormElement';
  74.  
  75.     /**
  76.     * Additional style information for different elements  
  77.     * @var array $_elementStyles
  78.     */
  79.     var $_elementStyles = array();
  80.  
  81.     /**
  82.     * true: collect all hidden elements into string; false: process them as usual form elements
  83.     * @var bool $_collectHidden
  84.     */
  85.     var $_collectHidden = false;
  86.    /**#@-*/
  87.  
  88.  
  89.     /**
  90.      * Constructor
  91.      *
  92.      * @param bool    true: collect all hidden elements
  93.      * @access public
  94.      */
  95.     function HTML_QuickForm_Renderer_Object($collecthidden = false) 
  96.     {
  97.         $this->HTML_QuickForm_Renderer();
  98.         $this->_collectHidden = $collecthidden;
  99.         $this->_obj = new QuickformForm;
  100.     }
  101.  
  102.     /**
  103.      * Return the rendered Object
  104.      * @access public
  105.      */
  106.     function toObject() 
  107.     {
  108.         return $this->_obj;
  109.     }
  110.  
  111.     /**
  112.      * Set the class of the form elements.  Defaults to QuickformElement.
  113.      * @param string   Name of element class
  114.      * @access public
  115.      */
  116.     function setElementType($type)
  117.     {
  118.         $this->_elementType = $type;
  119.     }
  120.  
  121.     function startForm(&$form) 
  122.     {
  123.         $this->_obj->frozen = $form->isFrozen();
  124.         $this->_obj->javascript = $form->getValidationScript();
  125.         $this->_obj->attributes = $form->getAttributes(true);
  126.         $this->_obj->requirednote = $form->getRequiredNote();
  127.         $this->_obj->errors = new StdClass;
  128.  
  129.         if($this->_collectHidden) {
  130.             $this->_obj->hidden = '';
  131.         }
  132.         $this->_elementIdx = 1;
  133.         $this->_currentSection = null;
  134.         $this->_sectionCount = 0;
  135.     } // end func startForm
  136.  
  137.     function renderHeader(&$header) 
  138.     {
  139.         $hobj = new StdClass;
  140.         $hobj->header = $header->toHtml();
  141.         $this->_obj->sections[$this->_sectionCount] = $hobj;
  142.         $this->_currentSection = $this->_sectionCount++;
  143.     }
  144.  
  145.     function renderElement(&$element, $required, $error) 
  146.     {
  147.         $elObj = $this->_elementToObject($element, $required, $error);
  148.         if(!empty($error)) {
  149.             $name = $elObj->name;
  150.             $this->_obj->errors->$name = $error;
  151.         }
  152.         $this->_storeObject($elObj);
  153.     } // end func renderElement
  154.  
  155.     function renderHidden(&$element)
  156.     {
  157.         if($this->_collectHidden) {
  158.             $this->_obj->hidden .= $element->toHtml() . "\n";
  159.         } else {
  160.             $this->renderElement($element, false, null);
  161.         }
  162.     } //end func renderHidden
  163.  
  164.     function startGroup(&$group, $required, $error) 
  165.     {
  166.         $this->_currentGroup = $this->_elementToObject($group, $required, $error);
  167.         if(!empty($error)) {
  168.             $name = $this->_currentGroup->name;
  169.             $this->_obj->errors->$name = $error;
  170.         }
  171.     } // end func startGroup
  172.  
  173.     function finishGroup(&$group) 
  174.     {
  175.         $this->_storeObject($this->_currentGroup);
  176.         $this->_currentGroup = null;
  177.     } // end func finishGroup
  178.  
  179.     /**
  180.      * Creates an object representing an element
  181.      *
  182.      * @access private
  183.      * @param HTML_QuickForm_element    form element being rendered
  184.      * @param required bool         Whether an element is required
  185.      * @param error string    Error associated with the element
  186.      * @return object
  187.      */
  188.     function _elementToObject(&$element, $required, $error) 
  189.     {
  190.         if($this->_elementType) {
  191.             $ret = new $this->_elementType;
  192.         }
  193.         $ret->name = $element->getName();
  194.         $ret->value = $element->getValue();
  195.         $ret->type = $element->getType();
  196.         $ret->frozen = $element->isFrozen();
  197.         $labels = $element->getLabel();
  198.         if (is_array($labels)) {
  199.             $ret->label = array_shift($labels);
  200.             foreach ($labels as $key => $label) {
  201.                 $key = is_int($key)? $key + 2: $key;
  202.                 $ret->{'label_' . $key} = $label;
  203.             }
  204.         } else {
  205.             $ret->label = $labels;
  206.         }
  207.         $ret->required = $required;
  208.         $ret->error = $error;
  209.  
  210.         if(isset($this->_elementStyles[$ret->name])) {
  211.             $ret->style = $this->_elementStyles[$ret->name];
  212.             $ret->styleTemplate = "styles/". $ret->style .".html";
  213.         }
  214.         if($ret->type == 'group') {
  215.             $ret->separator = $element->_separator;
  216.             $ret->elements = array();
  217.         } else {
  218.             $ret->html = $element->toHtml();
  219.         }
  220.         return $ret;
  221.     }
  222.  
  223.     /** 
  224.      * Stores an object representation of an element in the form array
  225.      *
  226.      * @access private
  227.      * @param QuickformElement     Object representation of an element
  228.      * @return void
  229.      */
  230.     function _storeObject($elObj) 
  231.     {
  232.         $name = $elObj->name;
  233.         if(is_object($this->_currentGroup) && $elObj->type != 'group') {
  234.             $this->_currentGroup->elements[] = $elObj;
  235.         } elseif (isset($this->_currentSection)) {
  236.             $this->_obj->sections[$this->_currentSection]->elements[] = $elObj;
  237.         } else {
  238.             $this->_obj->elements[] = $elObj;
  239.         }
  240.     }
  241.  
  242.     function setElementStyle($elementName, $styleName = null)
  243.     {
  244.         if(is_array($elementName)) {
  245.             $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
  246.         } else {
  247.             $this->_elementStyles[$elementName] = $styleName;
  248.         }
  249.     }
  250.  
  251. } // end class HTML_QuickForm_Renderer_Object
  252.  
  253.  
  254.  
  255. /**
  256.  * Convenience class for the form object passed to outputObject()
  257.  * 
  258.  * Eg.
  259.  * <pre>  
  260.  * {form.outputJavaScript():h}
  261.  * {form.outputHeader():h}
  262.  *   <table>
  263.  *     <tr>
  264.  *       <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
  265.  *     </tr>
  266.  *   </table>
  267.  * </form>
  268.  * </pre>
  269.  * 
  270.  * @category    HTML
  271.  * @package     HTML_QuickForm
  272.  * @author      Ron McClain <ron@humaniq.com>
  273.  * @version     Release: 3.2.10
  274.  * @since       3.1.1
  275.  */
  276. class QuickformForm
  277. {
  278.    /**
  279.     * Whether the form has been frozen
  280.     * @var boolean $frozen
  281.     */
  282.     var $frozen;
  283.  
  284.    /**
  285.     * Javascript for client-side validation
  286.     * @var string $javascript
  287.     */
  288.     var $javascript;
  289.  
  290.    /**
  291.     * Attributes for form tag
  292.     * @var string $attributes
  293.     */
  294.     var $attributes;
  295.  
  296.    /**
  297.     * Note about required elements
  298.     * @var string $requirednote
  299.     */
  300.     var $requirednote;
  301.  
  302.    /**
  303.     * Collected html of all hidden variables
  304.     * @var string $hidden
  305.     */
  306.     var $hidden;
  307.  
  308.    /**
  309.     * Set if there were validation errors.  
  310.     * StdClass object with element names for keys and their
  311.     * error messages as values
  312.     * @var object $errors
  313.     */
  314.     var $errors;
  315.  
  316.    /**
  317.     * Array of QuickformElementObject elements.  If there are headers in the form
  318.     * this will be empty and the elements will be in the 
  319.     * separate sections
  320.     * @var array $elements
  321.     */
  322.     var $elements;
  323.  
  324.    /**
  325.     * Array of sections contained in the document
  326.     * @var array $sections
  327.     */
  328.     var $sections;
  329.  
  330.    /**
  331.     * Output <form> header
  332.     * {form.outputHeader():h} 
  333.     * @return string    <form attributes>
  334.     */
  335.     function outputHeader()
  336.     {
  337.         return "<form " . $this->attributes . ">\n";
  338.     }
  339.  
  340.    /**
  341.     * Output form javascript
  342.     * {form.outputJavaScript():h}
  343.     * @return string    Javascript
  344.     */
  345.     function outputJavaScript()
  346.     {
  347.         return $this->javascript;
  348.     }
  349. } // end class QuickformForm
  350.  
  351.  
  352. /**
  353.  * Convenience class describing a form element.
  354.  *
  355.  * The properties defined here will be available from 
  356.  * your flexy templates by referencing
  357.  * {form.zip.label:h}, {form.zip.html:h}, etc.
  358.  *
  359.  * @category    HTML
  360.  * @package     HTML_QuickForm
  361.  * @author      Ron McClain <ron@humaniq.com>
  362.  * @version     Release: 3.2.10
  363.  * @since       3.1.1
  364.  */
  365. class QuickformElement
  366. {
  367.     /**
  368.      * Element name
  369.      * @var string $name
  370.      */
  371.     var $name;
  372.  
  373.     /**
  374.      * Element value
  375.      * @var mixed $value
  376.      */
  377.     var $value;
  378.  
  379.     /**
  380.      * Type of element
  381.      * @var string $type
  382.      */
  383.     var $type;
  384.  
  385.     /**
  386.      * Whether the element is frozen
  387.      * @var boolean $frozen
  388.      */
  389.     var $frozen;
  390.  
  391.     /**
  392.      * Label for the element
  393.      * @var string $label
  394.      */
  395.     var $label;
  396.  
  397.     /**
  398.      * Whether element is required
  399.      * @var boolean $required
  400.      */
  401.     var $required;
  402.  
  403.     /**
  404.      * Error associated with the element
  405.      * @var string $error
  406.      */
  407.     var $error;
  408.  
  409.     /**
  410.      * Some information about element style
  411.      * @var string $style
  412.      */
  413.     var $style;
  414.  
  415.     /**
  416.      * HTML for the element
  417.      * @var string $html
  418.      */
  419.     var $html;
  420.  
  421.     /**
  422.      * If element is a group, the group separator
  423.      * @var mixed $separator
  424.      */
  425.     var $separator;
  426.  
  427.     /**
  428.      * If element is a group, an array of subelements
  429.      * @var array $elements
  430.      */
  431.     var $elements;
  432.  
  433.     function isType($type)
  434.     {
  435.         return ($this->type == $type);
  436.     }
  437.  
  438.     function notFrozen()
  439.     {
  440.         return !$this->frozen;
  441.     }
  442.  
  443.     function isButton()
  444.     {
  445.         return ($this->type == "submit" || $this->type == "reset");
  446.     }
  447.  
  448.  
  449.    /**
  450.     * XXX: why does it use Flexy when all other stuff here does not depend on it?
  451.     */
  452.     function outputStyle()
  453.     {
  454.         ob_start();
  455.         HTML_Template_Flexy::staticQuickTemplate('styles/' . $this->style . '.html', $this);
  456.         $ret = ob_get_contents();
  457.         ob_end_clean();
  458.         return $ret;
  459.     }
  460. } // end class QuickformElement
  461. ?>
  462.