| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm\Field; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\HTMLForm\HTMLFormField; |
| 7 |
use MediaWiki\HTMLForm\HTMLNestedFilterable; |
| 8 |
use MediaWiki\HTMLForm\OOUIHTMLForm; |
| 9 |
use MediaWiki\Request\WebRequest; |
| 10 |
use MediaWiki\Xml\Xml; |
| 11 |
use RuntimeException; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable { |
| 19 |
|
| 20 |
private $mPlaceholder; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function __construct( $params ) { |
| 35 |
parent::__construct( $params ); |
| 36 |
|
| 37 |
|
| 38 |
if ( !isset( $this->mParams['disabled-options'] ) ) { |
| 39 |
$this->mParams['disabled-options'] = []; |
| 40 |
} |
| 41 |
|
| 42 |
if ( isset( $params['dropdown'] ) ) { |
| 43 |
$this->mClass .= ' mw-htmlform-dropdown'; |
| 44 |
if ( isset( $params['placeholder'] ) ) { |
| 45 |
$this->mPlaceholder = $params['placeholder']; |
| 46 |
} elseif ( isset( $params['placeholder-message'] ) ) { |
| 47 |
$this->mPlaceholder = $this->msg( $params['placeholder-message'] )->text(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if ( isset( $params['flatlist'] ) ) { |
| 52 |
$this->mClass .= ' mw-htmlform-flatlist'; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function validate( $value, $alldata ) { |
| 61 |
$p = parent::validate( $value, $alldata ); |
| 62 |
|
| 63 |
if ( $p !== true ) { |
| 64 |
return $p; |
| 65 |
} |
| 66 |
|
| 67 |
if ( !is_array( $value ) ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
$value = array_filter( $value, 'is_scalar' ); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() ); |
| 77 |
|
| 78 |
$validValues = array_intersect( $value, $validOptions ); |
| 79 |
if ( count( $validValues ) == count( $value ) ) { |
| 80 |
return true; |
| 81 |
} else { |
| 82 |
return $this->msg( 'htmlform-select-badoption' ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function getInputHTML( $value ) { |
| 91 |
$value = HTMLFormField::forceToStringRecursive( $value ); |
| 92 |
$html = $this->formatOptions( $this->getOptions(), $value ); |
| 93 |
|
| 94 |
return $html; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function formatOptions( $options, $value ) { |
| 106 |
$html = ''; |
| 107 |
|
| 108 |
$attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] ); |
| 109 |
|
| 110 |
foreach ( $options as $label => $info ) { |
| 111 |
if ( is_array( $info ) ) { |
| 112 |
$html .= Html::rawElement( 'h1', [], $label ) . "\n"; |
| 113 |
$html .= $this->formatOptions( $info, $value ); |
| 114 |
} else { |
| 115 |
$thisAttribs = [ |
| 116 |
'id' => "{$this->mID}-$info", |
| 117 |
'value' => $info, |
| 118 |
]; |
| 119 |
if ( in_array( $info, $this->mParams['disabled-options'], true ) ) { |
| 120 |
$thisAttribs['disabled'] = 'disabled'; |
| 121 |
} |
| 122 |
$checked = in_array( $info, $value, true ); |
| 123 |
|
| 124 |
$checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label ); |
| 125 |
|
| 126 |
$html .= ' ' . Html::rawElement( |
| 127 |
'div', |
| 128 |
[ 'class' => 'mw-htmlform-flatlist-item' ], |
| 129 |
$checkbox |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $html; |
| 135 |
} |
| 136 |
|
| 137 |
protected function getOneCheckbox( $checked, $attribs, $label ) { |
| 138 |
if ( $this->mParent instanceof OOUIHTMLForm ) { |
| 139 |
throw new RuntimeException( __METHOD__ . ' is not supported' ); |
| 140 |
} else { |
| 141 |
$elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ]; |
| 142 |
$checkbox = |
| 143 |
Xml::check( "{$this->mName}[]", $checked, $attribs ) . |
| 144 |
"\u{00A0}" . |
| 145 |
call_user_func( $elementFunc, |
| 146 |
'label', |
| 147 |
[ 'for' => $attribs['id'] ], |
| 148 |
$label |
| 149 |
); |
| 150 |
return $checkbox; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function getOptionsOOUI() { |
| 159 |
|
| 160 |
|
| 161 |
throw new RuntimeException( __METHOD__ . ' is not supported' ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function getInputOOUI( $value ) { |
| 177 |
$this->mParent->getOutput()->addModules( 'oojs-ui-widgets' ); |
| 178 |
|
| 179 |
|
| 180 |
$value = array_filter( $value, 'is_scalar' ); |
| 181 |
|
| 182 |
$hasSections = false; |
| 183 |
$optionsOouiSections = []; |
| 184 |
$options = $this->getOptions(); |
| 185 |
|
| 186 |
|
| 187 |
foreach ( $options as $label => $section ) { |
| 188 |
if ( is_array( $section ) ) { |
| 189 |
$optionsOouiSections[ $label ] = Html::listDropdownOptionsOoui( $section ); |
| 190 |
unset( $options[$label] ); |
| 191 |
$hasSections = true; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
if ( $options ) { |
| 197 |
$optionsOouiSections = array_merge( |
| 198 |
[ '' => Html::listDropdownOptionsOoui( $options ) ], |
| 199 |
$optionsOouiSections |
| 200 |
); |
| 201 |
} |
| 202 |
'@phan-var array[][] $optionsOouiSections'; |
| 203 |
|
| 204 |
$out = []; |
| 205 |
foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) { |
| 206 |
$attr = []; |
| 207 |
$attr['name'] = "{$this->mName}[]"; |
| 208 |
|
| 209 |
$attr['value'] = $value; |
| 210 |
|
| 211 |
$options = $optionsOoui; |
| 212 |
foreach ( $options as &$option ) { |
| 213 |
$option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true ); |
| 214 |
} |
| 215 |
if ( $this->mOptionsLabelsNotFromMessage ) { |
| 216 |
foreach ( $options as &$option ) { |
| 217 |
$option['label'] = new \OOUI\HtmlSnippet( $option['label'] ); |
| 218 |
} |
| 219 |
} |
| 220 |
unset( $option ); |
| 221 |
$attr['options'] = $options; |
| 222 |
|
| 223 |
$attr += \OOUI\Element::configFromHtmlAttributes( |
| 224 |
$this->getAttributes( [ 'disabled', 'tabindex' ] ) |
| 225 |
); |
| 226 |
|
| 227 |
if ( $this->mClass !== '' ) { |
| 228 |
$attr['classes'] = [ $this->mClass ]; |
| 229 |
} |
| 230 |
|
| 231 |
$widget = new \OOUI\CheckboxMultiselectInputWidget( $attr ); |
| 232 |
if ( $sectionLabel ) { |
| 233 |
if ( $this->mOptionsLabelsNotFromMessage ) { |
| 234 |
|
| 235 |
$sectionLabel = new \OOUI\HtmlSnippet( $sectionLabel ); |
| 236 |
} |
| 237 |
$out[] = new \OOUI\FieldsetLayout( [ |
| 238 |
'items' => [ $widget ], |
| 239 |
'label' => $sectionLabel, |
| 240 |
] ); |
| 241 |
} else { |
| 242 |
$out[] = $widget; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if ( !$hasSections && $out ) { |
| 247 |
if ( $this->mPlaceholder ) { |
| 248 |
$out[0]->setData( ( $out[0]->getData() ?: [] ) + [ |
| 249 |
'placeholder' => $this->mPlaceholder, |
| 250 |
] ); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
return $out[0]; |
| 255 |
} |
| 256 |
|
| 257 |
return implode( '', $out ); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public function loadDataFromRequest( $request ) { |
| 267 |
$fromRequest = $request->getArray( $this->mName, [] ); |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if ( $this->isSubmitAttempt( $request ) || $fromRequest ) { |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
return $fromRequest; |
| 276 |
} else { |
| 277 |
|
| 278 |
return $this->getDefault(); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public function getDefault() { |
| 287 |
return $this->mDefault ?? []; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function filterDataForSubmit( $data ) { |
| 295 |
$data = HTMLFormField::forceToStringRecursive( $data ); |
| 296 |
$options = HTMLFormField::flattenOptions( $this->getOptions() ); |
| 297 |
$forcedOn = array_intersect( $this->mParams['disabled-options'], $this->getDefault() ); |
| 298 |
|
| 299 |
$res = []; |
| 300 |
foreach ( $options as $opt ) { |
| 301 |
$res["$opt"] = in_array( $opt, $forcedOn, true ) || in_array( $opt, $data, true ); |
| 302 |
} |
| 303 |
|
| 304 |
return $res; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
protected function needsLabel() { |
| 312 |
return false; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
class_alias( HTMLMultiSelectField::class, 'HTMLMultiSelectField' ); |