f8aa0ac339fab953ab145de47234ff71f9d3881b
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, HTMLForm
$parent = null ) {
43 $field = parent
::loadInputFromParameters( $fieldname, $descriptor, $parent );
44 $field->setShowEmptyLabel( false );
48 function getButtons() {
51 if ( $this->mShowSubmit
) {
52 $attribs = array( 'infusable' => true );
54 if ( isset( $this->mSubmitID
) ) {
55 $attribs['id'] = $this->mSubmitID
;
58 if ( isset( $this->mSubmitName
) ) {
59 $attribs['name'] = $this->mSubmitName
;
62 if ( isset( $this->mSubmitTooltip
) ) {
63 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
66 $attribs['classes'] = array( 'mw-htmlform-submit' );
67 $attribs['type'] = 'submit';
68 $attribs['label'] = $this->getSubmitText();
69 $attribs['value'] = $this->getSubmitText();
70 $attribs['flags'] = $this->mSubmitFlags
;
72 $buttons .= new OOUI\
ButtonInputWidget( $attribs );
75 if ( $this->mShowReset
) {
76 $buttons .= new OOUI\
ButtonInputWidget( array(
78 'label' => $this->msg( 'htmlform-reset' )->text(),
82 foreach ( $this->mButtons
as $button ) {
85 if ( $button['attribs'] ) {
86 $attrs +
= $button['attribs'];
89 if ( isset( $button['id'] ) ) {
90 $attrs['id'] = $button['id'];
93 $attrs['classes'] = isset( $attrs['class'] ) ?
(array)$attrs['class'] : array();
95 $buttons .= new OOUI\
ButtonInputWidget( array(
97 'name' => $button['name'],
98 'value' => $button['value'],
99 'label' => $button['value'],
103 $html = Html
::rawElement( 'div',
104 array( 'class' => 'mw-htmlform-submit-buttons' ), "\n$buttons" ) . "\n";
110 * Put a form section together from the individual fields' HTML, merging it and wrapping.
111 * @param OOUI\\FieldLayout[] $fieldsHtml
112 * @param string $sectionName
113 * @param bool $anyFieldHasLabel Unused
114 * @return string HTML
116 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
118 'items' => $fieldsHtml,
120 if ( $sectionName ) {
121 $config['id'] = Sanitizer
::escapeId( $sectionName );
123 if ( is_string( $this->mWrapperLegend
) ) {
124 $config['label'] = $this->mWrapperLegend
;
126 return new OOUI\
FieldsetLayout( $config );
130 * @param string|array|Status $err
133 function getErrors( $err ) {
136 } elseif ( $err instanceof Status
) {
137 if ( $err->isOK() ) {
140 $errors = $err->getErrorsByType( 'error' );
141 foreach ( $errors as &$error ) {
142 // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) )
143 // Output: array( 'foo', 'a', 'b', 'c' )
144 $error = array_merge( array( $error['message'] ), $error['params'] );
149 if ( !is_array( $errors ) ) {
150 $errors = array( $errors );
154 foreach ( $errors as &$error ) {
155 if ( is_array( $error ) ) {
156 $msg = array_shift( $error );
161 $error = $this->msg( $msg, $error )->parse();
162 $error = new OOUI\
HtmlSnippet( $error );
166 $this->oouiErrors
= $errors;
170 function getHeaderText( $section = null ) {
171 if ( is_null( $section ) ) {
172 // We handle $this->mHeader elsewhere, in getBody()
175 return parent
::getHeaderText( $section );
180 $fieldset = parent
::getBody();
181 // FIXME This only works for forms with no subsections
182 if ( $fieldset instanceof OOUI\FieldsetLayout
) {
183 $classes = array( 'mw-htmlform-ooui-header' );
184 if ( !$this->mHeader
) {
185 $classes[] = 'mw-htmlform-ooui-header-empty';
187 if ( $this->oouiErrors
) {
188 $classes[] = 'mw-htmlform-ooui-header-errors';
190 $fieldset->addItems( array(
191 new OOUI\
FieldLayout(
192 new OOUI\
LabelWidget( array( 'label' => new OOUI\
HtmlSnippet( $this->mHeader
) ) ),
195 'errors' => $this->oouiErrors
,
196 'classes' => $classes,
204 function wrapForm( $html ) {
205 $form = new OOUI\
FormLayout( $this->getFormAttributes() +
array(
206 'classes' => array( 'mw-htmlform-ooui' ),
207 'content' => new OOUI\
HtmlSnippet( $html ),
210 // Include a wrapper for style, if requested.
211 $form = new OOUI\
PanelLayout( array(
212 'classes' => array( 'mw-htmlform-ooui-wrapper' ),
214 'padded' => $this->mWrapperLegend
!== false,
215 'framed' => $this->mWrapperLegend
!== false,