[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/ -> autocomplete.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   * autocomplete type form element
  20   *
  21   * Contains HTML class for a autocomplete type element
  22   *
  23   * @package   core_form
  24   * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->libdir . '/form/select.php');
  31  
  32  /**
  33   * Autocomplete as you type form element
  34   *
  35   * HTML class for a autocomplete type element
  36   *
  37   * @package   core_form
  38   * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class MoodleQuickForm_autocomplete extends MoodleQuickForm_select {
  42  
  43      /** @var boolean $tags Should we allow typing new entries to the field? */
  44      protected $tags = false;
  45      /** @var string $ajax Name of an AMD module to send/process ajax requests. */
  46      protected $ajax = '';
  47      /** @var string $placeholder Placeholder text for an empty list. */
  48      protected $placeholder = '';
  49      /** @var bool $casesensitive Whether the search has to be case-sensitive. */
  50      protected $casesensitive = false;
  51      /** @var bool $showsuggestions Show suggestions by default - but this can be turned off. */
  52      protected $showsuggestions = true;
  53      /** @var string $noselectionstring String that is shown when there are no selections. */
  54      protected $noselectionstring = '';
  55  
  56      /**
  57       * constructor
  58       *
  59       * @param string $elementName Select name attribute
  60       * @param mixed $elementLabel Label(s) for the select
  61       * @param mixed $options Data to be used to populate options
  62       * @param mixed $attributes Either a typical HTML attribute string or an associative array. Special options
  63       *                          "tags", "placeholder", "ajax", "multiple", "casesensitive" are supported.
  64       */
  65      public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
  66          // Even if the constructor gets called twice we do not really want 2x options (crazy forms!).
  67          $this->_options = array();
  68          if ($attributes === null) {
  69              $attributes = array();
  70          }
  71          if (isset($attributes['tags'])) {
  72              $this->tags = $attributes['tags'];
  73              unset($attributes['tags']);
  74          }
  75          if (isset($attributes['showsuggestions'])) {
  76              $this->showsuggestions = $attributes['showsuggestions'];
  77              unset($attributes['showsuggestions']);
  78          }
  79          $this->placeholder = get_string('search');
  80          if (isset($attributes['placeholder'])) {
  81              $this->placeholder = $attributes['placeholder'];
  82              unset($attributes['placeholder']);
  83          }
  84          $this->noselectionstring = get_string('noselection', 'form');
  85          if (isset($attributes['noselectionstring'])) {
  86              $this->noselectionstring = $attributes['noselectionstring'];
  87              unset($attributes['noselectionstring']);
  88          }
  89  
  90          if (isset($attributes['ajax'])) {
  91              $this->ajax = $attributes['ajax'];
  92              unset($attributes['ajax']);
  93          }
  94          if (isset($attributes['casesensitive'])) {
  95              $this->casesensitive = $attributes['casesensitive'] ? true : false;
  96              unset($attributes['casesensitive']);
  97          }
  98          parent::__construct($elementName, $elementLabel, $options, $attributes);
  99  
 100          $this->_type = 'autocomplete';
 101      }
 102  
 103      /**
 104       * Old syntax of class constructor. Deprecated in PHP7.
 105       *
 106       * @deprecated since Moodle 3.1
 107       */
 108      public function MoodleQuickForm_autocomplete($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
 109          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
 110          self::__construct($elementName, $elementLabel, $options, $attributes);
 111      }
 112  
 113      /**
 114       * Returns HTML for select form element.
 115       *
 116       * @return string
 117       */
 118      function toHtml(){
 119          global $PAGE;
 120  
 121          // Enhance the select with javascript.
 122          $this->_generateId();
 123          $id = $this->getAttribute('id');
 124          $PAGE->requires->js_call_amd('core/form-autocomplete', 'enhance', $params = array('#' . $id, $this->tags, $this->ajax,
 125              $this->placeholder, $this->casesensitive, $this->showsuggestions, $this->noselectionstring));
 126  
 127          return parent::toHTML();
 128      }
 129  
 130      /**
 131       * Search the current list of options to see if there are any options with this value.
 132       * @param  string $value to search
 133       * @return boolean
 134       */
 135      function optionExists($value) {
 136          foreach ($this->_options as $option) {
 137              if (isset($option['attr']['value']) && ($option['attr']['value'] == $value)) {
 138                  return true;
 139              }
 140          }
 141          return false;
 142      }
 143  
 144      /**
 145       * Set the value of this element. If values can be added or are unknown, we will
 146       * make sure they exist in the options array.
 147       * @param  mixed string|array $value The value to set.
 148       * @return boolean
 149       */
 150      function setValue($value) {
 151          $values = (array) $value;
 152          foreach ($values as $onevalue) {
 153              if (($this->tags || $this->ajax) &&
 154                      (!$this->optionExists($onevalue)) &&
 155                      ($onevalue !== '_qf__force_multiselect_submission')) {
 156                  $this->addOption($onevalue, $onevalue);
 157              }
 158          }
 159          return parent::setValue($value);
 160      }
 161  
 162      /**
 163       * Returns a 'safe' element's value
 164       *
 165       * @param  array   array of submitted values to search
 166       * @param  bool    whether to return the value as associative array
 167       * @access public
 168       * @return mixed
 169       */
 170      function exportValue(&$submitValues, $assoc = false) {
 171          if ($this->ajax || $this->tags) {
 172              // When this was an ajax request, we do not know the allowed list of values.
 173              $value = $this->_findValue($submitValues);
 174              if (null === $value) {
 175                  $value = $this->getValue();
 176              }
 177              // Quickforms inserts a duplicate element in the form with
 178              // this value so that a value is always submitted for this form element.
 179              // Normally this is cleaned as a side effect of it not being a valid option,
 180              // but in this case we need to detect and skip it manually.
 181              if ($value === '_qf__force_multiselect_submission' || $value === null) {
 182                  $value = '';
 183              }
 184              return $this->_prepareValue($value, $assoc);
 185          } else {
 186              return parent::exportValue($submitValues, $assoc);
 187          }
 188      }
 189  
 190      /**
 191       * Called by HTML_QuickForm whenever form event is made on this element
 192       *
 193       * @param string $event Name of event
 194       * @param mixed $arg event arguments
 195       * @param object $caller calling object
 196       * @return bool
 197       */
 198      function onQuickFormEvent($event, $arg, &$caller)
 199      {
 200          switch ($event) {
 201              case 'createElement':
 202                  $caller->setType($arg[0], PARAM_TAGLIST);
 203                  break;
 204          }
 205          return parent::onQuickFormEvent($event, $arg, $caller);
 206      }
 207  }


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