Add PreferencesFormPreSave hook
[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 } elseif ( array_key_exists( 'other-message', $params ) ) {
17 $params['other'] = wfMessage( $params['other-message'] )->plain();
18 } else {
19 $params['other'] = null;
20 }
21
22 if ( array_key_exists( 'options', $params ) ) {
23 # Options array already specified
24 } elseif ( array_key_exists( 'options-message', $params ) ) {
25 # Generate options array from a system message
26 $params['options'] =
27 self::parseMessage( wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
28 $params['other'] );
29 } else {
30 # Sulk
31 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
32 }
33 $this->mFlatOptions = self::flattenOptions( $params['options'] );
34
35 parent::__construct( $params );
36 }
37
38 /**
39 * Build a drop-down box from a textual list.
40 *
41 * @param string $string message text
42 * @param string $otherName name of "other reason" option
43 *
44 * @return Array
45 * @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication?
46 */
47 public static function parseMessage( $string, $otherName = null ) {
48 if ( $otherName === null ) {
49 $otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
50 }
51
52 $optgroup = false;
53 $options = array( $otherName => 'other' );
54
55 foreach ( explode( "\n", $string ) as $option ) {
56 $value = trim( $option );
57 if ( $value == '' ) {
58 continue;
59 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
60 # A new group is starting...
61 $value = trim( substr( $value, 1 ) );
62 $optgroup = $value;
63 } elseif ( substr( $value, 0, 2 ) == '**' ) {
64 # groupmember
65 $opt = trim( substr( $value, 2 ) );
66 if ( $optgroup === false ) {
67 $options[$opt] = $opt;
68 } else {
69 $options[$optgroup][$opt] = $opt;
70 }
71 } else {
72 # groupless reason list
73 $optgroup = false;
74 $options[$option] = $option;
75 }
76 }
77
78 return $options;
79 }
80
81 function getInputHTML( $value ) {
82 $select = parent::getInputHTML( $value[1] );
83
84 $textAttribs = array(
85 'id' => $this->mID . '-other',
86 'size' => $this->getSize(),
87 );
88
89 if ( $this->mClass !== '' ) {
90 $textAttribs['class'] = $this->mClass;
91 }
92
93 foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
94 if ( isset( $this->mParams[$param] ) ) {
95 $textAttribs[$param] = '';
96 }
97 }
98
99 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
100
101 return "$select<br />\n$textbox";
102 }
103
104 /**
105 * @param $request WebRequest
106 *
107 * @return Array("<overall message>","<select value>","<text field value>")
108 */
109 function loadDataFromRequest( $request ) {
110 if ( $request->getCheck( $this->mName ) ) {
111
112 $list = $request->getText( $this->mName );
113 $text = $request->getText( $this->mName . '-other' );
114
115 if ( $list == 'other' ) {
116 $final = $text;
117 } elseif ( !in_array( $list, $this->mFlatOptions ) ) {
118 # User has spoofed the select form to give an option which wasn't
119 # in the original offer. Sulk...
120 $final = $text;
121 } elseif ( $text == '' ) {
122 $final = $list;
123 } else {
124 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
125 }
126 } else {
127 $final = $this->getDefault();
128
129 $list = 'other';
130 $text = $final;
131 foreach ( $this->mFlatOptions as $option ) {
132 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
133 if ( strpos( $text, $match ) === 0 ) {
134 $list = $option;
135 $text = substr( $text, strlen( $match ) );
136 break;
137 }
138 }
139 }
140
141 return array( $final, $list, $text );
142 }
143
144 function getSize() {
145 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
146 }
147
148 function validate( $value, $alldata ) {
149 # HTMLSelectField forces $value to be one of the options in the select
150 # field, which is not useful here. But we do want the validation further up
151 # the chain
152 $p = parent::validate( $value[1], $alldata );
153
154 if ( $p !== true ) {
155 return $p;
156 }
157
158 if ( isset( $this->mParams['required'] )
159 && $this->mParams['required'] !== false
160 && $value[1] === ''
161 ) {
162 return $this->msg( 'htmlform-required' )->parse();
163 }
164
165 return true;
166 }
167 }