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

Documentation generated on Mon, 11 Mar 2019 13:52:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.