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

Source for file ITStatic.php

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

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