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

Source for file Tableless.php

Documentation is available at Tableless.php

  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: This source file is subject to the new BSD license that is bundled
  9.  * with this package in the file LICENSE.
  10.  * It is also available through the world-wide-web at this URL:
  11.  * http://www.opensource.org/licenses/bsd-license.php
  12.  * If you did not receive a copy of the license and are unable to
  13.  * obtain it through the world-wide-web, please send an email
  14.  * to wiesemann@php.net so we can send you a copy immediately.
  15.  *
  16.  * @category   HTML
  17.  * @package    HTML_QuickForm_Renderer_Tableless
  18.  * @author     Alexey Borzov <borz_off@cs.msu.su>
  19.  * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  20.  * @author     Bertrand Mansion <bmansion@mamasam.com>
  21.  * @author     Mark Wiesemann <wiesemann@php.net>
  22.  * @copyright  2005-2006 The PHP Group
  23.  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
  24.  * @version    CVS: $Id: Tableless.php,v 1.19 2006/11/09 21:12:37 wiesemann Exp $
  25.  * @link       http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
  26.  */
  27.  
  28. require_once 'HTML/QuickForm/Renderer/Default.php';
  29.  
  30. /**
  31.  * Replacement for the default renderer of HTML_QuickForm that uses only XHTML
  32.  * and CSS but no table tags, and generates fully valid XHTML output
  33.  * 
  34.  * You need to specify a stylesheet like the one that you find in
  35.  * data/stylesheet.css to make this work.
  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    Release: 0.4.2
  45.  * @link       http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
  46.  */
  47. class HTML_QuickForm_Renderer_Tableless extends HTML_QuickForm_Renderer_Default
  48. {
  49.    /**
  50.     * Header Template string
  51.     * @var      string 
  52.     * @access   private
  53.     */
  54.     var $_headerTemplate "\n\t\t<legend>{header}</legend>\n\t\t<ol>";
  55.  
  56.    /**
  57.     * Element template string
  58.     * @var      string 
  59.     * @access   private
  60.     */
  61.     var $_elementTemplate 
  62.         "\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>";
  63.  
  64.    /**
  65.     * Form template string
  66.     * @var      string 
  67.     * @access   private
  68.     */
  69.     var $_formTemplate 
  70.         "\n<form{attributes}>\n\t<div style=\"display: none;\">\n{hidden}\t</div>\n{content}\n</form>";
  71.  
  72.    /**
  73.     * Template used when opening a fieldset
  74.     * @var      string 
  75.     * @access   private
  76.     */
  77.     var $_openFieldsetTemplate "\n\t<fieldset{id}>";
  78.  
  79.    /**
  80.     * Template used when opening a hidden fieldset
  81.     * (i.e. a fieldset that is opened when there is no header element)
  82.     * @var      string 
  83.     * @access   private
  84.     */
  85.     var $_openHiddenFieldsetTemplate "\n\t<fieldset class=\"hidden\">\n\t\t<ol>";
  86.  
  87.    /**
  88.     * Template used when closing a fieldset
  89.     * @var      string 
  90.     * @access   private
  91.     */
  92.     var $_closeFieldsetTemplate "\n\t\t</ol>\n\t</fieldset>";
  93.  
  94.    /**
  95.     * Required Note template string
  96.     * @var      string 
  97.     * @access   private
  98.     */
  99.     var $_requiredNoteTemplate 
  100.         "\n\t\t\t<li class=\"reqnote\"><label class=\"element\">&nbsp;</label>{requiredNote}</li>";
  101.  
  102.    /**
  103.     * How many fieldsets are open
  104.     * @var      integer 
  105.     * @access   private
  106.     */
  107.    var $_fieldsetsOpen = 0;
  108.  
  109.    /**
  110.     * Array of element names that indicate the end of a fieldset
  111.     * (a new one will be opened when a the next header element occurs)
  112.     * @var      array 
  113.     * @access   private
  114.     */
  115.     var $_stopFieldsetElements = array();
  116.  
  117.    /**
  118.     * Constructor
  119.     *
  120.     * @access public
  121.     */
  122.     {
  123.         $this->HTML_QuickForm_Renderer_Default();
  124.     // end constructor
  125.  
  126.    
  127.    /**
  128.     * Called when visiting a header element
  129.     *
  130.     * @param    object     An HTML_QuickForm_header element being visited
  131.     * @access   public
  132.     * @return   void 
  133.     */
  134.     function renderHeader(&$header)
  135.     {
  136.         $name $header->getName();
  137.         $id = empty($name'' ' id="' $name '"';
  138.         if (!empty($name&& isset($this->_templates[$name])) {
  139.             $header_html str_replace('{header}'$header->toHtml()$this->_templates[$name]);
  140.         else {
  141.             $header_html str_replace('{header}'$header->toHtml()$this->_headerTemplate);
  142.         }
  143.         if ($this->_fieldsetsOpen > 0{
  144.             $this->_html .= $this->_closeFieldsetTemplate;
  145.             $this->_fieldsetsOpen--;
  146.         }
  147.         $openFieldsetTemplate str_replace('{id}'$id$this->_openFieldsetTemplate);
  148.         $this->_html .= $openFieldsetTemplate $header_html;
  149.         $this->_fieldsetsOpen++;
  150.     // end func renderHeader
  151.  
  152.    
  153.    /**
  154.     * Renders an element Html
  155.     * Called when visiting an element
  156.     *
  157.     * @param object     An HTML_QuickForm_element object being visited
  158.     * @param bool       Whether an element is required
  159.     * @param string     An error message associated with an element
  160.     * @access public
  161.     * @return void 
  162.     */
  163.     function renderElement(&$element$required$error)
  164.     {
  165.         // if the element name indicates the end of a fieldset, close the fieldset
  166.         if (   in_array($element->getName()$this->_stopFieldsetElements)
  167.             && $this->_fieldsetsOpen > 0
  168.            {
  169.             $this->_html .= $this->_closeFieldsetTemplate;
  170.             $this->_fieldsetsOpen--;
  171.         }
  172.         // if no fieldset was opened, we need to open a hidden one here to get
  173.         // XHTML validity
  174.         if ($this->_fieldsetsOpen === 0{
  175.             $this->_html .= $this->_openHiddenFieldsetTemplate;
  176.             $this->_fieldsetsOpen++;
  177.         }
  178.         if (!$this->_inGroup{
  179.             $html $this->_prepareTemplate($element->getName()$element->getLabel()$required$error);
  180.             // the following lines (until the "elseif") were changed / added
  181.             // compared to the default renderer
  182.             $element_html $element->toHtml();
  183.             if (!is_null($element->getAttribute('id'))) {
  184.                 $id $element->getAttribute('id');
  185.             else {
  186.                 $id $element->getName();
  187.             }
  188.             if (!empty($id)) {
  189.                 $html str_replace('<label''<label for="' $id '"'$html);
  190.                 $element_html preg_replace('#name="' $id '#',
  191.                                              'id="' $id '" name="' $id,
  192.                                              $element_html,
  193.                                              1);
  194.             }
  195.             $this->_html .= str_replace('{element}'$element_html$html);
  196.         elseif (!empty($this->_groupElementTemplate)) {
  197.             $html str_replace('{label}'$element->getLabel()$this->_groupElementTemplate);
  198.             if ($required{
  199.                 $html str_replace('<!-- BEGIN required -->'''$html);
  200.                 $html str_replace('<!-- END required -->'''$html);
  201.             else {
  202.                 $html preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i"''$html);
  203.             }
  204.             $this->_groupElements[str_replace('{element}'$element->toHtml()$html);
  205.  
  206.         else {
  207.             $this->_groupElements[$element->toHtml();
  208.         }
  209.     // end func renderElement
  210.  
  211.    
  212.    /**
  213.     * Renders an hidden element
  214.     * Called when visiting a hidden element
  215.     * 
  216.     * @param object     An HTML_QuickForm_hidden object being visited
  217.     * @access public
  218.     * @return void 
  219.     */
  220.     function renderHidden(&$element)
  221.     {
  222.         if (!is_null($element->getAttribute('id'))) {
  223.             $id $element->getAttribute('id');
  224.         else {
  225.             $id $element->getName();
  226.         }
  227.         $html $element->toHtml();
  228.         if (!empty($id)) {
  229.             $html str_replace('name="' $id,
  230.                                 'id="' $id '" name="' $id,
  231.                                 $html);
  232.         }
  233.         $this->_hiddenHtml .= $html "\n";
  234.     // end func renderHidden
  235.  
  236.    
  237.    /**
  238.     * Called when visiting a group, after processing all group elements
  239.     *
  240.     * @param    object      An HTML_QuickForm_group object being visited
  241.     * @access   public
  242.     * @return   void 
  243.     */
  244.     function finishGroup(&$group)
  245.     {
  246.         $separator $group->_separator;
  247.         if (is_array($separator)) {
  248.             $count count($separator);
  249.             $html  '';
  250.             for ($i = 0; $i count($this->_groupElements)$i++{
  251.                 $html .= (0 == $i''$separator[($i - 1$count]$this->_groupElements[$i];
  252.             }
  253.         else {
  254.             if (is_null($separator)) {
  255.                 $separator '&nbsp;';
  256.             }
  257.             $html implode((string)$separator$this->_groupElements);
  258.         }
  259.         if (!empty($this->_groupWrap)) {
  260.             $html str_replace('{content}'$html$this->_groupWrap);
  261.         }
  262.         if (!is_null($group->getAttribute('id'))) {
  263.             $id $group->getAttribute('id');
  264.         else {
  265.             $id $group->getName();
  266.         }
  267.         $groupTemplate $this->_groupTemplate;
  268.  
  269.         $this->_html   .= str_replace('{element}'$html$groupTemplate);
  270.         $this->_inGroup = false;
  271.     // end func finishGroup
  272.  
  273.     
  274.     /**
  275.     * Called when visiting a form, before processing any form elements
  276.     *
  277.     * @param    object      An HTML_QuickForm object being visited
  278.     * @access   public
  279.     * @return   void 
  280.     */
  281.     function startForm(&$form)
  282.     {
  283.         $this->_fieldsetsOpen = 0;
  284.         parent::startForm($form);
  285.     // end func startForm
  286.  
  287.    
  288.    /**
  289.     * Called when visiting a form, after processing all form elements
  290.     * Adds required note, form attributes, validation javascript and form content.
  291.     * 
  292.     * @param    object      An HTML_QuickForm object being visited
  293.     * @access   public
  294.     * @return   void 
  295.     */
  296.     function finishForm(&$form)
  297.     {
  298.         // add a required note, if one is needed
  299.         if (!empty($form->_required&& !$form->_freezeAll{
  300.             $requiredNote $form->getRequiredNote();
  301.             // replace default required note by DOM/XHTML optimized note
  302.             if ($requiredNote == '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>'{
  303.                 $requiredNote '<span class="required">*</span> denotes required field';
  304.             }
  305.             $this->_html .= str_replace('{requiredNote}'$requiredNote$this->_requiredNoteTemplate);
  306.         }
  307.         // close the open fieldset
  308.         if ($this->_fieldsetsOpen > 0{
  309.             $this->_html .= $this->_closeFieldsetTemplate;
  310.             $this->_fieldsetsOpen--;
  311.         }
  312.         // add form attributes and content
  313.         $html str_replace('{attributes}'$form->getAttributes(true)$this->_formTemplate);
  314.         if (strpos($this->_formTemplate'{hidden}')) {
  315.             $html str_replace('{hidden}'$this->_hiddenHtml$html);
  316.         else {
  317.             $this->_html .= $this->_hiddenHtml;
  318.         }
  319.         $this->_hiddenHtml '';
  320.         $this->_html str_replace('{content}'$this->_html$html);
  321.         $this->_html str_replace('></label>''>&nbsp;</label>'$this->_html);
  322.         // add a validation script
  323.         if ('' != ($script $form->getValidationScript())) {
  324.             $this->_html $script "\n" $this->_html;
  325.         }
  326.     // end func finishForm
  327.  
  328.     
  329.     /**
  330.      * Sets the template used when opening a fieldset
  331.      *
  332.      * @param       string      The HTML used when opening a fieldset
  333.      * @access      public
  334.      * @return      void 
  335.      */
  336.     function setOpenFieldsetTemplate($html)
  337.     {
  338.         $this->_openFieldsetTemplate $html;
  339.     // end func setOpenFieldsetTemplate
  340.  
  341.     
  342.     /**
  343.      * Sets the template used when opening a hidden fieldset
  344.      * (i.e. a fieldset that is opened when there is no header element)
  345.      *
  346.      * @param       string      The HTML used when opening a hidden fieldset
  347.      * @access      public
  348.      * @return      void 
  349.      */
  350.     function setOpenHiddenFieldsetTemplate($html)
  351.     {
  352.         $this->_openHiddenFieldsetTemplate $html;
  353.     // end func setOpenHiddenFieldsetTemplate
  354.  
  355.     
  356.     /**
  357.      * Sets the template used when closing a fieldset
  358.      *
  359.      * @param       string      The HTML used when closing a fieldset
  360.      * @access      public
  361.      * @return      void 
  362.      */
  363.     function setCloseFieldsetTemplate($html)
  364.     {
  365.         $this->_closeFieldsetTemplate $html;
  366.     // end func setCloseFieldsetTemplate
  367.  
  368.     
  369.     /**
  370.      * Adds one or more element names that indicate the end of a fieldset
  371.      * (a new one will be opened when a the next header element occurs)
  372.      *
  373.      * @param       mixed      Element name(s) (as array or string)
  374.      * @access      public
  375.      * @return      void 
  376.      */
  377.     function addStopFieldsetElements($element)
  378.     {
  379.         if (is_array($element)) {
  380.             $this->_stopFieldsetElements array_merge($this->_stopFieldsetElements,
  381.                                                        $element);
  382.         else {
  383.             $this->_stopFieldsetElements[$element;
  384.         }
  385.     // end func addStopFieldsetElements
  386.  
  387. // end class HTML_QuickForm_Renderer_Default
  388. ?>

Documentation generated on Thu, 09 Nov 2006 16:30:05 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.