[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  // +----------------------------------------------------------------------+
   4  // | PHP version 4.0                                                      |
   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  // | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
  17  // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  18  // +----------------------------------------------------------------------+
  19  //
  20  // $Id$
  21  
  22  require_once('HTML/Common.php');
  23  
  24  /**
  25   * Base class for form elements
  26   * 
  27   * @author       Adam Daniel <adaniel1@eesus.jnj.com>
  28   * @author       Bertrand Mansion <bmansion@mamasam.com>
  29   * @version      1.3
  30   * @since        PHP4.04pl1
  31   * @access       public
  32   * @abstract
  33   */
  34  class HTML_QuickForm_element extends HTML_Common
  35  {
  36      // {{{ properties
  37  
  38      /**
  39       * Label of the field
  40       * @var       string
  41       * @since     1.3
  42       * @access    private
  43       */
  44      var $_label = '';
  45  
  46      /**
  47       * Form element type
  48       * @var       string
  49       * @since     1.0
  50       * @access    private
  51       */
  52      var $_type = '';
  53  
  54      /**
  55       * Flag to tell if element is frozen
  56       * @var       boolean
  57       * @since     1.0
  58       * @access    private
  59       */
  60      var $_flagFrozen = false;
  61  
  62      /**
  63       * Does the element support persistant data when frozen
  64       * @var       boolean
  65       * @since     1.3
  66       * @access    private
  67       */
  68      var $_persistantFreeze = false;
  69      
  70      // }}}
  71      // {{{ constructor
  72      
  73      /**
  74       * Class constructor
  75       * 
  76       * @param    string     Name of the element
  77       * @param    mixed      Label(s) for the element
  78       * @param    mixed      Associative array of tag attributes or HTML attributes name="value" pairs
  79       * @since     1.0
  80       * @access    public
  81       * @return    void
  82       */
  83      public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
  84          parent::__construct($attributes);
  85          if (isset($elementName)) {
  86              $this->setName($elementName);
  87          }
  88          if (isset($elementLabel)) {
  89              $this->setLabel($elementLabel);
  90          }
  91      } //end constructor
  92  
  93      /**
  94       * Old syntax of class constructor. Deprecated in PHP7.
  95       *
  96       * @deprecated since Moodle 3.1
  97       */
  98      public function HTML_QuickForm_element($elementName=null, $elementLabel=null, $attributes=null) {
  99          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 100          self::__construct($elementName, $elementLabel, $attributes);
 101      }
 102      
 103      // }}}
 104      // {{{ apiVersion()
 105  
 106      /**
 107       * Returns the current API version
 108       *
 109       * @since     1.0
 110       * @access    public
 111       * @return    float
 112       */
 113      function apiVersion()
 114      {
 115          return 2.0;
 116      } // end func apiVersion
 117  
 118      // }}}
 119      // {{{ getType()
 120  
 121      /**
 122       * Returns element type
 123       *
 124       * @since     1.0
 125       * @access    public
 126       * @return    string
 127       */
 128      function getType()
 129      {
 130          return $this->_type;
 131      } // end func getType
 132  
 133      // }}}
 134      // {{{ setName()
 135  
 136      /**
 137       * Sets the input field name
 138       * 
 139       * @param     string    $name   Input field name attribute
 140       * @since     1.0
 141       * @access    public
 142       * @return    void
 143       */
 144      function setName($name)
 145      {
 146          // interface method
 147      } //end func setName
 148      
 149      // }}}
 150      // {{{ getName()
 151  
 152      /**
 153       * Returns the element name
 154       * 
 155       * @since     1.0
 156       * @access    public
 157       * @return    string
 158       */
 159      function getName()
 160      {
 161          // interface method
 162      } //end func getName
 163      
 164      // }}}
 165      // {{{ setValue()
 166  
 167      /**
 168       * Sets the value of the form element
 169       *
 170       * @param     string    $value      Default value of the form element
 171       * @since     1.0
 172       * @access    public
 173       * @return    void
 174       */
 175      function setValue($value)
 176      {
 177          // interface
 178      } // end func setValue
 179  
 180      // }}}
 181      // {{{ getValue()
 182  
 183      /**
 184       * Returns the value of the form element
 185       *
 186       * @since     1.0
 187       * @access    public
 188       * @return    mixed
 189       */
 190      function getValue()
 191      {
 192          // interface
 193          return null;
 194      } // end func getValue
 195      
 196      // }}}
 197      // {{{ freeze()
 198  
 199      /**
 200       * Freeze the element so that only its value is returned
 201       * 
 202       * @access    public
 203       * @return    void
 204       */
 205      function freeze()
 206      {
 207          $this->_flagFrozen = true;
 208      } //end func freeze
 209  
 210      // }}}
 211      // {{{ unfreeze()
 212  
 213     /**
 214      * Unfreezes the element so that it becomes editable
 215      *
 216      * @access public
 217      * @return void
 218      * @since  3.2.4
 219      */
 220      function unfreeze()
 221      {
 222          $this->_flagFrozen = false;
 223      }
 224  
 225      // }}}
 226      // {{{ getFrozenHtml()
 227  
 228      /**
 229       * Returns the value of field without HTML tags
 230       * 
 231       * @since     1.0
 232       * @access    public
 233       * @return    string
 234       */
 235      function getFrozenHtml()
 236      {
 237          $value = $this->getValue();
 238          return ('' != $value? htmlspecialchars($value): '&nbsp;') .
 239                 $this->_getPersistantData();
 240      } //end func getFrozenHtml
 241      
 242      // }}}
 243      // {{{ _getPersistantData()
 244  
 245     /**
 246      * Used by getFrozenHtml() to pass the element's value if _persistantFreeze is on
 247      * 
 248      * @access private
 249      * @return string
 250      */
 251      function _getPersistantData()
 252      {
 253          if (!$this->_persistantFreeze) {
 254              return '';
 255          } else {
 256              $id = $this->getAttribute('id');
 257              if (isset($id)) {
 258                  // Id of persistant input is different then the actual input.
 259                  $id = array('id' => $id . '_persistant');
 260              } else {
 261                  $id = array();
 262              }
 263  
 264              return '<input' . $this->_getAttrString(array(
 265                         'type'  => 'hidden',
 266                         'name'  => $this->getName(),
 267                         'value' => $this->getValue()
 268                     ) + $id) . ' />';
 269          }
 270      }
 271  
 272      // }}}
 273      // {{{ isFrozen()
 274  
 275      /**
 276       * Returns whether or not the element is frozen
 277       *
 278       * @since     1.3
 279       * @access    public
 280       * @return    bool
 281       */
 282      function isFrozen()
 283      {
 284          return $this->_flagFrozen;
 285      } // end func isFrozen
 286  
 287      // }}}
 288      // {{{ setPersistantFreeze()
 289  
 290      /**
 291       * Sets wether an element value should be kept in an hidden field
 292       * when the element is frozen or not
 293       * 
 294       * @param     bool    $persistant   True if persistant value
 295       * @since     2.0
 296       * @access    public
 297       * @return    void
 298       */
 299      function setPersistantFreeze($persistant=false)
 300      {
 301          $this->_persistantFreeze = $persistant;
 302      } //end func setPersistantFreeze
 303  
 304      // }}}
 305      // {{{ setLabel()
 306  
 307      /**
 308       * Sets display text for the element
 309       * 
 310       * @param     string    $label  Display text for the element
 311       * @since     1.3
 312       * @access    public
 313       * @return    void
 314       */
 315      function setLabel($label)
 316      {
 317          $this->_label = $label;
 318      } //end func setLabel
 319  
 320      // }}}
 321      // {{{ getLabel()
 322  
 323      /**
 324       * Returns display text for the element
 325       * 
 326       * @since     1.3
 327       * @access    public
 328       * @return    string
 329       */
 330      function getLabel()
 331      {
 332          return $this->_label;
 333      } //end func getLabel
 334  
 335      // }}}
 336      // {{{ _findValue()
 337  
 338      /**
 339       * Tries to find the element value from the values array
 340       * 
 341       * @since     2.7
 342       * @access    private
 343       * @return    mixed
 344       */
 345      function _findValue(&$values)
 346      {
 347          if (empty($values)) {
 348              return null;
 349          }
 350          $elementName = $this->getName();
 351          if (isset($values[$elementName])) {
 352              return $values[$elementName];
 353          } elseif (strpos($elementName, '[')) {
 354              $myVar = "['" . str_replace(array(']', '['), array('', "']['"), $elementName) . "']";
 355              return eval("return (isset(\$values$myVar)) ? \$values$myVar : null;");
 356          } else {
 357              return null;
 358          }
 359      } //end func _findValue
 360  
 361      // }}}
 362      // {{{ onQuickFormEvent()
 363  
 364      /**
 365       * Called by HTML_QuickForm whenever form event is made on this element
 366       *
 367       * @param     string    $event  Name of event
 368       * @param     mixed     $arg    event arguments
 369       * @param     object    $caller calling object
 370       * @since     1.0
 371       * @access    public
 372       * @return    void
 373       */
 374      function onQuickFormEvent($event, $arg, &$caller)
 375      {
 376          switch ($event) {
 377              case 'createElement':
 378                  static::__construct($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
 379                  break;
 380              case 'addElement':
 381                  $this->onQuickFormEvent('createElement', $arg, $caller);
 382                  $this->onQuickFormEvent('updateValue', null, $caller);
 383                  break;
 384              case 'updateValue':
 385                  // constant values override both default and submitted ones
 386                  // default values are overriden by submitted
 387                  $value = $this->_findValue($caller->_constantValues);
 388                  if (null === $value) {
 389                      $value = $this->_findValue($caller->_submitValues);
 390                      if (null === $value) {
 391                          $value = $this->_findValue($caller->_defaultValues);
 392                      }
 393                  }
 394                  if (null !== $value) {
 395                      $this->setValue($value);
 396                  }
 397                  break;
 398              case 'setGroupValue':
 399                  $this->setValue($arg);
 400          }
 401          return true;
 402      } // end func onQuickFormEvent
 403  
 404      // }}}
 405      // {{{ accept()
 406  
 407     /**
 408      * Accepts a renderer
 409      *
 410      * @param object     An HTML_QuickForm_Renderer object
 411      * @param bool       Whether an element is required
 412      * @param string     An error message associated with an element
 413      * @access public
 414      * @return void 
 415      */
 416      function accept(&$renderer, $required=false, $error=null)
 417      {
 418          $renderer->renderElement($this, $required, $error);
 419      } // end func accept
 420  
 421      // }}}
 422      // {{{ _generateId()
 423  
 424     /**
 425      * Automatically generates and assigns an 'id' attribute for the element.
 426      * 
 427      * Currently used to ensure that labels work on radio buttons and
 428      * checkboxes. Per idea of Alexander Radivanovich.
 429      *
 430      * @access private
 431      * @return void 
 432      */
 433      function _generateId() {
 434          if ($this->getAttribute('id')) {
 435              return;
 436          }
 437  
 438          $id = $this->getName();
 439          $id = 'id_' . str_replace(array('qf_', '[', ']'), array('', '_', ''), $id);
 440          $id = clean_param($id, PARAM_ALPHANUMEXT);
 441          $this->updateAttributes(array('id' => $id));
 442      }
 443  
 444      // }}}
 445      // {{{ exportValue()
 446  
 447     /**
 448      * Returns a 'safe' element's value
 449      *
 450      * @param  array   array of submitted values to search
 451      * @param  bool    whether to return the value as associative array
 452      * @access public
 453      * @return mixed
 454      */
 455      function exportValue(&$submitValues, $assoc = false)
 456      {
 457          $value = $this->_findValue($submitValues);
 458          if (null === $value) {
 459              $value = $this->getValue();
 460          }
 461          return $this->_prepareValue($value, $assoc);
 462      }
 463      
 464      // }}}
 465      // {{{ _prepareValue()
 466  
 467     /**
 468      * Used by exportValue() to prepare the value for returning
 469      *
 470      * @param  mixed   the value found in exportValue()
 471      * @param  bool    whether to return the value as associative array
 472      * @access private
 473      * @return mixed
 474      */
 475      function _prepareValue($value, $assoc)
 476      {
 477          if (null === $value) {
 478              return null;
 479          } elseif (!$assoc) {
 480              return $value;
 481          } else {
 482              $name = $this->getName();
 483              if (!strpos($name, '[')) {
 484                  return array($name => $value);
 485              } else {
 486                  $valueAry = array();
 487                  $myIndex  = "['" . str_replace(array(']', '['), array('', "']['"), $name) . "']";
 488                  eval("\$valueAry$myIndex = \$value;");
 489                  return $valueAry;
 490              }
 491          }
 492      }
 493      
 494      // }}}
 495  } // end class HTML_QuickForm_element
 496  ?>


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1