Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  // +----------------------------------------------------------------------+
   4  // | PHP Version 4                                                        |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997, 1998, 1999, 2000, 2001 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  // | Author: Adam Daniel <adaniel1@eesus.jnj.com>                         |
  17  // +----------------------------------------------------------------------+
  18  //
  19  // $Id$
  20  
  21  /**
  22   * Base class for all HTML classes
  23   * 
  24   * @author    Adam Daniel <adaniel1@eesus.jnj.com>
  25   * @category  HTML
  26   * @package   HTML_Common
  27   * @version   1.2.2
  28   * @abstract
  29   */
  30  
  31  /**
  32   * Base class for all HTML classes
  33   *
  34   * @author      Adam Daniel <adaniel1@eesus.jnj.com>
  35   * @version     1.7
  36   * @since       PHP 4.0.3pl1
  37   * @abstract
  38   */
  39  class HTML_Common {
  40  
  41      /**
  42       * Associative array of table attributes
  43       * @var     array
  44       * @access  private
  45       */
  46      var $_attributes = array();
  47  
  48      /**
  49       * Tab offset of the table
  50       * @var     int
  51       * @access  private
  52       */
  53      var $_tabOffset = 0;
  54  
  55      /**
  56       * Tab string
  57       * @var       string
  58       * @since     1.7
  59       * @access    private
  60       */
  61      var $_tab = "\11";
  62  
  63      /**
  64       * Contains the line end string
  65       * @var       string
  66       * @since     1.7
  67       * @access    private
  68       */
  69      var $_lineEnd = "\12";
  70  
  71      /**
  72       * HTML comment on the object
  73       * @var       string
  74       * @since     1.5
  75       * @access    private
  76       */
  77      var $_comment = '';
  78  
  79      /**
  80       * Class constructor
  81       * @param    mixed   $attributes     Associative array of table tag attributes 
  82       *                                   or HTML attributes name="value" pairs
  83       * @param    int     $tabOffset      Indent offset in tabs
  84       * @access   public
  85       */
  86      public function __construct($attributes = null, $tabOffset = 0)
  87      {
  88          $this->setAttributes($attributes);
  89          $this->setTabOffset($tabOffset);
  90      } // end constructor
  91  
  92      /**
  93       * Old syntax of class constructor. Deprecated in PHP7.
  94       *
  95       * @deprecated since Moodle 3.1
  96       */
  97      public function HTML_Common($attributes = null, $tabOffset = 0) {
  98          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  99          self::__construct($attributes, $tabOffset);
 100      }
 101  
 102      public static function raiseError($message = null,
 103                                         $code = null,
 104                                         $mode = null,
 105                                         $options = null,
 106                                         $userinfo = null,
 107                                         $error_class = null,
 108                                         $skipmsg = false) {
 109          $pear = new PEAR();
 110          return $pear->raiseError($message, $code, $mode, $options, $userinfo, $error_class, $skipmsg);
 111      }
 112  
 113      /**
 114       * Returns the current API version
 115       * @access   public
 116       * @returns  double
 117       */
 118      function apiVersion()
 119      {
 120          return 1.7;
 121      } // end func apiVersion
 122  
 123      /**
 124       * Returns the lineEnd
 125       * 
 126       * @since     1.7
 127       * @access    private
 128       * @return    string
 129       * @throws
 130       */
 131      function _getLineEnd()
 132      {
 133          return $this->_lineEnd;
 134      } // end func getLineEnd
 135  
 136      /**
 137       * Returns a string containing the unit for indenting HTML
 138       * 
 139       * @since     1.7
 140       * @access    private
 141       * @return    string
 142       */
 143      function _getTab()
 144      {
 145          return $this->_tab;
 146      } // end func _getTab
 147  
 148      /**
 149       * Returns a string containing the offset for the whole HTML code
 150       * 
 151       * @return    string
 152       * @access   private
 153       */
 154      function _getTabs()
 155      {
 156          return str_repeat($this->_getTab(), $this->_tabOffset);
 157      } // end func _getTabs
 158  
 159      /**
 160       * Returns an HTML formatted attribute string
 161       * @param    array   $attributes
 162       * @return   string
 163       * @access   private
 164       */
 165      function _getAttrString($attributes)
 166      {
 167          $strAttr = '';
 168  
 169          if (is_array($attributes)) {
 170              foreach ($attributes as $key => $value) {
 171                  $strAttr .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
 172              }
 173          }
 174          return $strAttr;
 175      } // end func _getAttrString
 176  
 177      /**
 178       * Returns a valid atrributes array from either a string or array
 179       * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
 180       * @access   private
 181       */
 182      function _parseAttributes($attributes)
 183      {
 184          if (is_array($attributes)) {
 185              $ret = array();
 186              foreach ($attributes as $key => $value) {
 187                  if (is_int($key)) {
 188                      $key = $value = strtolower($value);
 189                  } else {
 190                      $key = strtolower($key);
 191                  }
 192                  $ret[$key] = $value;
 193              }
 194              return $ret;
 195  
 196          } elseif (is_string($attributes)) {
 197              $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
 198                  "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
 199              if (preg_match_all($preg, $attributes, $regs)) {
 200                  for ($counter=0; $counter<count($regs[1]); $counter++) {
 201                      $name  = $regs[1][$counter];
 202                      $check = $regs[0][$counter];
 203                      $value = $regs[7][$counter];
 204                      if (trim($name) == trim($check)) {
 205                          $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
 206                      } else {
 207                          if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
 208                              $value = substr($value, 1, -1);
 209                          }
 210                          $arrAttr[strtolower(trim($name))] = trim($value);
 211                      }
 212                  }
 213                  return $arrAttr;
 214              }
 215          }
 216      } // end func _parseAttributes
 217  
 218      /**
 219       * Returns the array key for the given non-name-value pair attribute
 220       * 
 221       * @param     string    $attr         Attribute
 222       * @param     array     $attributes   Array of attribute
 223       * @since     1.0
 224       * @access    private
 225       * @return    bool
 226       * @throws
 227       */
 228      function _getAttrKey($attr, $attributes)
 229      {
 230          if (isset($attributes[strtolower($attr)])) {
 231              return true;
 232          } else {
 233              return null;
 234          }
 235      } //end func _getAttrKey
 236  
 237      /**
 238       * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
 239       * @param    array   $attr1      Original attributes array
 240       * @param    array   $attr2      New attributes array
 241       * @access   private
 242       */
 243      function _updateAttrArray(&$attr1, $attr2)
 244      {
 245          if (!is_array($attr2)) {
 246              return false;
 247          }
 248          foreach ($attr2 as $key => $value) {
 249              $attr1[$key] = $value;
 250          }
 251      } // end func _updateAtrrArray
 252  
 253      /**
 254       * Removes the given attribute from the given array
 255       * 
 256       * @param     string    $attr           Attribute name
 257       * @param     array     $attributes     Attribute array
 258       * @since     1.4
 259       * @access    private
 260       * @return    void
 261       * @throws
 262       */
 263      function _removeAttr($attr, &$attributes)
 264      {
 265          $attr = strtolower($attr);
 266          if (isset($attributes[$attr])) {
 267              unset($attributes[$attr]);
 268          }
 269      } //end func _removeAttr
 270  
 271      /**
 272       * Returns the value of the given attribute
 273       * 
 274       * @param     string    $attr   Attribute name
 275       * @since     1.5
 276       * @access    public
 277       * @return    void
 278       * @throws
 279       */
 280      function getAttribute($attr)
 281      {
 282          $attr = strtolower($attr);
 283          if (isset($this->_attributes[$attr])) {
 284              return $this->_attributes[$attr];
 285          }
 286          return null;
 287      } //end func getAttribute
 288  
 289      /**
 290       * Sets the HTML attributes
 291       * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
 292       * @access   public
 293       */
 294      function setAttributes($attributes)
 295      {
 296          $this->_attributes = $this->_parseAttributes($attributes);
 297      } // end func setAttributes
 298  
 299      /**
 300       * Returns the assoc array (default) or string of attributes
 301       *
 302       * @param     bool    Whether to return the attributes as string 
 303       * @since     1.6
 304       * @access    public
 305       * @return    mixed   attributes
 306       */
 307      function getAttributes($asString = false)
 308      {
 309          if ($asString) {
 310              return $this->_getAttrString($this->_attributes);
 311          } else {
 312              return $this->_attributes;
 313          }
 314      } //end func getAttributes
 315  
 316      /**
 317       * Updates the passed attributes without changing the other existing attributes
 318       * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
 319       * @access   public
 320       */
 321      function updateAttributes($attributes)
 322      {
 323          $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
 324      } // end func updateAttributes
 325  
 326      /**
 327       * Removes an attribute
 328       * 
 329       * @param     string    $attr   Attribute name
 330       * @since     1.4
 331       * @access    public
 332       * @return    void
 333       * @throws
 334       */
 335      function removeAttribute($attr)
 336      {
 337          $this->_removeAttr($attr, $this->_attributes);
 338      } //end func removeAttribute
 339  
 340      /**
 341       * Sets the line end style to Windows, Mac, Unix or a custom string.
 342       * 
 343       * @param   string  $style  "win", "mac", "unix" or custom string.
 344       * @since   1.7
 345       * @access  public
 346       * @return  void
 347       */
 348      function setLineEnd($style)
 349      {
 350          switch ($style) {
 351              case 'win':
 352                  $this->_lineEnd = "\15\12";
 353                  break;
 354              case 'unix':
 355                  $this->_lineEnd = "\12";
 356                  break;
 357              case 'mac':
 358                  $this->_lineEnd = "\15";
 359                  break;
 360              default:
 361                  $this->_lineEnd = $style;
 362          }
 363      } // end func setLineEnd
 364  
 365      /**
 366       * Sets the tab offset
 367       *
 368       * @param    int     $offset
 369       * @access   public
 370       */
 371      function setTabOffset($offset)
 372      {
 373          $this->_tabOffset = $offset;
 374      } // end func setTabOffset
 375  
 376      /**
 377       * Returns the tabOffset
 378       * 
 379       * @since     1.5
 380       * @access    public
 381       * @return    int
 382       */
 383      function getTabOffset()
 384      {
 385          return $this->_tabOffset;
 386      } //end func getTabOffset
 387  
 388      /**
 389       * Sets the string used to indent HTML
 390       * 
 391       * @since     1.7
 392       * @param     string    $string     String used to indent ("\11", "\t", '  ', etc.).
 393       * @access    public
 394       * @return    void
 395       */
 396      function setTab($string)
 397      {
 398          $this->_tab = $string;
 399      } // end func setTab
 400  
 401      /**
 402       * Sets the HTML comment to be displayed at the beginning of the HTML string
 403       *
 404       * @param     string
 405       * @since     1.4
 406       * @access    public
 407       * @return    void
 408       */
 409      function setComment($comment)
 410      {
 411          $this->_comment = $comment;
 412      } // end func setHtmlComment
 413  
 414      /**
 415       * Returns the HTML comment
 416       * 
 417       * @since     1.5
 418       * @access    public
 419       * @return    string
 420       */
 421      function getComment()
 422      {
 423          return $this->_comment;
 424      } //end func getComment
 425  
 426      /**
 427       * Abstract method.  Must be extended to return the objects HTML
 428       *
 429       * @access    public
 430       * @return    string
 431       * @abstract
 432       */
 433      function toHtml()
 434      {
 435          return '';
 436      } // end func toHtml
 437  
 438      /**
 439       * Displays the HTML to the screen
 440       *
 441       * @access    public
 442       */
 443      function display()
 444      {
 445          print $this->toHtml();
 446      } // end func display
 447  
 448  } // end class HTML_Common
 449  ?>