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