1 2 3 4 5 6 7 8 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
<?php
/**
* Multi-select field
*
* @stable to extend
*/
class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
/**
* @stable to call
*
* @param array $params
* In adition to the usual HTMLFormField parameters, this can take the following fields:
* - dropdown: If given, the options will be displayed inside a dropdown with a text field that
* can be used to filter them. This is desirable mostly for very long lists of options.
* This only works for users with JavaScript support and falls back to the list of checkboxes.
* - flatlist: If given, the options will be displayed on a single line (wrapping to following
* lines if necessary), rather than each one on a line of its own. This is desirable mostly
* for very short lists of concisely labelled options.
*/
public function __construct( $params ) {
parent::__construct( $params );
// If the disabled-options parameter is not provided, use an empty array
if ( isset( $this->mParams['disabled-options'] ) === false ) {
$this->mParams['disabled-options'] = [];
}
if ( isset( $params['dropdown'] ) ) {
$this->mClass .= ' mw-htmlform-dropdown';
}
if ( isset( $params['flatlist'] ) ) {
$this->mClass .= ' mw-htmlform-flatlist';
}
}
/**
* @inheritDoc
* @stable to override
*/
public function validate( $value, $alldata ) {
$p = parent::validate( $value, $alldata );
if ( $p !== true ) {
return $p;
}
if ( !is_array( $value ) ) {
return false;
}
# If all options are valid, array_intersect of the valid options
# and the provided options will return the provided options.
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
$validValues = array_intersect( $value, $validOptions );
if ( count( $validValues ) == count( $value ) ) {
return true;
} else {
return $this->msg( 'htmlform-select-badoption' );
}
}
/**
* @inheritDoc
* @stable to override
*/
public function getInputHTML( $value ) {
if ( isset( $this->mParams['dropdown'] ) ) {
$this->mParent->getOutput()->addModules( 'jquery.chosen' );
}
$value = HTMLFormField::forceToStringRecursive( $value );
$html = $this->formatOptions( $this->getOptions(), $value );
return $html;
}
/**
* @stable to override
*
* @param array $options
* @param mixed $value
*
* @return string
* @throws MWException
*/
public function formatOptions( $options, $value ) {
$html = '';
$attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
foreach ( $options as $label => $info ) {
if ( is_array( $info ) ) {
$html .= Html::rawElement( 'h1', [], $label ) . "\n";
$html .= $this->formatOptions( $info, $value );
} else {
$thisAttribs = [
'id' => "{$this->mID}-$info",
'value' => $info,
];
if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
$thisAttribs['disabled'] = 'disabled';
}
$checked = in_array( $info, $value, true );
$checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
$html .= ' ' . Html::rawElement(
'div',
[ 'class' => 'mw-htmlform-flatlist-item' ],
$checkbox
);
}
}
return $html;
}
protected function getOneCheckbox( $checked, $attribs, $label ) {
if ( $this->mParent instanceof OOUIHTMLForm ) {
throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
} else {
$elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
$checkbox =
Xml::check( "{$this->mName}[]", $checked, $attribs ) .
"\u{00A0}" .
call_user_func( $elementFunc,
'label',
[ 'for' => $attribs['id'] ],
$label
);
if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
$checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
$checkbox .
Html::closeElement( 'div' );
}
return $checkbox;
}
}
/**
* Get options and make them into arrays suitable for OOUI.
* @stable to override
* @throws MWException
*/
public function getOptionsOOUI() {
// Sections make this difficult. See getInputOOUI().
throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
}
/**
* Get the OOUI version of this field.
*
* Returns OOUI\CheckboxMultiselectInputWidget for fields that only have one section,
* string otherwise.
*
* @stable to override
* @since 1.28
* @param string[] $value
* @return string|OOUI\CheckboxMultiselectInputWidget
* @suppress PhanParamSignatureMismatch
*/
public function getInputOOUI( $value ) {
$this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
$hasSections = false;
$optionsOouiSections = [];
$options = $this->getOptions();
// If the options are supposed to be split into sections, each section becomes a separate
// CheckboxMultiselectInputWidget.
foreach ( $options as $label => $section ) {
if ( is_array( $section ) ) {
$optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
unset( $options[$label] );
$hasSections = true;
}
}
// If anything remains in the array, they are sectionless options. Put them in a separate widget
// at the beginning.
if ( $options ) {
$optionsOouiSections = array_merge(
[ '' => Xml::listDropDownOptionsOoui( $options ) ],
$optionsOouiSections
);
}
'@phan-var array[][] $optionsOouiSections';
$out = [];
foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
$attr = [];
$attr['name'] = "{$this->mName}[]";
$attr['value'] = $value;
$options = $optionsOoui;
foreach ( $options as &$option ) {
$option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
}
if ( $this->mOptionsLabelsNotFromMessage ) {
foreach ( $options as &$option ) {
$option['label'] = new OOUI\HtmlSnippet( $option['label'] );
}
}
unset( $option );
$attr['options'] = $options;
$attr += OOUI\Element::configFromHtmlAttributes(
$this->getAttributes( [ 'disabled', 'tabindex' ] )
);
if ( $this->mClass !== '' ) {
$attr['classes'] = [ $this->mClass ];
}
$widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
if ( $sectionLabel ) {
$out[] = new OOUI\FieldsetLayout( [
'items' => [ $widget ],
'label' => new OOUI\HtmlSnippet( $sectionLabel ),
] );
} else {
$out[] = $widget;
}
}
if ( !$hasSections && $out ) {
// Directly return the only OOUI\CheckboxMultiselectInputWidget.
// This allows it to be made infusable and later tweaked by JS code.
return $out[ 0 ];
}
return implode( '', $out );
}
/**
* @stable to override
* @param WebRequest $request
*
* @return string|array
*/
public function loadDataFromRequest( $request ) {
$fromRequest = $request->getArray( $this->mName, [] );
// Fetch the value in either one of the two following case:
// - we have a valid submit attempt (form was just submitted)
// - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
// Checkboxes are just not added to the request arrays if they're not checked,
// so it's perfectly possible for there not to be an entry at all
return $fromRequest;
} else {
// That's ok, the user has not yet submitted the form, so show the defaults
return $this->getDefault();
}
}
/**
* @inheritDoc
* @stable to override
*/
public function getDefault() {
return $this->mDefault ?? [];
}
/**
* @inheritDoc
* @stable to override
*/
public function filterDataForSubmit( $data ) {
$data = HTMLFormField::forceToStringRecursive( $data );
$options = HTMLFormField::flattenOptions( $this->getOptions() );
$res = [];
foreach ( $options as $opt ) {
$res["$opt"] = in_array( $opt, $data, true );
}
return $res;
}
/**
* @inheritDoc
* @stable to override
*/
protected function needsLabel() {
return false;
}
}
|