SessionManager: Ignore Session object destruction during global shutdown
[mediawiki.git] / includes / htmlform / OOUIHTMLForm.php
blob4f8365e019da151a6504969721d3cd5f2e54ab48
1 <?php
3 /**
4 * HTML form generation and submission handling, OOUI style.
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.
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.
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
21 * @file
24 /**
25 * Compact stacked vertical format for forms, implemented using OOUI widgets.
27 class OOUIHTMLForm extends HTMLForm {
28 private $oouiErrors;
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' );
36 /**
37 * Symbolic display format name.
38 * @var string
40 protected $displayFormat = 'ooui';
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;
50 function getButtons() {
51 $buttons = '';
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' ) );
56 if ( $this->mShowSubmit ) {
57 $attribs = [ 'infusable' => true ];
59 if ( isset( $this->mSubmitID ) ) {
60 $attribs['id'] = $this->mSubmitID;
63 if ( isset( $this->mSubmitName ) ) {
64 $attribs['name'] = $this->mSubmitName;
67 if ( isset( $this->mSubmitTooltip ) ) {
68 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
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;
78 $buttons .= new OOUI\ButtonInputWidget( $attribs );
81 if ( $this->mShowReset ) {
82 $buttons .= new OOUI\ButtonInputWidget( [
83 'type' => 'reset',
84 'label' => $this->msg( 'htmlform-reset' )->text(),
85 'useInputTag' => $isBadIE,
86 ] );
89 foreach ( $this->mButtons as $button ) {
90 $attrs = [];
92 if ( $button['attribs'] ) {
93 $attrs += $button['attribs'];
96 if ( isset( $button['id'] ) ) {
97 $attrs['id'] = $button['id'];
100 if ( $isBadIE ) {
101 $label = $button['value'];
102 } elseif ( isset( $button['label-message'] ) ) {
103 $label = new OOUI\HtmlSnippet( $this->msg( $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'];
112 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
114 $buttons .= new OOUI\ButtonInputWidget( [
115 'type' => 'submit',
116 'name' => $button['name'],
117 'value' => $button['value'],
118 'label' => $label,
119 'flags' => $button['flags'],
120 'useInputTag' => $isBadIE,
121 ] + $attrs );
124 $html = Html::rawElement( 'div',
125 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
127 return $html;
130 protected function wrapFieldSetSection( $legend, $section, $attributes ) {
131 // to get a user visible effect, wrap the fieldset into a framed panel layout
132 $layout = new OOUI\PanelLayout( [
133 'expanded' => false,
134 'padded' => true,
135 'framed' => true,
136 'infusable' => false,
137 ] );
139 $layout->appendContent(
140 new OOUI\FieldsetLayout( [
141 'label' => $legend,
142 'infusable' => false,
143 'items' => [
144 new OOUI\Widget( [
145 'content' => new OOUI\HtmlSnippet( $section )
146 ] ),
148 ] + $attributes )
150 return $layout;
154 * Put a form section together from the individual fields' HTML, merging it and wrapping.
155 * @param OOUI\FieldLayout[] $fieldsHtml
156 * @param string $sectionName
157 * @param bool $anyFieldHasLabel Unused
158 * @return string HTML
160 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
161 $config = [
162 'items' => $fieldsHtml,
164 if ( $sectionName ) {
165 $config['id'] = Sanitizer::escapeId( $sectionName );
167 if ( is_string( $this->mWrapperLegend ) ) {
168 $config['label'] = $this->mWrapperLegend;
170 return new OOUI\FieldsetLayout( $config );
174 * @param string|array|Status $err
175 * @return string
177 function getErrors( $err ) {
178 if ( !$err ) {
179 $errors = [];
180 } elseif ( $err instanceof Status ) {
181 if ( $err->isOK() ) {
182 $errors = [];
183 } else {
184 $errors = $err->getErrorsByType( 'error' );
185 foreach ( $errors as &$error ) {
186 // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) )
187 // Output: array( 'foo', 'a', 'b', 'c' )
188 $error = array_merge( [ $error['message'] ], $error['params'] );
191 } else {
192 $errors = $err;
193 if ( !is_array( $errors ) ) {
194 $errors = [ $errors ];
198 foreach ( $errors as &$error ) {
199 if ( is_array( $error ) ) {
200 $msg = array_shift( $error );
201 } else {
202 $msg = $error;
203 $error = [];
205 // if the error is already a message object, don't use it as a message key
206 if ( !$msg instanceof Message ) {
207 $error = $this->msg( $msg, $error )->parse();
208 } else {
209 $error = $msg->parse();
211 $error = new OOUI\HtmlSnippet( $error );
214 // Used in getBody()
215 $this->oouiErrors = $errors;
216 return '';
219 function getHeaderText( $section = null ) {
220 if ( is_null( $section ) ) {
221 // We handle $this->mHeader elsewhere, in getBody()
222 return '';
223 } else {
224 return parent::getHeaderText( $section );
228 function getBody() {
229 $fieldset = parent::getBody();
230 // FIXME This only works for forms with no subsections
231 if ( $fieldset instanceof OOUI\FieldsetLayout ) {
232 $classes = [ 'mw-htmlform-ooui-header' ];
233 if ( !$this->mHeader ) {
234 $classes[] = 'mw-htmlform-ooui-header-empty';
236 if ( $this->oouiErrors ) {
237 $classes[] = 'mw-htmlform-ooui-header-errors';
239 $fieldset->addItems( [
240 new OOUI\FieldLayout(
241 new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] ),
243 'align' => 'top',
244 'errors' => $this->oouiErrors,
245 'classes' => $classes,
248 ], 0 );
250 return $fieldset;
253 function wrapForm( $html ) {
254 $form = new OOUI\FormLayout( $this->getFormAttributes() + [
255 'classes' => [ 'mw-htmlform-ooui' ],
256 'content' => new OOUI\HtmlSnippet( $html ),
257 ] );
259 // Include a wrapper for style, if requested.
260 $form = new OOUI\PanelLayout( [
261 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
262 'expanded' => false,
263 'padded' => $this->mWrapperLegend !== false,
264 'framed' => $this->mWrapperLegend !== false,
265 'content' => $form,
266 ] );
268 return $form;