home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm / Renderer / Tableless.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  17.4 KB  |  477 lines

  1. <?php
  2. /**
  3.  * Replacement for the default renderer of HTML_QuickForm that uses only XHTML
  4.  * and CSS but no table tags, and generates fully valid XHTML output
  5.  *
  6.  * PHP versions 4 and 5
  7.  *
  8.  * LICENSE:
  9.  * 
  10.  * Copyright (c) 2005-2007, Mark Wiesemann <wiesemann@php.net>
  11.  * All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms, with or without
  14.  * modification, are permitted provided that the following conditions
  15.  * are met:
  16.  *
  17.  *    * Redistributions of source code must retain the above copyright
  18.  *      notice, this list of conditions and the following disclaimer.
  19.  *    * Redistributions in binary form must reproduce the above copyright
  20.  *      notice, this list of conditions and the following disclaimer in the 
  21.  *      documentation and/or other materials provided with the distribution.
  22.  *    * The names of the authors may not be used to endorse or promote products 
  23.  *      derived from this software without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  *
  37.  * @category   HTML
  38.  * @package    HTML_QuickForm_Renderer_Tableless
  39.  * @author     Alexey Borzov <borz_off@cs.msu.su>
  40.  * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  41.  * @author     Bertrand Mansion <bmansion@mamasam.com>
  42.  * @author     Mark Wiesemann <wiesemann@php.net>
  43.  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
  44.  * @version    CVS: $Id: Tableless.php,v 1.25 2007/10/24 20:13:34 wiesemann Exp $
  45.  * @link       http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
  46.  */
  47.  
  48. require_once 'HTML/QuickForm/Renderer/Default.php';
  49.  
  50. /**
  51.  * Replacement for the default renderer of HTML_QuickForm that uses only XHTML
  52.  * and CSS but no table tags, and generates fully valid XHTML output
  53.  * 
  54.  * You need to specify a stylesheet like the one that you find in
  55.  * data/stylesheet.css to make this work.
  56.  *
  57.  * @category   HTML
  58.  * @package    HTML_QuickForm_Renderer_Tableless
  59.  * @author     Alexey Borzov <borz_off@cs.msu.su>
  60.  * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  61.  * @author     Bertrand Mansion <bmansion@mamasam.com>
  62.  * @author     Mark Wiesemann <wiesemann@php.net>
  63.  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
  64.  * @version    Release: 0.6.0
  65.  * @link       http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
  66.  */
  67. class HTML_QuickForm_Renderer_Tableless extends HTML_QuickForm_Renderer_Default
  68. {
  69.    /**
  70.     * Header Template string
  71.     * @var      string
  72.     * @access   private
  73.     */
  74.     var $_headerTemplate = "\n\t\t<legend>{header}</legend>\n\t\t<ol>";
  75.  
  76.    /**
  77.     * Element template string
  78.     * @var      string
  79.     * @access   private
  80.     */
  81.     var $_elementTemplate = 
  82.         "\n\t\t\t<li><label class=\"element\"><!-- BEGIN required --><span class=\"required\">*</span><!-- END required -->{label}</label><div class=\"element<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></li>";
  83.  
  84.    /**
  85.     * Form template string
  86.     * @var      string
  87.     * @access   private
  88.     */
  89.     var $_formTemplate = 
  90.         "\n<form{attributes}>\n\t<div style=\"display: none;\">\n{hidden}\t</div>\n{content}\n</form>";
  91.  
  92.    /**
  93.     * Template used when opening a fieldset
  94.     * @var      string
  95.     * @access   private
  96.     */
  97.     var $_openFieldsetTemplate = "\n\t<fieldset{id}{attributes}>";
  98.  
  99.    /**
  100.     * Template used when opening a hidden fieldset
  101.     * (i.e. a fieldset that is opened when there is no header element)
  102.     * @var      string
  103.     * @access   private
  104.     */
  105.     var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden{class}\">\n\t\t<ol>";
  106.  
  107.    /**
  108.     * Template used when closing a fieldset
  109.     * @var      string
  110.     * @access   private
  111.     */
  112.     var $_closeFieldsetTemplate = "\n\t\t</ol>\n\t</fieldset>";
  113.  
  114.    /**
  115.     * Required Note template string
  116.     * @var      string
  117.     * @access   private
  118.     */
  119.     var $_requiredNoteTemplate = 
  120.         "\n\t\t\t<li class=\"reqnote\"><label class=\"element\"> </label>{requiredNote}</li>";
  121.  
  122.    /**
  123.     * How many fieldsets are open
  124.     * @var      integer
  125.     * @access   private
  126.     */
  127.    var $_fieldsetsOpen = 0;
  128.  
  129.    /**
  130.     * Array of element names that indicate the end of a fieldset
  131.     * (a new one will be opened when the next header element occurs)
  132.     * @var      array
  133.     * @access   private
  134.     */
  135.     var $_stopFieldsetElements = array();
  136.  
  137.    /**
  138.     * Constructor
  139.     *
  140.     * @access public
  141.     */
  142.     function HTML_QuickForm_Renderer_Tableless()
  143.     {
  144.         $this->HTML_QuickForm_Renderer_Default();
  145.     } // end constructor
  146.  
  147.    /**
  148.     * Called when visiting a header element
  149.     *
  150.     * @param    object     An HTML_QuickForm_header element being visited
  151.     * @access   public
  152.     * @return   void
  153.     */
  154.     function renderHeader(&$header)
  155.     {
  156.         $name = $header->getName();
  157.         $id = empty($name) ? '' : ' id="' . $name . '"';
  158.         if (!empty($name) && isset($this->_templates[$name])) {
  159.             $header_html = str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
  160.         } else {
  161.             $header_html = str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
  162.         }
  163.         $attributes = $header->getAttributes();
  164.         $strAttr = '';
  165.         if (is_array($attributes)) {
  166.             $charset = HTML_Common::charset();
  167.             foreach ($attributes as $key => $value) {
  168.                 if ($key == 'name') {
  169.                     continue;
  170.                 }
  171.                 $strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
  172.             }
  173.         }
  174.         if ($this->_fieldsetsOpen > 0) {
  175.             $this->_html .= $this->_closeFieldsetTemplate;
  176.             $this->_fieldsetsOpen--;
  177.         }
  178.         $openFieldsetTemplate = str_replace('{id}', $id, $this->_openFieldsetTemplate);
  179.         $openFieldsetTemplate = str_replace('{attributes}',
  180.                                             $strAttr,
  181.                                             $openFieldsetTemplate);
  182.         $this->_html .= $openFieldsetTemplate . $header_html;
  183.         $this->_fieldsetsOpen++;
  184.     } // end func renderHeader
  185.  
  186.    /**
  187.     * Renders an element Html
  188.     * Called when visiting an element
  189.     *
  190.     * @param object     An HTML_QuickForm_element object being visited
  191.     * @param bool       Whether an element is required
  192.     * @param string     An error message associated with an element
  193.     * @access public
  194.     * @return void
  195.     */
  196.     function renderElement(&$element, $required, $error)
  197.     {
  198.         $this->_handleStopFieldsetElements($element->getName());
  199.         if (!$this->_inGroup) {
  200.             $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
  201.             // the following lines (until the "elseif") were changed / added
  202.             // compared to the default renderer
  203.             $element_html = $element->toHtml();
  204.             if (!is_null($element->getAttribute('id'))) {
  205.                 $id = $element->getAttribute('id');
  206.             } else {
  207.                 $id = $element->getName();
  208.             }
  209.             if ($element->getType() != 'static' && !empty($id)) {
  210.                 $html = str_replace('<label', '<label for="' . $id . '"', $html);
  211.                 $element_html = preg_replace('#name="' . $id . '#',
  212.                                              'id="' . $id . '" name="' . $id,
  213.                                              $element_html,
  214.                                              1);
  215.             }
  216.             $this->_html .= str_replace('{element}', $element_html, $html);
  217.         } elseif (!empty($this->_groupElementTemplate)) {
  218.             $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
  219.             if ($required) {
  220.                 $html = str_replace('<!-- BEGIN required -->', '', $html);
  221.                 $html = str_replace('<!-- END required -->', '', $html);
  222.             } else {
  223.                 $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);
  224.             }
  225.             $this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html);
  226.  
  227.         } else {
  228.             $this->_groupElements[] = $element->toHtml();
  229.         }
  230.     } // end func renderElement
  231.  
  232.    /**
  233.     * Renders an hidden element
  234.     * Called when visiting a hidden element
  235.     * 
  236.     * @param object     An HTML_QuickForm_hidden object being visited
  237.     * @access public
  238.     * @return void
  239.     */
  240.     function renderHidden(&$element)
  241.     {
  242.         if (!is_null($element->getAttribute('id'))) {
  243.             $id = $element->getAttribute('id');
  244.         } else {
  245.             $id = $element->getName();
  246.         }
  247.         $html = $element->toHtml();
  248.         if (!empty($id)) {
  249.             $html = str_replace('name="' . $id,
  250.                                 'id="' . $id . '" name="' . $id,
  251.                                 $html);
  252.         }
  253.         $this->_hiddenHtml .= $html . "\n";
  254.     } // end func renderHidden
  255.  
  256.    /**
  257.     * Called when visiting a group, before processing any group elements
  258.     *
  259.     * @param object     An HTML_QuickForm_group object being visited
  260.     * @param bool       Whether a group is required
  261.     * @param string     An error message associated with a group
  262.     * @access public
  263.     * @return void
  264.     */
  265.     function startGroup(&$group, $required, $error)
  266.     {
  267.         $this->_handleStopFieldsetElements($group->getName());
  268.         parent::startGroup($group, $required, $error);
  269.     } // end func startGroup
  270.  
  271.     /**
  272.     * Called when visiting a group, after processing all group elements
  273.     *
  274.     * @param    object      An HTML_QuickForm_group object being visited
  275.     * @access   public
  276.     * @return   void
  277.     */
  278.     function finishGroup(&$group)
  279.     {
  280.         $separator = $group->_separator;
  281.         if (is_array($separator)) {
  282.             $count = count($separator);
  283.             $html  = '';
  284.             for ($i = 0; $i < count($this->_groupElements); $i++) {
  285.                 $html .= (0 == $i? '': $separator[($i - 1) % $count]) . $this->_groupElements[$i];
  286.             }
  287.         } else {
  288.             if (is_null($separator)) {
  289.                 $separator = ' ';
  290.             }
  291.             $html = implode((string)$separator, $this->_groupElements);
  292.         }
  293.         if (!empty($this->_groupWrap)) {
  294.             $html = str_replace('{content}', $html, $this->_groupWrap);
  295.         }
  296.         if (!is_null($group->getAttribute('id'))) {
  297.             $id = $group->getAttribute('id');
  298.         } else {
  299.             $id = $group->getName();
  300.         }
  301.         $groupTemplate = $this->_groupTemplate;
  302.  
  303.         $this->_html   .= str_replace('{element}', $html, $groupTemplate);
  304.         $this->_inGroup = false;
  305.     } // end func finishGroup
  306.  
  307.     /**
  308.     * Called when visiting a form, before processing any form elements
  309.     *
  310.     * @param    object      An HTML_QuickForm object being visited
  311.     * @access   public
  312.     * @return   void
  313.     */
  314.     function startForm(&$form)
  315.     {
  316.         $this->_fieldsetsOpen = 0;
  317.         parent::startForm($form);
  318.     } // end func startForm
  319.  
  320.    /**
  321.     * Called when visiting a form, after processing all form elements
  322.     * Adds required note, form attributes, validation javascript and form content.
  323.     * 
  324.     * @param    object      An HTML_QuickForm object being visited
  325.     * @access   public
  326.     * @return   void
  327.     */
  328.     function finishForm(&$form)
  329.     {
  330.         // add a required note, if one is needed
  331.         if (!empty($form->_required) && !$form->_freezeAll) {
  332.             $requiredNote = $form->getRequiredNote();
  333.             // replace default required note by DOM/XHTML optimized note
  334.             if ($requiredNote == '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>') {
  335.                 $requiredNote = '<span class="required">*</span> denotes required field';
  336.             }
  337.             $this->_html .= str_replace('{requiredNote}', $requiredNote, $this->_requiredNoteTemplate);
  338.         }
  339.         // close the open fieldset
  340.         if ($this->_fieldsetsOpen > 0) {
  341.             $this->_html .= $this->_closeFieldsetTemplate;
  342.             $this->_fieldsetsOpen--;
  343.         }
  344.         // add form attributes and content
  345.         $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate);
  346.         if (strpos($this->_formTemplate, '{hidden}')) {
  347.             $html = str_replace('{hidden}', $this->_hiddenHtml, $html);
  348.         } else {
  349.             $this->_html .= $this->_hiddenHtml;
  350.         }
  351.         $this->_hiddenHtml = '';
  352.         $this->_html = str_replace('{content}', $this->_html, $html);
  353.         $this->_html = str_replace('></label>', '> </label>', $this->_html);
  354.         // add a validation script
  355.         if ('' != ($script = $form->getValidationScript())) {
  356.             $this->_html = $script . "\n" . $this->_html;
  357.         }
  358.     } // end func finishForm
  359.  
  360.     /**
  361.      * Sets the template used when opening a fieldset
  362.      *
  363.      * @param       string      The HTML used when opening a fieldset
  364.      * @access      public
  365.      * @return      void
  366.      */
  367.     function setOpenFieldsetTemplate($html)
  368.     {
  369.         $this->_openFieldsetTemplate = $html;
  370.     } // end func setOpenFieldsetTemplate
  371.  
  372.     /**
  373.      * Sets the template used when opening a hidden fieldset
  374.      * (i.e. a fieldset that is opened when there is no header element)
  375.      *
  376.      * @param       string      The HTML used when opening a hidden fieldset
  377.      * @access      public
  378.      * @return      void
  379.      */
  380.     function setOpenHiddenFieldsetTemplate($html)
  381.     {
  382.         $this->_openHiddenFieldsetTemplate = $html;
  383.     } // end func setOpenHiddenFieldsetTemplate
  384.  
  385.     /**
  386.      * Sets the template used when closing a fieldset
  387.      *
  388.      * @param       string      The HTML used when closing a fieldset
  389.      * @access      public
  390.      * @return      void
  391.      */
  392.     function setCloseFieldsetTemplate($html)
  393.     {
  394.         $this->_closeFieldsetTemplate = $html;
  395.     } // end func setCloseFieldsetTemplate
  396.  
  397.     /**
  398.      * Adds one or more element names that indicate the end of a fieldset
  399.      * (a new one will be opened when a the next header element occurs)
  400.      *
  401.      * @param       mixed      Element name(s) (as array or string)
  402.      * @param       string     (optional) Class name for the fieldset(s)
  403.      * @access      public
  404.      * @return      void
  405.      */
  406.     function addStopFieldsetElements($element, $class = '')
  407.     {
  408.         if (is_array($element)) {
  409.             $elements = array();
  410.             foreach ($element as $name) {
  411.                 $elements[$name] = $class;
  412.             }
  413.             $this->_stopFieldsetElements = array_merge($this->_stopFieldsetElements,
  414.                                                        $elements);
  415.         } else {
  416.             $this->_stopFieldsetElements[$element] = $class;
  417.         }
  418.     } // end func addStopFieldsetElements
  419.  
  420.     /**
  421.      * Handle element/group names that indicate the end of a group
  422.      *
  423.      * @param string     The name of the element or group
  424.      * @access private
  425.      * @return void
  426.      */
  427.     function _handleStopFieldsetElements($element)
  428.     {
  429.         // if the element/group name indicates the end of a fieldset, close
  430.         // the fieldset
  431.         if (   array_key_exists($element, $this->_stopFieldsetElements)
  432.             && $this->_fieldsetsOpen > 0
  433.            ) {
  434.             $this->_html .= $this->_closeFieldsetTemplate;
  435.             $this->_fieldsetsOpen--;
  436.         }
  437.         // if no fieldset was opened, we need to open a hidden one here to get
  438.         // XHTML validity
  439.         if ($this->_fieldsetsOpen === 0) {
  440.             $replace = '';
  441.             if (   array_key_exists($element, $this->_stopFieldsetElements)
  442.                 && $this->_stopFieldsetElements[$element] != ''
  443.                ) {
  444.                 $replace = ' ' . $this->_stopFieldsetElements[$element];
  445.             }
  446.             $this->_html .= str_replace('{class}', $replace,
  447.                                         $this->_openHiddenFieldsetTemplate);
  448.             $this->_fieldsetsOpen++;
  449.         }
  450.     } // end func _handleStopFieldsetElements
  451.  
  452.     /**
  453.      * Sets element template 
  454.      *
  455.      * @param   string    The HTML surrounding an element 
  456.      * @param   mixed     (optional) Name(s) of the element to apply template
  457.      *                    for (either single element name as string or multiple
  458.      *                    element names as an array)
  459.      * @access  public
  460.      * @return  void
  461.      */
  462.     function setElementTemplate($html, $element = null)
  463.     {
  464.         if (is_null($element)) {
  465.             $this->_elementTemplate = $html;
  466.         } elseif (is_array($element)) {
  467.             foreach ($element as $name) {
  468.                 $this->_templates[$name] = $html;
  469.             }
  470.         } else {
  471.             $this->_templates[$element] = $html;
  472.         }
  473.     } // end func setElementTemplate
  474.  
  475. } // end class HTML_QuickForm_Renderer_Default
  476. ?>
  477.