Merge "Stop using the unholy trinity in DatabaseError"
[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 if ( !in_array( 'other', $params['options'], true ) ) {
9 $msg =
10 isset( $params['other'] )
11 ? $params['other']
12 : wfMessage( 'htmlform-selectorother-other' )->text();
13 $params['options'][$msg] = 'other';
14 }
15
16 parent::__construct( $params );
17 }
18
19 static function forceToStringRecursive( $array ) {
20 if ( is_array( $array ) ) {
21 return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array );
22 } else {
23 return strval( $array );
24 }
25 }
26
27 function getInputHTML( $value ) {
28 $valInSelect = false;
29
30 if ( $value !== false ) {
31 $valInSelect = in_array( $value, HTMLFormField::flattenOptions( $this->mParams['options'] ) );
32 }
33
34 $selected = $valInSelect ? $value : 'other';
35
36 $opts = self::forceToStringRecursive( $this->mParams['options'] );
37
38 $select = new XmlSelect( $this->mName, $this->mID, $selected );
39 $select->addOptions( $opts );
40
41 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
42
43 $tbAttribs = array( 'id' => $this->mID . '-other', 'size' => $this->getSize() );
44
45 if ( !empty( $this->mParams['disabled'] ) ) {
46 $select->setAttribute( 'disabled', 'disabled' );
47 $tbAttribs['disabled'] = 'disabled';
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 $request WebRequest
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 }