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.         return $this->_html;
  156.     // end func toHtml
  157.     
  158.    /**
  159.     * Called when visiting a form, before processing any form elements
  160.     *
  161.     * @param    object      An HTML_QuickForm object being visited
  162.     * @access   public
  163.     * @return   void 
  164.     */
  165.     function startForm(&$form)
  166.     {
  167.         $this->_html '';
  168.         $this->_hiddenHtml '';
  169.     // end func startForm
  170.  
  171.    /**
  172.     * Called when visiting a form, after processing all form elements
  173.     * Adds required note, form attributes, validation javascript and form content.
  174.     * 
  175.     * @param    object      An HTML_QuickForm object being visited
  176.     * @access   public
  177.     * @return   void 
  178.     */
  179.     function finishForm(&$form)
  180.     {
  181.         // add a required note, if one is needed
  182.         if (!empty($form->_required&& !$form->_freezeAll{
  183.             $this->_html .= str_replace('{requiredNote}'$form->getRequiredNote()$this->_requiredNoteTemplate);
  184.         }
  185.         // add form attributes and content
  186.         $html str_replace('{attributes}'$form->getAttributes(true)$this->_formTemplate);
  187.         if (strpos($this->_formTemplate'{hidden}')) {
  188.             $html str_replace('{hidden}'$this->_hiddenHtml$html);
  189.         else {
  190.             $this->_html .= $this->_hiddenHtml;
  191.         }
  192.         $this->_html str_replace('{content}'$this->_html$html);
  193.         // add a validation script
  194.         if ('' != ($script $form->getValidationScript())) {
  195.             $this->_html $script "\n" $this->_html;
  196.         }
  197.     // end func finishForm
  198.       
  199.    /**
  200.     * Called when visiting a header element
  201.     *
  202.     * @param    object     An HTML_QuickForm_header element being visited
  203.     * @access   public
  204.     * @return   void 
  205.     */
  206.     function renderHeader(&$header)
  207.     {
  208.         $name $header->getName();
  209.         if (!empty($name&& isset($this->_templates[$name])) {
  210.             $this->_html .= str_replace('{header}'$header->toHtml()$this->_templates[$name]);
  211.         else {
  212.             $this->_html .= str_replace('{header}'$header->toHtml()$this->_headerTemplate);
  213.         }
  214.     // end func renderHeader
  215.  
  216.    /**
  217.     * Helper method for renderElement
  218.     *
  219.     * @param    string      Element name
  220.     * @param    mixed       Element label (if using an array of labels, you should set the appropriate template)
  221.     * @param    bool        Whether an element is required
  222.     * @param    string      Error message associated with the element
  223.     * @access   private
  224.     * @see      renderElement()
  225.     * @return   string      Html for element
  226.     */
  227.     function _prepareTemplate($name$label$required$error)
  228.     {
  229.         if (is_array($label)) {
  230.             $nameLabel array_shift($label);
  231.         else {
  232.             $nameLabel $label;
  233.         }
  234.         if (isset($this->_templates[$name])) {
  235.             $html str_replace('{label}'$nameLabel$this->_templates[$name]);
  236.         else {
  237.             $html str_replace('{label}'$nameLabel$this->_elementTemplate);
  238.         }
  239.         if ($required{
  240.             $html str_replace('<!-- BEGIN required -->'''$html);
  241.             $html str_replace('<!-- END required -->'''$html);
  242.         else {
  243.             $html preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i"''$html);
  244.         }
  245.         if (isset($error)) {
  246.             $html str_replace('{error}'$error$html);
  247.             $html str_replace('<!-- BEGIN error -->'''$html);
  248.             $html str_replace('<!-- END error -->'''$html);
  249.         else {
  250.             $html preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/i"''$html);
  251.         }
  252.         if (is_array($label)) {
  253.             foreach($label as $key => $text{
  254.                 $key  is_int($key)$key + 2: $key;
  255.                 $html str_replace("{label_{$key}}"$text$html);
  256.                 $html str_replace("<!-- BEGIN label_{$key} -->"''$html);
  257.                 $html str_replace("<!-- END label_{$key} -->"''$html);
  258.             }
  259.         }
  260.         if (strpos($html'{label_')) {
  261.             $html preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i'''$html);
  262.         }
  263.         return $html;
  264.     // end func _prepareTemplate
  265.  
  266.    /**
  267.     * Renders an element Html
  268.     * Called when visiting an element
  269.     *
  270.     * @param object     An HTML_QuickForm_element object being visited
  271.     * @param bool       Whether an element is required
  272.     * @param string     An error message associated with an element
  273.     * @access public
  274.     * @return void 
  275.     */
  276.     function renderElement(&$element$required$error)
  277.     {
  278.         if (!$this->_inGroup{
  279.             $html $this->_prepareTemplate($element->getName()$element->getLabel()$required$error);
  280.             $this->_html .= str_replace('{element}'$element->toHtml()$html);
  281.  
  282.         elseif (!empty($this->_groupElementTemplate)) {
  283.             $html str_replace('{label}'$element->getLabel()$this->_groupElementTemplate);
  284.             if ($required{
  285.                 $html str_replace('<!-- BEGIN required -->'''$html);
  286.                 $html str_replace('<!-- END required -->'''$html);
  287.             else {
  288.                 $html preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i"''$html);
  289.             }
  290.             $this->_groupElements[str_replace('{element}'$element->toHtml()$html);
  291.  
  292.         else {
  293.             $this->_groupElements[$element->toHtml();
  294.         }
  295.     // end func renderElement
  296.    
  297.    /**
  298.     * Renders an hidden element
  299.     * Called when visiting a hidden element
  300.     * 
  301.     * @param object     An HTML_QuickForm_hidden object being visited
  302.     * @access public
  303.     * @return void 
  304.     */
  305.     function renderHidden(&$element)
  306.     {
  307.         $this->_hiddenHtml .= $element->toHtml("\n";
  308.     // end func renderHidden
  309.  
  310.    /**
  311.     * Called when visiting a raw HTML/text pseudo-element
  312.     * 
  313.     * @param  object     An HTML_QuickForm_html element being visited
  314.     * @access public
  315.     * @return void 
  316.     */
  317.     function renderHtml(&$data)
  318.     {
  319.         $this->_html .= $data->toHtml();
  320.     // end func renderHtml
  321.  
  322.    /**
  323.     * Called when visiting a group, before processing any group elements
  324.     *
  325.     * @param object     An HTML_QuickForm_group object being visited
  326.     * @param bool       Whether a group is required
  327.     * @param string     An error message associated with a group
  328.     * @access public
  329.     * @return void 
  330.     */
  331.     function startGroup(&$group$required$error)
  332.     {
  333.         $name $group->getName();
  334.         $this->_groupTemplate        $this->_prepareTemplate($name$group->getLabel()$required$error);
  335.         $this->_groupElementTemplate = empty($this->_groupTemplates[$name])''$this->_groupTemplates[$name];
  336.         $this->_groupWrap            = empty($this->_groupWraps[$name])''$this->_groupWraps[$name];
  337.         $this->_groupElements        = array();
  338.         $this->_inGroup              = true;
  339.     // end func startGroup
  340.  
  341.    /**
  342.     * Called when visiting a group, after processing all group elements
  343.     *
  344.     * @param    object      An HTML_QuickForm_group object being visited
  345.     * @access   public
  346.     * @return   void 
  347.     */
  348.     function finishGroup(&$group)
  349.     {
  350.         $separator $group->_separator;
  351.         if (is_array($separator)) {
  352.             $count count($separator);
  353.             $html  '';
  354.             for ($i = 0; $i count($this->_groupElements)$i++{
  355.                 $html .= (0 == $i''$separator[($i - 1$count]$this->_groupElements[$i];
  356.             }
  357.         else {
  358.             if (is_null($separator)) {
  359.                 $separator '&nbsp;';
  360.             }
  361.             $html implode((string)$separator$this->_groupElements);
  362.         }
  363.         if (!empty($this->_groupWrap)) {
  364.             $html str_replace('{content}'$html$this->_groupWrap);
  365.         }
  366.         $this->_html   .= str_replace('{element}'$html$this->_groupTemplate);
  367.         $this->_inGroup = false;
  368.     // end func finishGroup
  369.  
  370.     /**
  371.      * Sets element template
  372.      *
  373.      * @param       string      The HTML surrounding an element
  374.      * @param       string      (optional) Name of the element to apply template for
  375.      * @access      public
  376.      * @return      void 
  377.      */
  378.     function setElementTemplate($html$element = null)
  379.     {
  380.         if (is_null($element)) {
  381.             $this->_elementTemplate $html;
  382.         else {
  383.             $this->_templates[$element$html;
  384.         }
  385.     // end func setElementTemplate
  386.  
  387.  
  388.     /**
  389.      * Sets template for a group wrapper
  390.      * 
  391.      * This template is contained within a group-as-element template
  392.      * set via setTemplate() and contains group's element templates, set
  393.      * via setGroupElementTemplate()
  394.      *
  395.      * @param       string      The HTML surrounding group elements
  396.      * @param       string      Name of the group to apply template for
  397.      * @access      public
  398.      * @return      void 
  399.      */
  400.     function setGroupTemplate($html$group)
  401.     {
  402.         $this->_groupWraps[$group$html;
  403.     // end func setGroupTemplate
  404.  
  405.     /**
  406.      * Sets element template for elements within a group
  407.      *
  408.      * @param       string      The HTML surrounding an element
  409.      * @param       string      Name of the group to apply template for
  410.      * @access      public
  411.      * @return      void 
  412.      */
  413.     function setGroupElementTemplate($html$group)
  414.     {
  415.         $this->_groupTemplates[$group$html;
  416.     // end func setGroupElementTemplate
  417.  
  418.     /**
  419.      * Sets header template
  420.      *
  421.      * @param       string      The HTML surrounding the header
  422.      * @access      public
  423.      * @return      void 
  424.      */
  425.     function setHeaderTemplate($html)
  426.     {
  427.         $this->_headerTemplate $html;
  428.     // end func setHeaderTemplate
  429.  
  430.     /**
  431.      * Sets form template
  432.      *
  433.      * @param     string    The HTML surrounding the form tags
  434.      * @access    public
  435.      * @return    void 
  436.      */
  437.     function setFormTemplate($html)
  438.     {
  439.         $this->_formTemplate $html;
  440.     // end func setFormTemplate
  441.  
  442.     /**
  443.      * Sets the note indicating required fields template
  444.      *
  445.      * @param       string      The HTML surrounding the required note
  446.      * @access      public
  447.      * @return      void 
  448.      */
  449.     function setRequiredNoteTemplate($html)
  450.     {
  451.         $this->_requiredNoteTemplate $html;
  452.     // end func setRequiredNoteTemplate
  453.  
  454.     /**
  455.      * Clears all the HTML out of the templates that surround notes, elements, etc.
  456.      * Useful when you want to use addData() to create a completely custom form look
  457.      *
  458.      * @access  public
  459.      * @return  void 
  460.      */
  461.     function clearAllTemplates()
  462.     {
  463.         $this->setElementTemplate('{element}');
  464.         $this->setFormTemplate("\n\t<form{attributes}>{content}\n\t</form>\n");
  465.         $this->setRequiredNoteTemplate('');
  466.         $this->_templates = array();
  467.     // end func clearAllTemplates
  468. // end class HTML_QuickForm_Renderer_Default
  469. ?>

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