MediaWiki  1.27.1
HTMLAutoCompleteSelectField.php
Go to the documentation of this file.
1 <?php
2 
30  protected $autocomplete = [];
31 
32  function __construct( $params ) {
33  $params += [
34  'require-match' => false,
35  ];
36 
37  parent::__construct( $params );
38 
39  if ( array_key_exists( 'autocomplete-messages', $this->mParams ) ) {
40  foreach ( $this->mParams['autocomplete-messages'] as $key => $value ) {
41  $key = $this->msg( $key )->plain();
42  $this->autocomplete[$key] = strval( $value );
43  }
44  } elseif ( array_key_exists( 'autocomplete', $this->mParams ) ) {
45  foreach ( $this->mParams['autocomplete'] as $key => $value ) {
46  $this->autocomplete[$key] = strval( $value );
47  }
48  }
49  if ( !is_array( $this->autocomplete ) || !$this->autocomplete ) {
50  throw new MWException( 'HTMLAutoCompleteSelectField called without any autocompletions' );
51  }
52 
53  $this->getOptions();
54  if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) {
55  if ( isset( $params['other-message'] ) ) {
56  $msg = $this->getMessage( $params['other-message'] )->text();
57  } elseif ( isset( $params['other'] ) ) {
58  $msg = $params['other'];
59  } else {
60  $msg = wfMessage( 'htmlform-selectorother-other' )->text();
61  }
62  $this->mOptions[$msg] = 'other';
63  }
64  }
65 
67  if ( $request->getCheck( $this->mName ) ) {
68  $val = $request->getText( $this->mName . '-select', 'other' );
69 
70  if ( $val === 'other' ) {
71  $val = $request->getText( $this->mName );
72  if ( isset( $this->autocomplete[$val] ) ) {
73  $val = $this->autocomplete[$val];
74  }
75  }
76 
77  return $val;
78  } else {
79  return $this->getDefault();
80  }
81  }
82 
83  function validate( $value, $alldata ) {
84  $p = parent::validate( $value, $alldata );
85 
86  if ( $p !== true ) {
87  return $p;
88  }
89 
90  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
91 
92  if ( in_array( strval( $value ), $validOptions, true ) ) {
93  return true;
94  } elseif ( in_array( strval( $value ), $this->autocomplete, true ) ) {
95  return true;
96  } elseif ( $this->mParams['require-match'] ) {
97  return $this->msg( 'htmlform-select-badoption' )->parse();
98  }
99 
100  return true;
101  }
102 
103  // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :(
104  public function getAttributes( array $list ) {
105  $attribs = [
106  'type' => 'text',
107  'data-autocomplete' => FormatJson::encode( array_keys( $this->autocomplete ) ),
108  ] + parent::getAttributes( $list );
109 
110  if ( $this->getOptions() ) {
111  $attribs['data-hide-if'] = FormatJson::encode(
112  [ '!==', $this->mName . '-select', 'other' ]
113  );
114  }
115 
116  return $attribs;
117  }
118 
119  function getInputHTML( $value ) {
120  $oldClass = $this->mClass;
121  $this->mClass = (array)$this->mClass;
122 
123  $valInSelect = false;
124  $ret = '';
125 
126  if ( $this->getOptions() ) {
127  if ( $value !== false ) {
128  $value = strval( $value );
129  $valInSelect = in_array(
131  );
132  }
133 
134  $selected = $valInSelect ? $value : 'other';
135  $select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected );
136  $select->addOptions( $this->getOptions() );
137  $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
138 
139  if ( !empty( $this->mParams['disabled'] ) ) {
140  $select->setAttribute( 'disabled', 'disabled' );
141  }
142 
143  if ( isset( $this->mParams['tabindex'] ) ) {
144  $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
145  }
146 
147  $ret = $select->getHTML() . "<br />\n";
148 
149  $this->mClass[] = 'mw-htmlform-hide-if';
150  }
151 
152  if ( $valInSelect ) {
153  $value = '';
154  } else {
155  $key = array_search( strval( $value ), $this->autocomplete, true );
156  if ( $key !== false ) {
157  $value = $key;
158  }
159  }
160 
161  $this->mClass[] = 'mw-htmlform-autocomplete';
162  $ret .= parent::getInputHTML( $valInSelect ? '' : $value );
163  $this->mClass = $oldClass;
164 
165  return $ret;
166  }
167 
173  function getInputOOUI( $value ) {
174  // To be implemented, for now override the function from HTMLTextField
175  return false;
176  }
177 }
the array() calling protocol came about after MediaWiki 1.4rc1.
getOptions()
Fetch the array of options from the field's parameters.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
getMessage($value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a name + parameters array) into a Message.
msg()
Get a translated interface message.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1798
Class for generating HTML