| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm\Field; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\HTMLForm\HTMLFormField; |
| 7 |
use MediaWiki\Json\FormatJson; |
| 8 |
use MediaWiki\Xml\XmlSelect; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class HTMLAutoCompleteSelectField extends HTMLTextField { |
| 39 |
|
| 40 |
protected $autocompleteData = []; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( $params ) { |
| 47 |
$params += [ |
| 48 |
'require-match' => false, |
| 49 |
]; |
| 50 |
|
| 51 |
parent::__construct( $params ); |
| 52 |
|
| 53 |
if ( array_key_exists( 'autocomplete-data-messages', $this->mParams ) ) { |
| 54 |
foreach ( $this->mParams['autocomplete-data-messages'] as $key => $value ) { |
| 55 |
$key = $this->msg( $key )->plain(); |
| 56 |
$this->autocompleteData[$key] = strval( $value ); |
| 57 |
} |
| 58 |
} elseif ( array_key_exists( 'autocomplete-data', $this->mParams ) ) { |
| 59 |
foreach ( $this->mParams['autocomplete-data'] as $key => $value ) { |
| 60 |
$this->autocompleteData[$key] = strval( $value ); |
| 61 |
} |
| 62 |
} |
| 63 |
if ( !is_array( $this->autocompleteData ) || !$this->autocompleteData ) { |
| 64 |
throw new InvalidArgumentException( 'HTMLAutoCompleteSelectField called without any autocompletions' ); |
| 65 |
} |
| 66 |
|
| 67 |
$this->getOptions(); |
| 68 |
if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) { |
| 69 |
if ( isset( $params['other-message'] ) ) { |
| 70 |
$msg = $this->getMessage( $params['other-message'] )->text(); |
| 71 |
} elseif ( isset( $params['other'] ) ) { |
| 72 |
$msg = $params['other']; |
| 73 |
} else { |
| 74 |
$msg = wfMessage( 'htmlform-selectorother-other' )->text(); |
| 75 |
} |
| 76 |
$this->mOptions[$msg] = 'other'; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public function loadDataFromRequest( $request ) { |
| 81 |
if ( $request->getCheck( $this->mName ) ) { |
| 82 |
$val = $request->getText( $this->mName . '-select', 'other' ); |
| 83 |
|
| 84 |
if ( $val === 'other' ) { |
| 85 |
$val = $request->getText( $this->mName ); |
| 86 |
if ( isset( $this->autocompleteData[$val] ) ) { |
| 87 |
$val = $this->autocompleteData[$val]; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return $val; |
| 92 |
} else { |
| 93 |
return $this->getDefault(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public function validate( $value, $alldata ) { |
| 98 |
$p = parent::validate( $value, $alldata ); |
| 99 |
|
| 100 |
if ( $p !== true ) { |
| 101 |
return $p; |
| 102 |
} |
| 103 |
|
| 104 |
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() ?: [] ); |
| 105 |
|
| 106 |
if ( |
| 107 |
in_array( strval( $value ), $validOptions, true ) || |
| 108 |
in_array( strval( $value ), $this->autocompleteData, true ) |
| 109 |
) { |
| 110 |
return true; |
| 111 |
} elseif ( $this->mParams['require-match'] ) { |
| 112 |
return $this->msg( 'htmlform-select-badoption' ); |
| 113 |
} |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public function getAttributes( array $list ) { |
| 120 |
$attribs = [ |
| 121 |
'type' => 'text', |
| 122 |
'data-autocomplete' => FormatJson::encode( array_keys( $this->autocompleteData ) ), |
| 123 |
] + parent::getAttributes( $list ); |
| 124 |
|
| 125 |
if ( $this->getOptions() ) { |
| 126 |
$attribs['data-cond-state'] = FormatJson::encode( [ |
| 127 |
'hide' => [ '!==', $this->mName . '-select', 'other' ], |
| 128 |
] ); |
| 129 |
} |
| 130 |
|
| 131 |
return $attribs; |
| 132 |
} |
| 133 |
|
| 134 |
public function getInputHTML( $value ) { |
| 135 |
$oldClass = $this->mClass; |
| 136 |
$classes = (array)$this->mClass; |
| 137 |
|
| 138 |
$valInSelect = false; |
| 139 |
$ret = ''; |
| 140 |
|
| 141 |
if ( $this->getOptions() ) { |
| 142 |
if ( $value !== false ) { |
| 143 |
$value = strval( $value ); |
| 144 |
$valInSelect = in_array( |
| 145 |
$value, HTMLFormField::flattenOptions( $this->getOptions() ), true |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
$selected = $valInSelect ? $value : 'other'; |
| 150 |
$select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected ); |
| 151 |
$select->addOptions( $this->getOptions() ); |
| 152 |
|
| 153 |
if ( !empty( $this->mParams['disabled'] ) ) { |
| 154 |
$select->setAttribute( 'disabled', 'disabled' ); |
| 155 |
} |
| 156 |
|
| 157 |
if ( isset( $this->mParams['tabindex'] ) ) { |
| 158 |
$select->setAttribute( 'tabindex', $this->mParams['tabindex'] ); |
| 159 |
} |
| 160 |
|
| 161 |
$ret = $select->getHTML() . "<br />\n"; |
| 162 |
|
| 163 |
$classes[] = 'mw-htmlform-hide-if'; |
| 164 |
} |
| 165 |
|
| 166 |
if ( $valInSelect ) { |
| 167 |
$value = ''; |
| 168 |
} else { |
| 169 |
$key = array_search( strval( $value ), $this->autocompleteData, true ); |
| 170 |
if ( $key !== false ) { |
| 171 |
$value = $key; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
$classes[] = 'mw-htmlform-autocomplete'; |
| 176 |
$this->mClass = implode( ' ', $classes ); |
| 177 |
$ret .= parent::getInputHTML( $valInSelect ? '' : $value ); |
| 178 |
$this->mClass = $oldClass; |
| 179 |
|
| 180 |
return $ret; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function getInputOOUI( $value ) { |
| 189 |
|
| 190 |
return false; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
class_alias( HTMLAutoCompleteSelectField::class, 'HTMLAutoCompleteSelectField' ); |