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