Merge "Fix required field calculation in AuthenticationRequest"
[lhc/web/wiklou.git] / includes / htmlform / OOUIHTMLForm.php
1 <?php
2
3 /**
4 * HTML form generation and submission handling, OOUI style.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * Compact stacked vertical format for forms, implemented using OOUI widgets.
26 */
27 class OOUIHTMLForm extends HTMLForm {
28 private $oouiErrors;
29
30 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
31 parent::__construct( $descriptor, $context, $messagePrefix );
32 $this->getOutput()->enableOOUI();
33 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
34 }
35
36 /**
37 * Symbolic display format name.
38 * @var string
39 */
40 protected $displayFormat = 'ooui';
41
42 public static function loadInputFromParameters( $fieldname, $descriptor,
43 HTMLForm $parent = null
44 ) {
45 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
46 $field->setShowEmptyLabel( false );
47 return $field;
48 }
49
50 function getButtons() {
51 $buttons = '';
52
53 // IE<8 has bugs with <button>, so we'll need to avoid them.
54 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
55
56 if ( $this->mShowSubmit ) {
57 $attribs = [ 'infusable' => true ];
58
59 if ( isset( $this->mSubmitID ) ) {
60 $attribs['id'] = $this->mSubmitID;
61 }
62
63 if ( isset( $this->mSubmitName ) ) {
64 $attribs['name'] = $this->mSubmitName;
65 }
66
67 if ( isset( $this->mSubmitTooltip ) ) {
68 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
69 }
70
71 $attribs['classes'] = [ 'mw-htmlform-submit' ];
72 $attribs['type'] = 'submit';
73 $attribs['label'] = $this->getSubmitText();
74 $attribs['value'] = $this->getSubmitText();
75 $attribs['flags'] = $this->mSubmitFlags;
76 $attribs['useInputTag'] = $isBadIE;
77
78 $buttons .= new OOUI\ButtonInputWidget( $attribs );
79 }
80
81 if ( $this->mShowReset ) {
82 $buttons .= new OOUI\ButtonInputWidget( [
83 'type' => 'reset',
84 'label' => $this->msg( 'htmlform-reset' )->text(),
85 'useInputTag' => $isBadIE,
86 ] );
87 }
88
89 foreach ( $this->mButtons as $button ) {
90 $attrs = [];
91
92 if ( $button['attribs'] ) {
93 $attrs += $button['attribs'];
94 }
95
96 if ( isset( $button['id'] ) ) {
97 $attrs['id'] = $button['id'];
98 }
99
100 if ( $isBadIE ) {
101 $label = $button['value'];
102 } elseif ( isset( $button['label-message'] ) ) {
103 $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
104 } elseif ( isset( $button['label'] ) ) {
105 $label = $button['label'];
106 } elseif ( isset( $button['label-raw'] ) ) {
107 $label = new OOUI\HtmlSnippet( $button['label-raw'] );
108 } else {
109 $label = $button['value'];
110 }
111
112 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
113
114 $buttons .= new OOUI\ButtonInputWidget( [
115 'type' => 'submit',
116 'name' => $button['name'],
117 'value' => $button['value'],
118 'label' => $label,
119 'flags' => $button['flags'],
120 'framed' => $button['framed'],
121 'useInputTag' => $isBadIE,
122 ] + $attrs );
123 }
124
125 if ( !$buttons ) {
126 return '';
127 }
128
129 return Html::rawElement( 'div',
130 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
131 }
132
133 protected function wrapFieldSetSection( $legend, $section, $attributes ) {
134 // to get a user visible effect, wrap the fieldset into a framed panel layout
135 $layout = new OOUI\PanelLayout( [
136 'expanded' => false,
137 'padded' => true,
138 'framed' => true,
139 'infusable' => false,
140 ] );
141
142 $layout->appendContent(
143 new OOUI\FieldsetLayout( [
144 'label' => $legend,
145 'infusable' => false,
146 'items' => [
147 new OOUI\Widget( [
148 'content' => new OOUI\HtmlSnippet( $section )
149 ] ),
150 ],
151 ] + $attributes )
152 );
153 return $layout;
154 }
155
156 /**
157 * Put a form section together from the individual fields' HTML, merging it and wrapping.
158 * @param OOUI\FieldLayout[] $fieldsHtml
159 * @param string $sectionName
160 * @param bool $anyFieldHasLabel Unused
161 * @return string HTML
162 */
163 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
164 $config = [
165 'items' => $fieldsHtml,
166 ];
167 if ( $sectionName ) {
168 $config['id'] = Sanitizer::escapeId( $sectionName );
169 }
170 if ( is_string( $this->mWrapperLegend ) ) {
171 $config['label'] = $this->mWrapperLegend;
172 }
173 return new OOUI\FieldsetLayout( $config );
174 }
175
176 /**
177 * @param string|array|Status $err
178 * @return string
179 */
180 function getErrors( $err ) {
181 if ( !$err ) {
182 $errors = [];
183 } elseif ( $err instanceof Status ) {
184 if ( $err->isOK() ) {
185 $errors = [];
186 } else {
187 $errors = $err->getErrorsByType( 'error' );
188 foreach ( $errors as &$error ) {
189 // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) )
190 // Output: array( 'foo', 'a', 'b', 'c' )
191 $error = array_merge( [ $error['message'] ], $error['params'] );
192 }
193 }
194 } else {
195 $errors = $err;
196 if ( !is_array( $errors ) ) {
197 $errors = [ $errors ];
198 }
199 }
200
201 foreach ( $errors as &$error ) {
202 $error = $this->getMessage( $error )->parse();
203 $error = new OOUI\HtmlSnippet( $error );
204 }
205
206 // Used in getBody()
207 $this->oouiErrors = $errors;
208 return '';
209 }
210
211 function getHeaderText( $section = null ) {
212 if ( is_null( $section ) ) {
213 // We handle $this->mHeader elsewhere, in getBody()
214 return '';
215 } else {
216 return parent::getHeaderText( $section );
217 }
218 }
219
220 function getBody() {
221 $fieldset = parent::getBody();
222 // FIXME This only works for forms with no subsections
223 if ( $fieldset instanceof OOUI\FieldsetLayout ) {
224 $classes = [ 'mw-htmlform-ooui-header' ];
225 if ( $this->oouiErrors ) {
226 $classes[] = 'mw-htmlform-ooui-header-errors';
227 }
228 if ( $this->mHeader || $this->oouiErrors ) {
229 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
230 if ( $this->mHeader ) {
231 $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
232 } else {
233 $element = new OOUI\Widget( [] );
234 }
235 $fieldset->addItems( [
236 new OOUI\FieldLayout(
237 $element,
238 [
239 'align' => 'top',
240 'errors' => $this->oouiErrors,
241 'classes' => $classes,
242 ]
243 )
244 ], 0 );
245 }
246 }
247 return $fieldset;
248 }
249
250 function wrapForm( $html ) {
251 $form = new OOUI\FormLayout( $this->getFormAttributes() + [
252 'classes' => [ 'mw-htmlform-ooui' ],
253 'content' => new OOUI\HtmlSnippet( $html ),
254 ] );
255
256 // Include a wrapper for style, if requested.
257 $form = new OOUI\PanelLayout( [
258 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
259 'expanded' => false,
260 'padded' => $this->mWrapperLegend !== false,
261 'framed' => $this->mWrapperLegend !== false,
262 'content' => $form,
263 ] );
264
265 return $form;
266 }
267 }