Make mediawiki.special.pageLanguage work again
[lhc/web/wiklou.git] / includes / htmlform / HTMLFormFieldCloner.php
1 <?php
2
3 /**
4 * A container for HTMLFormFields that allows for multiple copies of the set of
5 * fields to be displayed to and entered by the user.
6 *
7 * Recognized parameters, besides the general ones, include:
8 * fields - HTMLFormField descriptors for the subfields this cloner manages.
9 * The format is just like for the HTMLForm. A field with key 'delete' is
10 * special: it must have type = submit and will serve to delete the group
11 * of fields.
12 * required - If specified, at least one group of fields must be submitted.
13 * format - HTMLForm display format to use when displaying the subfields:
14 * 'table', 'div', or 'raw'.
15 * row-legend - If non-empty, each group of subfields will be enclosed in a
16 * fieldset. The value is the name of a message key to use as the legend.
17 * create-button-message - Message key to use as the text of the button to
18 * add an additional group of fields.
19 * delete-button-message - Message key to use as the text of automatically-
20 * generated 'delete' button. Ignored if 'delete' is included in 'fields'.
21 *
22 * In the generated HTML, the subfields will be named along the lines of
23 * "clonerName[index][fieldname]", with ids "clonerId--index--fieldid". 'index'
24 * may be a number or an arbitrary string, and may likely change when the page
25 * is resubmitted. Cloners may be nested, resulting in field names along the
26 * lines of "cloner1Name[index1][cloner2Name][index2][fieldname]" and
27 * corresponding ids.
28 *
29 * Use of cloner may result in submissions of the page that are not submissions
30 * of the HTMLForm, when non-JavaScript clients use the create or remove buttons.
31 *
32 * The result is an array, with values being arrays mapping subfield names to
33 * their values. On non-HTMLForm-submission page loads, there may also be
34 * additional (string) keys present with other types of values.
35 *
36 * @since 1.23
37 */
38 class HTMLFormFieldCloner extends HTMLFormField {
39 private static $counter = 0;
40
41 /**
42 * @var string String uniquely identifying this cloner instance and
43 * unlikely to exist otherwise in the generated HTML, while still being
44 * valid as part of an HTML id.
45 */
46 protected $uniqueId;
47
48 public function __construct( $params ) {
49 $this->uniqueId = get_class( $this ) . ++self::$counter . 'x';
50 parent::__construct( $params );
51
52 if ( empty( $this->mParams['fields'] ) || !is_array( $this->mParams['fields'] ) ) {
53 throw new MWException( 'HTMLFormFieldCloner called without any fields' );
54 }
55
56 // Make sure the delete button, if explicitly specified, is sane
57 if ( isset( $this->mParams['fields']['delete'] ) ) {
58 $class = 'mw-htmlform-cloner-delete-button';
59 $info = $this->mParams['fields']['delete'] + array(
60 'cssclass' => $class
61 );
62 unset( $info['name'], $info['class'] );
63
64 if ( !isset( $info['type'] ) || $info['type'] !== 'submit' ) {
65 throw new MWException(
66 'HTMLFormFieldCloner delete field, if specified, must be of type "submit"'
67 );
68 }
69
70 if ( !in_array( $class, explode( ' ', $info['cssclass'] ) ) ) {
71 $info['cssclass'] .= " $class";
72 }
73
74 $this->mParams['fields']['delete'] = $info;
75 }
76 }
77
78 /**
79 * Create the HTMLFormFields that go inside this element, using the
80 * specified key.
81 *
82 * @param string $key Array key under which these fields should be named
83 * @return HTMLFormField[]
84 */
85 protected function createFieldsForKey( $key ) {
86 $fields = array();
87 foreach ( $this->mParams['fields'] as $fieldname => $info ) {
88 $name = "{$this->mName}[$key][$fieldname]";
89 if ( isset( $info['name'] ) ) {
90 $info['name'] = "{$this->mName}[$key][{$info['name']}]";
91 } else {
92 $info['name'] = $name;
93 }
94 if ( isset( $info['id'] ) ) {
95 $info['id'] = Sanitizer::escapeId( "{$this->mID}--$key--{$info['id']}" );
96 } else {
97 $info['id'] = Sanitizer::escapeId( "{$this->mID}--$key--$fieldname" );
98 }
99 $field = HTMLForm::loadInputFromParameters( $name, $info, $this->mParent );
100 $fields[$fieldname] = $field;
101 }
102 return $fields;
103 }
104
105 /**
106 * Re-key the specified values array to match the names applied by
107 * createFieldsForKey().
108 *
109 * @param string $key Array key under which these fields should be named
110 * @param array $values Values array from the request
111 * @return array
112 */
113 protected function rekeyValuesArray( $key, $values ) {
114 $data = array();
115 foreach ( $values as $fieldname => $value ) {
116 $name = "{$this->mName}[$key][$fieldname]";
117 $data[$name] = $value;
118 }
119 return $data;
120 }
121
122 protected function needsLabel() {
123 return false;
124 }
125
126 public function loadDataFromRequest( $request ) {
127 // It's possible that this might be posted with no fields. Detect that
128 // by looking for an edit token.
129 if ( !$request->getCheck( 'wpEditToken' ) && $request->getArray( $this->mName ) === null ) {
130 return $this->getDefault();
131 }
132
133 $values = $request->getArray( $this->mName );
134 if ( $values === null ) {
135 $values = array();
136 }
137
138 $ret = array();
139 foreach ( $values as $key => $value ) {
140 if ( $key === 'create' || isset( $value['delete'] ) ) {
141 $ret['nonjs'] = 1;
142 continue;
143 }
144
145 // Add back in $request->getValues() so things that look for e.g.
146 // wpEditToken don't fail.
147 $data = $this->rekeyValuesArray( $key, $value ) + $request->getValues();
148
149 $fields = $this->createFieldsForKey( $key );
150 $subrequest = new DerivativeRequest( $request, $data, $request->wasPosted() );
151 $row = array();
152 foreach ( $fields as $fieldname => $field ) {
153 if ( !empty( $field->mParams['nodata'] ) ) {
154 continue;
155 } elseif ( !empty( $field->mParams['disabled'] ) ) {
156 $row[$fieldname] = $field->getDefault();
157 } else {
158 $row[$fieldname] = $field->loadDataFromRequest( $subrequest );
159 }
160 }
161 $ret[] = $row;
162 }
163
164 if ( isset( $values['create'] ) ) {
165 // Non-JS client clicked the "create" button.
166 $fields = $this->createFieldsForKey( $this->uniqueId );
167 $row = array();
168 foreach ( $fields as $fieldname => $field ) {
169 if ( !empty( $field->mParams['nodata'] ) ) {
170 continue;
171 } else {
172 $row[$fieldname] = $field->getDefault();
173 }
174 }
175 $ret[] = $row;
176 }
177
178 return $ret;
179 }
180
181 public function getDefault() {
182 $ret = parent::getDefault();
183
184 // The default default is one entry with all subfields at their
185 // defaults.
186 if ( $ret === null ) {
187 $fields = $this->createFieldsForKey( $this->uniqueId );
188 $row = array();
189 foreach ( $fields as $fieldname => $field ) {
190 if ( !empty( $field->mParams['nodata'] ) ) {
191 continue;
192 } else {
193 $row[$fieldname] = $field->getDefault();
194 }
195 }
196 $ret = array( $row );
197 }
198
199 return $ret;
200 }
201
202 public function cancelSubmit( $values, $alldata ) {
203 if ( isset( $values['nonjs'] ) ) {
204 return true;
205 }
206
207 foreach ( $values as $key => $value ) {
208 $fields = $this->createFieldsForKey( $key );
209 foreach ( $fields as $fieldname => $field ) {
210 if ( !empty( $field->mParams['nodata'] ) ) {
211 continue;
212 }
213 if ( $field->cancelSubmit( $value[$fieldname], $alldata ) ) {
214 return true;
215 }
216 }
217 }
218
219 return parent::cancelSubmit( $values, $alldata );
220 }
221
222 public function validate( $values, $alldata ) {
223 if ( isset( $this->mParams['required'] )
224 && $this->mParams['required'] !== false
225 && !$values
226 ) {
227 return $this->msg( 'htmlform-cloner-required' )->parseAsBlock();
228 }
229
230 if ( isset( $values['nonjs'] ) ) {
231 // The submission was a non-JS create/delete click, so fail
232 // validation in case cancelSubmit() somehow didn't already handle
233 // it.
234 return false;
235 }
236
237 foreach ( $values as $key => $value ) {
238 $fields = $this->createFieldsForKey( $key );
239 foreach ( $fields as $fieldname => $field ) {
240 if ( !empty( $field->mParams['nodata'] ) ) {
241 continue;
242 }
243 $ok = $field->validate( $value[$fieldname], $alldata );
244 if ( $ok !== true ) {
245 return false;
246 }
247 }
248 }
249
250 return parent::validate( $values, $alldata );
251 }
252
253 /**
254 * Get the input HTML for the specified key.
255 *
256 * @param string $key Array key under which the fields should be named
257 * @param array $values
258 * @return string
259 */
260 protected function getInputHTMLForKey( $key, $values ) {
261 $displayFormat = isset( $this->mParams['format'] )
262 ? $this->mParams['format']
263 : $this->mParent->getDisplayFormat();
264
265 // Conveniently, PHP method names are case-insensitive.
266 $getFieldHtmlMethod = $displayFormat == 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
267
268 $html = '';
269 $hidden = '';
270 $hasLabel = false;
271
272 $fields = $this->createFieldsForKey( $key );
273 foreach ( $fields as $fieldname => $field ) {
274 $v = ( empty( $field->mParams['nodata'] ) && $values !== null )
275 ? $values[$fieldname]
276 : $field->getDefault();
277
278 if ( $field instanceof HTMLHiddenField ) {
279 // HTMLHiddenField doesn't generate its own HTML
280 list( $name, $value, $params ) = $field->getHiddenFieldData( $v );
281 $hidden .= Html::hidden( $name, $value, $params ) . "\n";
282 } else {
283 $html .= $field->$getFieldHtmlMethod( $v );
284
285 $labelValue = trim( $field->getLabel() );
286 if ( $labelValue != '&#160;' && $labelValue !== '' ) {
287 $hasLabel = true;
288 }
289 }
290 }
291
292 if ( !isset( $fields['delete'] ) ) {
293 $name = "{$this->mName}[$key][delete]";
294 $label = isset( $this->mParams['delete-button-message'] )
295 ? $this->mParams['delete-button-message']
296 : 'htmlform-cloner-delete';
297 $field = HTMLForm::loadInputFromParameters( $name, array(
298 'type' => 'submit',
299 'name' => $name,
300 'id' => Sanitizer::escapeId( "{$this->mID}--$key--delete" ),
301 'cssclass' => 'mw-htmlform-cloner-delete-button',
302 'default' => $this->msg( $label )->text(),
303 ), $this->mParent );
304 $v = $field->getDefault();
305
306 if ( $displayFormat === 'table' ) {
307 $html .= $field->$getFieldHtmlMethod( $v );
308 } else {
309 $html .= $field->getInputHTML( $v );
310 }
311 }
312
313 if ( $displayFormat !== 'raw' ) {
314 $classes = array(
315 'mw-htmlform-cloner-row',
316 );
317
318 if ( !$hasLabel ) { // Avoid strange spacing when no labels exist
319 $classes[] = 'mw-htmlform-nolabel';
320 }
321
322 $attribs = array(
323 'class' => implode( ' ', $classes ),
324 );
325
326 if ( $displayFormat === 'table' ) {
327 $html = Html::rawElement( 'table',
328 $attribs,
329 Html::rawElement( 'tbody', array(), "\n$html\n" ) ) . "\n";
330 } else {
331 $html = Html::rawElement( 'div', $attribs, "\n$html\n" );
332 }
333 }
334
335 $html .= $hidden;
336
337 if ( !empty( $this->mParams['row-legend'] ) ) {
338 $legend = $this->msg( $this->mParams['row-legend'] )->text();
339 $html = Xml::fieldset( $legend, $html );
340 }
341
342 return $html;
343 }
344
345 public function getInputHTML( $values ) {
346 $html = '';
347
348 foreach ( (array)$values as $key => $value ) {
349 if ( $key === 'nonjs' ) {
350 continue;
351 }
352 $html .= Html::rawElement( 'li', array( 'class' => 'mw-htmlform-cloner-li' ),
353 $this->getInputHTMLForKey( $key, $value )
354 );
355 }
356
357 $template = $this->getInputHTMLForKey( $this->uniqueId, null );
358 $html = Html::rawElement( 'ul', array(
359 'id' => "mw-htmlform-cloner-list-{$this->mID}",
360 'class' => 'mw-htmlform-cloner-ul',
361 'data-template' => $template,
362 'data-unique-id' => $this->uniqueId,
363 ), $html );
364
365 $name = "{$this->mName}[create]";
366 $label = isset( $this->mParams['create-button-message'] )
367 ? $this->mParams['create-button-message']
368 : 'htmlform-cloner-create';
369 $field = HTMLForm::loadInputFromParameters( $name, array(
370 'type' => 'submit',
371 'name' => $name,
372 'id' => Sanitizer::escapeId( "{$this->mID}--create" ),
373 'cssclass' => 'mw-htmlform-cloner-create-button',
374 'default' => $this->msg( $label )->text(),
375 ), $this->mParent );
376 $html .= $field->getInputHTML( $field->getDefault() );
377
378 return $html;
379 }
380 }