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 MediaWiki\HTMLForm\HTMLFormField;
|
2024-05-16 10:52:03 +00:00
|
|
|
use MediaWiki\Xml\XmlSelect;
|
2024-02-08 15:30:18 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
|
|
|
|
* A select dropdown field. Basically a wrapper for Xmlselect class
|
2020-07-10 19:23:59 +00:00
|
|
|
*
|
|
|
|
|
* @stable to extend
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
class HTMLSelectField extends HTMLFormField {
|
2020-07-10 19:23:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
* @stable to override
|
|
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function validate( $value, $alldata ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$p = parent::validate( $value, $alldata );
|
|
|
|
|
|
|
|
|
|
if ( $p !== true ) {
|
|
|
|
|
return $p;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-18 20:28:06 +00:00
|
|
|
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2014-03-05 20:15:41 +00:00
|
|
|
if ( in_array( strval( $value ), $validOptions, true ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
return true;
|
|
|
|
|
} else {
|
2016-11-02 17:13:43 +00:00
|
|
|
return $this->msg( 'htmlform-select-badoption' );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 19:23:59 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
* @stable to override
|
|
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getInputHTML( $value ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( !empty( $this->mParams['disabled'] ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$select->setAttribute( 'disabled', 'disabled' );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$allowedParams = [ 'tabindex', 'size' ];
|
2014-07-06 18:51:17 +00:00
|
|
|
$customParams = $this->getAttributes( $allowedParams );
|
2014-07-19 21:12:10 +00:00
|
|
|
foreach ( $customParams as $name => $value ) {
|
2014-07-06 18:51:17 +00:00
|
|
|
$select->setAttribute( $name, $value );
|
2013-05-02 18:27:44 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
if ( $this->mClass !== '' ) {
|
|
|
|
|
$select->setAttribute( 'class', $this->mClass );
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-18 20:28:06 +00:00
|
|
|
$select->addOptions( $this->getOptions() );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
return $select->getHTML();
|
|
|
|
|
}
|
2015-04-21 21:03:49 +00:00
|
|
|
|
2020-07-10 19:23:59 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
* @stable to override
|
|
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getInputOOUI( $value ) {
|
2015-04-21 21:03:49 +00:00
|
|
|
$disabled = false;
|
2016-02-17 09:09:32 +00:00
|
|
|
$allowedParams = [ 'tabindex' ];
|
2024-02-08 15:30:18 +00:00
|
|
|
$attribs = \OOUI\Element::configFromHtmlAttributes(
|
2016-03-02 19:09:53 +00:00
|
|
|
$this->getAttributes( $allowedParams )
|
|
|
|
|
);
|
2015-04-21 21:03:49 +00:00
|
|
|
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$attribs['classes'] = [ $this->mClass ];
|
2015-04-21 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !empty( $this->mParams['disabled'] ) ) {
|
|
|
|
|
$disabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:30:18 +00:00
|
|
|
return new \OOUI\DropdownInputWidget( [
|
2015-04-21 21:03:49 +00:00
|
|
|
'name' => $this->mName,
|
|
|
|
|
'id' => $this->mID,
|
|
|
|
|
'options' => $this->getOptionsOOUI(),
|
|
|
|
|
'value' => strval( $value ),
|
|
|
|
|
'disabled' => $disabled,
|
2016-02-17 09:09:32 +00:00
|
|
|
] + $attribs );
|
2015-04-21 21:03:49 +00:00
|
|
|
}
|
2016-07-26 12:12:21 +00:00
|
|
|
|
2024-03-12 20:44:35 +00:00
|
|
|
public function getInputCodex( $value, $hasErrors ) {
|
|
|
|
|
$select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
|
|
|
|
|
|
|
|
|
|
if ( !empty( $this->mParams['disabled'] ) ) {
|
|
|
|
|
$select->setAttribute( 'disabled', 'disabled' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$allowedParams = [ 'tabindex', 'size' ];
|
|
|
|
|
$customParams = $this->getAttributes( $allowedParams );
|
|
|
|
|
foreach ( $customParams as $name => $value ) {
|
|
|
|
|
$select->setAttribute( $name, $value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Add support for error class once it's implemented in the Codex CSS-only Select.
|
|
|
|
|
$selectClass = 'cdx-select';
|
|
|
|
|
$selectClass .= $this->mClass !== '' ? ' ' . $this->mClass : '';
|
|
|
|
|
$select->setAttribute( 'class', $selectClass );
|
|
|
|
|
|
|
|
|
|
$select->addOptions( $this->getOptions() );
|
|
|
|
|
|
|
|
|
|
return $select->getHTML();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 19:23:59 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
* @stable to override
|
|
|
|
|
*/
|
2016-07-26 12:12:21 +00:00
|
|
|
protected function shouldInfuseOOUI() {
|
|
|
|
|
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( HTMLSelectField::class, 'HTMLSelectField' );
|