2013-11-19 12:55:50 +00:00
|
|
|
<?php
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2024-02-08 15:30:18 +00:00
|
|
|
namespace MediaWiki\HTMLForm\Field;
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2023-02-16 19:27:21 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2023-09-07 11:46:15 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2024-02-08 15:30:18 +00:00
|
|
|
use MediaWiki\Widget\SelectWithInputWidget;
|
2023-02-16 19:27:21 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
|
|
|
|
* Double field with a dropdown list constructed from a system message in the format
|
|
|
|
|
* * Optgroup header
|
|
|
|
|
* ** <option value>
|
|
|
|
|
* * New Optgroup header
|
|
|
|
|
* Plus a text field underneath for an additional reason. The 'value' of the field is
|
|
|
|
|
* "<select>: <extra reason>", or "<extra reason>" if nothing has been selected in the
|
|
|
|
|
* select dropdown.
|
2020-07-10 19:23:59 +00:00
|
|
|
*
|
|
|
|
|
* @stable to extend
|
2013-11-19 12:55:50 +00:00
|
|
|
* @todo FIXME: If made 'required', only the text field should be compulsory.
|
|
|
|
|
*/
|
|
|
|
|
class HTMLSelectAndOtherField extends HTMLSelectField {
|
Apply ID and class more consistently in HTMLSelect(Or|And)OtherField
There were several inconsistencies in how ID vs cssclass vs own classes
were applied, both within the same widget + mode, between different
modes for the same widget, and different widgets. So uniform all of them
according to the plan detailed on phabricator. Include
HTMLSelectAndOtherField as well (that wasn't mentioned on phab) for
consistency. Also remove the select-or-other class from
HTMLAutoCompleteSelectField (which was only applied in HTML mode), since
the widget is not technically a SelectOrOther.
Adjust the logic in selectorother.js so that it works fine in HTML mode,
and disable it for OOUI mode since SelectWithInputWidget does that
already (and the field is autoinfused).
Also, in HTMLAutoCompleteSelectField, don't assign an array to
$this->mClass. This happens to be working right now, but it violates the
type definition of the property.
Luckily, according to codesearch this change seems to be
backwards-compatible because these fields are not commonly used, and
nobody seems to be passing cssclass to them.
Bug: T309883
Change-Id: If950f131d1c4bc1645de6021781045dccfc61966
2022-06-06 13:30:53 +00:00
|
|
|
private const FIELD_CLASS = 'mw-htmlform-select-and-other-field';
|
2019-09-11 09:30:12 +00:00
|
|
|
/** @var string[] */
|
|
|
|
|
private $mFlatOptions;
|
|
|
|
|
|
2020-07-23 09:41:58 +00:00
|
|
|
/**
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to call
|
2020-07-23 09:41:58 +00:00
|
|
|
* @inheritDoc
|
2020-07-10 19:23:59 +00:00
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function __construct( $params ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
if ( array_key_exists( 'other', $params ) ) {
|
2014-11-16 19:18:00 +00:00
|
|
|
// Do nothing
|
2013-11-19 12:55:50 +00:00
|
|
|
} elseif ( array_key_exists( 'other-message', $params ) ) {
|
2016-04-01 12:00:44 +00:00
|
|
|
$params['other'] = $this->getMessage( $params['other-message'] )->plain();
|
2013-11-19 12:55:50 +00:00
|
|
|
} else {
|
2016-04-01 12:00:44 +00:00
|
|
|
$params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
|
2014-02-18 20:28:06 +00:00
|
|
|
if ( $this->getOptions() === null ) {
|
2023-01-22 00:55:59 +00:00
|
|
|
throw new InvalidArgumentException( 'HTMLSelectAndOtherField called without any options' );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
2014-02-18 20:28:06 +00:00
|
|
|
if ( !in_array( 'other', $this->mOptions, true ) ) {
|
2014-04-10 15:31:25 +00:00
|
|
|
// Have 'other' always as first element
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mOptions = [ $params['other'] => 'other' ] + $this->mOptions;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
2014-02-18 20:28:06 +00:00
|
|
|
$this->mFlatOptions = self::flattenOptions( $this->getOptions() );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getInputHTML( $value ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$select = parent::getInputHTML( $value[1] );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$textAttribs = [
|
2013-11-19 12:55:50 +00:00
|
|
|
'size' => $this->getSize(),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2018-02-26 18:07:11 +00:00
|
|
|
if ( isset( $this->mParams['maxlength-unit'] ) ) {
|
|
|
|
|
$textAttribs['data-mw-maxlength-unit'] = $this->mParams['maxlength-unit'];
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$allowedParams = [
|
2013-05-02 18:27:44 +00:00
|
|
|
'required',
|
|
|
|
|
'autofocus',
|
|
|
|
|
'multiple',
|
|
|
|
|
'disabled',
|
2014-07-26 21:56:37 +00:00
|
|
|
'tabindex',
|
|
|
|
|
'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
|
2018-02-26 18:07:11 +00:00
|
|
|
'maxlength-unit', // 'bytes' or 'codepoints', see mediawiki.htmlform.js
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-05-02 18:27:44 +00:00
|
|
|
|
|
|
|
|
$textAttribs += $this->getAttributes( $allowedParams );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
$textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
Apply ID and class more consistently in HTMLSelect(Or|And)OtherField
There were several inconsistencies in how ID vs cssclass vs own classes
were applied, both within the same widget + mode, between different
modes for the same widget, and different widgets. So uniform all of them
according to the plan detailed on phabricator. Include
HTMLSelectAndOtherField as well (that wasn't mentioned on phab) for
consistency. Also remove the select-or-other class from
HTMLAutoCompleteSelectField (which was only applied in HTML mode), since
the widget is not technically a SelectOrOther.
Adjust the logic in selectorother.js so that it works fine in HTML mode,
and disable it for OOUI mode since SelectWithInputWidget does that
already (and the field is autoinfused).
Also, in HTMLAutoCompleteSelectField, don't assign an array to
$this->mClass. This happens to be working right now, but it violates the
type definition of the property.
Luckily, according to codesearch this change seems to be
backwards-compatible because these fields are not commonly used, and
nobody seems to be passing cssclass to them.
Bug: T309883
Change-Id: If950f131d1c4bc1645de6021781045dccfc61966
2022-06-06 13:30:53 +00:00
|
|
|
$wrapperAttribs = [
|
|
|
|
|
'id' => $this->mID,
|
|
|
|
|
'class' => self::FIELD_CLASS
|
|
|
|
|
];
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
|
|
|
|
$wrapperAttribs['class'] .= ' ' . $this->mClass;
|
|
|
|
|
}
|
|
|
|
|
return Html::rawElement(
|
|
|
|
|
'div',
|
|
|
|
|
$wrapperAttribs,
|
|
|
|
|
"$select<br />\n$textbox"
|
|
|
|
|
);
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2017-06-05 12:13:14 +00:00
|
|
|
protected function getOOUIModules() {
|
|
|
|
|
return [ 'mediawiki.widgets.SelectWithInputWidget' ];
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getInputOOUI( $value ) {
|
2017-06-05 12:13:14 +00:00
|
|
|
$this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
|
|
|
|
|
|
|
|
|
|
# TextInput
|
|
|
|
|
$textAttribs = [
|
|
|
|
|
'name' => $this->mName . '-other',
|
|
|
|
|
'value' => $value[2],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$allowedParams = [
|
|
|
|
|
'required',
|
|
|
|
|
'autofocus',
|
|
|
|
|
'multiple',
|
|
|
|
|
'disabled',
|
|
|
|
|
'tabindex',
|
|
|
|
|
'maxlength',
|
|
|
|
|
];
|
|
|
|
|
|
2024-02-08 15:30:18 +00:00
|
|
|
$textAttribs += \OOUI\Element::configFromHtmlAttributes(
|
2017-06-05 12:13:14 +00:00
|
|
|
$this->getAttributes( $allowedParams )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# DropdownInput
|
|
|
|
|
$dropdownInputAttribs = [
|
|
|
|
|
'name' => $this->mName,
|
|
|
|
|
'options' => $this->getOptionsOOUI(),
|
|
|
|
|
'value' => $value[1],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$allowedParams = [
|
|
|
|
|
'tabindex',
|
|
|
|
|
'disabled',
|
|
|
|
|
];
|
|
|
|
|
|
2024-02-08 15:30:18 +00:00
|
|
|
$dropdownInputAttribs += \OOUI\Element::configFromHtmlAttributes(
|
2017-06-05 12:13:14 +00:00
|
|
|
$this->getAttributes( $allowedParams )
|
|
|
|
|
);
|
|
|
|
|
|
2018-07-25 05:29:03 +00:00
|
|
|
$disabled = false;
|
|
|
|
|
if ( isset( $this->mParams[ 'disabled' ] ) && $this->mParams[ 'disabled' ] ) {
|
|
|
|
|
$disabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
Apply ID and class more consistently in HTMLSelect(Or|And)OtherField
There were several inconsistencies in how ID vs cssclass vs own classes
were applied, both within the same widget + mode, between different
modes for the same widget, and different widgets. So uniform all of them
according to the plan detailed on phabricator. Include
HTMLSelectAndOtherField as well (that wasn't mentioned on phab) for
consistency. Also remove the select-or-other class from
HTMLAutoCompleteSelectField (which was only applied in HTML mode), since
the widget is not technically a SelectOrOther.
Adjust the logic in selectorother.js so that it works fine in HTML mode,
and disable it for OOUI mode since SelectWithInputWidget does that
already (and the field is autoinfused).
Also, in HTMLAutoCompleteSelectField, don't assign an array to
$this->mClass. This happens to be working right now, but it violates the
type definition of the property.
Luckily, according to codesearch this change seems to be
backwards-compatible because these fields are not commonly used, and
nobody seems to be passing cssclass to them.
Bug: T309883
Change-Id: If950f131d1c4bc1645de6021781045dccfc61966
2022-06-06 13:30:53 +00:00
|
|
|
$inputClasses = [ self::FIELD_CLASS ];
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
|
|
|
|
$inputClasses = array_merge( $inputClasses, explode( ' ', $this->mClass ) );
|
|
|
|
|
}
|
2017-06-05 12:13:14 +00:00
|
|
|
return $this->getInputWidget( [
|
2017-09-25 18:49:51 +00:00
|
|
|
'id' => $this->mID,
|
2018-07-25 05:29:03 +00:00
|
|
|
'disabled' => $disabled,
|
2017-06-05 12:13:14 +00:00
|
|
|
'textinput' => $textAttribs,
|
|
|
|
|
'dropdowninput' => $dropdownInputAttribs,
|
|
|
|
|
'or' => false,
|
2019-07-22 22:53:14 +00:00
|
|
|
'required' => $this->mParams[ 'required' ] ?? false,
|
Apply ID and class more consistently in HTMLSelect(Or|And)OtherField
There were several inconsistencies in how ID vs cssclass vs own classes
were applied, both within the same widget + mode, between different
modes for the same widget, and different widgets. So uniform all of them
according to the plan detailed on phabricator. Include
HTMLSelectAndOtherField as well (that wasn't mentioned on phab) for
consistency. Also remove the select-or-other class from
HTMLAutoCompleteSelectField (which was only applied in HTML mode), since
the widget is not technically a SelectOrOther.
Adjust the logic in selectorother.js so that it works fine in HTML mode,
and disable it for OOUI mode since SelectWithInputWidget does that
already (and the field is autoinfused).
Also, in HTMLAutoCompleteSelectField, don't assign an array to
$this->mClass. This happens to be working right now, but it violates the
type definition of the property.
Luckily, according to codesearch this change seems to be
backwards-compatible because these fields are not commonly used, and
nobody seems to be passing cssclass to them.
Bug: T309883
Change-Id: If950f131d1c4bc1645de6021781045dccfc61966
2022-06-06 13:30:53 +00:00
|
|
|
'classes' => $inputClasses,
|
2018-02-26 18:07:11 +00:00
|
|
|
'data' => [
|
2017-10-06 22:17:58 +00:00
|
|
|
'maxlengthUnit' => $this->mParams['maxlength-unit'] ?? 'bytes'
|
2018-02-26 18:07:11 +00:00
|
|
|
],
|
2017-06-05 12:13:14 +00:00
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 19:23:59 +00:00
|
|
|
/**
|
|
|
|
|
* @stable to override
|
2023-03-24 02:48:54 +00:00
|
|
|
* @param array $params
|
|
|
|
|
* @return \MediaWiki\Widget\SelectWithInputWidget
|
2020-07-10 19:23:59 +00:00
|
|
|
*/
|
2017-06-05 12:13:14 +00:00
|
|
|
public function getInputWidget( $params ) {
|
2024-02-08 15:30:18 +00:00
|
|
|
return new SelectWithInputWidget( $params );
|
2015-07-26 23:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-12 20:44:35 +00:00
|
|
|
public function getInputCodex( $value, $hasErrors ) {
|
|
|
|
|
$select = parent::getInputCodex( $value[1], $hasErrors );
|
|
|
|
|
|
|
|
|
|
// Set up attributes for the text input.
|
|
|
|
|
$textInputAttribs = [
|
|
|
|
|
'size' => $this->getSize(),
|
|
|
|
|
'name' => $this->mName . '-other'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ( isset( $this->mParams['maxlength-unit'] ) ) {
|
|
|
|
|
$textInputAttribs['data-mw-maxlength-unit'] = $this->mParams['maxlength-unit'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allowedParams = [
|
|
|
|
|
'required',
|
|
|
|
|
'autofocus',
|
|
|
|
|
'multiple',
|
|
|
|
|
'disabled',
|
|
|
|
|
'tabindex',
|
|
|
|
|
'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
|
|
|
|
|
'maxlength-unit', // 'bytes' or 'codepoints', see mediawiki.htmlform.js
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$textInputAttribs += $this->getAttributes( $allowedParams );
|
|
|
|
|
|
|
|
|
|
// Get text input HTML.
|
|
|
|
|
$textInput = HTMLTextField::buildCodexComponent(
|
|
|
|
|
$value[2],
|
|
|
|
|
$hasErrors,
|
|
|
|
|
'text',
|
|
|
|
|
$this->mName . '-other',
|
|
|
|
|
$textInputAttribs
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Set up the wrapper element and return the entire component.
|
|
|
|
|
$wrapperAttribs = [
|
|
|
|
|
'id' => $this->mID,
|
|
|
|
|
'class' => [ self::FIELD_CLASS ]
|
|
|
|
|
];
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
|
|
|
|
$wrapperAttribs['class'][] = $this->mClass;
|
|
|
|
|
}
|
|
|
|
|
return Html::rawElement(
|
|
|
|
|
'div',
|
|
|
|
|
$wrapperAttribs,
|
|
|
|
|
"$select<br />\n$textInput"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 19:04:49 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function getDefault() {
|
|
|
|
|
$default = parent::getDefault();
|
|
|
|
|
|
|
|
|
|
// Default values of empty form
|
|
|
|
|
$final = '';
|
|
|
|
|
$list = 'other';
|
|
|
|
|
$text = '';
|
|
|
|
|
|
|
|
|
|
if ( $default !== null ) {
|
|
|
|
|
$final = $default;
|
|
|
|
|
// Assume the default is a text value, with the 'other' option selected.
|
|
|
|
|
// Then check if that assumption is correct, and update $list and $text if not.
|
|
|
|
|
$text = $final;
|
|
|
|
|
foreach ( $this->mFlatOptions as $option ) {
|
|
|
|
|
$match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
|
2022-12-12 18:54:24 +00:00
|
|
|
if ( str_starts_with( $final, $match ) ) {
|
2019-06-13 19:04:49 +00:00
|
|
|
$list = $option;
|
|
|
|
|
$text = substr( $final, strlen( $match ) );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [ $final, $list, $text ];
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param WebRequest $request
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2019-04-06 06:19:29 +00:00
|
|
|
* @return array ["<overall message>","<select value>","<text field value>"]
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function loadDataFromRequest( $request ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
if ( $request->getCheck( $this->mName ) ) {
|
|
|
|
|
$list = $request->getText( $this->mName );
|
|
|
|
|
$text = $request->getText( $this->mName . '-other' );
|
|
|
|
|
|
2014-07-26 21:56:37 +00:00
|
|
|
// Should be built the same as in mediawiki.htmlform.js
|
2013-11-19 12:55:50 +00:00
|
|
|
if ( $list == 'other' ) {
|
|
|
|
|
$final = $text;
|
2014-03-05 20:15:41 +00:00
|
|
|
} elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
# User has spoofed the select form to give an option which wasn't
|
|
|
|
|
# in the original offer. Sulk...
|
|
|
|
|
$final = $text;
|
|
|
|
|
} elseif ( $text == '' ) {
|
|
|
|
|
$final = $list;
|
|
|
|
|
} else {
|
|
|
|
|
$final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
|
|
|
|
|
}
|
2019-06-13 19:04:49 +00:00
|
|
|
return [ $final, $list, $text ];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
2019-06-13 19:04:49 +00:00
|
|
|
return $this->getDefault();
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getSize() {
|
2017-10-06 22:17:58 +00:00
|
|
|
return $this->mParams['size'] ?? 45;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:40:42 +00:00
|
|
|
public function validate( $value, $alldata ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
# HTMLSelectField forces $value to be one of the options in the select
|
|
|
|
|
# field, which is not useful here. But we do want the validation further up
|
|
|
|
|
# the chain
|
2013-11-19 13:08:16 +00:00
|
|
|
$p = parent::validate( $value[1], $alldata );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
if ( $p !== true ) {
|
|
|
|
|
return $p;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $this->mParams['required'] )
|
|
|
|
|
&& $this->mParams['required'] !== false
|
2019-06-13 19:04:49 +00:00
|
|
|
&& $value[0] === ''
|
2013-11-19 13:08:16 +00:00
|
|
|
) {
|
2016-11-02 17:13:43 +00:00
|
|
|
return $this->msg( 'htmlform-required' );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|
2024-02-08 15:30:18 +00:00
|
|
|
|
2024-03-07 21:56:58 +00:00
|
|
|
/** @deprecated class alias since 1.42 */
|
2024-02-08 15:30:18 +00:00
|
|
|
class_alias( HTMLSelectAndOtherField::class, 'HTMLSelectAndOtherField' );
|