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

Source for file ObjectFlexy.php

Documentation is available at ObjectFlexy.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: ObjectFlexy.php,v 1.6 2004/06/25 17:20:32 ths Exp $
  20.  
  21. require_once("HTML/QuickForm/Renderer/Object.php");
  22.  
  23. /**
  24.  * @abstract Long Description
  25.  *  A static renderer for HTML_Quickform.  Makes a QuickFormFlexyObject
  26.  *  from the form content suitable for use with a Flexy template
  27.  *
  28.  *  Usage:
  29.  *  $form =& new HTML_QuickForm('form', 'POST');
  30.  *  $template =& new HTML_Template_Flexy();
  31.  *  $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
  32.  *  $renderer->setHtmlTemplate("html.html");
  33.  *  $renderer->setLabelTemplate("label.html");
  34.  *  $form->accept($renderer);
  35.  *  $view = new StdClass;
  36.  *  $view->form = $renderer->toObject();
  37.  *  $template->compile("mytemplate.html");
  38.  *
  39.  * @see QuickFormFlexyObject
  40.  *
  41.  *  Based on the code for HTML_QuickForm_Renderer_ArraySmarty
  42.  *
  43.  * @public
  44.  */
  45.     /**
  46.      * HTML_Template_Flexy instance
  47.      * @var object $_flexy 
  48.      */
  49.     var $_flexy;
  50.  
  51.     /**
  52.      * Current element index
  53.      * @var integer $_elementIdx 
  54.      */
  55.     var $_elementIdx;
  56.  
  57.     /**
  58.      * The current element index inside a group
  59.      * @var integer $_groupElementIdx 
  60.      */
  61.      var $_groupElementIdx = 0;
  62.  
  63.     /**
  64.      * Name of template file for form html
  65.      * @var string $_html 
  66.      * @see     setRequiredTemplate()
  67.      */
  68.     var $_html '';
  69.  
  70.     /**
  71.      * Name of template file for form labels
  72.      * @var string $label 
  73.      * @see        setErrorTemplate()
  74.      */
  75.     var $label = '';
  76.  
  77.     /**
  78.      * Class of the element objects, so you can add your own
  79.      * element methods
  80.      * @var string $_elementType 
  81.      */
  82.     var $_elementType 'QuickformFlexyElement';
  83.  
  84.     /**
  85.      * Constructor
  86.      *
  87.      * @param $flexy object   HTML_Template_Flexy instance
  88.      * @public
  89.      */
  90.     function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
  91.     {
  92.         $this->HTML_QuickForm_Renderer_Object(true);
  93.         $this->_obj = new QuickformFlexyForm();
  94.         $this->_flexy =$flexy;
  95.     // end constructor
  96.  
  97.     function renderHeader(&$header)
  98.     {
  99.         if($name $header->getName()) {
  100.             $this->_obj->header->$name $header->toHtml();
  101.         else {
  102.             $this->_obj->header[$this->_sectionCount$header->toHtml();
  103.         }
  104.         $this->_currentSection $this->_sectionCount++;
  105.     // end func renderHeader
  106.  
  107.     function startGroup(&$group$required$error)
  108.     {
  109.         parent::startGroup($group$required$error);
  110.         $this->_groupElementIdx = 1;
  111.     //end func startGroup
  112.  
  113.     /**
  114.      * Creates an object representing an element containing
  115.      * the key for storing this
  116.      *
  117.      * @private
  118.      * @param element object     An HTML_QuickForm_element object
  119.      * @param required bool        Whether an element is required
  120.      * @param error string    Error associated with the element
  121.      * @return object 
  122.      */
  123.      function _elementToObject(&$element$required$error)
  124.      {
  125.         $ret = parent::_elementToObject($element$required$error);
  126.         if($ret->type == 'group'{
  127.             $ret->html = $element->toHtml();
  128.             unset($ret->elements);
  129.         }
  130.         if(!empty($this->_label)) {
  131.             $this->_renderLabel($ret);
  132.         }
  133.  
  134.         if(!empty($this->_html)) {
  135.             $this->_renderHtml($ret);
  136.             $ret->error = $error;
  137.         }
  138.  
  139.         // Create an element key from the name
  140.         if (false !== ($pos strpos($ret->name'[')) || is_object($this->_currentGroup)) {
  141.             if (!$pos{
  142.                 $keys '->{\'' $ret->name . '\'}';
  143.             else {
  144.                 $keys '->{\'' str_replace(array('['']')array('\'}->{\'''')$ret->name'\'}';
  145.             }
  146.             // special handling for elements in native groups
  147.             if (is_object($this->_currentGroup)) {
  148.                 // skip unnamed group items unless radios: no name -> no static access
  149.                 // identification: have the same key string as the parent group
  150.                 if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type{
  151.                     return false;
  152.                 }
  153.                 // reduce string of keys by remove leading group keys
  154.                 if (0 === strpos($keys$this->_currentGroup->keys)) {
  155.                     $keys substr_replace($keys''0strlen($this->_currentGroup->keys));
  156.                 }
  157.             }
  158.         elseif (0 == strlen($ret->name)) {
  159.             $keys '->{\'element_' $this->_elementIdx '\'}';
  160.         else {
  161.             $keys '->{\'' $ret->name . '\'}';
  162.         }
  163.         // for radios: add extra key from value
  164.         if ('radio' == $ret->type && '[]' != substr($keys-2)) {
  165.             $keys .= '->{\'' $ret->value . '\'}';
  166.         }
  167.         $ret->keys = $keys;
  168.         $this->_elementIdx++;
  169.         return $ret;
  170.     }
  171.  
  172.     /**
  173.      * Stores an object representation of an element in the
  174.      * QuickformFormObject instance
  175.      *
  176.      * @private
  177.      * @param elObj object  Object representation of an element
  178.      * @return void 
  179.      */
  180.     function _storeObject($elObj
  181.     {
  182.         if ($elObj{
  183.             $keys $elObj->keys;
  184.             unset($elObj->keys);
  185.             if(is_object($this->_currentGroup&& ('group' != $elObj->type)) {
  186.                 $code '$this->_currentGroup' $keys ' = $elObj;';
  187.             else {
  188.                 $code '$this->_obj' $keys ' = $elObj;';
  189.             }
  190.             eval($code);
  191.         }
  192.     }
  193.  
  194.     /**
  195.      * Set the filename of the template to render html elements.
  196.      * In your template, {html} is replaced by the unmodified html.
  197.      * If the element is required, {required} will be true.
  198.      * Eg.
  199.      * {if:error}
  200.      *   <font color="red" size="1">{error:h}</font><br />
  201.      * {end:}
  202.      * {html:h}
  203.      *
  204.      * @public
  205.      * @param template string   Filename of template
  206.      * @return void 
  207.      */
  208.     function setHtmlTemplate($template)
  209.     {
  210.         $this->_html $template;
  211.     
  212.  
  213.     /**
  214.      * Set the filename of the template to render form labels
  215.      * In your template, {label} is replaced by the unmodified label.
  216.      * {error} will be set to the error, if any.  {required} will
  217.      * be true if this is a required field
  218.      * Eg.
  219.      * {if:required}
  220.      * <font color="orange" size="1">*</font>
  221.      * {end:}
  222.      * {label:h}
  223.      *
  224.      * @public
  225.      * @param template string   Filename of template
  226.      * @return void 
  227.      */
  228.     function setLabelTemplate($template
  229.     {
  230.         $this->_label $template;
  231.     }
  232.  
  233.     function _renderLabel(&$ret)
  234.     {
  235.         $this->_flexy->compile($this->_label);
  236.         $ret->label = $this->_flexy->bufferedOutputObject($ret);
  237.     }
  238.  
  239.     function _renderHtml(&$ret)
  240.     {
  241.         $this->_flexy->compile($this->_html);
  242.         $ret->html = $this->_flexy->bufferedOutputObject($ret);
  243.     }
  244.  
  245. // end class HTML_QuickForm_Renderer_ObjectFlexy
  246.  
  247. /**
  248.  * @abstract Long Description
  249.  *  This class represents the object passed to outputObject()
  250.  * 
  251.  *  Eg.
  252.  *  {form.outputJavaScript():h}
  253.  *  {form.outputHeader():h}
  254.  *    <table>
  255.  *      <tr>
  256.  *        <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
  257.  *      </tr>
  258.  *    </table>
  259.  *  </form>
  260.  * 
  261.  * @public
  262.  */
  263.     /**
  264.      * Whether the form has been frozen
  265.      * @var boolean $frozen 
  266.      */
  267.     var $frozen;        
  268.     
  269.     /**
  270.      * Javascript for client-side validation
  271.      * @var string $javascript 
  272.      */
  273.      var $javascript;
  274.  
  275.      /**
  276.       * Attributes for form tag
  277.       * @var string $attributes 
  278.       */
  279.      var $attributes;
  280.  
  281.      /**
  282.       * Note about required elements
  283.       * @var string $requirednote 
  284.       */
  285.      var $requirednote;
  286.  
  287.      /**
  288.       * Collected html of all hidden variables
  289.       * @var string $hidden 
  290.       */
  291.      var $hidden;
  292.  
  293.      /**
  294.       * Set if there were validation errors.
  295.       * StdClass object with element names for keys and their
  296.       * error messages as values
  297.       * @var object $errors 
  298.       */
  299.      var $errors;
  300.  
  301.      /**
  302.       * Array of QuickformElementObject elements.  If there are headers in the form
  303.       * this will be empty and the elements will be in the
  304.       * separate sections
  305.       * @var array $elements 
  306.       */
  307.      var $elements;
  308.  
  309.      /**
  310.       * Array of sections contained in the document
  311.       * @var array $sections 
  312.       */
  313.      var $sections;
  314.  
  315.      /**
  316.       * Output &lt;form&gt; header
  317.       * {form.outputHeader():h}
  318.       * @return string    &lt;form attributes&gt;
  319.       */
  320.      function outputHeader()
  321.      {
  322.         $hdr "<form " $this->attributes . ">\n";
  323.         return $hdr;
  324.      }
  325.  
  326.      /**
  327.       * Output form javascript
  328.       * {form.outputJavaScript():h}
  329.       * @return string    Javascript
  330.       */
  331.      function outputJavaScript()
  332.      {
  333.         return $this->javascript;
  334.      }
  335. // end class QuickformFlexyForm
  336.  
  337. /**
  338.  * Convenience class describing a form element.
  339.  * The properties defined here will be available from
  340.  * your flexy templates by referencing
  341.  * {form.zip.label:h}, {form.zip.html:h}, etc.
  342.  */
  343.     
  344.     /**
  345.      * Element name
  346.      * @var string $name 
  347.      */
  348.     var $name;
  349.  
  350.     /**
  351.      * Element value
  352.      * @var mixed $value 
  353.      */
  354.     var $value;
  355.  
  356.     /**
  357.      * Type of element
  358.      * @var string $type 
  359.      */
  360.     var $type;
  361.  
  362.     /**
  363.      * Whether the element is frozen
  364.      * @var boolean $frozen 
  365.      */
  366.     var $frozen;
  367.  
  368.     /**
  369.      * Label for the element
  370.      * @var string $label 
  371.      */
  372.     var $label;
  373.  
  374.     /**
  375.      * Whether element is required
  376.      * @var boolean $required 
  377.      */
  378.     var $required;
  379.  
  380.     /**
  381.      * Error associated with the element
  382.      * @var string $error 
  383.      */
  384.     var $error;
  385.  
  386.     /**
  387.      * Some information about element style
  388.      * @var string $style 
  389.      */
  390.     var $style;
  391.  
  392.     /**
  393.      * HTML for the element
  394.      * @var string $html 
  395.      */
  396.     var $html;
  397.  
  398.     /**
  399.      * If element is a group, the group separator
  400.      * @var mixed $separator 
  401.      */
  402.     var $separator;
  403.  
  404.     /**
  405.      * If element is a group, an array of subelements
  406.      * @var array $elements 
  407.      */
  408.     var $elements;
  409. // end class QuickformFlexyElement
  410. ?>

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