2013-11-19 12:55:50 +00:00
|
|
|
<?php
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
|
|
|
|
* Select dropdown field, with an additional "other" textbox.
|
2015-11-08 15:21:58 +00:00
|
|
|
*
|
|
|
|
|
* HTMLComboboxField implements the same functionality using a single form field
|
|
|
|
|
* and should be used instead.
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
class HTMLSelectOrOtherField extends HTMLTextField {
|
2016-11-04 10:40:42 +00:00
|
|
|
public function __construct( $params ) {
|
2014-02-18 20:28:06 +00:00
|
|
|
parent::__construct( $params );
|
|
|
|
|
$this->getOptions();
|
|
|
|
|
if ( !in_array( 'other', $this->mOptions, true ) ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$msg =
|
|
|
|
|
isset( $params['other'] )
|
|
|
|
|
? $params['other']
|
|
|
|
|
: wfMessage( 'htmlform-selectorother-other' )->text();
|
2014-04-10 15:31:25 +00:00
|
|
|
// Have 'other' always as first element
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mOptions = [ $msg => 'other' ] + $this->mOptions;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getInputHTML( $value ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$valInSelect = false;
|
|
|
|
|
|
|
|
|
|
if ( $value !== false ) {
|
2014-03-05 20:15:41 +00:00
|
|
|
$value = strval( $value );
|
|
|
|
|
$valInSelect = in_array(
|
|
|
|
|
$value, HTMLFormField::flattenOptions( $this->getOptions() ), true
|
|
|
|
|
);
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$selected = $valInSelect ? $value : 'other';
|
|
|
|
|
|
|
|
|
|
$select = new XmlSelect( $this->mName, $this->mID, $selected );
|
2014-02-18 20:28:06 +00:00
|
|
|
$select->addOptions( $this->getOptions() );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
$select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$tbAttribs = [ 'id' => $this->mID . '-other', 'size' => $this->getSize() ];
|
2013-11-19 12:55:50 +00:00
|
|
|
|
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' );
|
2013-11-19 13:08:16 +00:00
|
|
|
$tbAttribs['disabled'] = 'disabled';
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-02 18:27:44 +00:00
|
|
|
if ( isset( $this->mParams['tabindex'] ) ) {
|
|
|
|
|
$select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
|
|
|
|
|
$tbAttribs['tabindex'] = $this->mParams['tabindex'];
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
$select = $select->getHTML();
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $this->mParams['maxlength'] ) ) {
|
|
|
|
|
$tbAttribs['maxlength'] = $this->mParams['maxlength'];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$tbAttribs['class'] = $this->mClass;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$textbox = Html::input( $this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs );
|
|
|
|
|
|
|
|
|
|
return "$select<br />\n$textbox";
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getInputOOUI( $value ) {
|
2015-07-26 23:22:56 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string
|
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 ) ) {
|
|
|
|
|
$val = $request->getText( $this->mName );
|
|
|
|
|
|
2014-03-05 20:15:41 +00:00
|
|
|
if ( $val === 'other' ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$val = $request->getText( $this->mName . '-other' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $val;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|