PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/form/group.php

http://github.com/moodle/moodle
PHP | 278 lines | 143 code | 33 blank | 102 comment | 30 complexity | 10c68facb1ed73e2e97d4849f88a7b2c MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Form element group
  18. *
  19. * Contains HTML class for group form element
  20. *
  21. * @package core_form
  22. * @copyright 2007 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/group.php");
  26. require_once('templatable_form_element.php');
  27. /**
  28. * HTML class for a form element group
  29. *
  30. * Overloaded {@link HTML_QuickForm_group} with default behavior modified for Moodle.
  31. *
  32. * @package core_form
  33. * @category form
  34. * @copyright 2007 Jamie Pratt <me@jamiep.org>
  35. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36. */
  37. class MoodleQuickForm_group extends HTML_QuickForm_group 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 MoodleQuickForm */
  44. protected $_mform = null;
  45. protected $_renderedfromtemplate = false;
  46. /**
  47. * constructor
  48. *
  49. * @param string $elementName (optional) name of the group
  50. * @param string $elementLabel (optional) group label
  51. * @param array $elements (optional) array of HTML_QuickForm_element elements to group
  52. * @param string $separator (optional) string to seperate elements.
  53. * @param string $appendName (optional) string to appened to grouped elements.
  54. */
  55. public function __construct($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
  56. parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
  57. }
  58. /**
  59. * Old syntax of class constructor. Deprecated in PHP7.
  60. *
  61. * @deprecated since Moodle 3.1
  62. */
  63. public function MoodleQuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
  64. debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  65. self::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
  66. }
  67. /** @var string template type, would cause problems with client side validation so will leave for now */
  68. //var $_elementTemplateType='fieldset';
  69. /**
  70. * set html for help button
  71. */
  72. function getHelpButton(){
  73. return $this->_helpbutton;
  74. }
  75. /**
  76. * Returns element template, nodisplay/static/fieldset
  77. *
  78. * @return string
  79. */
  80. function getElementTemplateType(){
  81. if ($this->_flagFrozen){
  82. if ($this->getGroupType() == 'submit'){
  83. return 'nodisplay';
  84. } else {
  85. return 'static';
  86. }
  87. } else {
  88. if ($this->getGroupType() == 'submit') {
  89. return 'actionbuttons';
  90. }
  91. return 'fieldset';
  92. }
  93. }
  94. /**
  95. * Sets the grouped elements and hides label
  96. *
  97. * @param array $elements
  98. */
  99. function setElements($elements){
  100. parent::setElements($elements);
  101. foreach ($this->_elements as $element){
  102. if (method_exists($element, 'setHiddenLabel')){
  103. $element->setHiddenLabel(true);
  104. }
  105. }
  106. }
  107. /**
  108. * Stores the form this element was added to
  109. * This object is later used by {@link MoodleQuickForm_group::createElement()}
  110. * @param null|MoodleQuickForm $mform
  111. */
  112. public function setMoodleForm($mform) {
  113. if ($mform && $mform instanceof MoodleQuickForm) {
  114. $this->_mform = $mform;
  115. }
  116. }
  117. /**
  118. * Called by HTML_QuickForm whenever form event is made on this element
  119. *
  120. * If this function is overridden and parent is not called the element must be responsible for
  121. * storing the MoodleQuickForm object, see {@link MoodleQuickForm_group::setMoodleForm()}
  122. *
  123. * @param string $event Name of event
  124. * @param mixed $arg event arguments
  125. * @param mixed $caller calling object
  126. */
  127. public function onQuickFormEvent($event, $arg, &$caller) {
  128. $this->setMoodleForm($caller);
  129. return parent::onQuickFormEvent($event, $arg, $caller);
  130. }
  131. /**
  132. * Creates an element to add to the group
  133. * Expects the same arguments as MoodleQuickForm::createElement()
  134. */
  135. public function createFormElement() {
  136. if (!$this->_mform) {
  137. throw new coding_exception('You can not call createFormElement() on the group element that was not yet added to a form.');
  138. }
  139. return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
  140. }
  141. public function export_for_template(renderer_base $output) {
  142. global $OUTPUT;
  143. $context = $this->export_for_template_base($output);
  144. $this->_renderedfromtemplate = true;
  145. include_once('HTML/QuickForm/Renderer/Default.php');
  146. $elements = [];
  147. $name = $this->getName();
  148. $i = 0;
  149. foreach ($this->_elements as $key => $element) {
  150. $elementname = '';
  151. if ($this->_appendName) {
  152. $elementname = $element->getName();
  153. if (isset($elementname)) {
  154. $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
  155. } else {
  156. $element->setName($name);
  157. }
  158. }
  159. $element->_generateId();
  160. $out = $OUTPUT->mform_element($element, false, false, '', true);
  161. if (empty($out)) {
  162. $renderer = new HTML_QuickForm_Renderer_Default();
  163. $renderer->setElementTemplate('{element}');
  164. $element->accept($renderer);
  165. $out = $renderer->toHtml();
  166. }
  167. // Replicates the separator logic from 'pear/HTML/QuickForm/Renderer/Default.php'.
  168. $separator = '';
  169. if ($i > 0) {
  170. if (is_array($this->_separator)) {
  171. $separator = $this->_separator[($i - 1) % count($this->_separator)];
  172. } else if ($this->_separator === null) {
  173. $separator = '&nbsp;';
  174. } else {
  175. $separator = (string) $this->_separator;
  176. }
  177. }
  178. $elements[] = [
  179. 'separator' => $separator,
  180. 'html' => $out
  181. ];
  182. // Restore the element's name.
  183. if ($this->_appendName) {
  184. $element->setName($elementname);
  185. }
  186. $i++;
  187. }
  188. $context['groupname'] = $name;
  189. $context['elements'] = $elements;
  190. return $context;
  191. }
  192. /**
  193. * Accepts a renderer
  194. *
  195. * @param object An HTML_QuickForm_Renderer object
  196. * @param bool Whether a group is required
  197. * @param string An error message associated with a group
  198. * @access public
  199. * @return void
  200. */
  201. public function accept(&$renderer, $required = false, $error = null) {
  202. $this->_createElementsIfNotExist();
  203. $renderer->startGroup($this, $required, $error);
  204. if (!$this->_renderedfromtemplate) {
  205. // Backwards compatible path - only do this if we didn't render the sub-elements already.
  206. $name = $this->getName();
  207. foreach (array_keys($this->_elements) as $key) {
  208. $element =& $this->_elements[$key];
  209. $elementname = '';
  210. if ($this->_appendName) {
  211. $elementname = $element->getName();
  212. if (isset($elementname)) {
  213. $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
  214. } else {
  215. $element->setName($name);
  216. }
  217. }
  218. $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
  219. $element->accept($renderer, $required);
  220. // Restore the element's name.
  221. if ($this->_appendName) {
  222. $element->setName($elementname);
  223. }
  224. }
  225. }
  226. $renderer->finishGroup($this);
  227. }
  228. /**
  229. * Calls the validateSubmitValue function for the containing elements and returns an error string as soon as it finds one.
  230. *
  231. * @param array $values Values of the containing elements.
  232. * @return string|null Validation error message or null.
  233. */
  234. public function validateSubmitValue($values) {
  235. foreach ($this->_elements as $element) {
  236. if (method_exists($element, 'validateSubmitValue')) {
  237. $value = $values[$element->getName()] ?? null;
  238. $result = $element->validateSubmitValue($value);
  239. if (!empty($result) && is_string($result)) {
  240. return $result;
  241. }
  242. }
  243. }
  244. }
  245. }