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

Source for file ArraySmarty.php

Documentation is available at ArraySmarty.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. // | Authors: Alexey Borzov <borz_off@cs.msu.su>                          |
  17. // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  18. // |          Thomas Schulz <ths@4bconsult.de>                            |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: ArraySmarty.php,v 1.8 2004/02/14 11:01:51 ths Exp $
  22.  
  23. require_once 'HTML/QuickForm/Renderer/Array.php';
  24.  
  25. /**
  26.  * A static renderer for HTML_QuickForm, makes an array of form content
  27.  * useful for an Smarty template
  28.  *
  29.  * Based on old toArray() code and ITStatic renderer.
  30.  *
  31.  * The form array structure is the following:
  32.  * Array (
  33.  *  [frozen]       => whether the complete form is frozen'
  34.  *  [javascript]   => javascript for client-side validation
  35.  *  [attributes]   => attributes for <form> tag
  36.  *  [hidden]       => html of all hidden elements
  37.  *  [requirednote] => note about the required elements
  38.  *  [errors] => Array
  39.  *      (
  40.  *          [1st_element_name] => Error for the 1st element
  41.  *          ...
  42.  *          [nth_element_name] => Error for the nth element
  43.  *      )
  44.  *
  45.  *  [header] => Array
  46.  *      (
  47.  *          [1st_header_name] => Header text for the 1st header
  48.  *          ...
  49.  *          [nth_header_name] => Header text for the nth header
  50.  *      )
  51.  *
  52.  *  [1st_element_name] => Array for the 1st element
  53.  *  ...
  54.  *  [nth_element_name] => Array for the nth element
  55.  *
  56.  * // where an element array has the form:
  57.  *      (
  58.  *          [name]      => element name
  59.  *          [value]     => element value,
  60.  *          [type]      => type of the element
  61.  *          [frozen]    => whether element is frozen
  62.  *          [label]     => label for the element
  63.  *          [required]  => whether element is required
  64.  * // if element is not a group:
  65.  *          [html]      => HTML for the element
  66.  * // if element is a group:
  67.  *          [separator] => separator for group elements
  68.  *          [1st_gitem_name] => Array for the 1st element in group
  69.  *          ...
  70.  *          [nth_gitem_name] => Array for the nth element in group
  71.  *      )
  72.  * )
  73.  *
  74.  * @access public
  75.  */
  76. {
  77.    /**
  78.     * The Smarty template engine instance
  79.     * @var object 
  80.     */
  81.     var $_tpl = null;
  82.  
  83.    /**
  84.     * Current element index
  85.     * @var integer 
  86.     */
  87.     var $_elementIdx = 0;
  88.  
  89.     /**
  90.     * The current element index inside a group
  91.     * @var integer 
  92.     */
  93.     var $_groupElementIdx = 0;
  94.  
  95.    /**
  96.     * How to handle the required tag for required fields
  97.     * @var string 
  98.     * @see      setRequiredTemplate()
  99.     */
  100.     var $_required '';
  101.  
  102.    /**
  103.     * How to handle error messages in form validation
  104.     * @var string 
  105.     * @see      setErrorTemplate()
  106.     */
  107.     var $_error '';
  108.  
  109.    /**
  110.     * Constructor
  111.     *
  112.     * @access public
  113.     */
  114.     function HTML_QuickForm_Renderer_ArraySmarty(&$tpl)
  115.     {
  116.         $this->HTML_QuickForm_Renderer_Array(true);
  117.         $this->_tpl =$tpl;
  118.     // end constructor
  119.  
  120.    /**
  121.     * Called when visiting a header element
  122.     *
  123.     * @param    object     An HTML_QuickForm_header element being visited
  124.     * @access   public
  125.     * @return   void 
  126.     */
  127.     function renderHeader(&$header)
  128.     {
  129.         if ($name $header->getName()) {
  130.             $this->_ary['header'][$name$header->toHtml();
  131.         else {
  132.             $this->_ary['header'][$this->_sectionCount$header->toHtml();
  133.         }
  134.         $this->_currentSection $this->_sectionCount++;
  135.     // end func renderHeader
  136.  
  137.    /**
  138.     * Called when visiting a group, before processing any group elements
  139.     *
  140.     * @param    object     An HTML_QuickForm_group object being visited
  141.     * @param    bool       Whether a group is required
  142.     * @param    string     An error message associated with a group
  143.     * @access   public
  144.     * @return   void 
  145.     */
  146.     function startGroup(&$group$required$error)
  147.     {
  148.         parent::startGroup($group$required$error);
  149.         $this->_groupElementIdx = 1;
  150.     // end func startGroup
  151.  
  152.    /**
  153.     * Creates an array representing an element containing
  154.     * the key for storing this
  155.     *
  156.     * @access private
  157.     * @param  object    An HTML_QuickForm_element object
  158.     * @param  bool      Whether an element is required
  159.     * @param  string    Error associated with the element
  160.     * @return array 
  161.     */
  162.     function _elementToArray(&$element$required$error)
  163.     {
  164.         $ret = parent::_elementToArray($element$required$error);
  165.  
  166.         if ('group' == $ret['type']{
  167.             $ret['html'$element->toHtml();
  168.             // we don't need the elements, see the array structure
  169.             unset($ret['elements']);
  170.         }
  171.         if (!empty($this->_required)){
  172.             $this->_renderRequired($ret['label']$ret['html']$required$error);
  173.         }
  174.         if (!empty($this->_error)) {
  175.             $this->_renderError($ret['label']$ret['html']$error);
  176.             $ret['error'$error;
  177.         }
  178.         // create keys for elements grouped by native group or name
  179.         if (strstr($ret['name']'['or $this->_currentGroup{
  180.             preg_match('/([^]]*)\\[([^]]*)\\]/'$ret['name']$matches);
  181.             if (isset($matches[1])) {
  182.                 $sKeysSub substr_replace($ret['name']''0strlen($matches[1]));
  183.                 $sKeysSub str_replace(
  184.                     array('['  ,   ']''[\'\']'),
  185.                     array('[\'''\']''[]'    ),
  186.                     $sKeysSub
  187.                 );
  188.                 $sKeys '[\'' $matches[1]  '\']' $sKeysSub;
  189.             else {
  190.                 $sKeys '[\'' $ret['name''\']';
  191.             }
  192.             // special handling for elements in native groups
  193.             if ($this->_currentGroup{
  194.                 // skip unnamed group items unless radios: no name -> no static access
  195.                 // identification: have the same key string as the parent group
  196.                 if ($this->_currentGroup['keys'== $sKeys and 'radio' != $ret['type']{
  197.                     return false;
  198.                 }
  199.                 // reduce string of keys by remove leading group keys
  200.                 if (0 === strpos($sKeys$this->_currentGroup['keys'])) {
  201.                     $sKeys substr_replace($sKeys''0strlen($this->_currentGroup['keys']));
  202.                 }
  203.             }
  204.         // element without a name
  205.         elseif ($ret['name'== ''{
  206.             $sKeys '[\'element_' $this->_elementIdx '\']';
  207.         // other elements
  208.         else {
  209.             $sKeys '[\'' $ret['name''\']';
  210.         }
  211.         // for radios: add extra key from value
  212.         if ('radio' == $ret['type'and substr($sKeys-2!= '[]'{
  213.             $sKeys .= '[\'' $ret['value''\']';
  214.         }
  215.         $this->_elementIdx++;
  216.         $ret['keys'$sKeys;
  217.         return $ret;
  218.     // end func _elementToArray
  219.  
  220.    /**
  221.     * Stores an array representation of an element in the form array
  222.     *
  223.     * @access private
  224.     * @param array  Array representation of an element
  225.     * @return void 
  226.     */
  227.     function _storeArray($elAry)
  228.     {
  229.         if ($elAry{
  230.             $sKeys $elAry['keys'];
  231.             unset($elAry['keys']);
  232.             // where should we put this element...
  233.             if (is_array($this->_currentGroup&& ('group' != $elAry['type'])) {
  234.                 $toEval '$this->_currentGroup' $sKeys ' = $elAry;';
  235.             else {
  236.                 $toEval '$this->_ary' $sKeys ' = $elAry;';
  237.             }
  238.             eval($toEval);
  239.         }
  240.         return;
  241.     }
  242.  
  243.    /**
  244.     * Called when an element is required
  245.     *
  246.     * This method will add the required tag to the element label and/or the element html
  247.     * such as defined with the method setRequiredTemplate.
  248.     *
  249.     * @param    string      The element label
  250.     * @param    string      The element html rendering
  251.     * @param    boolean     The element required
  252.     * @param    string      The element error
  253.     * @see      setRequiredTemplate()
  254.     * @access   private
  255.     * @return   void 
  256.     */
  257.     function _renderRequired(&$label&$html&$required&$error)
  258.     {
  259.         $this->_tpl->assign(array(
  260.             'label'    => $label,
  261.             'html'     => $html,
  262.             'required' => $required,
  263.             'error'    => $error
  264.         ));
  265.         if (!empty($label&& strpos($this->_required$this->_tpl->left_delimiter . '$label'!== false{
  266.             $label $this->_tplFetch($this->_required);
  267.         }
  268.         if (!empty($html&& strpos($this->_required$this->_tpl->left_delimiter . '$html'!== false{
  269.             $html $this->_tplFetch($this->_required);
  270.         }
  271.         $this->_tpl->clear_assign(array('label''html''required'));
  272.     // end func _renderRequired
  273.  
  274.    /**
  275.     * Called when an element has a validation error
  276.     *
  277.     * This method will add the error message to the element label or the element html
  278.     * such as defined with the method setErrorTemplate. If the error placeholder is not found
  279.     * in the template, the error will be displayed in the form error block.
  280.     *
  281.     * @param    string      The element label
  282.     * @param    string      The element html rendering
  283.     * @param    string      The element error
  284.     * @see      setErrorTemplate()
  285.     * @access   private
  286.     * @return   void 
  287.     */
  288.     function _renderError(&$label&$html&$error)
  289.     {
  290.         $this->_tpl->assign(array('label' => '''html' => '''error' => $error));
  291.         $error $this->_tplFetch($this->_error);
  292.         $this->_tpl->assign(array('label' => $label'html'  => $html));
  293.  
  294.         if (!empty($label&& strpos($this->_error$this->_tpl->left_delimiter . '$label'!== false{
  295.             $label $this->_tplFetch($this->_error);
  296.         elseif (!empty($html&& strpos($this->_error$this->_tpl->left_delimiter . '$html'!== false{
  297.             $html $this->_tplFetch($this->_error);
  298.         }
  299.         $this->_tpl->clear_assign(array('label''html''error'));
  300.     // end func _renderError
  301.  
  302.    /**
  303.     * Process an template sourced in a string with Smarty
  304.     *
  305.     * Smarty has no core function to render    a template given as a string.
  306.     * So we use the smarty eval plugin function    to do this.
  307.     *
  308.     * @param    string      The template source
  309.     * @access   private
  310.     * @return   void 
  311.     */
  312.     function _tplFetch($tplSource)
  313.     {
  314.         if (!function_exists('smarty_function_eval')) {
  315.             require SMARTY_DIR . '/plugins/function.eval.php';
  316.         }
  317.         return smarty_function_eval(array('var' => $tplSource)$this->_tpl);
  318.     }// end func _tplFetch
  319.  
  320.    /**
  321.     * Sets the way required elements are rendered
  322.     *
  323.     * You can use {$label} or {$html} placeholders to let the renderer know where
  324.     * where the element label or the element html are positionned according to the
  325.     * required tag. They will be replaced accordingly with the right value.    You
  326.     * can use the full smarty syntax here, especially a custom modifier for I18N.
  327.     * For example:
  328.     * {if $required}<span style="color: red;">*</span>{/if}{$label|translate}
  329.     * will put a red star in front of the label if the element is required and
  330.     * translate the label.
  331.     *
  332.     *
  333.     * @param    string      The required element template
  334.     * @access   public
  335.     * @return   void 
  336.     */
  337.     function setRequiredTemplate($template)
  338.     {
  339.         $this->_required $template;
  340.     // end func setRequiredTemplate
  341.  
  342.    /**
  343.     * Sets the way elements with validation errors are rendered
  344.     *
  345.     * You can use {$label} or {$html} placeholders to let the renderer know where
  346.     * where the element label or the element html are positionned according to the
  347.     * error message. They will be replaced accordingly with the right value.
  348.     * The error message will replace the {$error} placeholder.
  349.     * For example:
  350.     * {if $error}<span style="color: red;">{$error}</span>{/if}<br />{$html}
  351.     * will put the error message in red on top of the element html.
  352.     *
  353.     * If you want all error messages to be output in the main error block, use
  354.     * the {$form.errors} part of the rendered array that collects all raw error
  355.     * messages.
  356.     *
  357.     * If you want to place all error messages manually, do not specify {$html}
  358.     * nor {$label}.
  359.     *
  360.     * Groups can have special layouts. With this kind of groups, you have to
  361.     * place the formated error message manually. In this case, use {$form.group.error}
  362.     * where you want the formated error message to appear in the form.
  363.     *
  364.     * @param    string      The element error template
  365.     * @access   public
  366.     * @return   void 
  367.     */
  368.     function setErrorTemplate($template)
  369.     {
  370.         $this->_error $template;
  371.     // end func setErrorTemplate
  372. }
  373. ?>

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