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

Source for file Default.php

Documentation is available at Default.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. // |          Adam Daniel <adaniel1@eesus.jnj.com>                        |
  18. // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id$
  22.  
  23. require_once('HTML/QuickForm/Renderer.php');
  24.  
  25. /**
  26.  * A concrete renderer for HTML_QuickForm,
  27.  * based on QuickForm 2.x built-in one
  28.  * 
  29.  * @access public
  30.  */
  31. {
  32.    /**
  33.     * The HTML of the form
  34.     * @var      string 
  35.     * @access   private
  36.     */
  37.     var $_html;
  38.  
  39.    /**
  40.     * Header Template string
  41.     * @var      string 
  42.     * @access   private
  43.     */
  44.     var $_headerTemplate 
  45.         "\n\t<tr>\n\t\t<td style=\"white-space: nowrap; background-color: #CCCCCC;\" align=\"left\" valign=\"top\" colspan=\"2\"><b>{header}</b></td>\n\t</tr>";
  46.  
  47.    /**
  48.     * Element template string
  49.     * @var      string 
  50.     * @access   private
  51.     */
  52.     var $_elementTemplate 
  53.         "\n\t<tr>\n\t\t<td align=\"right\" valign=\"top\"><!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required --><b>{label}</b></td>\n\t\t<td valign=\"top\" align=\"left\"><!-- BEGIN error --><span style=\"color: #ff0000\">{error}</span><br /><!-- END error -->\t{element}</td>\n\t</tr>";
  54.  
  55.    /**
  56.     * Form template string
  57.     * @var      string 
  58.     * @access   private
  59.     */
  60.     var $_formTemplate 
  61.         "\n<form{attributes}>\n<div>\n{hidden}<table border=\"0\">\n{content}\n</table>\n</div>\n</form>";
  62.  
  63.    /**
  64.     * Required Note template string
  65.     * @var      string 
  66.     * @access   private
  67.     */
  68.     var $_requiredNoteTemplate 
  69.         "\n\t<tr>\n\t\t<td></td>\n\t<td align=\"left\" valign=\"top\">{requiredNote}</td>\n\t</tr>";
  70.  
  71.    /**
  72.     * Array containing the templates for customised elements
  73.     * @var      array 
  74.     * @access   private
  75.     */
  76.     var $_templates = array();
  77.  
  78.    /**
  79.     * Array containing the templates for group wraps.
  80.     * 
  81.     * These templates are wrapped around group elements and groups' own
  82.     * templates wrap around them. This is set by setGroupTemplate().
  83.     * 
  84.     * @var      array 
  85.     * @access   private
  86.     */
  87.     var $_groupWraps = array();
  88.  
  89.    /**
  90.     * Array containing the templates for elements within groups
  91.     * @var      array 
  92.     * @access   private
  93.     */
  94.     var $_groupTemplates = array();
  95.  
  96.    /**
  97.     * True if we are inside a group
  98.     * @var      bool 
  99.     * @access   private
  100.     */
  101.     var $_inGroup = false;
  102.  
  103.    /**
  104.     * Array with HTML generated for group elements
  105.     * @var      array 
  106.     * @access   private
  107.     */
  108.     var $_groupElements = array();
  109.  
  110.    /**
  111.     * Template for an element inside a group
  112.     * @var      string 
  113.     * @access   private
  114.     */
  115.     var $_groupElementTemplate '';
  116.  
  117.    /**
  118.     * HTML that wraps around the group elements
  119.     * @var      string 
  120.     * @access   private
  121.     */
  122.     var $_groupWrap '';
  123.  
  124.    /**
  125.     * HTML for the current group
  126.     * @var      string 
  127.     * @access   private
  128.     */
  129.     var $_groupTemplate '';
  130.     
  131.    /**
  132.     * Collected HTML of the hidden fields
  133.     * @var      string 
  134.     * @access   private
  135.     */
  136.     var $_hiddenHtml '';
  137.  
  138.    /**
  139.     * Constructor
  140.     *
  141.     * @access public
  142.     */
  143.     {
  144.         $this->HTML_QuickForm_Renderer();
  145.     // end constructor
  146.  
  147.    /**
  148.     * returns the HTML generated for the form
  149.     *
  150.     * @access public
  151.     * @return string 
  152.     */
  153.     function toHtml()
  154.     {
  155.         // _hiddenHtml is cleared in finishForm(), so this only matters when
  156.         // finishForm() was not called (e.g. group::toHtml(), bug #3511)
  157.         return $this->_hiddenHtml $this->_html;
  158.     // end func toHtml
  159.     
  160.    /**
  161.     * Called when visiting a form, before processing any form elements
  162.     *
  163.     * @param    object      An HTML_QuickForm object being visited
  164.     * @access   public
  165.     * @return   void 
  166.     */
  167.     function startForm(&$form)
  168.     {
  169.         $this->_html '';
  170.         $this->_hiddenHtml '';
  171.     // end func startForm
  172.  
  173.    /**
  174.     * Called when visiting a form, after processing all form elements
  175.     * Adds required note, form attributes, validation javascript and form content.
  176.     * 
  177.     * @param    object      An HTML_QuickForm object being visited
  178.     * @access   public
  179.     * @return   void 
  180.     */
  181.     function finishForm(&$form)
  182.     {
  183.         // add a required note, if one is needed
  184.         if (!empty($form->_required&& !$form->_freezeAll{
  185.             $this->_html .= str_replace('{requiredNote}'$form->getRequiredNote()$this->_requiredNoteTemplate);
  186.         }
  187.         // add form attributes and content
  188.         $html str_replace('{attributes}'$form->getAttributes(true)$this->_formTemplate);
  189.         if (strpos($this->_formTemplate'{hidden}')) {
  190.             $html str_replace('{hidden}'$this->_hiddenHtml$html);
  191.         else {
  192.             $this->_html .= $this->_hiddenHtml;
  193.         }
  194.         $this->_hiddenHtml '';
  195.         $this->_html str_replace('{content}'$this->_html$html);
  196.         // add a validation script
  197.         if ('' != ($script $form->getValidationScript())) {
  198.             $this->_html $script "\n" $this->_html;
  199.         }
  200.     // end func finishForm
  201.       
  202.    /**
  203.     * Called when visiting a header element
  204.     *
  205.     * @param    object     An HTML_QuickForm_header element being visited
  206.     * @access   public
  207.     * @return   void 
  208.     */
  209.     function renderHeader(&$header)
  210.     {
  211.         $name $header->getName();
  212.         if (!empty($name&& isset($this->_templates[$name])) {
  213.             $this->_html .= str_replace('{header}'$header->toHtml()$this->_templates[$name]);
  214.         else {
  215.             $this->_html .= str_replace('{header}'$header->toHtml()$this->_headerTemplate);
  216.         }
  217.     // end func renderHeader
  218.  
  219.    /**
  220.     * Helper method for renderElement
  221.     *
  222.     * @param    string      Element name
  223.     * @param    mixed       Element label (if using an array of labels, you should set the appropriate template)
  224.     * @param    bool        Whether an element is required
  225.     * @param    string      Error message associated with the element
  226.     * @access   private
  227.     * @see      renderElement()
  228.     * @return   string      Html for element
  229.     */
  230.     function _prepareTemplate($name$label$required$error)
  231.     {
  232.         if (is_array($label)) {
  233.             $nameLabel array_shift($label);
  234.         else {
  235.             $nameLabel $label;
  236.         }
  237.         if (isset($this->_templates[$name])) {
  238.             $html str_replace('{label}'$nameLabel$this->_templates[$name]);
  239.         else {
  240.             $html str_replace('{label}'$nameLabel$this->_elementTemplate);
  241.         }
  242.         if ($required{
  243.             $html str_replace('<!-- BEGIN required -->'''$html);
  244.             $html str_replace('<!-- END required -->'''$html);
  245.         else {
  246.             $html preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i"''$html);
  247.         }
  248.         if (isset($error)) {
  249.             $html str_replace('{error}'$error$html);
  250.             $html str_replace('<!-- BEGIN error -->'''$html);
  251.             $html str_replace('<!-- END error -->'''$html);
  252.         else {
  253.             $html preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/i"''$html);
  254.         }
  255.         if (is_array($label)) {
  256.             foreach($label as $key => $text{
  257.                 $key  is_int($key)$key + 2: $key;
  258.                 $html str_replace("{label_{$key}}"$text$html);
  259.                 $html str_replace("<!-- BEGIN label_{$key} -->"''$html);
  260.                 $html str_replace("<!-- END label_{$key} -->"''$html);
  261.             }
  262.         }
  263.         if (strpos($html'{label_')) {
  264.             $html preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i'''$html);
  265.         }
  266.         return $html;
  267.     // end func _prepareTemplate
  268.  
  269.    /**
  270.     * Renders an element Html
  271.     * Called when visiting an element
  272.     *
  273.     * @param object     An HTML_QuickForm_element object being visited
  274.     * @param bool       Whether an element is required
  275.     * @param string     An error message associated with an element
  276.     * @access public
  277.     * @return void 
  278.     */
  279.     function renderElement(&$element$required$error)
  280.     {
  281.         if (!$this->_inGroup{
  282.             $html $this->_prepareTemplate($element->getName()$element->getLabel()$required$error);
  283.             $this->_html .= str_replace('{element}'$element->toHtml()$html);
  284.  
  285.         elseif (!empty($this->_groupElementTemplate)) {
  286.             $html str_replace('{label}'$element->getLabel()$this->_groupElementTemplate);
  287.             if ($required{
  288.                 $html str_replace('<!-- BEGIN required -->'''$html);
  289.                 $html str_replace('<!-- END required -->'''$html);
  290.             else {
  291.                 $html preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i"''$html);
  292.             }
  293.             $this->_groupElements[str_replace('{element}'$element->toHtml()$html);
  294.  
  295.         else {
  296.             $this->_groupElements[$element->toHtml();
  297.         }
  298.     // end func renderElement
  299.    
  300.    /**
  301.     * Renders an hidden element
  302.     * Called when visiting a hidden element
  303.     * 
  304.     * @param object     An HTML_QuickForm_hidden object being visited
  305.     * @access public
  306.     * @return void 
  307.     */
  308.     function renderHidden(&$element)
  309.     {
  310.         $this->_hiddenHtml .= $element->toHtml("\n";
  311.     // end func renderHidden
  312.  
  313.    /**
  314.     * Called when visiting a raw HTML/text pseudo-element
  315.     * 
  316.     * @param  object     An HTML_QuickForm_html element being visited
  317.     * @access public
  318.     * @return void 
  319.     */
  320.     function renderHtml(&$data)
  321.     {
  322.         $this->_html .= $data->toHtml();
  323.     // end func renderHtml
  324.  
  325.    /**
  326.     * Called when visiting a group, before processing any group elements
  327.     *
  328.     * @param object     An HTML_QuickForm_group object being visited
  329.     * @param bool       Whether a group is required
  330.     * @param string     An error message associated with a group
  331.     * @access public
  332.     * @return void 
  333.     */
  334.     function startGroup(&$group$required$error)
  335.     {
  336.         $name $group->getName();
  337.         $this->_groupTemplate        $this->_prepareTemplate($name$group->getLabel()$required$error);
  338.         $this->_groupElementTemplate = empty($this->_groupTemplates[$name])''$this->_groupTemplates[$name];
  339.         $this->_groupWrap            = empty($this->_groupWraps[$name])''$this->_groupWraps[$name];
  340.         $this->_groupElements        = array();
  341.         $this->_inGroup              = true;
  342.     // end func startGroup
  343.  
  344.    /**
  345.     * Called when visiting a group, after processing all group elements
  346.     *
  347.     * @param    object      An HTML_QuickForm_group object being visited
  348.     * @access   public
  349.     * @return   void 
  350.     */
  351.     function finishGroup(&$group)
  352.     {
  353.         $separator $group->_separator;
  354.         if (is_array($separator)) {
  355.             $count count($separator);
  356.             $html  '';
  357.             for ($i = 0; $i count($this->_groupElements)$i++{
  358.                 $html .= (0 == $i''$separator[($i - 1$count]$this->_groupElements[$i];
  359.             }
  360.         else {
  361.             if (is_null($separator)) {
  362.                 $separator '&nbsp;';
  363.             }
  364.             $html implode((string)$separator$this->_groupElements);
  365.         }
  366.         if (!empty($this->_groupWrap)) {
  367.             $html str_replace('{content}'$html$this->_groupWrap);
  368.         }
  369.         $this->_html   .= str_replace('{element}'$html$this->_groupTemplate);
  370.         $this->_inGroup = false;
  371.     // end func finishGroup
  372.  
  373.     /**
  374.      * Sets element template
  375.      *
  376.      * @param       string      The HTML surrounding an element
  377.      * @param       string      (optional) Name of the element to apply template for
  378.      * @access      public
  379.      * @return      void 
  380.      */
  381.     function setElementTemplate($html$element = null)
  382.     {
  383.         if (is_null($element)) {
  384.             $this->_elementTemplate $html;
  385.         else {
  386.             $this->_templates[$element$html;
  387.         }
  388.     // end func setElementTemplate
  389.  
  390.  
  391.     /**
  392.      * Sets template for a group wrapper
  393.      * 
  394.      * This template is contained within a group-as-element template
  395.      * set via setTemplate() and contains group's element templates, set
  396.      * via setGroupElementTemplate()
  397.      *
  398.      * @param       string      The HTML surrounding group elements
  399.      * @param       string      Name of the group to apply template for
  400.      * @access      public
  401.      * @return      void 
  402.      */
  403.     function setGroupTemplate($html$group)
  404.     {
  405.         $this->_groupWraps[$group$html;
  406.     // end func setGroupTemplate
  407.  
  408.     /**
  409.      * Sets element template for elements within a group
  410.      *
  411.      * @param       string      The HTML surrounding an element
  412.      * @param       string      Name of the group to apply template for
  413.      * @access      public
  414.      * @return      void 
  415.      */
  416.     function setGroupElementTemplate($html$group)
  417.     {
  418.         $this->_groupTemplates[$group$html;
  419.     // end func setGroupElementTemplate
  420.  
  421.     /**
  422.      * Sets header template
  423.      *
  424.      * @param       string      The HTML surrounding the header
  425.      * @access      public
  426.      * @return      void 
  427.      */
  428.     function setHeaderTemplate($html)
  429.     {
  430.         $this->_headerTemplate $html;
  431.     // end func setHeaderTemplate
  432.  
  433.     /**
  434.      * Sets form template
  435.      *
  436.      * @param     string    The HTML surrounding the form tags
  437.      * @access    public
  438.      * @return    void 
  439.      */
  440.     function setFormTemplate($html)
  441.     {
  442.         $this->_formTemplate $html;
  443.     // end func setFormTemplate
  444.  
  445.     /**
  446.      * Sets the note indicating required fields template
  447.      *
  448.      * @param       string      The HTML surrounding the required note
  449.      * @access      public
  450.      * @return      void 
  451.      */
  452.     function setRequiredNoteTemplate($html)
  453.     {
  454.         $this->_requiredNoteTemplate $html;
  455.     // end func setRequiredNoteTemplate
  456.  
  457.     /**
  458.      * Clears all the HTML out of the templates that surround notes, elements, etc.
  459.      * Useful when you want to use addData() to create a completely custom form look
  460.      *
  461.      * @access  public
  462.      * @return  void 
  463.      */
  464.     function clearAllTemplates()
  465.     {
  466.         $this->setElementTemplate('{element}');
  467.         $this->setFormTemplate("\n\t<form{attributes}>{content}\n\t</form>\n");
  468.         $this->setRequiredNoteTemplate('');
  469.         $this->_templates = array();
  470.     // end func clearAllTemplates
  471. // end class HTML_QuickForm_Renderer_Default
  472. ?>

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