Move up devunt's name to Developers
[lhc/web/wiklou.git] / includes / htmlform / HTMLSelectOrOtherField.php
1 <?php
2
3 /**
4 * Select dropdown field, with an additional "other" textbox.
5 */
6 class HTMLSelectOrOtherField extends HTMLTextField {
7 function __construct( $params ) {
8 parent::__construct( $params );
9 $this->getOptions();
10 if ( !in_array( 'other', $this->mOptions, true ) ) {
11 $msg =
12 isset( $params['other'] )
13 ? $params['other']
14 : wfMessage( 'htmlform-selectorother-other' )->text();
15 // Have 'other' always as first element
16 $this->mOptions = array( $msg => 'other' ) + $this->mOptions;
17 }
18
19 }
20
21 function getInputHTML( $value ) {
22 $valInSelect = false;
23
24 if ( $value !== false ) {
25 $value = strval( $value );
26 $valInSelect = in_array(
27 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
28 );
29 }
30
31 $selected = $valInSelect ? $value : 'other';
32
33 $select = new XmlSelect( $this->mName, $this->mID, $selected );
34 $select->addOptions( $this->getOptions() );
35
36 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
37
38 $tbAttribs = array( 'id' => $this->mID . '-other', 'size' => $this->getSize() );
39
40 if ( !empty( $this->mParams['disabled'] ) ) {
41 $select->setAttribute( 'disabled', 'disabled' );
42 $tbAttribs['disabled'] = 'disabled';
43 }
44
45 if ( isset( $this->mParams['tabindex'] ) ) {
46 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
47 $tbAttribs['tabindex'] = $this->mParams['tabindex'];
48 }
49
50 $select = $select->getHTML();
51
52 if ( isset( $this->mParams['maxlength'] ) ) {
53 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
54 }
55
56 if ( $this->mClass !== '' ) {
57 $tbAttribs['class'] = $this->mClass;
58 }
59
60 $textbox = Html::input( $this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs );
61
62 return "$select<br />\n$textbox";
63 }
64
65 /**
66 * @param WebRequest $request
67 *
68 * @return string
69 */
70 function loadDataFromRequest( $request ) {
71 if ( $request->getCheck( $this->mName ) ) {
72 $val = $request->getText( $this->mName );
73
74 if ( $val === 'other' ) {
75 $val = $request->getText( $this->mName . '-other' );
76 }
77
78 return $val;
79 } else {
80 return $this->getDefault();
81 }
82 }
83 }