Move up devunt's name to Developers
[lhc/web/wiklou.git] / includes / htmlform / HTMLAutoCompleteSelectField.php
1 <?php
2
3 /**
4 * Text field for selecting a value from a large list of possible values, with
5 * auto-completion and optionally with a select dropdown for selecting common
6 * options.
7 *
8 * If one of 'options-messages', 'options', or 'options-message' is provided
9 * and non-empty, the select dropdown will be shown. An 'other' key will be
10 * appended using message 'htmlform-selectorother-other' if not already
11 * present.
12 *
13 * Besides the parameters recognized by HTMLTextField, the following are
14 * recognized:
15 * options-messages - As for HTMLSelectField
16 * options - As for HTMLSelectField
17 * options-message - As for HTMLSelectField
18 * autocomplete - Associative array mapping display text to values.
19 * autocomplete-messages - Like autocomplete, but keys are message names.
20 * require-match - Boolean, if true the value must be in the options or the
21 * autocomplete.
22 * other-message - Message to use instead of htmlform-selectorother-other for
23 * the 'other' message.
24 * other - Raw text to use for the 'other' message
25 *
26 */
27 class HTMLAutoCompleteSelectField extends HTMLTextField {
28 protected $autocomplete = array();
29
30 function __construct( $params ) {
31 $params += array(
32 'require-match' => false,
33 );
34
35 parent::__construct( $params );
36
37 if ( array_key_exists( 'autocomplete-messages', $this->mParams ) ) {
38 foreach ( $this->mParams['autocomplete-messages'] as $key => $value ) {
39 $key = $this->msg( $key )->plain();
40 $this->autocomplete[$key] = strval( $value );
41 }
42 } elseif ( array_key_exists( 'autocomplete', $this->mParams ) ) {
43 foreach ( $this->mParams['autocomplete'] as $key => $value ) {
44 $this->autocomplete[$key] = strval( $value );
45 }
46 }
47 if ( !is_array( $this->autocomplete ) || !$this->autocomplete ) {
48 throw new MWException( 'HTMLAutoCompleteSelectField called without any autocompletions' );
49 }
50
51 $this->getOptions();
52 if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) {
53 if ( isset( $params['other-message'] ) ) {
54 $msg = wfMessage( $params['other-message'] )->text();
55 } elseif ( isset( $params['other'] ) ) {
56 $msg = $params['other'];
57 } else {
58 $msg = wfMessage( 'htmlform-selectorother-other' )->text();
59 }
60 $this->mOptions[$msg] = 'other';
61 }
62 }
63
64 function loadDataFromRequest( $request ) {
65 if ( $request->getCheck( $this->mName ) ) {
66 $val = $request->getText( $this->mName . '-select', 'other' );
67
68 if ( $val === 'other' ) {
69 $val = $request->getText( $this->mName );
70 if ( isset( $this->autocomplete[$val] ) ) {
71 $val = $this->autocomplete[$val];
72 }
73 }
74
75 return $val;
76 } else {
77 return $this->getDefault();
78 }
79 }
80
81 function validate( $value, $alldata ) {
82 $p = parent::validate( $value, $alldata );
83
84 if ( $p !== true ) {
85 return $p;
86 }
87
88 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
89
90 if ( in_array( strval( $value ), $validOptions, true ) ) {
91 return true;
92 } elseif ( in_array( strval( $value ), $this->autocomplete, true ) ) {
93 return true;
94 } elseif ( $this->mParams['require-match'] ) {
95 return $this->msg( 'htmlform-select-badoption' )->parse();
96 }
97
98 return true;
99 }
100
101 function getAttributes( array $list ) {
102 $attribs = array(
103 'type' => 'text',
104 'data-autocomplete' => FormatJson::encode( array_keys( $this->autocomplete ) ),
105 ) + parent::getAttributes( $list );
106
107 if ( $this->getOptions() ) {
108 $attribs['data-hide-if'] = FormatJson::encode(
109 array( '!==', $this->mName . '-select', 'other' )
110 );
111 }
112
113 return $attribs;
114 }
115
116 function getInputHTML( $value ) {
117 $oldClass = $this->mClass;
118 $this->mClass = (array)$this->mClass;
119
120 $valInSelect = false;
121 $ret = '';
122
123 if ( $this->getOptions() ) {
124 if ( $value !== false ) {
125 $value = strval( $value );
126 $valInSelect = in_array(
127 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
128 );
129 }
130
131 $selected = $valInSelect ? $value : 'other';
132 $select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected );
133 $select->addOptions( $this->getOptions() );
134 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
135
136 if ( !empty( $this->mParams['disabled'] ) ) {
137 $select->setAttribute( 'disabled', 'disabled' );
138 }
139
140 if ( isset( $this->mParams['tabindex'] ) ) {
141 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
142 }
143
144 $ret = $select->getHTML() . "<br />\n";
145
146 $this->mClass[] = 'mw-htmlform-hide-if';
147 }
148
149 if ( $valInSelect ) {
150 $value = '';
151 } else {
152 $key = array_search( strval( $value ), $this->autocomplete, true );
153 if ( $key !== false ) {
154 $value = $key;
155 }
156 }
157
158 $this->mClass[] = 'mw-htmlform-autocomplete';
159 $ret .= parent::getInputHTML( $valInSelect ? '' : $value );
160 $this->mClass = $oldClass;
161
162 return $ret;
163 }
164
165 }