X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fhtmlform%2Ffields%2FHTMLMultiSelectField.php;h=05a2ba6d5e9c08ef3a617585906b2e9f6c8e7bcd;hb=a8379682a46a428320c88702c800a6107c015137;hp=c9fcb0992f578421216f2790a7aa3220fd13c247;hpb=dc36560cdf22db860133ecb58db1898e285f25ea;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/htmlform/fields/HTMLMultiSelectField.php b/includes/htmlform/fields/HTMLMultiSelectField.php index c9fcb0992f..515166c5f1 100644 --- a/includes/htmlform/fields/HTMLMultiSelectField.php +++ b/includes/htmlform/fields/HTMLMultiSelectField.php @@ -17,6 +17,11 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable 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'] = []; + } + // For backwards compatibility, also handle the old way with 'cssclass' => 'mw-chosen' if ( isset( $params['dropdown'] ) || strpos( $this->mClass, 'mw-chosen' ) !== false ) { $this->mClass .= ' mw-htmlform-dropdown'; @@ -27,7 +32,7 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable } } - function validate( $value, $alldata ) { + public function validate( $value, $alldata ) { $p = parent::validate( $value, $alldata ); if ( $p !== true ) { @@ -46,11 +51,11 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable if ( count( $validValues ) == count( $value ) ) { return true; } else { - return $this->msg( 'htmlform-select-badoption' )->parse(); + return $this->msg( 'htmlform-select-badoption' ); } } - function getInputHTML( $value ) { + public function getInputHTML( $value ) { if ( isset( $this->mParams['dropdown'] ) ) { $this->mParent->getOutput()->addModules( 'jquery.chosen' ); } @@ -61,7 +66,7 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable return $html; } - function formatOptions( $options, $value ) { + public function formatOptions( $options, $value ) { $html = ''; $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] ); @@ -75,6 +80,9 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable '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 ); @@ -112,6 +120,15 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable } } + /** + * Get options and make them into arrays suitable for OOUI. + * @return array Options for inclusion in a select or whatever. + */ + public function getOptionsOOUI() { + // Sections make this difficult. See getInputOOUI(). + throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' ); + } + /** * Get the OOUI version of this field. * @@ -120,36 +137,72 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable * @return OOUI\CheckboxMultiselectInputWidget */ public function getInputOOUI( $value ) { - $attr = $this->getTooltipAndAccessKey(); - $attr['id'] = $this->mID; - $attr['name'] = "{$this->mName}[]"; + $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' ); + + $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] ); + } + } + // 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 + ); + } + + $out = ''; + foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) { + $attr = []; + $attr['name'] = "{$this->mName}[]"; - $attr['value'] = $value; - $attr['options'] = $this->getOptionsOOUI(); + $attr['value'] = $value; + $attr['options'] = $optionsOoui; - if ( $this->mOptionsLabelsNotFromMessage ) { foreach ( $attr['options'] as &$option ) { - $option['label'] = new OOUI\HtmlSnippet( $option['label'] ); + $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true ); + } + if ( $this->mOptionsLabelsNotFromMessage ) { + foreach ( $attr['options'] as &$option ) { + $option['label'] = new OOUI\HtmlSnippet( $option['label'] ); + } } - } - $attr += OOUI\Element::configFromHtmlAttributes( - $this->getAttributes( [ 'disabled', 'tabindex' ] ) - ); + $attr += OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( [ 'disabled', 'tabindex' ] ) + ); - if ( $this->mClass !== '' ) { - $attr['classes'] = [ $this->mClass ]; + if ( $this->mClass !== '' ) { + $attr['classes'] = [ $this->mClass ]; + } + + $widget = new OOUI\CheckboxMultiselectInputWidget( $attr ); + if ( $sectionLabel ) { + $out .= new OOUI\FieldsetLayout( [ + 'items' => [ $widget ], + 'label' => $sectionLabel, + ] ); + } else { + $out .= $widget; + } } - return new OOUI\CheckboxMultiselectInputWidget( $attr ); + return $out; } /** * @param WebRequest $request * - * @return string + * @return string|array */ - function loadDataFromRequest( $request ) { + public function loadDataFromRequest( $request ) { if ( $this->isSubmitAttempt( $request ) ) { // 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 @@ -160,7 +213,7 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable } } - function getDefault() { + public function getDefault() { if ( isset( $this->mDefault ) ) { return $this->mDefault; } else { @@ -168,7 +221,7 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable } } - function filterDataForSubmit( $data ) { + public function filterDataForSubmit( $data ) { $data = HTMLFormField::forceToStringRecursive( $data ); $options = HTMLFormField::flattenOptions( $this->getOptions() );