MDL-56989 boost: don't double-escape page titles
[moodle.git] / lib / form / autocomplete.php
blob0e501ab39656b6b1938038bf22e0a0204e99bbb0
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.
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/>.
18 /**
19 * autocomplete type form element
21 * Contains HTML class for a autocomplete type element
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
28 global $CFG;
30 require_once($CFG->libdir . '/form/select.php');
32 /**
33 * Autocomplete as you type form element
35 * HTML class for a autocomplete type element
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
41 class MoodleQuickForm_autocomplete extends MoodleQuickForm_select {
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 = '';
56 /**
57 * constructor
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.
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();
71 if (isset($attributes['tags'])) {
72 $this->tags = $attributes['tags'];
73 unset($attributes['tags']);
75 if (isset($attributes['showsuggestions'])) {
76 $this->showsuggestions = $attributes['showsuggestions'];
77 unset($attributes['showsuggestions']);
79 $this->placeholder = get_string('search');
80 if (isset($attributes['placeholder'])) {
81 $this->placeholder = $attributes['placeholder'];
82 unset($attributes['placeholder']);
84 $this->noselectionstring = get_string('noselection', 'form');
85 if (isset($attributes['noselectionstring'])) {
86 $this->noselectionstring = $attributes['noselectionstring'];
87 unset($attributes['noselectionstring']);
90 if (isset($attributes['ajax'])) {
91 $this->ajax = $attributes['ajax'];
92 unset($attributes['ajax']);
94 if (isset($attributes['casesensitive'])) {
95 $this->casesensitive = $attributes['casesensitive'] ? true : false;
96 unset($attributes['casesensitive']);
98 parent::__construct($elementName, $elementLabel, $options, $attributes);
100 $this->_type = 'autocomplete';
104 * Old syntax of class constructor. Deprecated in PHP7.
106 * @deprecated since Moodle 3.1
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);
114 * Returns HTML for select form element.
116 * @return string
118 function toHtml(){
119 global $PAGE;
121 // Enhance the select with javascript.
122 $this->_generateId();
123 $id = $this->getAttribute('id');
125 if (!$this->isFrozen()) {
126 $PAGE->requires->js_call_amd('core/form-autocomplete', 'enhance', $params = array('#' . $id, $this->tags, $this->ajax,
127 $this->placeholder, $this->casesensitive, $this->showsuggestions, $this->noselectionstring));
130 return parent::toHTML();
134 * Search the current list of options to see if there are any options with this value.
135 * @param string $value to search
136 * @return boolean
138 function optionExists($value) {
139 foreach ($this->_options as $option) {
140 if (isset($option['attr']['value']) && ($option['attr']['value'] == $value)) {
141 return true;
144 return false;
148 * Set the value of this element. If values can be added or are unknown, we will
149 * make sure they exist in the options array.
150 * @param mixed string|array $value The value to set.
151 * @return boolean
153 function setValue($value) {
154 $values = (array) $value;
155 foreach ($values as $onevalue) {
156 if (($this->tags || $this->ajax) &&
157 (!$this->optionExists($onevalue)) &&
158 ($onevalue !== '_qf__force_multiselect_submission')) {
159 $this->addOption($onevalue, $onevalue);
162 return parent::setValue($value);
166 * Returns a 'safe' element's value
168 * @param array array of submitted values to search
169 * @param bool whether to return the value as associative array
170 * @access public
171 * @return mixed
173 function exportValue(&$submitValues, $assoc = false) {
174 if ($this->ajax || $this->tags) {
175 // When this was an ajax request, we do not know the allowed list of values.
176 $value = $this->_findValue($submitValues);
177 if (null === $value) {
178 $value = $this->getValue();
180 // Quickforms inserts a duplicate element in the form with
181 // this value so that a value is always submitted for this form element.
182 // Normally this is cleaned as a side effect of it not being a valid option,
183 // but in this case we need to detect and skip it manually.
184 if ($value === '_qf__force_multiselect_submission' || $value === null) {
185 $value = '';
187 return $this->_prepareValue($value, $assoc);
188 } else {
189 return parent::exportValue($submitValues, $assoc);
194 * Called by HTML_QuickForm whenever form event is made on this element
196 * @param string $event Name of event
197 * @param mixed $arg event arguments
198 * @param object $caller calling object
199 * @return bool
201 function onQuickFormEvent($event, $arg, &$caller)
203 switch ($event) {
204 case 'createElement':
205 $caller->setType($arg[0], PARAM_TAGLIST);
206 break;
208 return parent::onQuickFormEvent($event, $arg, $caller);
211 public function export_for_template(renderer_base $output) {
212 global $PAGE;
214 $this->_generateId();
215 $context = parent::export_for_template($output);
216 $context['tags'] = !empty($this->tags);
217 $context['ajax'] = $this->ajax;
218 $context['placeholder'] = $this->placeholder;
219 $context['casesensitive'] = !empty($this->casesensitive);
220 $context['showsuggestions'] = !empty($this->showsuggestions);
221 $context['noselectionstring'] = $this->noselectionstring;
223 return $context;