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