MediaWiki REL1_30
OOUIHTMLForm.php
Go to the documentation of this file.
1<?php
2
27class OOUIHTMLForm extends HTMLForm {
28 private $oouiErrors;
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
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 ) ) {
62 }
63
64 if ( isset( $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();
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 ) {
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 'infusable' => false,
155 ] );
156
157 $layout->appendContent(
158 new OOUI\FieldsetLayout( [
159 'label' => $legend,
160 'infusable' => false,
161 'items' => [
162 new OOUI\Widget( [
163 'content' => new OOUI\HtmlSnippet( $section )
164 ] ),
165 ],
166 ] + $attributes )
167 );
168 return $layout;
169 }
170
178 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
179 $config = [
180 'items' => $fieldsHtml,
181 ];
182 if ( $sectionName ) {
183 $config['id'] = Sanitizer::escapeIdForAttribute( $sectionName );
184 }
185 if ( is_string( $this->mWrapperLegend ) ) {
186 $config['label'] = $this->mWrapperLegend;
187 }
188 return new OOUI\FieldsetLayout( $config );
189 }
190
196 public function getErrorsOrWarnings( $elements, $elementsType ) {
197 if ( $elements === '' ) {
198 return '';
199 }
200
201 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
202 throw new DomainException( $elementsType . ' is not a valid type.' );
203 }
204 $errors = [];
205 if ( $elements instanceof Status ) {
206 if ( !$elements->isGood() ) {
207 $errors = $elements->getErrorsByType( $elementsType );
208 foreach ( $errors as &$error ) {
209 // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
210 // Output: [ 'foo', 'a', 'b', 'c' ]
211 $error = array_merge( [ $error['message'] ], $error['params'] );
212 }
213 }
214 } elseif ( $elementsType === 'error' ) {
215 if ( is_array( $elements ) ) {
216 $errors = $elements;
217 } elseif ( is_string( $elements ) ) {
218 $errors = [ $elements ];
219 }
220 }
221
222 foreach ( $errors as &$error ) {
223 $error = $this->getMessage( $error )->parse();
224 $error = new OOUI\HtmlSnippet( $error );
225 }
226
227 // Used in getBody()
228 if ( $elementsType === 'error' ) {
229 $this->oouiErrors = $errors;
230 } else {
231 $this->oouiWarnings = $errors;
232 }
233 return '';
234 }
235
236 public function getHeaderText( $section = null ) {
237 if ( is_null( $section ) ) {
238 // We handle $this->mHeader elsewhere, in getBody()
239 return '';
240 } else {
241 return parent::getHeaderText( $section );
242 }
243 }
244
245 public function getBody() {
246 $fieldset = parent::getBody();
247 // FIXME This only works for forms with no subsections
248 if ( $fieldset instanceof OOUI\FieldsetLayout ) {
249 $classes = [ 'mw-htmlform-ooui-header' ];
250 if ( $this->oouiErrors ) {
251 $classes[] = 'mw-htmlform-ooui-header-errors';
252 }
253 if ( $this->oouiWarnings ) {
254 $classes[] = 'mw-htmlform-ooui-header-warnings';
255 }
256 if ( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) {
257 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
258 if ( $this->mHeader ) {
259 $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
260 } else {
261 $element = new OOUI\Widget( [] );
262 }
263 $fieldset->addItems( [
264 new OOUI\FieldLayout(
265 $element,
266 [
267 'align' => 'top',
268 'errors' => $this->oouiErrors,
269 'notices' => $this->oouiWarnings,
270 'classes' => $classes,
271 ]
272 )
273 ], 0 );
274 }
275 }
276 return $fieldset;
277 }
278
279 public function wrapForm( $html ) {
280 $form = new OOUI\FormLayout( $this->getFormAttributes() + [
281 'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ],
282 'content' => new OOUI\HtmlSnippet( $html ),
283 ] );
284
285 // Include a wrapper for style, if requested.
286 $form = new OOUI\PanelLayout( [
287 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
288 'expanded' => false,
289 'padded' => $this->mWrapperLegend !== false,
290 'framed' => $this->mWrapperLegend !== false,
291 'content' => $form,
292 ] );
293
294 return $form;
295 }
296}
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
getRequest()
Get the WebRequest object.
getOutput()
Get the OutputPage object.
IContextSource $context
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition HTMLForm.php:128
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:1951
static accesskey( $name)
Given the id of an interface element, constructs the appropriate accesskey attribute from the system ...
Definition Linker.php:1994
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.
getErrorsOrWarnings( $elements, $elementsType)
wrapFieldSetSection( $legend, $section, $attributes)
Wraps the given $section into an user-visible fieldset.
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.
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:40
Represents a title within MediaWiki.
Definition Title.php:39
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
the array() calling protocol came about after MediaWiki 1.4rc1.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:1976
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:1983
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:1984
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition hooks.txt:2990