MediaWiki REL1_35
HTMLAutoCompleteSelectField.php
Go to the documentation of this file.
1<?php
2
36 protected $autocompleteData = [];
37
38 /*
39 * @stable to call
40 */
41 public function __construct( $params ) {
42 $params += [
43 'require-match' => false,
44 ];
45
46 // FIXME B/C, remove in 1.30
47 if (
48 array_key_exists( 'autocomplete', $params )
49 && !array_key_exists( 'autocomplete-data', $params )
50 ) {
51 $params['autocomplete-data'] = $params['autocomplete'];
52 unset( $params['autocomplete'] );
53 }
54 if (
55 array_key_exists( 'autocomplete-messages', $params )
56 && !array_key_exists( 'autocomplete-data-messages', $params )
57 ) {
58 $params['autocomplete-data-messages'] = $params['autocomplete-messages'];
59 unset( $params['autocomplete-messages'] );
60 }
61
62 parent::__construct( $params );
63
64 if ( array_key_exists( 'autocomplete-data-messages', $this->mParams ) ) {
65 foreach ( $this->mParams['autocomplete-data-messages'] as $key => $value ) {
66 $key = $this->msg( $key )->plain();
67 $this->autocompleteData[$key] = strval( $value );
68 }
69 } elseif ( array_key_exists( 'autocomplete-data', $this->mParams ) ) {
70 foreach ( $this->mParams['autocomplete-data'] as $key => $value ) {
71 $this->autocompleteData[$key] = strval( $value );
72 }
73 }
74 if ( !is_array( $this->autocompleteData ) || !$this->autocompleteData ) {
75 throw new MWException( 'HTMLAutoCompleteSelectField called without any autocompletions' );
76 }
77
78 $this->getOptions();
79 if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) {
80 if ( isset( $params['other-message'] ) ) {
81 $msg = $this->getMessage( $params['other-message'] )->text();
82 } elseif ( isset( $params['other'] ) ) {
83 $msg = $params['other'];
84 } else {
85 $msg = wfMessage( 'htmlform-selectorother-other' )->text();
86 }
87 $this->mOptions[$msg] = 'other';
88 }
89 }
90
91 public function loadDataFromRequest( $request ) {
92 if ( $request->getCheck( $this->mName ) ) {
93 $val = $request->getText( $this->mName . '-select', 'other' );
94
95 if ( $val === 'other' ) {
96 $val = $request->getText( $this->mName );
97 if ( isset( $this->autocompleteData[$val] ) ) {
98 $val = $this->autocompleteData[$val];
99 }
100 }
101
102 return $val;
103 } else {
104 return $this->getDefault();
105 }
106 }
107
108 public function validate( $value, $alldata ) {
109 $p = parent::validate( $value, $alldata );
110
111 if ( $p !== true ) {
112 return $p;
113 }
114
115 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() ?: [] );
116
117 if ( in_array( strval( $value ), $validOptions, true ) ) {
118 return true;
119 } elseif ( in_array( strval( $value ), $this->autocompleteData, true ) ) {
120 return true;
121 } elseif ( $this->mParams['require-match'] ) {
122 return $this->msg( 'htmlform-select-badoption' );
123 }
124
125 return true;
126 }
127
128 // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :(
129 public function getAttributes( array $list ) {
130 $attribs = [
131 'type' => 'text',
132 'data-autocomplete' => FormatJson::encode( array_keys( $this->autocompleteData ) ),
133 ] + parent::getAttributes( $list );
134
135 if ( $this->getOptions() ) {
136 $attribs['data-hide-if'] = FormatJson::encode(
137 [ '!==', $this->mName . '-select', 'other' ]
138 );
139 }
140
141 return $attribs;
142 }
143
144 public function getInputHTML( $value ) {
145 $oldClass = $this->mClass;
146 $this->mClass = (array)$this->mClass;
147
148 $valInSelect = false;
149 $ret = '';
150
151 if ( $this->getOptions() ) {
152 if ( $value !== false ) {
153 $value = strval( $value );
154 $valInSelect = in_array(
155 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
156 );
157 }
158
159 $selected = $valInSelect ? $value : 'other';
160 $select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected );
161 $select->addOptions( $this->getOptions() );
162 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
163
164 if ( !empty( $this->mParams['disabled'] ) ) {
165 $select->setAttribute( 'disabled', 'disabled' );
166 }
167
168 if ( isset( $this->mParams['tabindex'] ) ) {
169 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
170 }
171
172 $ret = $select->getHTML() . "<br />\n";
173
174 $this->mClass[] = 'mw-htmlform-hide-if';
175 }
176
177 if ( $valInSelect ) {
178 $value = '';
179 } else {
180 $key = array_search( strval( $value ), $this->autocompleteData, true );
181 if ( $key !== false ) {
182 $value = $key;
183 }
184 }
185
186 $this->mClass[] = 'mw-htmlform-autocomplete';
187 $ret .= parent::getInputHTML( $valInSelect ? '' : $value );
188 $this->mClass = $oldClass;
189
190 return $ret;
191 }
192
198 public function getInputOOUI( $value ) {
199 // To be implemented, for now override the function from HTMLTextField
200 return false;
201 }
202}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Text field for selecting a value from a large list of possible values, with auto-completion and optio...
getInputOOUI( $value)
Get the OOUI version of this input.
getAttributes(array $list)
Returns the given attributes from the parameters Stable to override.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getOptions()
Fetch the array of options from the field's parameters.
msg( $key,... $params)
Get a translated interface message.
getDefault()
Stable to override.
<input> field.
MediaWiki exception.
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:26