Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLSelectAndOtherField.php
1 <?php
2
3 /**
4 * Double field with a dropdown list constructed from a system message in the format
5 * * Optgroup header
6 * ** <option value>
7 * * New Optgroup header
8 * Plus a text field underneath for an additional reason. The 'value' of the field is
9 * "<select>: <extra reason>", or "<extra reason>" if nothing has been selected in the
10 * select dropdown.
11 * @todo FIXME: If made 'required', only the text field should be compulsory.
12 */
13 class HTMLSelectAndOtherField extends HTMLSelectField {
14 public function __construct( $params ) {
15 if ( array_key_exists( 'other', $params ) ) {
16 // Do nothing
17 } elseif ( array_key_exists( 'other-message', $params ) ) {
18 $params['other'] = $this->getMessage( $params['other-message'] )->plain();
19 } else {
20 $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
21 }
22
23 parent::__construct( $params );
24
25 if ( $this->getOptions() === null ) {
26 // Sulk
27 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
28 }
29 if ( !in_array( 'other', $this->mOptions, true ) ) {
30 // Have 'other' always as first element
31 $this->mOptions = [ $params['other'] => 'other' ] + $this->mOptions;
32 }
33 $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
34 }
35
36 public function getInputHTML( $value ) {
37 $select = parent::getInputHTML( $value[1] );
38
39 $textAttribs = [
40 'id' => $this->mID . '-other',
41 'size' => $this->getSize(),
42 'class' => [ 'mw-htmlform-select-and-other-field' ],
43 'data-id-select' => $this->mID,
44 ];
45
46 if ( $this->mClass !== '' ) {
47 $textAttribs['class'][] = $this->mClass;
48 }
49
50 $allowedParams = [
51 'required',
52 'autofocus',
53 'multiple',
54 'disabled',
55 'tabindex',
56 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
57 ];
58
59 $textAttribs += $this->getAttributes( $allowedParams );
60
61 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
62
63 return "$select<br />\n$textbox";
64 }
65
66 protected function getOOUIModules() {
67 return [ 'mediawiki.widgets.SelectWithInputWidget' ];
68 }
69
70 public function getInputOOUI( $value ) {
71 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
72
73 # TextInput
74 $textAttribs = [
75 'id' => $this->mID . '-other',
76 'name' => $this->mName . '-other',
77 'size' => $this->getSize(),
78 'class' => [ 'mw-htmlform-select-and-other-field' ],
79 'data-id-select' => $this->mID,
80 'value' => $value[2],
81 ];
82
83 $allowedParams = [
84 'required',
85 'autofocus',
86 'multiple',
87 'disabled',
88 'tabindex',
89 'maxlength',
90 ];
91
92 $textAttribs += OOUI\Element::configFromHtmlAttributes(
93 $this->getAttributes( $allowedParams )
94 );
95
96 if ( $this->mClass !== '' ) {
97 $textAttribs['classes'] = [ $this->mClass ];
98 }
99
100 # DropdownInput
101 $dropdownInputAttribs = [
102 'name' => $this->mName,
103 'id' => $this->mID,
104 'options' => $this->getOptionsOOUI(),
105 'value' => $value[1],
106 ];
107
108 $allowedParams = [
109 'tabindex',
110 'disabled',
111 ];
112
113 $dropdownInputAttribs += OOUI\Element::configFromHtmlAttributes(
114 $this->getAttributes( $allowedParams )
115 );
116
117 if ( $this->mClass !== '' ) {
118 $dropdownInputAttribs['classes'] = [ $this->mClass ];
119 }
120
121 return $this->getInputWidget( [
122 'textinput' => $textAttribs,
123 'dropdowninput' => $dropdownInputAttribs,
124 'or' => false,
125 ] );
126 }
127
128 public function getInputWidget( $params ) {
129 return new Mediawiki\Widget\SelectWithInputWidget( $params );
130 }
131
132 /**
133 * @param WebRequest $request
134 *
135 * @return array("<overall message>","<select value>","<text field value>")
136 */
137 public function loadDataFromRequest( $request ) {
138 if ( $request->getCheck( $this->mName ) ) {
139 $list = $request->getText( $this->mName );
140 $text = $request->getText( $this->mName . '-other' );
141
142 // Should be built the same as in mediawiki.htmlform.js
143 if ( $list == 'other' ) {
144 $final = $text;
145 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
146 # User has spoofed the select form to give an option which wasn't
147 # in the original offer. Sulk...
148 $final = $text;
149 } elseif ( $text == '' ) {
150 $final = $list;
151 } else {
152 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
153 }
154 } else {
155 $final = $this->getDefault();
156
157 $list = 'other';
158 $text = $final;
159 foreach ( $this->mFlatOptions as $option ) {
160 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
161 if ( strpos( $text, $match ) === 0 ) {
162 $list = $option;
163 $text = substr( $text, strlen( $match ) );
164 break;
165 }
166 }
167 }
168
169 return [ $final, $list, $text ];
170 }
171
172 public function getSize() {
173 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
174 }
175
176 public function validate( $value, $alldata ) {
177 # HTMLSelectField forces $value to be one of the options in the select
178 # field, which is not useful here. But we do want the validation further up
179 # the chain
180 $p = parent::validate( $value[1], $alldata );
181
182 if ( $p !== true ) {
183 return $p;
184 }
185
186 if ( isset( $this->mParams['required'] )
187 && $this->mParams['required'] !== false
188 && $value[1] === ''
189 ) {
190 return $this->msg( 'htmlform-required' );
191 }
192
193 return true;
194 }
195 }