Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / form / selectwithlink.php
blobfc4a91598ebfcf1e100d1ded908dfb9d9bbbc167
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 * select type form element
21 * Contains HTML class for a select type element with options containing link
23 * @package core_form
24 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('HTML/QuickForm/select.php');
29 require_once('templatable_form_element.php');
31 /**
32 * select type form element
34 * HTML class for a select type element with options containing link
36 * @deprecated since 3.2
37 * @package core_form
38 * @category form
39 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select implements templatable {
44 use templatable_form_element {
45 export_for_template as export_for_template_base;
47 /** @var string html for help button, if empty then no help */
48 var $_helpbutton='';
50 /** @var bool if true label will be hidden */
51 var $_hiddenLabel=false;
53 /** @var string url to which select option will be posted */
54 var $_link=null;
56 /** @var string data which will be posted to link */
57 var $_linklabel=null;
59 /** @var string url return link */
60 var $_linkreturn=null;
62 /**
63 * constructor
65 * @param string $elementName Select name attribute
66 * @param mixed $elementLabel Label(s) for the select
67 * @param array $options Data to be used to populate options
68 * @param mixed $attributes Either a typical HTML attribute string or an associative array
69 * @param bool $linkdata data to be posted
71 public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null) {
72 if (!empty($linkdata['link']) && !empty($linkdata['label'])) {
73 $this->_link = $linkdata['link'];
74 $this->_linklabel = $linkdata['label'];
77 if (!empty($linkdata['return'])) {
78 $this->_linkreturn = $linkdata['return'];
81 parent::__construct($elementName, $elementLabel, $options, $attributes);
83 $this->_type = 'selectwithlink';
86 /**
87 * Old syntax of class constructor. Deprecated in PHP7.
89 * @deprecated since Moodle 3.1
91 public function MoodleQuickForm_selectwithlink($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null) {
92 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
93 self::__construct($elementName, $elementLabel, $options, $attributes, $linkdata);
96 /**
97 * Sets label to be hidden
99 * @param bool $hiddenLabel sets if label should be hidden
101 function setHiddenLabel($hiddenLabel){
102 $this->_hiddenLabel = $hiddenLabel;
106 * Returns the SELECT in HTML
108 * @return string
110 function toHtml(){
111 $retval = '';
112 if ($this->_hiddenLabel){
113 $this->_generateId();
114 $retval = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
115 $this->getLabel().'</label>'.parent::toHtml();
116 } else {
117 $retval = parent::toHtml();
120 if (!empty($this->_link)) {
121 if (!empty($this->_linkreturn) && is_array($this->_linkreturn)) {
122 $appendchar = '?';
123 if (strstr($this->_link, '?')) {
124 $appendchar = '&amp;';
127 foreach ($this->_linkreturn as $key => $val) {
128 $this->_link .= $appendchar."$key=$val";
129 $appendchar = '&amp;';
133 $retval .= '<a style="margin-left: 5px" href="'.$this->_link.'">'.$this->_linklabel.'</a>';
136 return $retval;
140 * get html for help button
142 * @return string html for help button
144 function getHelpButton(){
145 return $this->_helpbutton;
149 * Removes an OPTION from the SELECT
151 * @param string $value Value for the OPTION to remove
153 function removeOption($value)
155 $key=array_search($value, $this->_values);
156 if ($key!==FALSE and $key!==null) {
157 unset($this->_values[$key]);
159 foreach ($this->_options as $key=>$option){
160 if ($option['attr']['value']==$value){
161 unset($this->_options[$key]);
162 return;
168 * Removes all OPTIONs from the SELECT
170 function removeOptions()
172 $this->_options = array();
176 * Slightly different container template when frozen. Don't want to use a label tag
177 * with a for attribute in that case for the element label but instead use a div.
178 * Templates are defined in renderer constructor.
180 * @return string
182 function getElementTemplateType(){
183 if ($this->_flagFrozen){
184 return 'static';
185 } else {
186 return 'default';
191 * We check the options and return only the values that _could_ have been
192 * selected. We also return a scalar value if select is not "multiple"
194 * @param array $submitValues submitted values
195 * @param bool $assoc if true the retured value is associated array
196 * @return mixed
198 function exportValue(&$submitValues, $assoc = false)
200 if (empty($this->_options)) {
201 return $this->_prepareValue(null, $assoc);
204 $value = $this->_findValue($submitValues);
205 if (is_null($value)) {
206 $value = $this->getValue();
208 $value = (array)$value;
210 $cleaned = array();
211 foreach ($value as $v) {
212 foreach ($this->_options as $option) {
213 if ((string)$option['attr']['value'] === (string)$v) {
214 $cleaned[] = (string)$option['attr']['value'];
215 break;
220 if (empty($cleaned)) {
221 return $this->_prepareValue(null, $assoc);
223 if ($this->getMultiple()) {
224 return $this->_prepareValue($cleaned, $assoc);
225 } else {
226 return $this->_prepareValue($cleaned[0], $assoc);
230 public function export_for_template(renderer_base $output) {
231 $context = $this->export_for_template_base($output);
233 $options = [];
234 // Standard option attributes.
235 $standardoptionattributes = ['text', 'value', 'selected', 'disabled'];
236 foreach ($this->_options as $option) {
237 if (is_array($this->_values) && in_array( (string) $option['attr']['value'], $this->_values)) {
238 $this->_updateAttrArray($option['attr'], ['selected' => 'selected']);
240 $o = [
241 'text' => $option['text'],
242 'value' => $option['attr']['value'],
243 'selected' => !empty($option['attr']['selected']),
244 'disabled' => !empty($option['attr']['disabled']),
246 // Set other attributes.
247 $otheroptionattributes = [];
248 foreach ($option['attr'] as $attr => $value) {
249 if (!in_array($attr, $standardoptionattributes) && $attr != 'class' && !is_object($value)) {
250 $otheroptionattributes[] = $attr . '="' . s($value) . '"';
253 $o['optionattributes'] = implode(' ', $otheroptionattributes);
254 $options[] = $o;
256 $context['options'] = $options;
257 if (!empty($this->_link)) {
258 if (!empty($this->_linkreturn) && is_array($this->_linkreturn)) {
259 $appendchar = '?';
260 if (strstr($this->_link, '?')) {
261 $appendchar = '&amp;';
264 foreach ($this->_linkreturn as $key => $val) {
265 $this->_link .= $appendchar."$key=$val";
266 $appendchar = '&amp;';
270 $context['link'] = $this->_link;
271 $context['linklabel'] = $this->_linklabel;
273 return $context;