Merge "TitleInputWidget: Add 'maxLength' of 255 and use $.byteLimit"
[lhc/web/wiklou.git] / includes / htmlform / OOUIHTMLForm.php
1 <?php
2
3 /**
4 * HTML form generation and submission handling, OOUI style.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 */
23
24 /**
25 * Compact stacked vertical format for forms, implemented using OOUI widgets.
26 */
27 class OOUIHTMLForm extends HTMLForm {
28 private $oouiErrors;
29
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' );
34 }
35
36 /**
37 * Symbolic display format name.
38 * @var string
39 */
40 protected $displayFormat = 'ooui';
41
42 public static function loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent = null ) {
43 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
44 $field->setShowEmptyLabel( false );
45 return $field;
46 }
47
48 function getButtons() {
49 $buttons = '';
50
51 if ( $this->mShowSubmit ) {
52 $attribs = array( 'infusable' => true );
53
54 if ( isset( $this->mSubmitID ) ) {
55 $attribs['id'] = $this->mSubmitID;
56 }
57
58 if ( isset( $this->mSubmitName ) ) {
59 $attribs['name'] = $this->mSubmitName;
60 }
61
62 if ( isset( $this->mSubmitTooltip ) ) {
63 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
64 }
65
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;
71
72 $buttons .= new OOUI\ButtonInputWidget( $attribs );
73 }
74
75 if ( $this->mShowReset ) {
76 $buttons .= new OOUI\ButtonInputWidget( array(
77 'type' => 'reset',
78 'label' => $this->msg( 'htmlform-reset' )->text(),
79 ) );
80 }
81
82 foreach ( $this->mButtons as $button ) {
83 $attrs = array();
84
85 if ( $button['attribs'] ) {
86 $attrs += $button['attribs'];
87 }
88
89 if ( isset( $button['id'] ) ) {
90 $attrs['id'] = $button['id'];
91 }
92
93 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : array();
94
95 $buttons .= new OOUI\ButtonInputWidget( array(
96 'type' => 'submit',
97 'name' => $button['name'],
98 'value' => $button['value'],
99 'label' => $button['value'],
100 ) + $attrs );
101 }
102
103 $html = Html::rawElement( 'div',
104 array( 'class' => 'mw-htmlform-submit-buttons' ), "\n$buttons" ) . "\n";
105
106 return $html;
107 }
108
109 /**
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
115 */
116 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
117 $config = array(
118 'items' => $fieldsHtml,
119 );
120 if ( $sectionName ) {
121 $config['id'] = Sanitizer::escapeId( $sectionName );
122 }
123 if ( is_string( $this->mWrapperLegend ) ) {
124 $config['label'] = $this->mWrapperLegend;
125 }
126 return new OOUI\FieldsetLayout( $config );
127 }
128
129 /**
130 * @param string|array|Status $err
131 * @return string
132 */
133 function getErrors( $err ) {
134 if ( !$err ) {
135 $errors = array();
136 } else if ( $err instanceof Status ) {
137 if ( $err->isOK() ) {
138 $errors = array();
139 } else {
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'] );
145 }
146 }
147 } else {
148 $errors = $err;
149 if ( !is_array( $errors ) ) {
150 $errors = array( $errors );
151 }
152 }
153
154 foreach ( $errors as &$error ) {
155 if ( is_array( $error ) ) {
156 $msg = array_shift( $error );
157 } else {
158 $msg = $error;
159 $error = array();
160 }
161 $error = $this->msg( $msg, $error )->parse();
162 $error = new OOUI\HtmlSnippet( $error );
163 }
164
165 // Used in getBody()
166 $this->oouiErrors = $errors;
167 return '';
168 }
169
170 function getHeaderText( $section = null ) {
171 if ( is_null( $section ) ) {
172 // We handle $this->mHeader elsewhere, in getBody()
173 return '';
174 } else {
175 return parent::getHeaderText( $section );
176 }
177 }
178
179 function getBody() {
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';
186 }
187 if ( $this->oouiErrors ) {
188 $classes[] = 'mw-htmlform-ooui-header-errors';
189 }
190 $fieldset->addItems( array(
191 new OOUI\FieldLayout(
192 new OOUI\LabelWidget( array( 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ) ),
193 array(
194 'align' => 'top',
195 'errors' => $this->oouiErrors,
196 'classes' => $classes,
197 )
198 )
199 ), 0 );
200 }
201 return $fieldset;
202 }
203
204 function wrapForm( $html ) {
205 $form = new OOUI\FormLayout( $this->getFormAttributes() + array(
206 'classes' => array( 'mw-htmlform-ooui' ),
207 'content' => new OOUI\HtmlSnippet( $html ),
208 ) );
209
210 // Include a wrapper for style, if requested.
211 $form = new OOUI\PanelLayout( array(
212 'classes' => array( 'mw-htmlform-ooui-wrapper' ),
213 'expanded' => false,
214 'padded' => $this->mWrapperLegend !== false,
215 'framed' => $this->mWrapperLegend !== false,
216 'content' => $form,
217 ) );
218
219 return $form;
220 }
221 }