Merge "Add comment to unused EDIT_DEFER_UPDATES in Defines.php"
[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 /**
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
117 */
118 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
119 $config = array(
120 'items' => $fieldsHtml,
121 );
122 if ( $sectionName ) {
123 $config['id'] = Sanitizer::escapeId( $sectionName );
124 }
125 if ( is_string( $this->mWrapperLegend ) ) {
126 $config['label'] = $this->mWrapperLegend;
127 }
128 return new OOUI\FieldsetLayout( $config );
129 }
130
131 /**
132 * @param string|array|Status $err
133 * @return string
134 */
135 function getErrors( $err ) {
136 if ( !$err ) {
137 $errors = array();
138 } elseif ( $err instanceof Status ) {
139 if ( $err->isOK() ) {
140 $errors = array();
141 } else {
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'] );
147 }
148 }
149 } else {
150 $errors = $err;
151 if ( !is_array( $errors ) ) {
152 $errors = array( $errors );
153 }
154 }
155
156 foreach ( $errors as &$error ) {
157 if ( is_array( $error ) ) {
158 $msg = array_shift( $error );
159 } else {
160 $msg = $error;
161 $error = array();
162 }
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();
166 } else {
167 $error = $msg->parse();
168 }
169 $error = new OOUI\HtmlSnippet( $error );
170 }
171
172 // Used in getBody()
173 $this->oouiErrors = $errors;
174 return '';
175 }
176
177 function getHeaderText( $section = null ) {
178 if ( is_null( $section ) ) {
179 // We handle $this->mHeader elsewhere, in getBody()
180 return '';
181 } else {
182 return parent::getHeaderText( $section );
183 }
184 }
185
186 function getBody() {
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';
193 }
194 if ( $this->oouiErrors ) {
195 $classes[] = 'mw-htmlform-ooui-header-errors';
196 }
197 $fieldset->addItems( array(
198 new OOUI\FieldLayout(
199 new OOUI\LabelWidget( array( 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ) ),
200 array(
201 'align' => 'top',
202 'errors' => $this->oouiErrors,
203 'classes' => $classes,
204 )
205 )
206 ), 0 );
207 }
208 return $fieldset;
209 }
210
211 function wrapForm( $html ) {
212 $form = new OOUI\FormLayout( $this->getFormAttributes() + array(
213 'classes' => array( 'mw-htmlform-ooui' ),
214 'content' => new OOUI\HtmlSnippet( $html ),
215 ) );
216
217 // Include a wrapper for style, if requested.
218 $form = new OOUI\PanelLayout( array(
219 'classes' => array( 'mw-htmlform-ooui-wrapper' ),
220 'expanded' => false,
221 'padded' => $this->mWrapperLegend !== false,
222 'framed' => $this->mWrapperLegend !== false,
223 'content' => $form,
224 ) );
225
226 return $form;
227 }
228 }