Fix missing commit() flag in postgres savepoint class
[mediawiki.git] / includes / htmlform / fields / HTMLSelectAndOtherField.php
blobe75c2b2507d2e8a0f08711c67bf8edafa1c5f7e9
1 <?php
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.
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'] = $this->getMessage( $params['other-message'] )->plain();
19 } else {
20 $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
23 parent::__construct( $params );
25 if ( $this->getOptions() === null ) {
26 // Sulk
27 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
29 if ( !in_array( 'other', $this->mOptions, true ) ) {
30 // Have 'other' always as first element
31 $this->mOptions = [ $params['other'] => 'other' ] + $this->mOptions;
33 $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
37 function getInputHTML( $value ) {
38 $select = parent::getInputHTML( $value[1] );
40 $textAttribs = [
41 'id' => $this->mID . '-other',
42 'size' => $this->getSize(),
43 'class' => [ 'mw-htmlform-select-and-other-field' ],
44 'data-id-select' => $this->mID,
47 if ( $this->mClass !== '' ) {
48 $textAttribs['class'][] = $this->mClass;
51 $allowedParams = [
52 'required',
53 'autofocus',
54 'multiple',
55 'disabled',
56 'tabindex',
57 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
60 $textAttribs += $this->getAttributes( $allowedParams );
62 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
64 return "$select<br />\n$textbox";
67 function getInputOOUI( $value ) {
68 return false;
71 /**
72 * @param WebRequest $request
74 * @return array("<overall message>","<select value>","<text field value>")
76 function loadDataFromRequest( $request ) {
77 if ( $request->getCheck( $this->mName ) ) {
78 $list = $request->getText( $this->mName );
79 $text = $request->getText( $this->mName . '-other' );
81 // Should be built the same as in mediawiki.htmlform.js
82 if ( $list == 'other' ) {
83 $final = $text;
84 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
85 # User has spoofed the select form to give an option which wasn't
86 # in the original offer. Sulk...
87 $final = $text;
88 } elseif ( $text == '' ) {
89 $final = $list;
90 } else {
91 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
93 } else {
94 $final = $this->getDefault();
96 $list = 'other';
97 $text = $final;
98 foreach ( $this->mFlatOptions as $option ) {
99 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
100 if ( strpos( $text, $match ) === 0 ) {
101 $list = $option;
102 $text = substr( $text, strlen( $match ) );
103 break;
108 return [ $final, $list, $text ];
111 function getSize() {
112 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
115 function validate( $value, $alldata ) {
116 # HTMLSelectField forces $value to be one of the options in the select
117 # field, which is not useful here. But we do want the validation further up
118 # the chain
119 $p = parent::validate( $value[1], $alldata );
121 if ( $p !== true ) {
122 return $p;
125 if ( isset( $this->mParams['required'] )
126 && $this->mParams['required'] !== false
127 && $value[1] === ''
129 return $this->msg( 'htmlform-required' )->parse();
132 return true;