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";
111 protected function wrapFieldSetSection( $legend, $section, $attributes ) {
112 // to get a user visible effect, wrap the fieldset into a framed panel layout
113 $layout = new OOUI\
PanelLayout( array(
117 'infusable' => false,
120 $layout->appendContent(
121 new OOUI\
FieldsetLayout( array(
123 'infusable' => false,
125 new OOUI\
Widget( array(
126 'content' => new OOUI\
HtmlSnippet( $section )
135 * Put a form section together from the individual fields' HTML, merging it and wrapping.
136 * @param OOUI\FieldLayout[] $fieldsHtml
137 * @param string $sectionName
138 * @param bool $anyFieldHasLabel Unused
139 * @return string HTML
141 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
143 'items' => $fieldsHtml,
145 if ( $sectionName ) {
146 $config['id'] = Sanitizer
::escapeId( $sectionName );
148 if ( is_string( $this->mWrapperLegend
) ) {
149 $config['label'] = $this->mWrapperLegend
;
151 return new OOUI\
FieldsetLayout( $config );
155 * @param string|array|Status $err
158 function getErrors( $err ) {
161 } elseif ( $err instanceof Status
) {
162 if ( $err->isOK() ) {
165 $errors = $err->getErrorsByType( 'error' );
166 foreach ( $errors as &$error ) {
167 // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) )
168 // Output: array( 'foo', 'a', 'b', 'c' )
169 $error = array_merge( array( $error['message'] ), $error['params'] );
174 if ( !is_array( $errors ) ) {
175 $errors = array( $errors );
179 foreach ( $errors as &$error ) {
180 if ( is_array( $error ) ) {
181 $msg = array_shift( $error );
186 // if the error is already a message object, don't use it as a message key
187 if ( !$msg instanceof Message
) {
188 $error = $this->msg( $msg, $error )->parse();
190 $error = $msg->parse();
192 $error = new OOUI\
HtmlSnippet( $error );
196 $this->oouiErrors
= $errors;
200 function getHeaderText( $section = null ) {
201 if ( is_null( $section ) ) {
202 // We handle $this->mHeader elsewhere, in getBody()
205 return parent
::getHeaderText( $section );
210 $fieldset = parent
::getBody();
211 // FIXME This only works for forms with no subsections
212 if ( $fieldset instanceof OOUI\FieldsetLayout
) {
213 $classes = array( 'mw-htmlform-ooui-header' );
214 if ( !$this->mHeader
) {
215 $classes[] = 'mw-htmlform-ooui-header-empty';
217 if ( $this->oouiErrors
) {
218 $classes[] = 'mw-htmlform-ooui-header-errors';
220 $fieldset->addItems( array(
221 new OOUI\
FieldLayout(
222 new OOUI\
LabelWidget( array( 'label' => new OOUI\
HtmlSnippet( $this->mHeader
) ) ),
225 'errors' => $this->oouiErrors
,
226 'classes' => $classes,
234 function wrapForm( $html ) {
235 $form = new OOUI\
FormLayout( $this->getFormAttributes() +
array(
236 'classes' => array( 'mw-htmlform-ooui' ),
237 'content' => new OOUI\
HtmlSnippet( $html ),
240 // Include a wrapper for style, if requested.
241 $form = new OOUI\
PanelLayout( array(
242 'classes' => array( 'mw-htmlform-ooui-wrapper' ),
244 'padded' => $this->mWrapperLegend
!== false,
245 'framed' => $this->mWrapperLegend
!== false,