40 private static $counter = 0;
57 $this->uniqueId = $this->
getClassName() . ++self::$counter .
'x';
58 parent::__construct( $params );
60 if ( empty( $this->mParams[
'fields'] ) || !is_array( $this->mParams[
'fields'] ) ) {
61 throw new MWException(
'HTMLFormFieldCloner called without any fields' );
65 if ( isset( $this->mParams[
'fields'][
'delete'] ) ) {
66 $class =
'mw-htmlform-cloner-delete-button';
67 $info = $this->mParams[
'fields'][
'delete'] + [
68 'formnovalidate' =>
true,
71 unset( $info[
'name'], $info[
'class'] );
73 if ( !isset( $info[
'type'] ) || $info[
'type'] !==
'submit' ) {
75 'HTMLFormFieldCloner delete field, if specified, must be of type "submit"'
79 if ( !in_array( $class, explode(
' ', $info[
'cssclass'] ) ) ) {
80 $info[
'cssclass'] .=
" $class";
83 $this->mParams[
'fields'][
'delete'] = $info;
92 if ( !isset( $this->mFields[$key] ) ) {
95 return $this->mFields[$key];
107 foreach ( $this->mParams[
'fields'] as $fieldname => $info ) {
108 $name =
"{$this->mName}[$key][$fieldname]";
109 if ( isset( $info[
'name'] ) ) {
110 $info[
'name'] =
"{$this->mName}[$key][{$info['name']}]";
112 $info[
'name'] = $name;
114 if ( isset( $info[
'id'] ) ) {
115 $info[
'id'] = Sanitizer::escapeIdForAttribute(
"{$this->mID}--$key--{$info['id']}" );
117 $info[
'id'] = Sanitizer::escapeIdForAttribute(
"{$this->mID}--$key--$fieldname" );
121 if ( $this->mCondState ) {
122 foreach ( [
'hide',
'disable' ] as
$type ) {
123 if ( !isset( $this->mCondState[
$type] ) ) {
126 $param =
$type .
'-if';
127 if ( isset( $info[$param] ) ) {
129 $info[$param] = [
'OR', $info[$param], $this->mCondState[
$type] ];
132 $info[$param] = $this->mCondState[
$type];
137 $info[
'cloner'] = &$cloner;
138 $info[
'cloner-key'] = $key;
139 $field = HTMLForm::loadInputFromParameters( $fieldname, $info, $this->mParent );
140 $fields[$fieldname] = $field;
155 foreach ( $values as $fieldname => $value ) {
156 $name =
"{$this->mName}[$key][$fieldname]";
157 $data[$name] = $value;
168 while ( preg_match(
'/^(.+)\[([^\]]+)\]$/', $name, $m ) ) {
169 array_unshift( $fieldKeys, $m[2] );
172 array_unshift( $fieldKeys, $name );
188 if ( count( $findPath ) > 1 ) {
191 if ( !isset( $this->mParams[
'fields'][$find] ) ) {
192 if ( isset( $this->mParams[
'cloner'] ) ) {
193 return $this->mParams[
'cloner']->findNearestField( $this, $find );
198 return $fields[$find];
206 $path = [ $this->mParams[
'fieldname'], $field->mParams[
'cloner-key'] ];
207 if ( isset( $this->mParams[
'cloner'] ) ) {
222 $alldata = $alldata[$key];
224 return $alldata[$field->mParams[
'fieldname']];
234 if ( !$request->getCheck(
'wpEditToken' ) && $request->getArray( $this->mName ) ===
null ) {
238 $values = $request->getArray( $this->mName );
239 if ( $values ===
null ) {
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 list( $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 list( $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 );
Similar to FauxRequest, but only fakes URL parameters and method (POST or GET) and use the base reque...