home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / ITStatic.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  15.0 KB  |  430 lines

  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: Bertrand Mansion <bmansion@mamasam.com>                      |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: ITStatic.php,v 1.5 2004/03/20 11:24:26 avb Exp $
  20.  
  21. require_once('HTML/QuickForm/Renderer.php');
  22.  
  23. /**
  24.  * A static renderer for HTML_QuickForm compatible 
  25.  * with HTML_Template_IT and HTML_Template_Sigma.
  26.  *
  27.  * As opposed to the dynamic renderer, this renderer needs
  28.  * every elements and labels in the form to be specified by
  29.  * placeholders at the position you want them to be displayed.
  30.  * 
  31.  * @author Bertrand Mansion <bmansion@mamasam.com>
  32.  * @access public
  33.  */
  34. class HTML_QuickForm_Renderer_ITStatic extends HTML_QuickForm_Renderer
  35. {
  36.    /**
  37.     * An HTML_Template_IT or some other API compatible Template instance
  38.     * @var object
  39.     */
  40.     var $_tpl = null;
  41.  
  42.    /**
  43.     * Rendered form name
  44.     * @var string
  45.     */
  46.     var $_formName = 'form';
  47.  
  48.    /**
  49.     * The errors that were not shown near concrete fields go here
  50.     * @var array
  51.     */
  52.     var $_errors = array();
  53.  
  54.    /**
  55.     * Show the block with required note?
  56.     * @var bool
  57.     */
  58.     var $_showRequired = false;
  59.  
  60.    /**
  61.     * Which group are we currently parsing ?
  62.     * @var string
  63.     */
  64.     var $_inGroup;
  65.  
  66.    /**
  67.     * Index of the element in its group
  68.     * @var int
  69.     */
  70.     var $_elementIndex = 0;
  71.  
  72.    /**
  73.     * If elements have been added with the same name
  74.     * @var array
  75.     */
  76.     var $_duplicateElements = array();
  77.  
  78.    /**
  79.     * How to handle the required tag for required fields
  80.     * @var string
  81.     */
  82.     var $_required = '{label}<font size="1" color="red">*</font>';
  83.  
  84.    /**
  85.     * How to handle error messages in form validation
  86.     * @var string
  87.     */
  88.     var $_error = '<font color="red">{error}</font><br />{html}';
  89.  
  90.    /**
  91.     * Collected HTML for hidden elements, if needed  
  92.     * @var string
  93.     */
  94.     var $_hidden = '';
  95.  
  96.    /**
  97.     * Constructor
  98.     *
  99.     * @param object     An HTML_Template_IT or other compatible Template object to use
  100.     */
  101.     function HTML_QuickForm_Renderer_ITStatic(&$tpl)
  102.     {
  103.         $this->HTML_QuickForm_Renderer();
  104.         $this->_tpl =& $tpl;
  105.     } // end constructor
  106.  
  107.    /**
  108.     * Called when visiting a form, before processing any form elements
  109.     *
  110.     * @param    object      An HTML_QuickForm object being visited
  111.     * @access   public
  112.     * @return   void
  113.     */
  114.     function startForm(&$form)
  115.     {
  116.         $this->_formName = $form->getAttribute('id');
  117.  
  118.         if (count($form->_duplicateIndex) > 0) {
  119.             // Take care of duplicate elements
  120.             foreach ($form->_duplicateIndex as $elementName => $indexes) {
  121.                 $this->_duplicateElements[$elementName] = 0;
  122.             }
  123.         }
  124.     } // end func startForm
  125.  
  126.    /**
  127.     * Called when visiting a form, after processing all form elements
  128.     * 
  129.     * @param    object     An HTML_QuickForm object being visited
  130.     * @access   public
  131.     * @return   void
  132.     */
  133.     function finishForm(&$form)
  134.     {
  135.         // display errors above form
  136.         if (!empty($this->_errors) && $this->_tpl->blockExists($this->_formName.'_error_loop')) {
  137.             foreach ($this->_errors as $error) {
  138.                 $this->_tpl->setVariable($this->_formName.'_error', $error);
  139.                 $this->_tpl->parse($this->_formName.'_error_loop');
  140.             }
  141.         }
  142.         // show required note
  143.         if ($this->_showRequired) {
  144.             $this->_tpl->setVariable($this->_formName.'_required_note', $form->getRequiredNote());
  145.         }
  146.         // add hidden elements, if collected
  147.         if (!empty($this->_hidden)) {
  148.             $this->_tpl->setVariable($this->_formName . '_hidden', $this->_hidden);
  149.         }
  150.         // assign form attributes
  151.         $this->_tpl->setVariable($this->_formName.'_attributes', $form->getAttributes(true));
  152.         // assign javascript validation rules
  153.         $this->_tpl->setVariable($this->_formName.'_javascript', $form->getValidationScript());
  154.     } // end func finishForm
  155.  
  156.    /**
  157.     * Called when visiting a header element
  158.     *
  159.     * @param    object     An HTML_QuickForm_header element being visited
  160.     * @access   public
  161.     * @return   void
  162.     */
  163.     function renderHeader(&$header)
  164.     {
  165.         $name = $header->getName();
  166.         $varName = $this->_formName.'_header';
  167.  
  168.         // Find placeHolder
  169.         if (!empty($name) && $this->_tpl->placeHolderExists($this->_formName.'_header_'.$name)) {
  170.             $varName = $this->_formName.'_header_'.$name;
  171.         }
  172.         $this->_tpl->setVariable($varName, $header->toHtml());
  173.     } // end func renderHeader
  174.  
  175.    /**
  176.     * Called when visiting an element
  177.     *
  178.     * @param    object     An HTML_QuickForm_element object being visited
  179.     * @param    bool       Whether an element is required
  180.     * @param    string     An error message associated with an element
  181.     * @access   public
  182.     * @return   void
  183.     */
  184.     function renderElement(&$element, $required, $error)
  185.     {
  186.         $name = $element->getName();
  187.  
  188.         // are we inside a group?
  189.         if (!empty($this->_inGroup)) {
  190.             $varName = $this->_formName.'_'.str_replace(array('[', ']'), '_', $name);
  191.             if (substr($varName, -2) == '__') {
  192.                 // element name is of type : group[]
  193.                 $varName = $this->_inGroup.'_'.$this->_elementIndex.'_';
  194.                 $this->_elementIndex++;
  195.             }
  196.             if ($varName != $this->_inGroup) {
  197.                 $varName .= '_' == substr($varName, -1)? '': '_';
  198.                 // element name is of type : group[name]
  199.                 $label = $element->getLabel();
  200.                 $html = $element->toHtml();
  201.  
  202.                 if ($required && !$element->isFrozen()) {
  203.                     $this->_renderRequired($label, $html);
  204.                     $this->_showRequired = true;
  205.                 }
  206.                 if (!empty($label)) {
  207.                     if (is_array($label)) {
  208.                         foreach ($label as $key => $value) {
  209.                             $this->_tpl->setVariable($varName.'label_'.$key, $value);
  210.                         }
  211.                     } else {
  212.                         $this->_tpl->setVariable($varName.'label', $label);
  213.                     }
  214.                 }
  215.                 $this->_tpl->setVariable($varName.'html', $html);
  216.             }
  217.  
  218.         } else {
  219.  
  220.             $name = str_replace(array('[', ']'), array('_', ''), $name);
  221.  
  222.             if (isset($this->_duplicateElements[$name])) {
  223.                 // Element is a duplicate
  224.                 $varName = $this->_formName.'_'.$name.'_'.$this->_duplicateElements[$name];
  225.                 $this->_duplicateElements[$name]++;
  226.             } else {
  227.                 $varName = $this->_formName.'_'.$name;
  228.             }
  229.  
  230.             $label = $element->getLabel();
  231.             $html = $element->toHtml();
  232.  
  233.             if ($required) {
  234.                 $this->_showRequired = true;
  235.                 $this->_renderRequired($label, $html);
  236.             }
  237.             if (!empty($error)) {
  238.                 $this->_renderError($label, $html, $error);
  239.             }
  240.             if (is_array($label)) {
  241.                 foreach ($label as $key => $value) {
  242.                     $this->_tpl->setVariable($varName.'_label_'.$key, $value);
  243.                 }
  244.             } else {
  245.                 $this->_tpl->setVariable($varName.'_label', $label);
  246.             }
  247.             $this->_tpl->setVariable($varName.'_html', $html);
  248.         }
  249.     } // end func renderElement
  250.  
  251.    /**
  252.     * Called when visiting a hidden element
  253.     * 
  254.     * @param    object     An HTML_QuickForm_hidden object being visited
  255.     * @access   public
  256.     * @return   void
  257.     */
  258.     function renderHidden(&$element)
  259.     {
  260.         if ($this->_tpl->placeholderExists($this->_formName . '_hidden')) {
  261.             $this->_hidden .= $element->toHtml();
  262.         } else {
  263.             $this->_tpl->setVariable($this->_formName.'_'.$element->getName().'_html', $element->toHtml());
  264.         }
  265.     } // end func renderHidden
  266.  
  267.    /**
  268.     * Called when visiting a group, before processing any group elements
  269.     *
  270.     * @param    object     An HTML_QuickForm_group object being visited
  271.     * @param    bool       Whether a group is required
  272.     * @param    string     An error message associated with a group
  273.     * @access   public
  274.     * @return   void
  275.     */
  276.     function startGroup(&$group, $required, $error)
  277.     {
  278.         $name = $group->getName();
  279.         $varName = $this->_formName.'_'.$name;
  280.  
  281.         $this->_elementIndex = 0;
  282.  
  283.         $html = $this->_tpl->placeholderExists($varName.'_html') ? $group->toHtml() : '';
  284.         $label = $group->getLabel();
  285.  
  286.         if ($required) {
  287.             $this->_renderRequired($label, $html);
  288.         }
  289.         if (!empty($error)) {
  290.             $this->_renderError($label, $html, $error);
  291.         }
  292.         if (!empty($html)) {
  293.             $this->_tpl->setVariable($varName.'_html', $html);
  294.         } else {
  295.             // Uses error blocks to set the special groups layout error
  296.             // <!-- BEGIN form_group_error -->{form_group_error}<!-- END form_group_error -->
  297.             if (!empty($error)) {
  298.                 if ($this->_tpl->placeholderExists($varName.'_error') &&
  299.                    (strpos($this->_error, '{html}') !== false || strpos($this->_error, '{label}') !== false)) {
  300.                     $error = str_replace('{error}', $error, $this->_error);
  301.                     $this->_tpl->setVariable($varName.'_error', $error);
  302.                     array_pop($this->_errors);
  303.                 }
  304.             }
  305.         }
  306.         if (is_array($label)) {
  307.             foreach ($label as $key => $value) {
  308.                 $this->_tpl->setVariable($varName.'_label_'.$key, $value);
  309.             }
  310.         } else {
  311.             $this->_tpl->setVariable($varName.'_label', $label);
  312.         }
  313.         $this->_inGroup = $varName;
  314.     } // end func startGroup
  315.  
  316.    /**
  317.     * Called when visiting a group, after processing all group elements
  318.     *
  319.     * @param    object     An HTML_QuickForm_group object being visited
  320.     * @access   public
  321.     * @return   void
  322.     */
  323.     function finishGroup(&$group)
  324.     {
  325.         $this->_inGroup = '';
  326.     } // end func finishGroup
  327.  
  328.    /**
  329.     * Sets the way required elements are rendered
  330.     *
  331.     * You can use {label} or {html} placeholders to let the renderer know where
  332.     * where the element label or the element html are positionned according to the
  333.     * required tag. They will be replaced accordingly with the right value.
  334.     * For example:
  335.     * <font color="red">*</font>{label}
  336.     * will put a red star in front of the label if the element is required.
  337.     *
  338.     * @param    string      The required element template
  339.     * @access   public
  340.     * @return   void
  341.     */
  342.     function setRequiredTemplate($template)
  343.     {
  344.         $this->_required = $template;
  345.     } // end func setRequiredTemplate
  346.  
  347.    /**
  348.     * Sets the way elements with validation errors are rendered
  349.     *
  350.     * You can use {label} or {html} placeholders to let the renderer know where
  351.     * where the element label or the element html are positionned according to the
  352.     * error message. They will be replaced accordingly with the right value.
  353.     * The error message will replace the {error} place holder.
  354.     * For example:
  355.     * <font color="red">{error}</font><br />{html}
  356.     * will put the error message in red on top of the element html.
  357.     *
  358.     * If you want all error messages to be output in the main error block, do not specify
  359.     * {html} nor {label}.
  360.     *
  361.     * Groups can have special layouts. With this kind of groups, the renderer will need
  362.     * to know where to place the error message. In this case, use error blocks like:
  363.     * <!-- BEGIN form_group_error -->{form_group_error}<!-- END form_group_error -->
  364.     * where you want the error message to appear in the form.
  365.     *
  366.     * @param    string      The element error template
  367.     * @access   public
  368.     * @return   void
  369.     */
  370.     function setErrorTemplate($template)
  371.     {
  372.         $this->_error = $template;
  373.     } // end func setErrorTemplate
  374.  
  375.    /**
  376.     * Called when an element is required
  377.     *
  378.     * This method will add the required tag to the element label and/or the element html
  379.     * such as defined with the method setRequiredTemplate
  380.     *
  381.     * @param    string      The element label
  382.     * @param    string      The element html rendering
  383.     * @see      setRequiredTemplate()
  384.     * @access   private
  385.     * @return   void
  386.     */
  387.     function _renderRequired(&$label, &$html)
  388.     {
  389.         if (!empty($label) && strpos($this->_required, '{label}') !== false) {
  390.             if (is_array($label)) {
  391.                 $label[0] = str_replace('{label}', $label[0], $this->_required);
  392.             } else {
  393.                 $label = str_replace('{label}', $label, $this->_required);
  394.             }
  395.         }
  396.         if (!empty($html) && strpos($this->_required, '{html}') !== false) {
  397.             $html = str_replace('{html}', $html, $this->_required);
  398.         }
  399.     } // end func _renderRequired
  400.  
  401.    /**
  402.     * Called when an element has a validation error
  403.     *
  404.     * This method will add the error message to the element label or the element html
  405.     * such as defined with the method setErrorTemplate. If the error placeholder is not found
  406.     * in the template, the error will be displayed in the form error block.
  407.     *
  408.     * @param    string      The element label
  409.     * @param    string      The element html rendering
  410.     * @param    string      The element error
  411.     * @see      setErrorTemplate()
  412.     * @access   private
  413.     * @return   void
  414.     */
  415.     function _renderError(&$label, &$html, $error)
  416.     {
  417.         if (!empty($label) && strpos($this->_error, '{label}') !== false) {
  418.             if (is_array($label)) {
  419.                 $label[0] = str_replace(array('{label}', '{error}'), array($label[0], $error), $this->_error);
  420.             } else {
  421.                 $label = str_replace(array('{label}', '{error}'), array($label, $error), $this->_error);
  422.             }
  423.         } elseif (!empty($html) && strpos($this->_error, '{html}') !== false) {
  424.             $html = str_replace(array('{html}', '{error}'), array($html, $error), $this->_error);
  425.         } else {
  426.             $this->_errors[] = $error;
  427.         }
  428.     }// end func _renderError
  429. } // end class HTML_QuickForm_Renderer_ITStatic
  430. ?>