MDL-65069 mod_forum: Behat test for the inline form
[moodle.git] / lib / form / submit.php
CommitLineData
a23f0aaf 1<?php
6c1fd304
RT
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 * submit type form element
20 *
21 * Contains HTML class for a submit type element
22 *
23 * @package core_form
24 * @copyright 2006 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
a23f0aaf 28require_once("HTML/QuickForm/submit.php");
344321e1 29require_once('templatable_form_element.php');
a23f0aaf 30
31/**
6c1fd304
RT
32 * submit type form element
33 *
a23f0aaf 34 * HTML class for a submit type element
35 *
6c1fd304
RT
36 * @package core_form
37 * @category form
38 * @copyright 2006 Jamie Pratt <me@jamiep.org>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a23f0aaf 40 */
91bda4cd 41class MoodleQuickForm_submit extends HTML_QuickForm_submit implements templatable {
344321e1
DW
42 use templatable_form_element {
43 export_for_template as export_for_template_base;
44 }
45
a91a4dd7
MN
46 /**
47 * @var bool $primary Is this button a primary button?
48 */
49 protected $primary;
50
e455d6f4
P
51 /**
52 * Any class apart from 'btn' would be override with this content
53 *
54 * @var string $customclassoverride Custom class override for the input element
55 */
56 protected $customclassoverride;
57
6c1fd304
RT
58 /**
59 * constructor
60 *
61 * @param string $elementName (optional) name of the field
62 * @param string $value (optional) field label
63 * @param string $attributes (optional) Either a typical HTML attribute string or an associative array
a91a4dd7 64 * @param bool|null $primary Is this button a primary button?
6c1fd304 65 */
a91a4dd7 66 public function __construct($elementName=null, $value=null, $attributes=null, $primary = null) {
1a0df553 67 parent::__construct($elementName, $value, $attributes);
a91a4dd7
MN
68
69 // Fallback to legacy behaviour if no value specified.
70 if (is_null($primary)) {
71 $this->primary = $this->getName() != 'cancel';
72 } else {
73 $this->primary = $primary;
74 }
e455d6f4
P
75
76 $class = $this->getAttribute('customclassoverride');
77 if ($class) {
78 $this->removeAttribute('customclassoverride');
79 $this->customclassoverride = $class;
80 }
1a0df553
MG
81 }
82
83 /**
84 * Old syntax of class constructor. Deprecated in PHP7.
85 *
86 * @deprecated since Moodle 3.1
87 */
a91a4dd7 88 public function MoodleQuickForm_submit($elementName=null, $value=null, $attributes=null, $primary = null) {
1a0df553 89 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
a91a4dd7 90 self::__construct($elementName, $value, $attributes, $primary);
3e8b394e 91 }
6c1fd304 92
a23f0aaf 93 /**
94 * Called by HTML_QuickForm whenever form event is made on this element
95 *
6c1fd304
RT
96 * @param string $event Name of event
97 * @param mixed $arg event arguments
98 * @param object $caller calling object
a23f0aaf 99 */
100 function onQuickFormEvent($event, $arg, &$caller)
101 {
102 switch ($event) {
103 case 'createElement':
104 parent::onQuickFormEvent($event, $arg, $caller);
105 if ($caller->isNoSubmitButton($arg[0])){
106 //need this to bypass client validation
107 //for buttons that submit but do not process the
108 //whole form.
109 $onClick = $this->getAttribute('onclick');
110 $skip = 'skipClientValidation = true;';
111 $onClick = ($onClick !== null)?$skip.' '.$onClick:$skip;
112 $this->updateAttributes(array('onclick'=>$onClick));
113 }
114 return true;
115 break;
116 }
117 return parent::onQuickFormEvent($event, $arg, $caller);
118
6c1fd304
RT
119 }
120
64360ee7 121 /**
122 * Slightly different container template when frozen. Don't want to display a submit
123 * button if the form is frozen.
124 *
125 * @return string
126 */
127 function getElementTemplateType(){
128 if ($this->_flagFrozen){
129 return 'nodisplay';
130 } else {
95114b18 131 return 'actionbuttons';
64360ee7 132 }
133 }
4a0e2e63 134
6c1fd304
RT
135 /**
136 * Freeze the element so that only its value is returned
137 */
64360ee7 138 function freeze(){
139 $this->_flagFrozen = true;
140 }
4a0e2e63 141
91bda4cd 142 public function export_for_template(renderer_base $output) {
344321e1 143 $context = $this->export_for_template_base($output);
a91a4dd7 144 if (!$this->primary) {
ce18fb1e 145 $context['secondary'] = true;
91bda4cd 146 }
e455d6f4
P
147
148 if ($this->customclassoverride) {
149 $context['customclass'] = $this->customclassoverride;
150 }
91bda4cd
DW
151 return $context;
152 }
a23f0aaf 153}