MediaWiki
REL1_24
|
00001 <?php 00002 00027 class HTMLAutoCompleteSelectField extends HTMLTextField { 00028 protected $autocomplete = array(); 00029 00030 function __construct( $params ) { 00031 $params += array( 00032 'require-match' => false, 00033 ); 00034 00035 parent::__construct( $params ); 00036 00037 if ( array_key_exists( 'autocomplete-messages', $this->mParams ) ) { 00038 foreach ( $this->mParams['autocomplete-messages'] as $key => $value ) { 00039 $key = $this->msg( $key )->plain(); 00040 $this->autocomplete[$key] = strval( $value ); 00041 } 00042 } elseif ( array_key_exists( 'autocomplete', $this->mParams ) ) { 00043 foreach ( $this->mParams['autocomplete'] as $key => $value ) { 00044 $this->autocomplete[$key] = strval( $value ); 00045 } 00046 } 00047 if ( !is_array( $this->autocomplete ) || !$this->autocomplete ) { 00048 throw new MWException( 'HTMLAutoCompleteSelectField called without any autocompletions' ); 00049 } 00050 00051 $this->getOptions(); 00052 if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) { 00053 if ( isset( $params['other-message'] ) ) { 00054 $msg = wfMessage( $params['other-message'] )->text(); 00055 } elseif ( isset( $params['other'] ) ) { 00056 $msg = $params['other']; 00057 } else { 00058 $msg = wfMessage( 'htmlform-selectorother-other' )->text(); 00059 } 00060 $this->mOptions[$msg] = 'other'; 00061 } 00062 } 00063 00064 function loadDataFromRequest( $request ) { 00065 if ( $request->getCheck( $this->mName ) ) { 00066 $val = $request->getText( $this->mName . '-select', 'other' ); 00067 00068 if ( $val === 'other' ) { 00069 $val = $request->getText( $this->mName ); 00070 if ( isset( $this->autocomplete[$val] ) ) { 00071 $val = $this->autocomplete[$val]; 00072 } 00073 } 00074 00075 return $val; 00076 } else { 00077 return $this->getDefault(); 00078 } 00079 } 00080 00081 function validate( $value, $alldata ) { 00082 $p = parent::validate( $value, $alldata ); 00083 00084 if ( $p !== true ) { 00085 return $p; 00086 } 00087 00088 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() ); 00089 00090 if ( in_array( strval( $value ), $validOptions, true ) ) { 00091 return true; 00092 } elseif ( in_array( strval( $value ), $this->autocomplete, true ) ) { 00093 return true; 00094 } elseif ( $this->mParams['require-match'] ) { 00095 return $this->msg( 'htmlform-select-badoption' )->parse(); 00096 } 00097 00098 return true; 00099 } 00100 00101 function getAttributes( array $list ) { 00102 $attribs = array( 00103 'type' => 'text', 00104 'data-autocomplete' => FormatJson::encode( array_keys( $this->autocomplete ) ), 00105 ) + parent::getAttributes( $list ); 00106 00107 if ( $this->getOptions() ) { 00108 $attribs['data-hide-if'] = FormatJson::encode( 00109 array( '!==', $this->mName . '-select', 'other' ) 00110 ); 00111 } 00112 00113 return $attribs; 00114 } 00115 00116 function getInputHTML( $value ) { 00117 $oldClass = $this->mClass; 00118 $this->mClass = (array)$this->mClass; 00119 00120 $valInSelect = false; 00121 $ret = ''; 00122 00123 if ( $this->getOptions() ) { 00124 if ( $value !== false ) { 00125 $value = strval( $value ); 00126 $valInSelect = in_array( 00127 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true 00128 ); 00129 } 00130 00131 $selected = $valInSelect ? $value : 'other'; 00132 $select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected ); 00133 $select->addOptions( $this->getOptions() ); 00134 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' ); 00135 00136 if ( !empty( $this->mParams['disabled'] ) ) { 00137 $select->setAttribute( 'disabled', 'disabled' ); 00138 } 00139 00140 if ( isset( $this->mParams['tabindex'] ) ) { 00141 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] ); 00142 } 00143 00144 $ret = $select->getHTML() . "<br />\n"; 00145 00146 $this->mClass[] = 'mw-htmlform-hide-if'; 00147 } 00148 00149 if ( $valInSelect ) { 00150 $value = ''; 00151 } else { 00152 $key = array_search( strval( $value ), $this->autocomplete, true ); 00153 if ( $key !== false ) { 00154 $value = $key; 00155 } 00156 } 00157 00158 $this->mClass[] = 'mw-htmlform-autocomplete'; 00159 $ret .= parent::getInputHTML( $valInSelect ? '' : $value ); 00160 $this->mClass = $oldClass; 00161 00162 return $ret; 00163 } 00164 00165 }