1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
<?php
namespace MediaWiki\HTMLForm\Field;
use InvalidArgumentException;
use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\HTMLForm\HTMLFormField;
use MediaWiki\Parser\Sanitizer;
use MediaWiki\Request\DerivativeRequest;
use MediaWiki\Xml\Xml;
/**
* A container for HTMLFormFields that allows for multiple copies of the set of
* fields to be displayed to and entered by the user.
*
* Recognized parameters, besides the general ones, include:
* fields - HTMLFormField descriptors for the subfields this cloner manages.
* The format is just like for the HTMLForm. A field with key 'delete' is
* special: it must have type = submit and will serve to delete the group
* of fields.
* required - If specified, at least one group of fields must be submitted.
* format - HTMLForm display format to use when displaying the subfields:
* 'table', 'div', or 'raw'. This is ignored when using OOUI.
* row-legend - If non-empty, each group of subfields will be enclosed in a
* fieldset. The value is the name of a message key to use as the legend.
* create-button-message - Message to use as the text of the button to
* add an additional group of fields.
* delete-button-message - Message to use as the text of automatically-
* generated 'delete' button. Ignored if 'delete' is included in 'fields'.
*
* In the generated HTML, the subfields will be named along the lines of
* "clonerName[index][fieldname]", with ids "clonerId--index--fieldid". 'index'
* may be a number or an arbitrary string, and may likely change when the page
* is resubmitted. Cloners may be nested, resulting in field names along the
* lines of "cloner1Name[index1][cloner2Name][index2][fieldname]" and
* corresponding ids.
*
* Use of cloner may result in submissions of the page that are not submissions
* of the HTMLForm, when non-JavaScript clients use the create or remove buttons.
*
* The result is an array, with values being arrays mapping subfield names to
* their values. On non-HTMLForm-submission page loads, there may also be
* additional (string) keys present with other types of values.
*
* @since 1.23
* @stable to extend
*/
class HTMLFormFieldCloner extends HTMLFormField {
/** @var int */
private static $counter = 0;
/**
* @var string String uniquely identifying this cloner instance and
* unlikely to exist otherwise in the generated HTML, while still being
* valid as part of an HTML id.
*/
protected $uniqueId;
/** @var array<string, HTMLFormField[]> */
protected $mFields = [];
/**
* @stable to call
* @inheritDoc
*/
public function __construct( $params ) {
$this->uniqueId = $this->getClassName() . ++self::$counter . 'x';
parent::__construct( $params );
if ( empty( $this->mParams['fields'] ) || !is_array( $this->mParams['fields'] ) ) {
throw new InvalidArgumentException( 'HTMLFormFieldCloner called without any fields' );
}
// Make sure the delete button, if explicitly specified, is sensible
if ( isset( $this->mParams['fields']['delete'] ) ) {
$class = 'mw-htmlform-cloner-delete-button';
$info = $this->mParams['fields']['delete'] + [
'formnovalidate' => true,
'cssclass' => $class
];
unset( $info['name'], $info['class'] );
if ( !isset( $info['type'] ) || $info['type'] !== 'submit' ) {
throw new InvalidArgumentException(
'HTMLFormFieldCloner delete field, if specified, must be of type "submit"'
);
}
if ( !in_array( $class, explode( ' ', $info['cssclass'] ) ) ) {
$info['cssclass'] .= " $class";
}
$this->mParams['fields']['delete'] = $info;
}
}
/**
* @param string $key Array key under which these fields should be named
* @return HTMLFormField[]
*/
protected function getFieldsForKey( $key ) {
if ( !isset( $this->mFields[$key] ) ) {
$this->mFields[$key] = $this->createFieldsForKey( $key );
}
return $this->mFields[$key];
}
/**
* Create the HTMLFormFields that go inside this element, using the
* specified key.
*
* @param string $key Array key under which these fields should be named
* @return HTMLFormField[]
*/
protected function createFieldsForKey( $key ) {
$fields = [];
foreach ( $this->mParams['fields'] as $fieldname => $info ) {
$name = "{$this->mName}[$key][$fieldname]";
if ( isset( $info['name'] ) ) {
$info['name'] = "{$this->mName}[$key][{$info['name']}]";
} else {
$info['name'] = $name;
}
if ( isset( $info['id'] ) ) {
$info['id'] = Sanitizer::escapeIdForAttribute( "{$this->mID}--$key--{$info['id']}" );
} else {
$info['id'] = Sanitizer::escapeIdForAttribute( "{$this->mID}--$key--$fieldname" );
}
// Copy the hide-if and disable-if rules to "child" fields, so that the JavaScript code handling them
// (resources/src/mediawiki.htmlform/cond-state.js) doesn't have to handle nested fields.
if ( $this->mCondState ) {
foreach ( [ 'hide', 'disable' ] as $type ) {
if ( !isset( $this->mCondState[$type] ) ) {
continue;
}
$param = $type . '-if';
if ( isset( $info[$param] ) ) {
// Hide or disable child field if either its rules say so, or parent's rules say so.
$info[$param] = [ 'OR', $info[$param], $this->mCondState[$type] ];
} else {
// Hide or disable child field if parent's rules say so.
$info[$param] = $this->mCondState[$type];
}
}
}
$cloner = $this;
$info['cloner'] = &$cloner;
$info['cloner-key'] = $key;
$field = HTMLForm::loadInputFromParameters( $fieldname, $info, $this->mParent );
$fields[$fieldname] = $field;
}
return $fields;
}
/**
* Re-key the specified values array to match the names applied by
* createFieldsForKey().
*
* @param string $key Array key under which these fields should be named
* @param array $values Values array from the request
* @return array
*/
protected function rekeyValuesArray( $key, $values ) {
$data = [];
foreach ( $values as $fieldname => $value ) {
$name = "{$this->mName}[$key][$fieldname]";
$data[$name] = $value;
}
return $data;
}
/**
* @param string $name
* @return string[]
*/
protected function parseFieldPath( $name ) {
$fieldKeys = [];
while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $name, $m ) ) {
array_unshift( $fieldKeys, $m[2] );
$name = $m[1];
}
array_unshift( $fieldKeys, $name );
return $fieldKeys;
}
/**
* Find the nearest field to a field in this cloner matched the given name,
* walk through the chain of cloners.
*
* @param HTMLFormField $field
* @param string $find
* @return HTMLFormField|null
*/
public function findNearestField( $field, $find ) {
$findPath = $this->parseFieldPath( $find );
// Access to fields as child or in other group is not allowed.
// Further support for a more complicated path may conduct here.
if ( count( $findPath ) > 1 ) {
return null;
}
if ( !isset( $this->mParams['fields'][$find] ) ) {
$cloner = $this->mParams['cloner'] ?? null;
if ( $cloner instanceof self ) {
return $cloner->findNearestField( $this, $find );
}
return null;
}
$fields = $this->getFieldsForKey( $field->mParams['cloner-key'] );
return $fields[$find];
}
/**
* @param HTMLFormField $field
* @return string[]
*/
protected function getFieldPath( $field ) {
$path = [ $this->mParams['fieldname'], $field->mParams['cloner-key'] ];
$cloner = $this->mParams['cloner'] ?? null;
if ( $cloner instanceof self ) {
$path = array_merge( $cloner->getFieldPath( $this ), $path );
}
return $path;
}
/**
* Extract field data for a given field that belongs to this cloner.
*
* @param HTMLFormField $field
* @param mixed[] $alldata
* @return mixed
*/
public function extractFieldData( $field, $alldata ) {
foreach ( $this->getFieldPath( $field ) as $key ) {
$alldata = $alldata[$key];
}
return $alldata[$field->mParams['fieldname']];
}
protected function needsLabel() {
return false;
}
public function loadDataFromRequest( $request ) {
// It's possible that this might be posted with no fields. Detect that
// by looking for an edit token.
if ( !$request->getCheck( 'wpEditToken' ) && $request->getArray( $this->mName ) === null ) {
return $this->getDefault();
}
$values = $request->getArray( $this->mName ) ?? [];
$ret = [];
foreach ( $values as $key => $value ) {
if ( $key === 'create' || isset( $value['delete'] ) ) {
$ret['nonjs'] = 1;
continue;
}
// Add back in $request->getValues() so things that look for e.g.
// wpEditToken don't fail.
$data = $this->rekeyValuesArray( $key, $value ) + $request->getValues();
$fields = $this->getFieldsForKey( $key );
$subrequest = new DerivativeRequest( $request, $data, $request->wasPosted() );
$row = [];
foreach ( $fields as $fieldname => $field ) {
if ( $field->skipLoadData( $subrequest ) ) {
continue;
}
if ( !empty( $field->mParams['disabled'] ) ) {
$row[$fieldname] = $field->getDefault();
} else {
$row[$fieldname] = $field->loadDataFromRequest( $subrequest );
}
}
$ret[] = $row;
}
if ( isset( $values['create'] ) ) {
// Non-JS client clicked the "create" button.
$fields = $this->getFieldsForKey( $this->uniqueId );
$row = [];
foreach ( $fields as $fieldname => $field ) {
if ( !empty( $field->mParams['nodata'] ) ) {
continue;
}
$row[$fieldname] = $field->getDefault();
}
$ret[] = $row;
}
return $ret;
}
public function getDefault() {
$ret = parent::getDefault();
// The default is one entry with all subfields at their defaults.
if ( $ret === null ) {
$fields = $this->getFieldsForKey( $this->uniqueId );
$row = [];
foreach ( $fields as $fieldname => $field ) {
if ( !empty( $field->mParams['nodata'] ) ) {
continue;
}
$row[$fieldname] = $field->getDefault();
}
$ret = [ $row ];
}
return $ret;
}
/**
* @inheritDoc
* @phan-param array[] $values
*/
public function cancelSubmit( $values, $alldata ) {
if ( isset( $values['nonjs'] ) ) {
return true;
}
foreach ( $values as $key => $value ) {
$fields = $this->getFieldsForKey( $key );
foreach ( $fields as $fieldname => $field ) {
if ( !array_key_exists( $fieldname, $value ) ) {
continue;
}
if ( $field->cancelSubmit( $value[$fieldname], $alldata ) ) {
return true;
}
}
}
return parent::cancelSubmit( $values, $alldata );
}
/**
* @inheritDoc
* @phan-param array[] $values
*/
public function validate( $values, $alldata ) {
if ( isset( $this->mParams['required'] )
&& $this->mParams['required'] !== false
&& !$values
) {
return $this->msg( 'htmlform-cloner-required' );
}
if ( isset( $values['nonjs'] ) ) {
// The submission was a non-JS create/delete click, so fail
// validation in case cancelSubmit() somehow didn't already handle
// it.
return false;
}
foreach ( $values as $key => $value ) {
$fields = $this->getFieldsForKey( $key );
foreach ( $fields as $fieldname => $field ) {
if ( !array_key_exists( $fieldname, $value ) || $field->isHidden( $alldata ) ) {
continue;
}
$ok = $field->validate( $value[$fieldname], $alldata );
if ( $ok !== true ) {
return false;
}
}
}
return parent::validate( $values, $alldata );
}
/**
* Get the input HTML for the specified key.
*
* @param string $key Array key under which the fields should be named
* @param array $values
* @return string
*/
protected function getInputHTMLForKey( $key, array $values ) {
$displayFormat = $this->mParams['format'] ?? $this->mParent->getDisplayFormat();
// Conveniently, PHP method names are case-insensitive.
$getFieldHtmlMethod = $displayFormat == 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
$html = '';
$hidden = '';
$hasLabel = false;
$fields = $this->getFieldsForKey( $key );
foreach ( $fields as $fieldname => $field ) {
$v = array_key_exists( $fieldname, $values )
? $values[$fieldname]
: $field->getDefault();
if ( $field instanceof HTMLHiddenField ) {
// HTMLHiddenField doesn't generate its own HTML
[ $name, $value, $params ] = $field->getHiddenFieldData( $v );
$hidden .= Html::hidden( $name, $value, $params ) . "\n";
} else {
$html .= $field->$getFieldHtmlMethod( $v );
$labelValue = trim( $field->getLabel() );
if ( $labelValue !== "\u{00A0}" && $labelValue !== ' ' && $labelValue !== '' ) {
$hasLabel = true;
}
}
}
if ( !isset( $fields['delete'] ) ) {
$field = $this->getDeleteButtonHtml( $key );
if ( $displayFormat === 'table' ) {
$html .= $field->$getFieldHtmlMethod( $field->getDefault() );
} else {
$html .= $field->getInputHTML( $field->getDefault() );
}
}
if ( $displayFormat !== 'raw' ) {
$classes = [ 'mw-htmlform-cloner-row' ];
if ( !$hasLabel ) { // Avoid strange spacing when no labels exist
$classes[] = 'mw-htmlform-nolabel';
}
$attribs = [ 'class' => $classes ];
if ( $displayFormat === 'table' ) {
$html = Html::rawElement( 'table',
$attribs,
Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
} else {
$html = Html::rawElement( 'div', $attribs, "\n$html\n" );
}
}
$html .= $hidden;
if ( !empty( $this->mParams['row-legend'] ) ) {
$legend = $this->msg( $this->mParams['row-legend'] )->text();
$html = Xml::fieldset( $legend, $html );
}
return $html;
}
/**
* @param string $key Array key indicating to which field the delete button belongs
* @return HTMLFormField
*/
protected function getDeleteButtonHtml( $key ): HTMLFormField {
$name = "{$this->mName}[$key][delete]";
$label = $this->mParams['delete-button-message'] ?? 'htmlform-cloner-delete';
$field = HTMLForm::loadInputFromParameters( $name, [
'type' => 'submit',
'formnovalidate' => true,
'name' => $name,
'id' => Sanitizer::escapeIdForAttribute( "{$this->mID}--$key--delete" ),
'cssclass' => 'mw-htmlform-cloner-delete-button',
'default' => $this->getMessage( $label )->text(),
'disabled' => $this->mParams['disabled'] ?? false,
], $this->mParent );
return $field;
}
protected function getCreateButtonHtml(): HTMLFormField {
$name = "{$this->mName}[create]";
$label = $this->mParams['create-button-message'] ?? 'htmlform-cloner-create';
return HTMLForm::loadInputFromParameters( $name, [
'type' => 'submit',
'formnovalidate' => true,
'name' => $name,
'id' => Sanitizer::escapeIdForAttribute( "{$this->mID}--create" ),
'cssclass' => 'mw-htmlform-cloner-create-button',
'default' => $this->getMessage( $label )->text(),
'disabled' => $this->mParams['disabled'] ?? false,
], $this->mParent );
}
public function getInputHTML( $values ) {
$html = '';
foreach ( (array)$values as $key => $value ) {
if ( $key === 'nonjs' ) {
continue;
}
$html .= Html::rawElement( 'li', [ 'class' => 'mw-htmlform-cloner-li' ],
$this->getInputHTMLForKey( $key, $value )
);
}
$template = $this->getInputHTMLForKey( $this->uniqueId, [] );
$html = Html::rawElement( 'ul', [
'id' => "mw-htmlform-cloner-list-{$this->mID}",
'class' => 'mw-htmlform-cloner-ul',
'data-template' => $template,
'data-unique-id' => $this->uniqueId,
], $html );
$field = $this->getCreateButtonHtml();
$html .= $field->getInputHTML( $field->getDefault() );
return $html;
}
/**
* Get the input OOUI HTML for the specified key.
*
* @param string $key Array key under which the fields should be named
* @param array $values
* @return string
*/
protected function getInputOOUIForKey( $key, array $values ) {
$html = '';
$hidden = '';
$fields = $this->getFieldsForKey( $key );
foreach ( $fields as $fieldname => $field ) {
$v = array_key_exists( $fieldname, $values )
? $values[$fieldname]
: $field->getDefault();
if ( $field instanceof HTMLHiddenField ) {
// HTMLHiddenField doesn't generate its own HTML
[ $name, $value, $params ] = $field->getHiddenFieldData( $v );
$hidden .= Html::hidden( $name, $value, $params ) . "\n";
} else {
$html .= $field->getOOUI( $v );
}
}
if ( !isset( $fields['delete'] ) ) {
$field = $this->getDeleteButtonHtml( $key );
$fieldHtml = $field->getInputOOUI( $field->getDefault() );
$fieldHtml->setInfusable( true );
$html .= $fieldHtml;
}
$html = Html::rawElement( 'div', [ 'class' => 'mw-htmlform-cloner-row' ], "\n$html\n" );
$html .= $hidden;
if ( !empty( $this->mParams['row-legend'] ) ) {
$legend = $this->msg( $this->mParams['row-legend'] )->text();
$html = Xml::fieldset( $legend, $html );
}
return $html;
}
public function getInputOOUI( $values ) {
$html = '';
foreach ( (array)$values as $key => $value ) {
if ( $key === 'nonjs' ) {
continue;
}
$html .= Html::rawElement( 'li', [ 'class' => 'mw-htmlform-cloner-li' ],
$this->getInputOOUIForKey( $key, $value )
);
}
$template = $this->getInputOOUIForKey( $this->uniqueId, [] );
$html = Html::rawElement( 'ul', [
'id' => "mw-htmlform-cloner-list-{$this->mID}",
'class' => 'mw-htmlform-cloner-ul',
'data-template' => $template,
'data-unique-id' => $this->uniqueId,
], $html );
$field = $this->getCreateButtonHtml();
$fieldHtml = $field->getInputOOUI( $field->getDefault() );
$fieldHtml->setInfusable( true );
$html .= $fieldHtml;
return $html;
}
}
/** @deprecated class alias since 1.42 */
class_alias( HTMLFormFieldCloner::class, 'HTMLFormFieldCloner' );
|