[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/pear/HTML/QuickForm/Renderer/ -> Tableless.php (source)

   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 [email protected] so we can mail you a copy immediately.
  13   *
  14   * @category   HTML
  15   * @package    HTML_QuickForm_Renderer_Tableless
  16   * @author     Alexey Borzov <[email protected]>
  17   * @author     Adam Daniel <[email protected]>
  18   * @author     Bertrand Mansion <[email protected]>
  19   * @author     Mark Wiesemann <[email protected]>
  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 <[email protected]>
  38   * @author     Adam Daniel <[email protected]>
  39   * @author     Bertrand Mansion <[email protected]>
  40   * @author     Mark Wiesemann <[email protected]>
  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      function HTML_QuickForm_Renderer_Tableless()
 122      {
 123          $this->HTML_QuickForm_Renderer_Default();
 124      } // end constructor
 125  
 126     /**
 127      * Called when visiting a header element
 128      *
 129      * @param    object     An HTML_QuickForm_header element being visited
 130      * @access   public
 131      * @return   void
 132      */
 133      function renderHeader(&$header)
 134      {
 135          $name = $header->getName();
 136          $id = empty($name) ? '' : ' id="' . $name . '"';
 137          if (is_null($header->_text)) {
 138              $header_html = '';
 139          }
 140          elseif (!empty($name) && isset($this->_templates[$name])) {
 141              $header_html = str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
 142          } else {
 143              $header_html = str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
 144          }
 145          if ($this->_fieldsetsOpen > 0) {
 146              $this->_html .= $this->_closeFieldsetTemplate;
 147              $this->_fieldsetsOpen--;
 148          }
 149          $openFieldsetTemplate = str_replace('{id}', $id, $this->_openFieldsetTemplate);
 150          $this->_html .= $openFieldsetTemplate . $header_html;
 151          $this->_fieldsetsOpen++;
 152      } // end func renderHeader
 153  
 154     /**
 155      * Renders an element Html
 156      * Called when visiting an element
 157      *
 158      * @param object     An HTML_QuickForm_element object being visited
 159      * @param bool       Whether an element is required
 160      * @param string     An error message associated with an element
 161      * @access public
 162      * @return void
 163      */
 164      function renderElement(&$element, $required, $error)
 165      {
 166          // if the element name indicates the end of a fieldset, close the fieldset
 167          if (   in_array($element->getName(), $this->_stopFieldsetElements)
 168              && $this->_fieldsetsOpen > 0
 169             ) {
 170              $this->_html .= $this->_closeFieldsetTemplate;
 171              $this->_fieldsetsOpen--;
 172          }
 173          // if no fieldset was opened, we need to open a hidden one here to get
 174          // XHTML validity
 175          if ($this->_fieldsetsOpen === 0) {
 176              $this->_html .= $this->_openHiddenFieldsetTemplate;
 177              $this->_fieldsetsOpen++;
 178          }
 179          if (!$this->_inGroup) {
 180              $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
 181              // the following lines (until the "elseif") were changed / added
 182              // compared to the default renderer
 183              $element_html = $element->toHtml();
 184              if (!is_null($element->getAttribute('id'))) {
 185                  $id = $element->getAttribute('id');
 186              } else {
 187                  $id = $element->getName();
 188              }
 189              if (!empty($id) and !$element->isFrozen() and !is_a($element, 'MoodleQuickForm_group') and !is_a($element, 'HTML_QuickForm_static')) { // moodle hack
 190                  $html = str_replace('<label', '<label for="' . $id . '"', $html);
 191                  $element_html = preg_replace('#name="' . $id . '#',
 192                                               'id="' . $id . '" name="' . $id . '',
 193                                               $element_html,
 194                                               1);
 195              }
 196              $this->_html .= str_replace('{element}', $element_html, $html);
 197          } elseif (!empty($this->_groupElementTemplate)) {
 198              $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
 199              if ($required) {
 200                  $html = str_replace('<!-- BEGIN required -->', '', $html);
 201                  $html = str_replace('<!-- END required -->', '', $html);
 202              } else {
 203                  $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);
 204              }
 205              $this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html);
 206  
 207          } else {
 208              $this->_groupElements[] = $element->toHtml();
 209          }
 210      } // end func renderElement
 211  
 212     /**
 213      * Called when visiting a form, before processing any form elements
 214      *
 215      * @param    object      An HTML_QuickForm object being visited
 216      * @access   public
 217      * @return   void
 218      */
 219      function startForm(&$form)
 220      {
 221          $this->_fieldsetsOpen = 0;
 222          parent::startForm($form);
 223      } // end func startForm
 224  
 225     /**
 226      * Called when visiting a form, after processing all form elements
 227      * Adds required note, form attributes, validation javascript and form content.
 228      * 
 229      * @param    object      An HTML_QuickForm object being visited
 230      * @access   public
 231      * @return   void
 232      */
 233      function finishForm(&$form)
 234      {
 235          // add a required note, if one is needed
 236          if (!empty($form->_required) && !$form->_freezeAll) {
 237              $requiredNote = $form->getRequiredNote();
 238              // replace default required note by DOM/XHTML optimized note
 239              if ($requiredNote == '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>') {
 240                  $requiredNote = '<span class="required">*</span> denotes required field';
 241              }
 242              $this->_html .= str_replace('{requiredNote}', $requiredNote, $this->_requiredNoteTemplate);
 243          }
 244          // close the open fieldset
 245          if ($this->_fieldsetsOpen > 0) {
 246              $this->_html .= $this->_closeFieldsetTemplate;
 247              $this->_fieldsetsOpen--;
 248          }
 249          // add form attributes and content
 250          $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate);
 251          if (strpos($this->_formTemplate, '{hidden}')) {
 252              $html = str_replace('{hidden}', $this->_hiddenHtml, $html);
 253          } else {
 254              $this->_html .= $this->_hiddenHtml;
 255          }
 256          $this->_hiddenHtml = '';
 257          $this->_html = str_replace('{content}', $this->_html, $html);
 258          $this->_html = str_replace('></label>', '>&nbsp;</label>', $this->_html);
 259          // add a validation script
 260          if ('' != ($script = $form->getValidationScript())) {
 261              $this->_html = $script . "\n" . $this->_html;
 262          }
 263      } // end func finishForm
 264  
 265      /**
 266       * Sets the template used when opening a fieldset
 267       *
 268       * @param       string      The HTML used when opening a fieldset
 269       * @access      public
 270       * @return      void
 271       */
 272      function setOpenFieldsetTemplate($html)
 273      {
 274          $this->_openFieldsetTemplate = $html;
 275      } // end func setOpenFieldsetTemplate
 276  
 277      /**
 278       * Sets the template used when opening a hidden fieldset
 279       * (i.e. a fieldset that is opened when there is no header element)
 280       *
 281       * @param       string      The HTML used when opening a hidden fieldset
 282       * @access      public
 283       * @return      void
 284       */
 285      function setOpenHiddenFieldsetTemplate($html)
 286      {
 287          $this->_openHiddenFieldsetTemplate = $html;
 288      } // end func setOpenHiddenFieldsetTemplate
 289  
 290      /**
 291       * Sets the template used when closing a fieldset
 292       *
 293       * @param       string      The HTML used when closing a fieldset
 294       * @access      public
 295       * @return      void
 296       */
 297      function setCloseFieldsetTemplate($html)
 298      {
 299          $this->_closeFieldsetTemplate = $html;
 300      } // end func setCloseFieldsetTemplate
 301  
 302      /**
 303       * Adds one or more element names that indicate the end of a fieldset
 304       * (a new one will be opened when a the next header element occurs)
 305       *
 306       * @param       mixed      Element name(s) (as array or string)
 307       * @access      public
 308       * @return      void
 309       */
 310      function addStopFieldsetElements($element)
 311      {
 312          if (is_array($element)) {
 313              $this->_stopFieldsetElements = array_merge($this->_stopFieldsetElements,
 314                                                         $element);
 315          } else {
 316              $this->_stopFieldsetElements[] = $element;
 317          }
 318      } // end func addStopFieldsetElements
 319  
 320  } // end class HTML_QuickForm_Renderer_Default
 321  ?>


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1