Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / htmlform / HTMLSelectOrOtherField.php
1 <?php
2
3 /**
4 * Select dropdown field, with an additional "other" textbox.
5 *
6 * HTMLComboboxField implements the same functionality using a single form field
7 * and should be used instead.
8 */
9 class HTMLSelectOrOtherField extends HTMLTextField {
10 function __construct( $params ) {
11 parent::__construct( $params );
12 $this->getOptions();
13 if ( !in_array( 'other', $this->mOptions, true ) ) {
14 $msg =
15 isset( $params['other'] )
16 ? $params['other']
17 : wfMessage( 'htmlform-selectorother-other' )->text();
18 // Have 'other' always as first element
19 $this->mOptions = [ $msg => 'other' ] + $this->mOptions;
20 }
21
22 }
23
24 function getInputHTML( $value ) {
25 $valInSelect = false;
26
27 if ( $value !== false ) {
28 $value = strval( $value );
29 $valInSelect = in_array(
30 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
31 );
32 }
33
34 $selected = $valInSelect ? $value : 'other';
35
36 $select = new XmlSelect( $this->mName, $this->mID, $selected );
37 $select->addOptions( $this->getOptions() );
38
39 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
40
41 $tbAttribs = [ 'id' => $this->mID . '-other', 'size' => $this->getSize() ];
42
43 if ( !empty( $this->mParams['disabled'] ) ) {
44 $select->setAttribute( 'disabled', 'disabled' );
45 $tbAttribs['disabled'] = 'disabled';
46 }
47
48 if ( isset( $this->mParams['tabindex'] ) ) {
49 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
50 $tbAttribs['tabindex'] = $this->mParams['tabindex'];
51 }
52
53 $select = $select->getHTML();
54
55 if ( isset( $this->mParams['maxlength'] ) ) {
56 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
57 }
58
59 if ( $this->mClass !== '' ) {
60 $tbAttribs['class'] = $this->mClass;
61 }
62
63 $textbox = Html::input( $this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs );
64
65 return "$select<br />\n$textbox";
66 }
67
68 function getInputOOUI( $value ) {
69 return false;
70 }
71
72 /**
73 * @param WebRequest $request
74 *
75 * @return string
76 */
77 function loadDataFromRequest( $request ) {
78 if ( $request->getCheck( $this->mName ) ) {
79 $val = $request->getText( $this->mName );
80
81 if ( $val === 'other' ) {
82 $val = $request->getText( $this->mName . '-other' );
83 }
84
85 return $val;
86 } else {
87 return $this->getDefault();
88 }
89 }
90 }