PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/form/select.php

https://github.com/mackensen/moodle
PHP | 231 lines | 116 code | 22 blank | 93 comment | 18 complexity | 0bdf50051d11b8f5b3117bde50522161 MD5 | raw file
  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. * select type form element
  18. *
  19. * Contains HTML class for a select type element
  20. *
  21. * @package core_form
  22. * @copyright 2006 Jamie Pratt <me@jamiep.org>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. require_once('HTML/QuickForm/select.php');
  26. require_once('templatable_form_element.php');
  27. /**
  28. * select type form element
  29. *
  30. * HTML class for a select type element
  31. *
  32. * @package core_form
  33. * @category form
  34. * @copyright 2006 Jamie Pratt <me@jamiep.org>
  35. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36. */
  37. class MoodleQuickForm_select extends HTML_QuickForm_select implements templatable {
  38. use templatable_form_element {
  39. export_for_template as export_for_template_base;
  40. }
  41. /** @var string html for help button, if empty then no help */
  42. var $_helpbutton='';
  43. /** @var bool if true label will be hidden */
  44. var $_hiddenLabel=false;
  45. /**
  46. * constructor
  47. *
  48. * @param string $elementName Select name attribute
  49. * @param mixed $elementLabel Label(s) for the select
  50. * @param mixed $options Data to be used to populate options
  51. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  52. */
  53. public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
  54. parent::__construct($elementName, $elementLabel, $options, $attributes);
  55. }
  56. /**
  57. * Old syntax of class constructor. Deprecated in PHP7.
  58. *
  59. * @deprecated since Moodle 3.1
  60. */
  61. public function MoodleQuickForm_select($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
  62. debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  63. self::__construct($elementName, $elementLabel, $options, $attributes);
  64. }
  65. /**
  66. * Sets label to be hidden
  67. *
  68. * @param bool $hiddenLabel sets if label should be hidden
  69. */
  70. function setHiddenLabel($hiddenLabel){
  71. $this->_hiddenLabel = $hiddenLabel;
  72. }
  73. /**
  74. * Returns HTML for select form element.
  75. *
  76. * @return string
  77. */
  78. function toHtml(){
  79. $html = '';
  80. if ($this->getMultiple()) {
  81. // Adding an hidden field forces the browser to send an empty data even though the user did not
  82. // select any element. This value will be cleaned up in self::exportValue() as it will not be part
  83. // of the select options.
  84. $html .= '<input type="hidden" name="'.$this->getName().'" value="_qf__force_multiselect_submission">';
  85. }
  86. if ($this->_hiddenLabel){
  87. $this->_generateId();
  88. $html .= '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.$this->getLabel().'</label>';
  89. }
  90. $html .= parent::toHtml();
  91. return $html;
  92. }
  93. /**
  94. * get html for help button
  95. *
  96. * @return string html for help button
  97. */
  98. function getHelpButton(){
  99. return $this->_helpbutton;
  100. }
  101. /**
  102. * Removes an OPTION from the SELECT
  103. *
  104. * @param string $value Value for the OPTION to remove
  105. * @return void
  106. */
  107. function removeOption($value)
  108. {
  109. $key=array_search($value, $this->_values);
  110. if ($key!==FALSE and $key!==null) {
  111. unset($this->_values[$key]);
  112. }
  113. foreach ($this->_options as $key=>$option){
  114. if ($option['attr']['value']==$value){
  115. unset($this->_options[$key]);
  116. // we must reindex the options because the ugly code in quickforms' select.php expects that keys are 0,1,2,3... !?!?
  117. $this->_options = array_merge($this->_options);
  118. return;
  119. }
  120. }
  121. }
  122. /**
  123. * Removes all OPTIONs from the SELECT
  124. */
  125. function removeOptions()
  126. {
  127. $this->_options = array();
  128. }
  129. /**
  130. * Slightly different container template when frozen. Don't want to use a label tag
  131. * with a for attribute in that case for the element label but instead use a div.
  132. * Templates are defined in renderer constructor.
  133. *
  134. * @return string
  135. */
  136. function getElementTemplateType(){
  137. if ($this->_flagFrozen){
  138. return 'static';
  139. } else {
  140. return 'default';
  141. }
  142. }
  143. /**
  144. * We check the options and return only the values that _could_ have been
  145. * selected. We also return a scalar value if select is not "multiple"
  146. *
  147. * @param array $submitValues submitted values
  148. * @param bool $assoc if true the retured value is associated array
  149. * @return mixed
  150. */
  151. function exportValue(&$submitValues, $assoc = false)
  152. {
  153. $emptyvalue = $this->getMultiple() ? [] : null;
  154. if (empty($this->_options)) {
  155. return $this->_prepareValue($emptyvalue, $assoc);
  156. }
  157. $value = $this->_findValue($submitValues);
  158. if (is_null($value)) {
  159. $value = $this->getValue();
  160. }
  161. $value = (array)$value;
  162. $cleaned = array();
  163. foreach ($value as $v) {
  164. foreach ($this->_options as $option) {
  165. if ((string)$option['attr']['value'] === (string)$v) {
  166. $cleaned[] = (string)$option['attr']['value'];
  167. break;
  168. }
  169. }
  170. }
  171. if (empty($cleaned)) {
  172. return $this->_prepareValue($emptyvalue, $assoc);
  173. }
  174. if ($this->getMultiple()) {
  175. return $this->_prepareValue($cleaned, $assoc);
  176. } else {
  177. return $this->_prepareValue($cleaned[0], $assoc);
  178. }
  179. }
  180. public function export_for_template(renderer_base $output) {
  181. $context = $this->export_for_template_base($output);
  182. $options = [];
  183. // Standard option attributes.
  184. $standardoptionattributes = ['text', 'value', 'selected', 'disabled'];
  185. foreach ($this->_options as $option) {
  186. if (is_array($this->_values) && in_array( (string) $option['attr']['value'], $this->_values)) {
  187. $this->_updateAttrArray($option['attr'], ['selected' => 'selected']);
  188. }
  189. $o = [
  190. 'text' => $option['text'],
  191. 'value' => $option['attr']['value'],
  192. 'selected' => !empty($option['attr']['selected']),
  193. 'disabled' => !empty($option['attr']['disabled']),
  194. ];
  195. // Set other attributes.
  196. $otheroptionattributes = [];
  197. foreach ($option['attr'] as $attr => $value) {
  198. if (!in_array($attr, $standardoptionattributes) && $attr != 'class' && !is_object($value)) {
  199. $otheroptionattributes[] = $attr . '="' . s($value) . '"';
  200. }
  201. }
  202. $o['optionattributes'] = implode(' ', $otheroptionattributes);
  203. $options[] = $o;
  204. }
  205. $context['options'] = $options;
  206. $context['nameraw'] = $this->getName();
  207. return $context;
  208. }
  209. }