e7e3ff633395868176578dc05bfbecd396f9ac5b
[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 private $oouiWarnings;
30
31 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
32 parent::__construct( $descriptor, $context, $messagePrefix );
33 $this->getOutput()->enableOOUI();
34 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
35 }
36
37 /**
38 * Symbolic display format name.
39 * @var string
40 */
41 protected $displayFormat = 'ooui';
42
43 public static function loadInputFromParameters( $fieldname, $descriptor,
44 HTMLForm $parent = null
45 ) {
46 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
47 $field->setShowEmptyLabel( false );
48 return $field;
49 }
50
51 public function getButtons() {
52 $buttons = '';
53
54 // IE<8 has bugs with <button>, so we'll need to avoid them.
55 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
56
57 if ( $this->mShowSubmit ) {
58 $attribs = [ 'infusable' => true ];
59
60 if ( isset( $this->mSubmitID ) ) {
61 $attribs['id'] = $this->mSubmitID;
62 }
63
64 if ( isset( $this->mSubmitName ) ) {
65 $attribs['name'] = $this->mSubmitName;
66 }
67
68 if ( isset( $this->mSubmitTooltip ) ) {
69 $attribs += [
70 'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
71 'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
72 ];
73 }
74
75 $attribs['classes'] = [ 'mw-htmlform-submit' ];
76 $attribs['type'] = 'submit';
77 $attribs['label'] = $this->getSubmitText();
78 $attribs['value'] = $this->getSubmitText();
79 $attribs['flags'] = $this->mSubmitFlags;
80 $attribs['useInputTag'] = $isBadIE;
81
82 $buttons .= new OOUI\ButtonInputWidget( $attribs );
83 }
84
85 if ( $this->mShowReset ) {
86 $buttons .= new OOUI\ButtonInputWidget( [
87 'type' => 'reset',
88 'label' => $this->msg( 'htmlform-reset' )->text(),
89 'useInputTag' => $isBadIE,
90 ] );
91 }
92
93 if ( $this->mShowCancel ) {
94 $target = $this->mCancelTarget ?: Title::newMainPage();
95 if ( $target instanceof Title ) {
96 $target = $target->getLocalURL();
97 }
98 $buttons .= new OOUI\ButtonWidget( [
99 'label' => $this->msg( 'cancel' )->text(),
100 'href' => $target,
101 ] );
102 }
103
104 foreach ( $this->mButtons as $button ) {
105 $attrs = [];
106
107 if ( $button['attribs'] ) {
108 $attrs += $button['attribs'];
109 }
110
111 if ( isset( $button['id'] ) ) {
112 $attrs['id'] = $button['id'];
113 }
114
115 if ( $isBadIE ) {
116 $label = $button['value'];
117 } elseif ( isset( $button['label-message'] ) ) {
118 $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
119 } elseif ( isset( $button['label'] ) ) {
120 $label = $button['label'];
121 } elseif ( isset( $button['label-raw'] ) ) {
122 $label = new OOUI\HtmlSnippet( $button['label-raw'] );
123 } else {
124 $label = $button['value'];
125 }
126
127 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
128
129 $buttons .= new OOUI\ButtonInputWidget( [
130 'type' => 'submit',
131 'name' => $button['name'],
132 'value' => $button['value'],
133 'label' => $label,
134 'flags' => $button['flags'],
135 'framed' => $button['framed'],
136 'useInputTag' => $isBadIE,
137 ] + $attrs );
138 }
139
140 if ( !$buttons ) {
141 return '';
142 }
143
144 return Html::rawElement( 'div',
145 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
146 }
147
148 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
149 // to get a user visible effect, wrap the fieldset into a framed panel layout
150 $layout = new OOUI\PanelLayout( [
151 'expanded' => false,
152 'padded' => true,
153 'framed' => true,
154 ] );
155
156 $layout->appendContent(
157 new OOUI\FieldsetLayout( [
158 'label' => $legend,
159 'items' => [
160 new OOUI\Widget( [
161 'content' => new OOUI\HtmlSnippet( $section )
162 ] ),
163 ],
164 ] + $attributes )
165 );
166 return $layout;
167 }
168
169 /**
170 * Put a form section together from the individual fields' HTML, merging it and wrapping.
171 * @param OOUI\FieldLayout[] $fieldsHtml
172 * @param string $sectionName
173 * @param bool $anyFieldHasLabel Unused
174 * @return string HTML
175 */
176 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
177 if ( !$fieldsHtml ) {
178 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
179 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
180 return '';
181 }
182
183 $html = implode( '', $fieldsHtml );
184
185 if ( $sectionName ) {
186 $html = Html::rawElement(
187 'div',
188 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
189 $html
190 );
191 }
192 return $html;
193 }
194
195 /**
196 * @param string|array|Status $elements
197 * @param string $elementsType
198 * @return string
199 */
200 public function getErrorsOrWarnings( $elements, $elementsType ) {
201 if ( $elements === '' ) {
202 return '';
203 }
204
205 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
206 throw new DomainException( $elementsType . ' is not a valid type.' );
207 }
208 $errors = [];
209 if ( $elements instanceof Status ) {
210 if ( !$elements->isGood() ) {
211 $errors = $elements->getErrorsByType( $elementsType );
212 foreach ( $errors as &$error ) {
213 // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
214 // Output: [ 'foo', 'a', 'b', 'c' ]
215 $error = array_merge( [ $error['message'] ], $error['params'] );
216 }
217 }
218 } elseif ( $elementsType === 'error' ) {
219 if ( is_array( $elements ) ) {
220 $errors = $elements;
221 } elseif ( is_string( $elements ) ) {
222 $errors = [ $elements ];
223 }
224 }
225
226 foreach ( $errors as &$error ) {
227 $error = $this->getMessage( $error )->parse();
228 $error = new OOUI\HtmlSnippet( $error );
229 }
230
231 // Used in getBody()
232 if ( $elementsType === 'error' ) {
233 $this->oouiErrors = $errors;
234 } else {
235 $this->oouiWarnings = $errors;
236 }
237 return '';
238 }
239
240 public function getHeaderText( $section = null ) {
241 if ( is_null( $section ) ) {
242 // We handle $this->mHeader elsewhere, in getBody()
243 return '';
244 } else {
245 return parent::getHeaderText( $section );
246 }
247 }
248
249 public function getBody() {
250 $html = parent::getBody();
251 if ( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) {
252 $classes = [ 'mw-htmlform-ooui-header' ];
253 if ( $this->oouiErrors ) {
254 $classes[] = 'mw-htmlform-ooui-header-errors';
255 }
256 if ( $this->oouiWarnings ) {
257 $classes[] = 'mw-htmlform-ooui-header-warnings';
258 }
259 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
260 if ( $this->mHeader ) {
261 $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
262 } else {
263 $element = new OOUI\Widget( [] );
264 }
265 $html = new OOUI\FieldLayout(
266 $element,
267 [
268 'align' => 'top',
269 'errors' => $this->oouiErrors,
270 'notices' => $this->oouiWarnings,
271 'classes' => $classes,
272 ]
273 ) . $html;
274 }
275 return $html;
276 }
277
278 public function wrapForm( $html ) {
279 if ( is_string( $this->mWrapperLegend ) ) {
280 $content = new OOUI\FieldsetLayout( [
281 'label' => $this->mWrapperLegend,
282 'items' => [
283 new OOUI\Widget( [
284 'content' => new OOUI\HtmlSnippet( $html )
285 ] ),
286 ],
287 ] );
288 } else {
289 $content = new OOUI\HtmlSnippet( $html );
290 }
291
292 $form = new OOUI\FormLayout( $this->getFormAttributes() + [
293 'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ],
294 'content' => $content,
295 ] );
296
297 // Include a wrapper for style, if requested.
298 $form = new OOUI\PanelLayout( [
299 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
300 'expanded' => false,
301 'padded' => $this->mWrapperLegend !== false,
302 'framed' => $this->mWrapperLegend !== false,
303 'content' => $form,
304 ] );
305
306 return $form;
307 }
308 }