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