Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  /**
   3   * A renderer for HTML_QuickForm that only uses XHTML and CSS but no
   4   * table tags
   5   *
   6   * PHP versions 4 and 5
   7   *
   8   * LICENSE: This source file is subject to version 3.01 of the PHP license
   9   * that is available through the world-wide-web at the following URI:
  10   * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
  11   * the PHP License and are unable to obtain it through the web, please
  12   * send a note to license@php.net so we can mail you a copy immediately.
  13   *
  14   * @category   HTML
  15   * @package    HTML_QuickForm_Renderer_Tableless
  16   * @author     Alexey Borzov <borz_off@cs.msu.su>
  17   * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  18   * @author     Bertrand Mansion <bmansion@mamasam.com>
  19   * @author     Mark Wiesemann <wiesemann@php.net>
  20   * @copyright  2005-2006 The PHP Group
  21   * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  22   * @version    CVS: $Id$
  23   * @link       http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
  24   */
  25  
  26  require_once 'HTML/QuickForm/Renderer/Default.php';
  27  
  28  /**
  29   * A renderer for HTML_QuickForm that only uses XHTML and CSS but no
  30   * table tags
  31   * 
  32   * You need to specify a stylesheet like the one that you find in
  33   * data/stylesheet.css to make this work.
  34   *
  35   * @category   HTML
  36   * @package    HTML_QuickForm_Renderer_Tableless
  37   * @author     Alexey Borzov <borz_off@cs.msu.su>
  38   * @author     Adam Daniel <adaniel1@eesus.jnj.com>
  39   * @author     Bertrand Mansion <bmansion@mamasam.com>
  40   * @author     Mark Wiesemann <wiesemann@php.net>
  41   * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  42   * @version    Release: 0.3.4
  43   * @link       http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
  44   */
  45  class HTML_QuickForm_Renderer_Tableless extends HTML_QuickForm_Renderer_Default
  46  {
  47     /**
  48      * Header Template string
  49      * @var      string
  50      * @access   private
  51      */
  52      var $_headerTemplate = 
  53          "\n\t\t<legend>{header}</legend>";
  54  
  55     /**
  56      * Element template string
  57      * @var      string
  58      * @access   private
  59      */
  60      var $_elementTemplate = 
  61          "\n\t\t<div class=\"qfrow\"><label class=\"qflabel\"><!-- BEGIN required --><span class=\"required\">*</span><!-- END required -->{label}</label><div class=\"qfelement<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></div><br />";
  62  
  63     /**
  64      * Form template string
  65      * @var      string
  66      * @access   private
  67      */
  68      var $_formTemplate = 
  69          "\n<form{attributes}>\n\t<div style=\"display: none;\">{hidden}</div>\n{content}\n</form>";
  70  
  71     /**
  72      * Template used when opening a fieldset
  73      * @var      string
  74      * @access   private
  75      */
  76      var $_openFieldsetTemplate = "\n\t<fieldset{id}>";
  77  
  78     /**
  79      * Template used when opening a hidden fieldset
  80      * (i.e. a fieldset that is opened when there is no header element)
  81      * @var      string
  82      * @access   private
  83      */
  84      var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\">";
  85  
  86     /**
  87      * Template used when closing a fieldset
  88      * @var      string
  89      * @access   private
  90      */
  91      var $_closeFieldsetTemplate = "\n\t</fieldset>";
  92  
  93     /**
  94      * Required Note template string
  95      * @var      string
  96      * @access   private
  97      */
  98      var $_requiredNoteTemplate = 
  99          "\n\t\t<div class=\"qfreqnote\">{requiredNote}</div>";
 100  
 101     /**
 102      * How many fieldsets are open
 103      * @var      integer
 104      * @access   private
 105      */
 106     var $_fieldsetsOpen = 0;
 107  
 108     /**
 109      * Array of element names that indicate the end of a fieldset
 110      * (a new one will be opened when a the next header element occurs)
 111      * @var      array
 112      * @access   private
 113      */
 114      var $_stopFieldsetElements = array();
 115  
 116     /**
 117      * Constructor
 118      *
 119      * @access public
 120      */
 121      public function __construct() {
 122          parent::__construct();
 123      } // end constructor
 124  
 125      /**
 126       * Old syntax of class constructor. Deprecated in PHP7.
 127       *
 128       * @deprecated since Moodle 3.1
 129       */
 130      public function HTML_QuickForm_Renderer_Tableless() {
 131          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 132          self::__construct();
 133      }
 134  
 135     /**
 136      * Called when visiting a header element
 137      *
 138      * @param    object     An HTML_QuickForm_header element being visited
 139      * @access   public
 140      * @return   void
 141      */
 142      function renderHeader(&$header)
 143      {
 144          $name = $header->getName();
 145          $id = empty($name) ? '' : ' id="' . $name . '"';
 146          if (is_null($header->_text)) {
 147              $header_html = '';
 148          }
 149          elseif (!empty($name) && isset($this->_templates[$name])) {
 150              $header_html = str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
 151          } else {
 152              $header_html = str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
 153          }
 154          if ($this->_fieldsetsOpen > 0) {
 155              $this->_html .= $this->_closeFieldsetTemplate;
 156              $this->_fieldsetsOpen--;
 157          }
 158          $openFieldsetTemplate = str_replace('{id}', $id, $this->_openFieldsetTemplate);
 159          $this->_html .= $openFieldsetTemplate . $header_html;
 160          $this->_fieldsetsOpen++;
 161      } // end func renderHeader
 162  
 163     /**
 164      * Renders an element Html
 165      * Called when visiting an element
 166      *
 167      * @param object     An HTML_QuickForm_element object being visited
 168      * @param bool       Whether an element is required
 169      * @param string     An error message associated with an element
 170      * @access public
 171      * @return void
 172      */
 173      function renderElement(&$element, $required, $error)
 174      {
 175          // if the element name indicates the end of a fieldset, close the fieldset
 176          if (   in_array($element->getName(), $this->_stopFieldsetElements)
 177              && $this->_fieldsetsOpen > 0
 178             ) {
 179              $this->_html .= $this->_closeFieldsetTemplate;
 180              $this->_fieldsetsOpen--;
 181          }
 182          // if no fieldset was opened, we need to open a hidden one here to get
 183          // XHTML validity
 184          if ($this->_fieldsetsOpen === 0) {
 185              $this->_html .= $this->_openHiddenFieldsetTemplate;
 186              $this->_fieldsetsOpen++;
 187          }
 188          if (!$this->_inGroup) {
 189              $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
 190              // the following lines (until the "elseif") were changed / added
 191              // compared to the default renderer
 192              $element_html = $element->toHtml();
 193              if (!is_null($element->getAttribute('id'))) {
 194                  $id = $element->getAttribute('id');
 195              } else {
 196                  $id = $element->getName();
 197              }
 198              if (!empty($id) and !is_a($element, 'MoodleQuickForm_group') and !is_a($element, 'HTML_QuickForm_static')) { // moodle hack
 199                  $html = str_replace('<label', '<label for="' . $id . '"', $html);
 200                  $element_html = preg_replace('#name="' . $id . '#',
 201                                               'id="' . $id . '" name="' . $id . '',
 202                                               $element_html,
 203                                               1);
 204              }
 205              $this->_html .= str_replace('{element}', $element_html, $html);
 206          } elseif (!empty($this->_groupElementTemplate)) {
 207              $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
 208              if ($required) {
 209                  $html = str_replace('<!-- BEGIN required -->', '', $html);
 210                  $html = str_replace('<!-- END required -->', '', $html);
 211              } else {
 212                  $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);
 213              }
 214              $this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html);
 215  
 216          } else {
 217              $this->_groupElements[] = $element->toHtml();
 218          }
 219      } // end func renderElement
 220  
 221     /**
 222      * Called when visiting a form, before processing any form elements
 223      *
 224      * @param    object      An HTML_QuickForm object being visited
 225      * @access   public
 226      * @return   void
 227      */
 228      function startForm(&$form)
 229      {
 230          $this->_fieldsetsOpen = 0;
 231          parent::startForm($form);
 232      } // end func startForm
 233  
 234     /**
 235      * Called when visiting a form, after processing all form elements
 236      * Adds required note, form attributes, validation javascript and form content.
 237      * 
 238      * @param    object      An HTML_QuickForm object being visited
 239      * @access   public
 240      * @return   void
 241      */
 242      function finishForm(&$form)
 243      {
 244          // add a required note, if one is needed
 245          if (!empty($form->_required) && !$form->_freezeAll) {
 246              $requiredNote = $form->getRequiredNote();
 247              // replace default required note by DOM/XHTML optimized note
 248              if ($requiredNote == '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>') {
 249                  $requiredNote = '<span class="required">*</span> denotes required field';
 250              }
 251              $this->_html .= str_replace('{requiredNote}', $requiredNote, $this->_requiredNoteTemplate);
 252          }
 253          // close the open fieldset
 254          if ($this->_fieldsetsOpen > 0) {
 255              $this->_html .= $this->_closeFieldsetTemplate;
 256              $this->_fieldsetsOpen--;
 257          }
 258          // add form attributes and content
 259          $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate);
 260          if (strpos($this->_formTemplate, '{hidden}')) {
 261              $html = str_replace('{hidden}', $this->_hiddenHtml, $html);
 262          } else {
 263              $this->_html .= $this->_hiddenHtml;
 264          }
 265          $this->_hiddenHtml = '';
 266          $this->_html = str_replace('{content}', $this->_html, $html);
 267          $this->_html = str_replace('></label>', '>&nbsp;</label>', $this->_html);
 268          // add a validation script
 269          if ('' != ($script = $form->getValidationScript())) {
 270              $this->_html = $this->_html . "\n" . $script;
 271          }
 272      } // end func finishForm
 273  
 274      /**
 275       * Sets the template used when opening a fieldset
 276       *
 277       * @param       string      The HTML used when opening a fieldset
 278       * @access      public
 279       * @return      void
 280       */
 281      function setOpenFieldsetTemplate($html)
 282      {
 283          $this->_openFieldsetTemplate = $html;
 284      } // end func setOpenFieldsetTemplate
 285  
 286      /**
 287       * Sets the template used when opening a hidden fieldset
 288       * (i.e. a fieldset that is opened when there is no header element)
 289       *
 290       * @param       string      The HTML used when opening a hidden fieldset
 291       * @access      public
 292       * @return      void
 293       */
 294      function setOpenHiddenFieldsetTemplate($html)
 295      {
 296          $this->_openHiddenFieldsetTemplate = $html;
 297      } // end func setOpenHiddenFieldsetTemplate
 298  
 299      /**
 300       * Sets the template used when closing a fieldset
 301       *
 302       * @param       string      The HTML used when closing a fieldset
 303       * @access      public
 304       * @return      void
 305       */
 306      function setCloseFieldsetTemplate($html)
 307      {
 308          $this->_closeFieldsetTemplate = $html;
 309      } // end func setCloseFieldsetTemplate
 310  
 311      /**
 312       * Adds one or more element names that indicate the end of a fieldset
 313       * (a new one will be opened when a the next header element occurs)
 314       *
 315       * @param       mixed      Element name(s) (as array or string)
 316       * @access      public
 317       * @return      void
 318       */
 319      function addStopFieldsetElements($element)
 320      {
 321          if (is_array($element)) {
 322              $this->_stopFieldsetElements = array_merge($this->_stopFieldsetElements,
 323                                                         $element);
 324          } else {
 325              $this->_stopFieldsetElements[] = $element;
 326          }
 327      } // end func addStopFieldsetElements
 328  
 329  } // end class HTML_QuickForm_Renderer_Default
 330  ?>