MediaWiki
REL1_23
|
00001 <?php 00002 00013 class HTMLSelectAndOtherField extends HTMLSelectField { 00014 function __construct( $params ) { 00015 if ( array_key_exists( 'other', $params ) ) { 00016 } elseif ( array_key_exists( 'other-message', $params ) ) { 00017 $params['other'] = wfMessage( $params['other-message'] )->plain(); 00018 } else { 00019 $params['other'] = wfMessage( 'htmlform-selectorother-other' )->plain(); 00020 } 00021 00022 parent::__construct( $params ); 00023 00024 if ( $this->getOptions() === null ) { 00025 # Sulk 00026 throw new MWException( 'HTMLSelectAndOtherField called without any options' ); 00027 } 00028 if ( !in_array( 'other', $this->mOptions, true ) ) { 00029 $this->mOptions[$params['other']] = 'other'; 00030 } 00031 $this->mFlatOptions = self::flattenOptions( $this->getOptions() ); 00032 00033 } 00034 00035 function getInputHTML( $value ) { 00036 $select = parent::getInputHTML( $value[1] ); 00037 00038 $textAttribs = array( 00039 'id' => $this->mID . '-other', 00040 'size' => $this->getSize(), 00041 ); 00042 00043 if ( $this->mClass !== '' ) { 00044 $textAttribs['class'] = $this->mClass; 00045 } 00046 00047 $allowedParams = array( 00048 'required', 00049 'autofocus', 00050 'multiple', 00051 'disabled', 00052 'tabindex' 00053 ); 00054 00055 $textAttribs += $this->getAttributes( $allowedParams ); 00056 00057 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs ); 00058 00059 return "$select<br />\n$textbox"; 00060 } 00061 00067 function loadDataFromRequest( $request ) { 00068 if ( $request->getCheck( $this->mName ) ) { 00069 00070 $list = $request->getText( $this->mName ); 00071 $text = $request->getText( $this->mName . '-other' ); 00072 00073 if ( $list == 'other' ) { 00074 $final = $text; 00075 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) { 00076 # User has spoofed the select form to give an option which wasn't 00077 # in the original offer. Sulk... 00078 $final = $text; 00079 } elseif ( $text == '' ) { 00080 $final = $list; 00081 } else { 00082 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text; 00083 } 00084 } else { 00085 $final = $this->getDefault(); 00086 00087 $list = 'other'; 00088 $text = $final; 00089 foreach ( $this->mFlatOptions as $option ) { 00090 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text(); 00091 if ( strpos( $text, $match ) === 0 ) { 00092 $list = $option; 00093 $text = substr( $text, strlen( $match ) ); 00094 break; 00095 } 00096 } 00097 } 00098 00099 return array( $final, $list, $text ); 00100 } 00101 00102 function getSize() { 00103 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45; 00104 } 00105 00106 function validate( $value, $alldata ) { 00107 # HTMLSelectField forces $value to be one of the options in the select 00108 # field, which is not useful here. But we do want the validation further up 00109 # the chain 00110 $p = parent::validate( $value[1], $alldata ); 00111 00112 if ( $p !== true ) { 00113 return $p; 00114 } 00115 00116 if ( isset( $this->mParams['required'] ) 00117 && $this->mParams['required'] !== false 00118 && $value[1] === '' 00119 ) { 00120 return $this->msg( 'htmlform-required' )->parse(); 00121 } 00122 00123 return true; 00124 } 00125 }