Move up devunt's name to Developers
[lhc/web/wiklou.git] / includes / htmlform / HTMLSelectAndOtherField.php
1 <?php
2
3 /**
4 * Double field with a dropdown list constructed from a system message in the format
5 * * Optgroup header
6 * ** <option value>
7 * * New Optgroup header
8 * Plus a text field underneath for an additional reason. The 'value' of the field is
9 * "<select>: <extra reason>", or "<extra reason>" if nothing has been selected in the
10 * select dropdown.
11 * @todo FIXME: If made 'required', only the text field should be compulsory.
12 */
13 class HTMLSelectAndOtherField extends HTMLSelectField {
14 function __construct( $params ) {
15 if ( array_key_exists( 'other', $params ) ) {
16 // Do nothing
17 } elseif ( array_key_exists( 'other-message', $params ) ) {
18 $params['other'] = wfMessage( $params['other-message'] )->plain();
19 } else {
20 $params['other'] = wfMessage( 'htmlform-selectorother-other' )->plain();
21 }
22
23 parent::__construct( $params );
24
25 if ( $this->getOptions() === null ) {
26 // Sulk
27 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
28 }
29 if ( !in_array( 'other', $this->mOptions, true ) ) {
30 // Have 'other' always as first element
31 $this->mOptions = array( $params['other'] => 'other' ) + $this->mOptions;
32 }
33 $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
34
35 }
36
37 function getInputHTML( $value ) {
38 $select = parent::getInputHTML( $value[1] );
39
40 $textAttribs = array(
41 'id' => $this->mID . '-other',
42 'size' => $this->getSize(),
43 'class' => array( 'mw-htmlform-select-and-other-field' ),
44 'data-id-select' => $this->mID,
45 );
46
47 if ( $this->mClass !== '' ) {
48 $textAttribs['class'][] = $this->mClass;
49 }
50
51 $allowedParams = array(
52 'required',
53 'autofocus',
54 'multiple',
55 'disabled',
56 'tabindex',
57 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
58 );
59
60 $textAttribs += $this->getAttributes( $allowedParams );
61
62 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
63
64 return "$select<br />\n$textbox";
65 }
66
67 /**
68 * @param WebRequest $request
69 *
70 * @return array("<overall message>","<select value>","<text field value>")
71 */
72 function loadDataFromRequest( $request ) {
73 if ( $request->getCheck( $this->mName ) ) {
74
75 $list = $request->getText( $this->mName );
76 $text = $request->getText( $this->mName . '-other' );
77
78 // Should be built the same as in mediawiki.htmlform.js
79 if ( $list == 'other' ) {
80 $final = $text;
81 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
82 # User has spoofed the select form to give an option which wasn't
83 # in the original offer. Sulk...
84 $final = $text;
85 } elseif ( $text == '' ) {
86 $final = $list;
87 } else {
88 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
89 }
90 } else {
91 $final = $this->getDefault();
92
93 $list = 'other';
94 $text = $final;
95 foreach ( $this->mFlatOptions as $option ) {
96 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
97 if ( strpos( $text, $match ) === 0 ) {
98 $list = $option;
99 $text = substr( $text, strlen( $match ) );
100 break;
101 }
102 }
103 }
104
105 return array( $final, $list, $text );
106 }
107
108 function getSize() {
109 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
110 }
111
112 function validate( $value, $alldata ) {
113 # HTMLSelectField forces $value to be one of the options in the select
114 # field, which is not useful here. But we do want the validation further up
115 # the chain
116 $p = parent::validate( $value[1], $alldata );
117
118 if ( $p !== true ) {
119 return $p;
120 }
121
122 if ( isset( $this->mParams['required'] )
123 && $this->mParams['required'] !== false
124 && $value[1] === ''
125 ) {
126 return $this->msg( 'htmlform-required' )->parse();
127 }
128
129 return true;
130 }
131 }