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.7 2005/06/17 20:00:57 avb Exp $
  20.  
  21. require_once("HTML/QuickForm/Renderer/Object.php");
  22.  
  23. /**
  24.  * QuickForm renderer for Flexy template engine, static version.
  25.  * 
  26.  * A static renderer for HTML_Quickform.  Makes a QuickFormFlexyObject
  27.  * from the form content suitable for use with a Flexy template
  28.  *
  29.  * Usage:
  30.  * $form =& new HTML_QuickForm('form', 'POST');
  31.  * $template =& new HTML_Template_Flexy();
  32.  * $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
  33.  * $renderer->setHtmlTemplate("html.html");
  34.  * $renderer->setLabelTemplate("label.html");
  35.  * $form->accept($renderer);
  36.  * $view = new StdClass;
  37.  * $view->form = $renderer->toObject();
  38.  * $template->compile("mytemplate.html");
  39.  *
  40.  * Based on the code for HTML_QuickForm_Renderer_ArraySmarty
  41.  *
  42.  * @see QuickFormFlexyObject
  43.  * @access public
  44.  */
  45. {
  46.     /**
  47.      * HTML_Template_Flexy instance
  48.      * @var object $_flexy 
  49.      */
  50.     var $_flexy;
  51.  
  52.     /**
  53.      * Current element index
  54.      * @var integer $_elementIdx 
  55.      */
  56.     var $_elementIdx;
  57.  
  58.     /**
  59.      * The current element index inside a group
  60.      * @var integer $_groupElementIdx 
  61.      */
  62.     var $_groupElementIdx = 0;
  63.  
  64.     /**
  65.      * Name of template file for form html
  66.      * @var string $_html 
  67.      * @see     setRequiredTemplate()
  68.      */
  69.     var $_html '';
  70.  
  71.     /**
  72.      * Name of template file for form labels
  73.      * @var string $label 
  74.      * @see        setErrorTemplate()
  75.      */
  76.     var $label = '';
  77.  
  78.     /**
  79.      * Class of the element objects, so you can add your own
  80.      * element methods
  81.      * @var string $_elementType 
  82.      */
  83.     var $_elementType 'QuickformFlexyElement';
  84.  
  85.     /**
  86.      * Constructor
  87.      *
  88.      * @param $flexy object   HTML_Template_Flexy instance
  89.      * @public
  90.      */
  91.     function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
  92.     {
  93.         $this->HTML_QuickForm_Renderer_Object(true);
  94.         $this->_obj = new QuickformFlexyForm();
  95.         $this->_flexy =$flexy;
  96.     // end constructor
  97.  
  98.     function renderHeader(&$header)
  99.     {
  100.         if($name $header->getName()) {
  101.             $this->_obj->header->$name $header->toHtml();
  102.         else {
  103.             $this->_obj->header[$this->_sectionCount$header->toHtml();
  104.         }
  105.         $this->_currentSection $this->_sectionCount++;
  106.     // end func renderHeader
  107.  
  108.     function startGroup(&$group$required$error)
  109.     {
  110.         parent::startGroup($group$required$error);
  111.         $this->_groupElementIdx = 1;
  112.     //end func startGroup
  113.  
  114.     /**
  115.      * Creates an object representing an element containing
  116.      * the key for storing this
  117.      *
  118.      * @access private
  119.      * @param element object     An HTML_QuickForm_element object
  120.      * @param required bool        Whether an element is required
  121.      * @param error string    Error associated with the element
  122.      * @return object 
  123.      */
  124.     function _elementToObject(&$element$required$error)
  125.     {
  126.         $ret = parent::_elementToObject($element$required$error);
  127.         if($ret->type == 'group'{
  128.             $ret->html = $element->toHtml();
  129.             unset($ret->elements);
  130.         }
  131.         if(!empty($this->_label)) {
  132.             $this->_renderLabel($ret);
  133.         }
  134.  
  135.         if(!empty($this->_html)) {
  136.             $this->_renderHtml($ret);
  137.             $ret->error = $error;
  138.         }
  139.  
  140.         // Create an element key from the name
  141.         if (false !== ($pos strpos($ret->name'[')) || is_object($this->_currentGroup)) {
  142.             if (!$pos{
  143.                 $keys '->{\'' $ret->name . '\'}';
  144.             else {
  145.                 $keys '->{\'' str_replace(array('['']')array('\'}->{\'''')$ret->name'\'}';
  146.             }
  147.             // special handling for elements in native groups
  148.             if (is_object($this->_currentGroup)) {
  149.                 // skip unnamed group items unless radios: no name -> no static access
  150.                 // identification: have the same key string as the parent group
  151.                 if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type{
  152.                     return false;
  153.                 }
  154.                 // reduce string of keys by remove leading group keys
  155.                 if (0 === strpos($keys$this->_currentGroup->keys)) {
  156.                     $keys substr_replace($keys''0strlen($this->_currentGroup->keys));
  157.                 }
  158.             }
  159.         elseif (0 == strlen($ret->name)) {
  160.             $keys '->{\'element_' $this->_elementIdx '\'}';
  161.         else {
  162.             $keys '->{\'' $ret->name . '\'}';
  163.         }
  164.         // for radios: add extra key from value
  165.         if ('radio' == $ret->type && '[]' != substr($keys-2)) {
  166.             $keys .= '->{\'' $ret->value . '\'}';
  167.         }
  168.         $ret->keys = $keys;
  169.         $this->_elementIdx++;
  170.         return $ret;
  171.     }
  172.  
  173.     /**
  174.      * Stores an object representation of an element in the
  175.      * QuickformFormObject instance
  176.      *
  177.      * @access private
  178.      * @param elObj object  Object representation of an element
  179.      * @return void 
  180.      */
  181.     function _storeObject($elObj
  182.     {
  183.         if ($elObj{
  184.             $keys $elObj->keys;
  185.             unset($elObj->keys);
  186.             if(is_object($this->_currentGroup&& ('group' != $elObj->type)) {
  187.                 $code '$this->_currentGroup' $keys ' = $elObj;';
  188.             else {
  189.                 $code '$this->_obj' $keys ' = $elObj;';
  190.             }
  191.             eval($code);
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * Set the filename of the template to render html elements.
  197.      * In your template, {html} is replaced by the unmodified html.
  198.      * If the element is required, {required} will be true.
  199.      * Eg.
  200.      * {if:error}
  201.      *   <font color="red" size="1">{error:h}</font><br />
  202.      * {end:}
  203.      * {html:h}
  204.      *
  205.      * @access public
  206.      * @param template string   Filename of template
  207.      * @return void 
  208.      */
  209.     function setHtmlTemplate($template)
  210.     {
  211.         $this->_html $template;
  212.     
  213.  
  214.     /**
  215.      * Set the filename of the template to render form labels
  216.      * In your template, {label} is replaced by the unmodified label.
  217.      * {error} will be set to the error, if any.  {required} will
  218.      * be true if this is a required field
  219.      * Eg.
  220.      * {if:required}
  221.      * <font color="orange" size="1">*</font>
  222.      * {end:}
  223.      * {label:h}
  224.      *
  225.      * @access public
  226.      * @param template string   Filename of template
  227.      * @return void 
  228.      */
  229.     function setLabelTemplate($template
  230.     {
  231.         $this->_label $template;
  232.     }
  233.  
  234.     function _renderLabel(&$ret)
  235.     {
  236.         $this->_flexy->compile($this->_label);
  237.         $ret->label = $this->_flexy->bufferedOutputObject($ret);
  238.     }
  239.  
  240.     function _renderHtml(&$ret)
  241.     {
  242.         $this->_flexy->compile($this->_html);
  243.         $ret->html = $this->_flexy->bufferedOutputObject($ret);
  244.     }
  245. // end class HTML_QuickForm_Renderer_ObjectFlexy
  246.  
  247. /**
  248.  * Adds nothing to QuickformForm, left for backwards compatibility
  249.  */
  250. {
  251. }
  252.  
  253. /**
  254.  * Adds nothing to QuickformElement, left for backwards compatibility
  255.  */
  256. {
  257. }
  258. ?>

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