Merge "Merge namespace aliases like we merge namespace names"
[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,
43 HTMLForm $parent = null
44 ) {
45 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
46 $field->setShowEmptyLabel( false );
47 return $field;
48 }
49
50 function getButtons() {
51 $buttons = '';
52
53 if ( $this->mShowSubmit ) {
54 $attribs = array( 'infusable' => true );
55
56 if ( isset( $this->mSubmitID ) ) {
57 $attribs['id'] = $this->mSubmitID;
58 }
59
60 if ( isset( $this->mSubmitName ) ) {
61 $attribs['name'] = $this->mSubmitName;
62 }
63
64 if ( isset( $this->mSubmitTooltip ) ) {
65 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
66 }
67
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;
73
74 $buttons .= new OOUI\ButtonInputWidget( $attribs );
75 }
76
77 if ( $this->mShowReset ) {
78 $buttons .= new OOUI\ButtonInputWidget( array(
79 'type' => 'reset',
80 'label' => $this->msg( 'htmlform-reset' )->text(),
81 ) );
82 }
83
84 foreach ( $this->mButtons as $button ) {
85 $attrs = array();
86
87 if ( $button['attribs'] ) {
88 $attrs += $button['attribs'];
89 }
90
91 if ( isset( $button['id'] ) ) {
92 $attrs['id'] = $button['id'];
93 }
94
95 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : array();
96
97 $buttons .= new OOUI\ButtonInputWidget( array(
98 'type' => 'submit',
99 'name' => $button['name'],
100 'value' => $button['value'],
101 'label' => $button['value'],
102 ) + $attrs );
103 }
104
105 $html = Html::rawElement( 'div',
106 array( 'class' => 'mw-htmlform-submit-buttons' ), "\n$buttons" ) . "\n";
107
108 return $html;
109 }
110
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(
114 'expanded' => false,
115 'padded' => true,
116 'framed' => true,
117 'infusable' => false,
118 ) );
119
120 $layout->appendContent(
121 new OOUI\FieldsetLayout( array(
122 'label' => $legend,
123 'infusable' => false,
124 'items' => array(
125 new OOUI\Widget( array(
126 'content' => new OOUI\HtmlSnippet( $section )
127 ) ),
128 ),
129 ) + $attributes )
130 );
131 return $layout;
132 }
133
134 /**
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
140 */
141 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
142 $config = array(
143 'items' => $fieldsHtml,
144 );
145 if ( $sectionName ) {
146 $config['id'] = Sanitizer::escapeId( $sectionName );
147 }
148 if ( is_string( $this->mWrapperLegend ) ) {
149 $config['label'] = $this->mWrapperLegend;
150 }
151 return new OOUI\FieldsetLayout( $config );
152 }
153
154 /**
155 * @param string|array|Status $err
156 * @return string
157 */
158 function getErrors( $err ) {
159 if ( !$err ) {
160 $errors = array();
161 } elseif ( $err instanceof Status ) {
162 if ( $err->isOK() ) {
163 $errors = array();
164 } else {
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'] );
170 }
171 }
172 } else {
173 $errors = $err;
174 if ( !is_array( $errors ) ) {
175 $errors = array( $errors );
176 }
177 }
178
179 foreach ( $errors as &$error ) {
180 if ( is_array( $error ) ) {
181 $msg = array_shift( $error );
182 } else {
183 $msg = $error;
184 $error = array();
185 }
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();
189 } else {
190 $error = $msg->parse();
191 }
192 $error = new OOUI\HtmlSnippet( $error );
193 }
194
195 // Used in getBody()
196 $this->oouiErrors = $errors;
197 return '';
198 }
199
200 function getHeaderText( $section = null ) {
201 if ( is_null( $section ) ) {
202 // We handle $this->mHeader elsewhere, in getBody()
203 return '';
204 } else {
205 return parent::getHeaderText( $section );
206 }
207 }
208
209 function getBody() {
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';
216 }
217 if ( $this->oouiErrors ) {
218 $classes[] = 'mw-htmlform-ooui-header-errors';
219 }
220 $fieldset->addItems( array(
221 new OOUI\FieldLayout(
222 new OOUI\LabelWidget( array( 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ) ),
223 array(
224 'align' => 'top',
225 'errors' => $this->oouiErrors,
226 'classes' => $classes,
227 )
228 )
229 ), 0 );
230 }
231 return $fieldset;
232 }
233
234 function wrapForm( $html ) {
235 $form = new OOUI\FormLayout( $this->getFormAttributes() + array(
236 'classes' => array( 'mw-htmlform-ooui' ),
237 'content' => new OOUI\HtmlSnippet( $html ),
238 ) );
239
240 // Include a wrapper for style, if requested.
241 $form = new OOUI\PanelLayout( array(
242 'classes' => array( 'mw-htmlform-ooui-wrapper' ),
243 'expanded' => false,
244 'padded' => $this->mWrapperLegend !== false,
245 'framed' => $this->mWrapperLegend !== false,
246 'content' => $form,
247 ) );
248
249 return $form;
250 }
251 }