Merge "Align "What's this" vertically"
[lhc/web/wiklou.git] / includes / htmlform / fields / 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 public 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 public function getInputHTML( $value ) {
24 $valInSelect = false;
25
26 if ( $value !== false ) {
27 $value = strval( $value );
28 $valInSelect = in_array(
29 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
30 );
31 }
32
33 $selected = $valInSelect ? $value : 'other';
34
35 $select = new XmlSelect( $this->mName, $this->mID, $selected );
36 $select->addOptions( $this->getOptions() );
37
38 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
39
40 $tbAttribs = [ 'id' => $this->mID . '-other', 'size' => $this->getSize() ];
41
42 if ( !empty( $this->mParams['disabled'] ) ) {
43 $select->setAttribute( 'disabled', 'disabled' );
44 $tbAttribs['disabled'] = 'disabled';
45 }
46
47 if ( isset( $this->mParams['tabindex'] ) ) {
48 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
49 $tbAttribs['tabindex'] = $this->mParams['tabindex'];
50 }
51
52 $select = $select->getHTML();
53
54 if ( isset( $this->mParams['maxlength'] ) ) {
55 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
56 }
57
58 if ( $this->mClass !== '' ) {
59 $tbAttribs['class'] = $this->mClass;
60 }
61
62 $textbox = Html::input( $this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs );
63
64 return "$select<br />\n$textbox";
65 }
66
67 protected function shouldInfuseOOUI() {
68 return true;
69 }
70
71 protected function getOOUIModules() {
72 return [ 'mediawiki.widgets.SelectWithInputWidget' ];
73 }
74
75 public function getInputOOUI( $value ) {
76 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
77
78 $valInSelect = false;
79 if ( $value !== false ) {
80 $value = strval( $value );
81 $valInSelect = in_array(
82 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
83 );
84 }
85
86 # DropdownInput
87 $dropdownAttribs = [
88 'id' => $this->mID,
89 'name' => $this->mName,
90 'options' => $this->getOptionsOOUI(),
91 'value' => $valInSelect ? $value : 'other',
92 'class' => [ 'mw-htmlform-select-or-other' ],
93 ];
94
95 $allowedParams = [
96 'disabled',
97 'tabindex',
98 ];
99
100 $dropdownAttribs += OOUI\Element::configFromHtmlAttributes(
101 $this->getAttributes( $allowedParams )
102 );
103
104 # TextInput
105 $textAttribs = [
106 'id' => $this->mID . '-other',
107 'name' => $this->mName . '-other',
108 'size' => $this->getSize(),
109 'value' => $valInSelect ? '' : $value,
110 ];
111
112 $allowedParams = [
113 'required',
114 'autofocus',
115 'multiple',
116 'disabled',
117 'tabindex',
118 'maxlength',
119 ];
120
121 $textAttribs += OOUI\Element::configFromHtmlAttributes(
122 $this->getAttributes( $allowedParams )
123 );
124
125 if ( $this->mClass !== '' ) {
126 $textAttribs['classes'] = [ $this->mClass ];
127 }
128 if ( $this->mPlaceholder !== '' ) {
129 $textAttribs['placeholder'] = $this->mPlaceholder;
130 }
131
132 return $this->getInputWidget( [
133 'textinput' => $textAttribs,
134 'dropdowninput' => $dropdownAttribs,
135 'or' => true,
136 ] );
137 }
138
139 public function getInputWidget( $params ) {
140 return new Mediawiki\Widget\SelectWithInputWidget( $params );
141 }
142
143 /**
144 * @param WebRequest $request
145 *
146 * @return string
147 */
148 public function loadDataFromRequest( $request ) {
149 if ( $request->getCheck( $this->mName ) ) {
150 $val = $request->getText( $this->mName );
151
152 if ( $val === 'other' ) {
153 $val = $request->getText( $this->mName . '-other' );
154 }
155
156 return $val;
157 } else {
158 return $this->getDefault();
159 }
160 }
161 }