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