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
25 * Compact stacked vertical format for forms, implemented using OOUI widgets.
27 class OOUIHTMLForm
extends HTMLForm
{
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' );
37 * Symbolic display format name.
40 protected $displayFormat = 'ooui';
42 public static function loadInputFromParameters( $fieldname, $descriptor,
43 HTMLForm
$parent = null
45 $field = parent
::loadInputFromParameters( $fieldname, $descriptor, $parent );
46 $field->setShowEmptyLabel( false );
50 function getButtons() {
53 if ( $this->mShowSubmit
) {
54 $attribs = array( 'infusable' => true );
56 if ( isset( $this->mSubmitID
) ) {
57 $attribs['id'] = $this->mSubmitID
;
60 if ( isset( $this->mSubmitName
) ) {
61 $attribs['name'] = $this->mSubmitName
;
64 if ( isset( $this->mSubmitTooltip
) ) {
65 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
68 $attribs['classes'] = array( 'mw-htmlform-submit' );
69 $attribs['type'] = 'submit';
70 $attribs['label'] = $this->getSubmitText();
71 $attribs['value'] = $this->getSubmitText();
72 $attribs['flags'] = $this->mSubmitFlags
;
74 $buttons .= new OOUI\
ButtonInputWidget( $attribs );
77 if ( $this->mShowReset
) {
78 $buttons .= new OOUI\
ButtonInputWidget( array(
80 'label' => $this->msg( 'htmlform-reset' )->text(),
84 foreach ( $this->mButtons
as $button ) {
87 if ( $button['attribs'] ) {
88 $attrs +
= $button['attribs'];
91 if ( isset( $button['id'] ) ) {
92 $attrs['id'] = $button['id'];
95 $attrs['classes'] = isset( $attrs['class'] ) ?
(array)$attrs['class'] : array();
97 $buttons .= new OOUI\
ButtonInputWidget( array(
99 'name' => $button['name'],
100 'value' => $button['value'],
101 'label' => $button['value'],
105 $html = Html
::rawElement( 'div',
106 array( 'class' => 'mw-htmlform-submit-buttons' ), "\n$buttons" ) . "\n";
112 * Put a form section together from the individual fields' HTML, merging it and wrapping.
113 * @param OOUI\\FieldLayout[] $fieldsHtml
114 * @param string $sectionName
115 * @param bool $anyFieldHasLabel Unused
116 * @return string HTML
118 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
120 'items' => $fieldsHtml,
122 if ( $sectionName ) {
123 $config['id'] = Sanitizer
::escapeId( $sectionName );
125 if ( is_string( $this->mWrapperLegend
) ) {
126 $config['label'] = $this->mWrapperLegend
;
128 return new OOUI\
FieldsetLayout( $config );
132 * @param string|array|Status $err
135 function getErrors( $err ) {
138 } elseif ( $err instanceof Status
) {
139 if ( $err->isOK() ) {
142 $errors = $err->getErrorsByType( 'error' );
143 foreach ( $errors as &$error ) {
144 // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) )
145 // Output: array( 'foo', 'a', 'b', 'c' )
146 $error = array_merge( array( $error['message'] ), $error['params'] );
151 if ( !is_array( $errors ) ) {
152 $errors = array( $errors );
156 foreach ( $errors as &$error ) {
157 if ( is_array( $error ) ) {
158 $msg = array_shift( $error );
163 // if the error is already a message object, don't use it as a message key
164 if ( !$msg instanceof Message
) {
165 $error = $this->msg( $msg, $error )->parse();
167 $error = $msg->parse();
169 $error = new OOUI\
HtmlSnippet( $error );
173 $this->oouiErrors
= $errors;
177 function getHeaderText( $section = null ) {
178 if ( is_null( $section ) ) {
179 // We handle $this->mHeader elsewhere, in getBody()
182 return parent
::getHeaderText( $section );
187 $fieldset = parent
::getBody();
188 // FIXME This only works for forms with no subsections
189 if ( $fieldset instanceof OOUI\FieldsetLayout
) {
190 $classes = array( 'mw-htmlform-ooui-header' );
191 if ( !$this->mHeader
) {
192 $classes[] = 'mw-htmlform-ooui-header-empty';
194 if ( $this->oouiErrors
) {
195 $classes[] = 'mw-htmlform-ooui-header-errors';
197 $fieldset->addItems( array(
198 new OOUI\
FieldLayout(
199 new OOUI\
LabelWidget( array( 'label' => new OOUI\
HtmlSnippet( $this->mHeader
) ) ),
202 'errors' => $this->oouiErrors
,
203 'classes' => $classes,
211 function wrapForm( $html ) {
212 $form = new OOUI\
FormLayout( $this->getFormAttributes() +
array(
213 'classes' => array( 'mw-htmlform-ooui' ),
214 'content' => new OOUI\
HtmlSnippet( $html ),
217 // Include a wrapper for style, if requested.
218 $form = new OOUI\
PanelLayout( array(
219 'classes' => array( 'mw-htmlform-ooui-wrapper' ),
221 'padded' => $this->mWrapperLegend
!== false,
222 'framed' => $this->mWrapperLegend
!== false,