MediaWiki REL1_35
OOUIHTMLForm.php
Go to the documentation of this file.
1<?php
2
29class OOUIHTMLForm extends HTMLForm {
30 private $oouiErrors;
32
33 /*
34 * @stable to call
35 */
36 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
37 parent::__construct( $descriptor, $context, $messagePrefix );
38 $this->getOutput()->enableOOUI();
39 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
40 }
41
46 protected $displayFormat = 'ooui';
47
48 public static function loadInputFromParameters( $fieldname, $descriptor,
49 HTMLForm $parent = null
50 ) {
51 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
52 $field->setShowEmptyLabel( false );
53 return $field;
54 }
55
56 public function getButtons() {
57 $buttons = '';
58
59 // IE<8 has bugs with <button>, so we'll need to avoid them.
60 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
61
62 if ( $this->mShowSubmit ) {
63 $attribs = [ 'infusable' => true ];
64
65 if ( isset( $this->mSubmitID ) ) {
66 $attribs['id'] = $this->mSubmitID;
67 }
68
69 if ( isset( $this->mSubmitName ) ) {
70 $attribs['name'] = $this->mSubmitName;
71 }
72
73 if ( isset( $this->mSubmitTooltip ) ) {
74 $attribs += [
75 'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
76 'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
77 ];
78 }
79
80 $attribs['classes'] = [ 'mw-htmlform-submit' ];
81 $attribs['type'] = 'submit';
82 $attribs['label'] = $this->getSubmitText();
83 $attribs['value'] = $this->getSubmitText();
84 $attribs['flags'] = $this->mSubmitFlags;
85 $attribs['useInputTag'] = $isBadIE;
86
87 $buttons .= new OOUI\ButtonInputWidget( $attribs );
88 }
89
90 if ( $this->mShowReset ) {
91 $buttons .= new OOUI\ButtonInputWidget( [
92 'type' => 'reset',
93 'label' => $this->msg( 'htmlform-reset' )->text(),
94 'useInputTag' => $isBadIE,
95 ] );
96 }
97
98 if ( $this->mShowCancel ) {
99 $target = $this->mCancelTarget ?: Title::newMainPage();
100 if ( $target instanceof Title ) {
101 $target = $target->getLocalURL();
102 }
103 $buttons .= new OOUI\ButtonWidget( [
104 'label' => $this->msg( 'cancel' )->text(),
105 'href' => $target,
106 ] );
107 }
108
109 foreach ( $this->mButtons as $button ) {
110 $attrs = [];
111
112 if ( $button['attribs'] ) {
113 $attrs += $button['attribs'];
114 }
115
116 if ( isset( $button['id'] ) ) {
117 $attrs['id'] = $button['id'];
118 }
119
120 if ( $isBadIE ) {
121 $label = $button['value'];
122 } elseif ( isset( $button['label-message'] ) ) {
123 $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
124 } elseif ( isset( $button['label'] ) ) {
125 $label = $button['label'];
126 } elseif ( isset( $button['label-raw'] ) ) {
127 $label = new OOUI\HtmlSnippet( $button['label-raw'] );
128 } else {
129 $label = $button['value'];
130 }
131
132 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
133
134 $buttons .= new OOUI\ButtonInputWidget( [
135 'type' => 'submit',
136 'name' => $button['name'],
137 'value' => $button['value'],
138 'label' => $label,
139 'flags' => $button['flags'],
140 'framed' => $button['framed'],
141 'useInputTag' => $isBadIE,
142 ] + $attrs );
143 }
144
145 if ( !$buttons ) {
146 return '';
147 }
148
149 return Html::rawElement( 'div',
150 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
151 }
152
157 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
158 // to get a user visible effect, wrap the fieldset into a framed panel layout
159 $layout = new OOUI\PanelLayout( [
160 'expanded' => false,
161 'padded' => true,
162 'framed' => true,
163 ] );
164
165 $layout->appendContent(
166 new OOUI\FieldsetLayout( [
167 'label' => $legend,
168 'items' => [
169 new OOUI\Widget( [
170 'content' => new OOUI\HtmlSnippet( $section )
171 ] ),
172 ],
173 ] + $attributes )
174 );
175 return $layout;
176 }
177
185 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
186 if ( !$fieldsHtml ) {
187 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
188 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
189 return '';
190 }
191
192 $html = implode( '', $fieldsHtml );
193
194 if ( $sectionName ) {
195 $html = Html::rawElement(
196 'div',
197 [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
198 $html
199 );
200 }
201 return $html;
202 }
203
209 public function getErrorsOrWarnings( $elements, $elementsType ) {
210 if ( $elements === '' ) {
211 return '';
212 }
213
214 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
215 throw new DomainException( $elementsType . ' is not a valid type.' );
216 }
217 $errors = [];
218 if ( $elements instanceof Status ) {
219 if ( !$elements->isGood() ) {
220 $errors = $elements->getErrorsByType( $elementsType );
221 foreach ( $errors as &$error ) {
222 // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
223 // Output: [ 'foo', 'a', 'b', 'c' ]
224 $error = array_merge( [ $error['message'] ], $error['params'] );
225 }
226 }
227 } elseif ( $elementsType === 'error' ) {
228 if ( is_array( $elements ) ) {
229 $errors = $elements;
230 } elseif ( is_string( $elements ) ) {
231 $errors = [ $elements ];
232 }
233 }
234
235 foreach ( $errors as &$error ) {
236 $error = $this->getMessage( $error )->parse();
237 $error = new OOUI\HtmlSnippet( $error );
238 }
239
240 // Used in formatFormHeader()
241 if ( $elementsType === 'error' ) {
242 $this->oouiErrors = $errors;
243 } else {
244 $this->oouiWarnings = $errors;
245 }
246 return '';
247 }
248
249 public function getHeaderText( $section = null ) {
250 if ( $section === null ) {
251 // We handle $this->mHeader elsewhere, in getBody()
252 return '';
253 } else {
254 return parent::getHeaderText( $section );
255 }
256 }
257
258 protected function formatFormHeader() {
259 if ( !( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) ) {
260 return '';
261 }
262 $classes = [ 'mw-htmlform-ooui-header' ];
263 if ( $this->oouiErrors ) {
264 $classes[] = 'mw-htmlform-ooui-header-errors';
265 }
266 if ( $this->oouiWarnings ) {
267 $classes[] = 'mw-htmlform-ooui-header-warnings';
268 }
269 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
270 if ( $this->mHeader ) {
271 $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
272 } else {
273 $element = new OOUI\Widget( [] );
274 }
275 return new OOUI\FieldLayout(
276 $element,
277 [
278 'align' => 'top',
279 'errors' => $this->oouiErrors,
280 'notices' => $this->oouiWarnings,
281 'classes' => $classes,
282 ]
283 );
284 }
285
286 public function getBody() {
287 $html = parent::getBody();
288 $html = $this->formatFormHeader() . $html;
289 return $html;
290 }
291
292 public function wrapForm( $html ) {
293 if ( is_string( $this->mWrapperLegend ) ) {
294 $phpClass = $this->mCollapsible ? CollapsibleFieldsetLayout::class : OOUI\FieldsetLayout::class;
295 $content = new $phpClass( [
296 'label' => $this->mWrapperLegend,
297 'collapsed' => $this->mCollapsed,
298 'items' => [
299 new OOUI\Widget( [
300 'content' => new OOUI\HtmlSnippet( $html )
301 ] ),
302 ],
303 ] + OOUI\Element::configFromHtmlAttributes( $this->mWrapperAttributes ) );
304 } else {
305 $content = new OOUI\HtmlSnippet( $html );
306 }
307
308 $classes = [ 'mw-htmlform', 'mw-htmlform-ooui' ];
309 $form = new OOUI\FormLayout( $this->getFormAttributes() + [
310 'classes' => $classes,
311 'content' => $content,
312 ] );
313
314 // Include a wrapper for style, if requested.
315 $form = new OOUI\PanelLayout( [
316 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
317 'expanded' => false,
318 'padded' => $this->mWrapperLegend !== false,
319 'framed' => $this->mWrapperLegend !== false,
320 'content' => $form,
321 ] );
322
323 return $form;
324 }
325}
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
IContextSource $context
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:135
string[] $mSubmitFlags
Definition HTMLForm.php:194
getSubmitText()
Get the text for the submit button, either customised or a default.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getFormAttributes()
Get HTML attributes for the <form> tag.
static titleAttrib( $name, $options=null, array $msgParams=[])
Given the id of an interface element, constructs the appropriate title attribute from the system mess...
Definition Linker.php:2120
static accesskey( $name)
Given the id of an interface element, constructs the appropriate accesskey attribute from the system ...
Definition Linker.php:2168
Compact stacked vertical format for forms, implemented using OOUI widgets.
__construct( $descriptor, $context=null, $messagePrefix='')
Build a new HTMLForm from an array of field attributes.
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into an user-visible fieldset.Stable to overridestring The fieldset's Html
getErrorsOrWarnings( $elements, $elementsType)
getHeaderText( $section=null)
Get header text.
getBody()
Get the whole body of the form.
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
getButtons()
Get the submit and (potentially) reset buttons.
string $displayFormat
Symbolic display format name.
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field Stable to override.
wrapForm( $html)
Wrap the form innards in an actual "<form>" element Stable to override.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Represents a title within MediaWiki.
Definition Title.php:42
$content
Definition router.php:76
return true
Definition router.php:92