[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/ -> select.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  
  18  /**
  19   * select type form element
  20   *
  21   * Contains HTML class for a select type element
  22   *
  23   * @package   core_form
  24   * @copyright 2006 Jamie Pratt <me@jamiep.org>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  require_once('HTML/QuickForm/select.php');
  29  
  30  /**
  31   * select type form element
  32   *
  33   * HTML class for a select type element
  34   *
  35   * @package   core_form
  36   * @category  form
  37   * @copyright 2006 Jamie Pratt <me@jamiep.org>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class MoodleQuickForm_select extends HTML_QuickForm_select{
  41      /** @var string html for help button, if empty then no help */
  42      var $_helpbutton='';
  43  
  44      /** @var bool if true label will be hidden */
  45      var $_hiddenLabel=false;
  46  
  47      /**
  48       * constructor
  49       *
  50       * @param string $elementName Select name attribute
  51       * @param mixed $elementLabel Label(s) for the select
  52       * @param mixed $options Data to be used to populate options
  53       * @param mixed $attributes Either a typical HTML attribute string or an associative array
  54       */
  55      public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
  56          parent::__construct($elementName, $elementLabel, $options, $attributes);
  57      }
  58  
  59      /**
  60       * Old syntax of class constructor. Deprecated in PHP7.
  61       *
  62       * @deprecated since Moodle 3.1
  63       */
  64      public function MoodleQuickForm_select($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
  65          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  66          self::__construct($elementName, $elementLabel, $options, $attributes);
  67      }
  68  
  69      /**
  70       * Sets label to be hidden
  71       *
  72       * @param bool $hiddenLabel sets if label should be hidden
  73       */
  74      function setHiddenLabel($hiddenLabel){
  75          $this->_hiddenLabel = $hiddenLabel;
  76      }
  77  
  78      /**
  79       * Returns HTML for select form element.
  80       *
  81       * @return string
  82       */
  83      function toHtml(){
  84          $html = '';
  85          if ($this->getMultiple()) {
  86              // Adding an hidden field forces the browser to send an empty data even though the user did not
  87              // select any element. This value will be cleaned up in self::exportValue() as it will not be part
  88              // of the select options.
  89              $html .= '<input type="hidden" name="'.$this->getName().'" value="_qf__force_multiselect_submission">';
  90          }
  91          if ($this->_hiddenLabel){
  92              $this->_generateId();
  93              $html .= '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.$this->getLabel().'</label>';
  94          }
  95          $html .= parent::toHtml();
  96          return $html;
  97      }
  98  
  99      /**
 100       * get html for help button
 101       *
 102       * @return string html for help button
 103       */
 104      function getHelpButton(){
 105          return $this->_helpbutton;
 106      }
 107  
 108      /**
 109       * Removes an OPTION from the SELECT
 110       *
 111       * @param string $value Value for the OPTION to remove
 112       * @return void
 113       */
 114      function removeOption($value)
 115      {
 116          $key=array_search($value, $this->_values);
 117          if ($key!==FALSE and $key!==null) {
 118              unset($this->_values[$key]);
 119          }
 120          foreach ($this->_options as $key=>$option){
 121              if ($option['attr']['value']==$value){
 122                  unset($this->_options[$key]);
 123                  // we must reindex the options because the ugly code in quickforms' select.php expects that keys are 0,1,2,3... !?!?
 124                  $this->_options = array_merge($this->_options);
 125                  return;
 126              }
 127          }
 128      }
 129  
 130      /**
 131       * Removes all OPTIONs from the SELECT
 132       */
 133      function removeOptions()
 134      {
 135          $this->_options = array();
 136      }
 137  
 138      /**
 139       * Slightly different container template when frozen. Don't want to use a label tag
 140       * with a for attribute in that case for the element label but instead use a div.
 141       * Templates are defined in renderer constructor.
 142       *
 143       * @return string
 144       */
 145      function getElementTemplateType(){
 146          if ($this->_flagFrozen){
 147              return 'static';
 148          } else {
 149              return 'default';
 150          }
 151      }
 152  
 153     /**
 154      * We check the options and return only the values that _could_ have been
 155      * selected. We also return a scalar value if select is not "multiple"
 156      *
 157      * @param array $submitValues submitted values
 158      * @param bool $assoc if true the retured value is associated array
 159      * @return mixed
 160      */
 161      function exportValue(&$submitValues, $assoc = false)
 162      {
 163          if (empty($this->_options)) {
 164              return $this->_prepareValue(null, $assoc);
 165          }
 166  
 167          $value = $this->_findValue($submitValues);
 168          if (is_null($value)) {
 169              $value = $this->getValue();
 170          }
 171          $value = (array)$value;
 172  
 173          $cleaned = array();
 174          foreach ($value as $v) {
 175              foreach ($this->_options as $option) {
 176                  if ((string)$option['attr']['value'] === (string)$v) {
 177                      $cleaned[] = (string)$option['attr']['value'];
 178                      break;
 179                  }
 180              }
 181          }
 182  
 183          if (empty($cleaned)) {
 184              return $this->_prepareValue(null, $assoc);
 185          }
 186          if ($this->getMultiple()) {
 187              return $this->_prepareValue($cleaned, $assoc);
 188          } else {
 189              return $this->_prepareValue($cleaned[0], $assoc);
 190          }
 191      }
 192  }


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