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 / ObjectFlexy.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  8.8 KB  |  292 lines

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