MediaWiki REL1_37
HTMLFormFieldCloner.php
Go to the documentation of this file.
1<?php
2
40 private static $counter = 0;
41
47 protected $uniqueId;
48
53 public function __construct( $params ) {
54 $this->uniqueId = $this->getClassName() . ++self::$counter . 'x';
55 parent::__construct( $params );
56
57 if ( empty( $this->mParams['fields'] ) || !is_array( $this->mParams['fields'] ) ) {
58 throw new MWException( 'HTMLFormFieldCloner called without any fields' );
59 }
60
61 // Make sure the delete button, if explicitly specified, is sane
62 if ( isset( $this->mParams['fields']['delete'] ) ) {
63 $class = 'mw-htmlform-cloner-delete-button';
64 $info = $this->mParams['fields']['delete'] + [
65 'formnovalidate' => true,
66 'cssclass' => $class
67 ];
68 unset( $info['name'], $info['class'] );
69
70 if ( !isset( $info['type'] ) || $info['type'] !== 'submit' ) {
71 throw new MWException(
72 'HTMLFormFieldCloner delete field, if specified, must be of type "submit"'
73 );
74 }
75
76 if ( !in_array( $class, explode( ' ', $info['cssclass'] ) ) ) {
77 $info['cssclass'] .= " $class";
78 }
79
80 $this->mParams['fields']['delete'] = $info;
81 }
82 }
83
91 protected function createFieldsForKey( $key ) {
92 $fields = [];
93 foreach ( $this->mParams['fields'] as $fieldname => $info ) {
94 $name = "{$this->mName}[$key][$fieldname]";
95 if ( isset( $info['name'] ) ) {
96 $info['name'] = "{$this->mName}[$key][{$info['name']}]";
97 } else {
98 $info['name'] = $name;
99 }
100 if ( isset( $info['id'] ) ) {
101 $info['id'] = Sanitizer::escapeIdForAttribute( "{$this->mID}--$key--{$info['id']}" );
102 } else {
103 $info['id'] = Sanitizer::escapeIdForAttribute( "{$this->mID}--$key--$fieldname" );
104 }
105 // Copy the hide-if rules to "child" fields, so that the JavaScript code handling them
106 // (resources/src/mediawiki/htmlform/hide-if.js) doesn't have to handle nested fields.
107 if ( $this->mHideIf ) {
108 if ( isset( $info['hide-if'] ) ) {
109 // Hide child field if either its rules say it's hidden, or parent's rules say it's hidden
110 $info['hide-if'] = [ 'OR', $info['hide-if'], $this->mHideIf ];
111 } else {
112 // Hide child field if parent's rules say it's hidden
113 $info['hide-if'] = $this->mHideIf;
114 }
115 }
116 $field = HTMLForm::loadInputFromParameters( $name, $info, $this->mParent );
117 $fields[$fieldname] = $field;
118 }
119 return $fields;
120 }
121
130 protected function rekeyValuesArray( $key, $values ) {
131 $data = [];
132 foreach ( $values as $fieldname => $value ) {
133 $name = "{$this->mName}[$key][$fieldname]";
134 $data[$name] = $value;
135 }
136 return $data;
137 }
138
139 protected function needsLabel() {
140 return false;
141 }
142
143 public function loadDataFromRequest( $request ) {
144 // It's possible that this might be posted with no fields. Detect that
145 // by looking for an edit token.
146 if ( !$request->getCheck( 'wpEditToken' ) && $request->getArray( $this->mName ) === null ) {
147 return $this->getDefault();
148 }
149
150 $values = $request->getArray( $this->mName );
151 if ( $values === null ) {
152 $values = [];
153 }
154
155 $ret = [];
156 foreach ( $values as $key => $value ) {
157 if ( $key === 'create' || isset( $value['delete'] ) ) {
158 $ret['nonjs'] = 1;
159 continue;
160 }
161
162 // Add back in $request->getValues() so things that look for e.g.
163 // wpEditToken don't fail.
164 $data = $this->rekeyValuesArray( $key, $value ) + $request->getValues();
165
166 $fields = $this->createFieldsForKey( $key );
167 $subrequest = new DerivativeRequest( $request, $data, $request->wasPosted() );
168 $row = [];
169 foreach ( $fields as $fieldname => $field ) {
170 if ( $field->skipLoadData( $subrequest ) ) {
171 continue;
172 }
173 if ( !empty( $field->mParams['disabled'] ) ) {
174 $row[$fieldname] = $field->getDefault();
175 } else {
176 $row[$fieldname] = $field->loadDataFromRequest( $subrequest );
177 }
178 }
179 $ret[] = $row;
180 }
181
182 if ( isset( $values['create'] ) ) {
183 // Non-JS client clicked the "create" button.
184 $fields = $this->createFieldsForKey( $this->uniqueId );
185 $row = [];
186 foreach ( $fields as $fieldname => $field ) {
187 if ( !empty( $field->mParams['nodata'] ) ) {
188 continue;
189 }
190 $row[$fieldname] = $field->getDefault();
191 }
192 $ret[] = $row;
193 }
194
195 return $ret;
196 }
197
198 public function getDefault() {
199 $ret = parent::getDefault();
200
201 // The default is one entry with all subfields at their defaults.
202 if ( $ret === null ) {
203 $fields = $this->createFieldsForKey( $this->uniqueId );
204 $row = [];
205 foreach ( $fields as $fieldname => $field ) {
206 if ( !empty( $field->mParams['nodata'] ) ) {
207 continue;
208 }
209 $row[$fieldname] = $field->getDefault();
210 }
211 $ret = [ $row ];
212 }
213
214 return $ret;
215 }
216
221 public function cancelSubmit( $values, $alldata ) {
222 if ( isset( $values['nonjs'] ) ) {
223 return true;
224 }
225
226 foreach ( $values as $key => $value ) {
227 $fields = $this->createFieldsForKey( $key );
228 foreach ( $fields as $fieldname => $field ) {
229 if ( !array_key_exists( $fieldname, $value ) ) {
230 continue;
231 }
232 if ( $field->cancelSubmit( $value[$fieldname], $alldata ) ) {
233 return true;
234 }
235 }
236 }
237
238 return parent::cancelSubmit( $values, $alldata );
239 }
240
245 public function validate( $values, $alldata ) {
246 if ( isset( $this->mParams['required'] )
247 && $this->mParams['required'] !== false
248 && !$values
249 ) {
250 return $this->msg( 'htmlform-cloner-required' );
251 }
252
253 if ( isset( $values['nonjs'] ) ) {
254 // The submission was a non-JS create/delete click, so fail
255 // validation in case cancelSubmit() somehow didn't already handle
256 // it.
257 return false;
258 }
259
260 foreach ( $values as $key => $value ) {
261 $fields = $this->createFieldsForKey( $key );
262 foreach ( $fields as $fieldname => $field ) {
263 if ( !array_key_exists( $fieldname, $value ) ) {
264 continue;
265 }
266 if ( $field->isHidden( $alldata ) ) {
267 continue;
268 }
269 $ok = $field->validate( $value[$fieldname], $alldata );
270 if ( $ok !== true ) {
271 return false;
272 }
273 }
274 }
275
276 return parent::validate( $values, $alldata );
277 }
278
286 protected function getInputHTMLForKey( $key, array $values ) {
287 $displayFormat = $this->mParams['format'] ?? $this->mParent->getDisplayFormat();
288
289 // Conveniently, PHP method names are case-insensitive.
290 $getFieldHtmlMethod = $displayFormat == 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
291
292 $html = '';
293 $hidden = '';
294 $hasLabel = false;
295
296 $fields = $this->createFieldsForKey( $key );
297 foreach ( $fields as $fieldname => $field ) {
298 $v = array_key_exists( $fieldname, $values )
299 ? $values[$fieldname]
300 : $field->getDefault();
301
302 if ( $field instanceof HTMLHiddenField ) {
303 // HTMLHiddenField doesn't generate its own HTML
304 list( $name, $value, $params ) = $field->getHiddenFieldData( $v );
305 $hidden .= Html::hidden( $name, $value, $params ) . "\n";
306 } else {
307 $html .= $field->$getFieldHtmlMethod( $v );
308
309 $labelValue = trim( $field->getLabel() );
310 if ( $labelValue !== "\u{00A0}" && $labelValue !== '&#160;' && $labelValue !== '' ) {
311 $hasLabel = true;
312 }
313 }
314 }
315
316 if ( !isset( $fields['delete'] ) ) {
317 $field = $this->getDeleteButtonHtml( $key );
318
319 if ( $displayFormat === 'table' ) {
320 $html .= $field->$getFieldHtmlMethod( $field->getDefault() );
321 } else {
322 $html .= $field->getInputHTML( $field->getDefault() );
323 }
324 }
325
326 if ( $displayFormat !== 'raw' ) {
327 $classes = [ 'mw-htmlform-cloner-row' ];
328
329 if ( !$hasLabel ) { // Avoid strange spacing when no labels exist
330 $classes[] = 'mw-htmlform-nolabel';
331 }
332
333 $attribs = [ 'class' => $classes ];
334
335 if ( $displayFormat === 'table' ) {
336 $html = Html::rawElement( 'table',
337 $attribs,
338 Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
339 } else {
340 $html = Html::rawElement( 'div', $attribs, "\n$html\n" );
341 }
342 }
343
344 $html .= $hidden;
345
346 if ( !empty( $this->mParams['row-legend'] ) ) {
347 $legend = $this->msg( $this->mParams['row-legend'] )->text();
348 $html = Xml::fieldset( $legend, $html );
349 }
350
351 return $html;
352 }
353
358 protected function getDeleteButtonHtml( $key ): HTMLFormField {
359 $name = "{$this->mName}[$key][delete]";
360 $label = $this->mParams['delete-button-message'] ?? 'htmlform-cloner-delete';
361 $field = HTMLForm::loadInputFromParameters( $name, [
362 'type' => 'submit',
363 'formnovalidate' => true,
364 'name' => $name,
365 'id' => Sanitizer::escapeIdForAttribute( "{$this->mID}--$key--delete" ),
366 'cssclass' => 'mw-htmlform-cloner-delete-button',
367 'default' => $this->getMessage( $label )->text(),
368 'disabled' => $this->mParams['disabled'] ?? false,
369 ], $this->mParent );
370 return $field;
371 }
372
373 protected function getCreateButtonHtml(): HTMLFormField {
374 $name = "{$this->mName}[create]";
375 $label = $this->mParams['create-button-message'] ?? 'htmlform-cloner-create';
376 return HTMLForm::loadInputFromParameters( $name, [
377 'type' => 'submit',
378 'formnovalidate' => true,
379 'name' => $name,
380 'id' => Sanitizer::escapeIdForAttribute( "{$this->mID}--create" ),
381 'cssclass' => 'mw-htmlform-cloner-create-button',
382 'default' => $this->getMessage( $label )->text(),
383 'disabled' => $this->mParams['disabled'] ?? false,
384 ], $this->mParent );
385 }
386
387 public function getInputHTML( $values ) {
388 $html = '';
389
390 foreach ( (array)$values as $key => $value ) {
391 if ( $key === 'nonjs' ) {
392 continue;
393 }
394 $html .= Html::rawElement( 'li', [ 'class' => 'mw-htmlform-cloner-li' ],
395 $this->getInputHTMLForKey( $key, $value )
396 );
397 }
398
399 $template = $this->getInputHTMLForKey( $this->uniqueId, [] );
400 // @phan-suppress-next-line SecurityCheck-DoubleEscaped data-template contains html, but that is okay here
401 $html = Html::rawElement( 'ul', [
402 'id' => "mw-htmlform-cloner-list-{$this->mID}",
403 'class' => 'mw-htmlform-cloner-ul',
404 'data-template' => $template,
405 'data-unique-id' => $this->uniqueId,
406 ], $html );
407
408 $field = $this->getCreateButtonHtml();
409 $html .= $field->getInputHTML( $field->getDefault() );
410
411 return $html;
412 }
413
421 protected function getInputOOUIForKey( $key, array $values ) {
422 $html = '';
423 $hidden = '';
424
425 $fields = $this->createFieldsForKey( $key );
426 foreach ( $fields as $fieldname => $field ) {
427 $v = array_key_exists( $fieldname, $values )
428 ? $values[$fieldname]
429 : $field->getDefault();
430
431 if ( $field instanceof HTMLHiddenField ) {
432 // HTMLHiddenField doesn't generate its own HTML
433 list( $name, $value, $params ) = $field->getHiddenFieldData( $v );
434 $hidden .= Html::hidden( $name, $value, $params ) . "\n";
435 } else {
436 $html .= $field->getOOUI( $v );
437 }
438 }
439
440 if ( !isset( $fields['delete'] ) ) {
441 $field = $this->getDeleteButtonHtml( $key );
442 $fieldHtml = $field->getInputOOUI( $field->getDefault() );
443 $fieldHtml->setInfusable( true );
444
445 $html .= $fieldHtml;
446 }
447
448 $html = Html::rawElement( 'div', [ 'class' => 'mw-htmlform-cloner-row' ], "\n$html\n" );
449
450 $html .= $hidden;
451
452 if ( !empty( $this->mParams['row-legend'] ) ) {
453 $legend = $this->msg( $this->mParams['row-legend'] )->text();
454 $html = Xml::fieldset( $legend, $html );
455 }
456
457 return $html;
458 }
459
460 public function getInputOOUI( $values ) {
461 $html = '';
462
463 foreach ( (array)$values as $key => $value ) {
464 if ( $key === 'nonjs' ) {
465 continue;
466 }
467 $html .= Html::rawElement( 'li', [ 'class' => 'mw-htmlform-cloner-li' ],
468 $this->getInputOOUIForKey( $key, $value )
469 );
470 }
471
472 $template = $this->getInputOOUIForKey( $this->uniqueId, [] );
473 // @phan-suppress-next-line SecurityCheck-DoubleEscaped data-template contains html, but that is okay here
474 $html = Html::rawElement( 'ul', [
475 'id' => "mw-htmlform-cloner-list-{$this->mID}",
476 'class' => 'mw-htmlform-cloner-ul',
477 'data-template' => $template,
478 'data-unique-id' => $this->uniqueId,
479 ], $html );
480
481 $field = $this->getCreateButtonHtml();
482 $fieldHtml = $field->getInputOOUI( $field->getDefault() );
483 $fieldHtml->setInfusable( true );
484
485 $html .= $fieldHtml;
486
487 return $html;
488 }
489}
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...
A container for HTMLFormFields that allows for multiple copies of the set of fields to be displayed t...
rekeyValuesArray( $key, $values)
Re-key the specified values array to match the names applied by createFieldsForKey().
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
validate( $values, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
createFieldsForKey( $key)
Create the HTMLFormFields that go inside this element, using the specified key.
getInputOOUI( $values)
Same as getInputHTML, but returns an OOUI object.
string $uniqueId
String uniquely identifying this cloner instance and unlikely to exist otherwise in the generated HTM...
getInputOOUIForKey( $key, array $values)
Get the input OOUI HTML for the specified key.
getInputHTMLForKey( $key, array $values)
Get the input HTML for the specified key.
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
getInputHTML( $values)
This function must be implemented to return the HTML to generate the input object itself.
cancelSubmit( $values, $alldata)
Override this function if the control can somehow trigger a form submission that shouldn't actually s...
The parent class to generate form fields.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getClassName()
Gets the non namespaced class name.
msg( $key,... $params)
Get a translated interface message.
MediaWiki exception.