MWException: Don't send headers multiple times
[mediawiki.git] / includes / htmlform / HTMLSelectAndOtherField.php
blob19b2f7c56f711e16ab533f6a5e1c465a08f48c0e
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 } elseif ( array_key_exists( 'other-message', $params ) ) {
17 $params['other'] = wfMessage( $params['other-message'] )->plain();
18 } else {
19 $params['other'] = wfMessage( 'htmlform-selectorother-other' )->plain();
22 parent::__construct( $params );
24 if ( $this->getOptions() === null ) {
25 # Sulk
26 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
28 if ( !in_array( 'other', $this->mOptions, true ) ) {
29 $this->mOptions[$params['other']] = 'other';
31 $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
35 function getInputHTML( $value ) {
36 $select = parent::getInputHTML( $value[1] );
38 $textAttribs = array(
39 'id' => $this->mID . '-other',
40 'size' => $this->getSize(),
43 if ( $this->mClass !== '' ) {
44 $textAttribs['class'] = $this->mClass;
47 $allowedParams = array(
48 'required',
49 'autofocus',
50 'multiple',
51 'disabled',
52 'tabindex'
55 $textAttribs += $this->getAttributes( $allowedParams );
57 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
59 return "$select<br />\n$textbox";
62 /**
63 * @param WebRequest $request
65 * @return array("<overall message>","<select value>","<text field value>")
67 function loadDataFromRequest( $request ) {
68 if ( $request->getCheck( $this->mName ) ) {
70 $list = $request->getText( $this->mName );
71 $text = $request->getText( $this->mName . '-other' );
73 if ( $list == 'other' ) {
74 $final = $text;
75 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
76 # User has spoofed the select form to give an option which wasn't
77 # in the original offer. Sulk...
78 $final = $text;
79 } elseif ( $text == '' ) {
80 $final = $list;
81 } else {
82 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
84 } else {
85 $final = $this->getDefault();
87 $list = 'other';
88 $text = $final;
89 foreach ( $this->mFlatOptions as $option ) {
90 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
91 if ( strpos( $text, $match ) === 0 ) {
92 $list = $option;
93 $text = substr( $text, strlen( $match ) );
94 break;
99 return array( $final, $list, $text );
102 function getSize() {
103 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
106 function validate( $value, $alldata ) {
107 # HTMLSelectField forces $value to be one of the options in the select
108 # field, which is not useful here. But we do want the validation further up
109 # the chain
110 $p = parent::validate( $value[1], $alldata );
112 if ( $p !== true ) {
113 return $p;
116 if ( isset( $this->mParams['required'] )
117 && $this->mParams['required'] !== false
118 && $value[1] === ''
120 return $this->msg( 'htmlform-required' )->parse();
123 return true;