43 private static $counter = 0;
60 $this->uniqueId = $this->
getClassName() . ++self::$counter .
'x';
61 parent::__construct( $params );
63 if ( empty( $this->mParams[
'fields'] ) || !is_array( $this->mParams[
'fields'] ) ) {
64 throw new InvalidArgumentException(
'HTMLFormFieldCloner called without any fields' );
68 if ( isset( $this->mParams[
'fields'][
'delete'] ) ) {
69 $class =
'mw-htmlform-cloner-delete-button';
70 $info = $this->mParams[
'fields'][
'delete'] + [
71 'formnovalidate' =>
true,
74 unset( $info[
'name'], $info[
'class'] );
76 if ( !isset( $info[
'type'] ) || $info[
'type'] !==
'submit' ) {
77 throw new InvalidArgumentException(
78 'HTMLFormFieldCloner delete field, if specified, must be of type "submit"'
82 if ( !in_array( $class, explode(
' ', $info[
'cssclass'] ) ) ) {
83 $info[
'cssclass'] .=
" $class";
86 $this->mParams[
'fields'][
'delete'] = $info;
95 if ( !isset( $this->mFields[$key] ) ) {
98 return $this->mFields[$key];
110 foreach ( $this->mParams[
'fields'] as $fieldname => $info ) {
111 $name =
"{$this->mName}[$key][$fieldname]";
112 if ( isset( $info[
'name'] ) ) {
113 $info[
'name'] =
"{$this->mName}[$key][{$info['name']}]";
115 $info[
'name'] = $name;
117 if ( isset( $info[
'id'] ) ) {
118 $info[
'id'] = Sanitizer::escapeIdForAttribute(
"{$this->mID}--$key--{$info['id']}" );
120 $info[
'id'] = Sanitizer::escapeIdForAttribute(
"{$this->mID}--$key--$fieldname" );
124 if ( $this->mCondState ) {
125 foreach ( [
'hide',
'disable' ] as
$type ) {
126 if ( !isset( $this->mCondState[
$type] ) ) {
129 $param =
$type .
'-if';
130 if ( isset( $info[$param] ) ) {
132 $info[$param] = [
'OR', $info[$param], $this->mCondState[
$type] ];
135 $info[$param] = $this->mCondState[
$type];
140 $info[
'cloner'] = &$cloner;
141 $info[
'cloner-key'] = $key;
142 $field = HTMLForm::loadInputFromParameters( $fieldname, $info, $this->mParent );
143 $fields[$fieldname] = $field;
158 foreach ( $values as $fieldname => $value ) {
159 $name =
"{$this->mName}[$key][$fieldname]";
160 $data[$name] = $value;
171 while ( preg_match(
'/^(.+)\[([^\]]+)\]$/', $name, $m ) ) {
172 array_unshift( $fieldKeys, $m[2] );
175 array_unshift( $fieldKeys, $name );
191 if ( count( $findPath ) > 1 ) {
194 if ( !isset( $this->mParams[
'fields'][$find] ) ) {
195 if ( isset( $this->mParams[
'cloner'] ) ) {
196 return $this->mParams[
'cloner']->findNearestField( $this, $find );
201 return $fields[$find];
209 $path = [ $this->mParams[
'fieldname'], $field->mParams[
'cloner-key'] ];
210 if ( isset( $this->mParams[
'cloner'] ) ) {
225 $alldata = $alldata[$key];
227 return $alldata[$field->mParams[
'fieldname']];
237 if ( !$request->getCheck(
'wpEditToken' ) && $request->getArray( $this->mName ) ===
null ) {
241 $values = $request->getArray( $this->mName ) ?? [];
244 foreach ( $values as $key => $value ) {
245 if ( $key ===
'create' || isset( $value[
'delete'] ) ) {
257 foreach ( $fields as $fieldname => $field ) {
258 if ( $field->skipLoadData( $subrequest ) ) {
261 if ( !empty( $field->mParams[
'disabled'] ) ) {
262 $row[$fieldname] = $field->getDefault();
264 $row[$fieldname] = $field->loadDataFromRequest( $subrequest );
270 if ( isset( $values[
'create'] ) ) {
274 foreach ( $fields as $fieldname => $field ) {
275 if ( !empty( $field->mParams[
'nodata'] ) ) {
278 $row[$fieldname] = $field->getDefault();
287 $ret = parent::getDefault();
290 if ( $ret ===
null ) {
293 foreach ( $fields as $fieldname => $field ) {
294 if ( !empty( $field->mParams[
'nodata'] ) ) {
297 $row[$fieldname] = $field->getDefault();
310 if ( isset( $values[
'nonjs'] ) ) {
314 foreach ( $values as $key => $value ) {
316 foreach ( $fields as $fieldname => $field ) {
317 if ( !array_key_exists( $fieldname, $value ) ) {
320 if ( $field->cancelSubmit( $value[$fieldname], $alldata ) ) {
326 return parent::cancelSubmit( $values, $alldata );
334 if ( isset( $this->mParams[
'required'] )
335 && $this->mParams[
'required'] !==
false
338 return $this->
msg(
'htmlform-cloner-required' );
341 if ( isset( $values[
'nonjs'] ) ) {
348 foreach ( $values as $key => $value ) {
350 foreach ( $fields as $fieldname => $field ) {
351 if ( !array_key_exists( $fieldname, $value ) ) {
354 if ( $field->isHidden( $alldata ) ) {
357 $ok = $field->validate( $value[$fieldname], $alldata );
358 if ( $ok !==
true ) {
364 return parent::validate( $values, $alldata );
375 $displayFormat = $this->mParams[
'format'] ?? $this->mParent->getDisplayFormat();
378 $getFieldHtmlMethod = $displayFormat ==
'table' ?
'getTableRow' : (
'get' . $displayFormat );
385 foreach ( $fields as $fieldname => $field ) {
386 $v = array_key_exists( $fieldname, $values )
387 ? $values[$fieldname]
388 : $field->getDefault();
392 [ $name, $value, $params ] = $field->getHiddenFieldData( $v );
393 $hidden .= Html::hidden( $name, $value, $params ) .
"\n";
395 $html .= $field->$getFieldHtmlMethod( $v );
397 $labelValue = trim( $field->getLabel() );
398 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
404 if ( !isset( $fields[
'delete'] ) ) {
407 if ( $displayFormat ===
'table' ) {
408 $html .= $field->$getFieldHtmlMethod( $field->getDefault() );
410 $html .= $field->getInputHTML( $field->getDefault() );
414 if ( $displayFormat !==
'raw' ) {
415 $classes = [
'mw-htmlform-cloner-row' ];
418 $classes[] =
'mw-htmlform-nolabel';
421 $attribs = [
'class' => $classes ];
423 if ( $displayFormat ===
'table' ) {
424 $html = Html::rawElement(
'table',
426 Html::rawElement(
'tbody', [],
"\n$html\n" ) ) .
"\n";
428 $html = Html::rawElement(
'div', $attribs,
"\n$html\n" );
434 if ( !empty( $this->mParams[
'row-legend'] ) ) {
435 $legend = $this->
msg( $this->mParams[
'row-legend'] )->text();
436 $html = Xml::fieldset( $legend, $html );
447 $name =
"{$this->mName}[$key][delete]";
448 $label = $this->mParams[
'delete-button-message'] ??
'htmlform-cloner-delete';
449 $field = HTMLForm::loadInputFromParameters( $name, [
451 'formnovalidate' =>
true,
453 'id' => Sanitizer::escapeIdForAttribute(
"{$this->mID}--$key--delete" ),
454 'cssclass' =>
'mw-htmlform-cloner-delete-button',
455 'default' => $this->
getMessage( $label )->text(),
456 'disabled' => $this->mParams[
'disabled'] ??
false,
462 $name =
"{$this->mName}[create]";
463 $label = $this->mParams[
'create-button-message'] ??
'htmlform-cloner-create';
464 return HTMLForm::loadInputFromParameters( $name, [
466 'formnovalidate' =>
true,
468 'id' => Sanitizer::escapeIdForAttribute(
"{$this->mID}--create" ),
469 'cssclass' =>
'mw-htmlform-cloner-create-button',
470 'default' => $this->getMessage( $label )->text(),
471 'disabled' => $this->mParams[
'disabled'] ??
false,
478 foreach ( (array)$values as $key => $value ) {
479 if ( $key ===
'nonjs' ) {
482 $html .= Html::rawElement(
'li', [
'class' =>
'mw-htmlform-cloner-li' ],
483 $this->getInputHTMLForKey( $key, $value )
487 $template = $this->getInputHTMLForKey( $this->uniqueId, [] );
488 $html = Html::rawElement(
'ul', [
489 'id' =>
"mw-htmlform-cloner-list-{$this->mID}",
490 'class' =>
'mw-htmlform-cloner-ul',
491 'data-template' => $template,
492 'data-unique-id' => $this->uniqueId,
495 $field = $this->getCreateButtonHtml();
496 $html .= $field->getInputHTML( $field->getDefault() );
512 $fields = $this->getFieldsForKey( $key );
513 foreach ( $fields as $fieldname => $field ) {
514 $v = array_key_exists( $fieldname, $values )
515 ? $values[$fieldname]
516 : $field->getDefault();
520 [ $name, $value, $params ] = $field->getHiddenFieldData( $v );
521 $hidden .= Html::hidden( $name, $value, $params ) .
"\n";
523 $html .= $field->getOOUI( $v );
527 if ( !isset( $fields[
'delete'] ) ) {
528 $field = $this->getDeleteButtonHtml( $key );
529 $fieldHtml = $field->getInputOOUI( $field->getDefault() );
530 $fieldHtml->setInfusable(
true );
535 $html = Html::rawElement(
'div', [
'class' =>
'mw-htmlform-cloner-row' ],
"\n$html\n" );
539 if ( !empty( $this->mParams[
'row-legend'] ) ) {
540 $legend = $this->msg( $this->mParams[
'row-legend'] )->text();
541 $html = Xml::fieldset( $legend, $html );
550 foreach ( (array)$values as $key => $value ) {
551 if ( $key ===
'nonjs' ) {
554 $html .= Html::rawElement(
'li', [
'class' =>
'mw-htmlform-cloner-li' ],
555 $this->getInputOOUIForKey( $key, $value )
559 $template = $this->getInputOOUIForKey( $this->uniqueId, [] );
560 $html = Html::rawElement(
'ul', [
561 'id' =>
"mw-htmlform-cloner-list-{$this->mID}",
562 'class' =>
'mw-htmlform-cloner-ul',
563 'data-template' => $template,
564 'data-unique-id' => $this->uniqueId,
567 $field = $this->getCreateButtonHtml();
568 $fieldHtml = $field->getInputOOUI( $field->getDefault() );
569 $fieldHtml->setInfusable(
true );