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

Source for file Object.php

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

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