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