Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / HTMLMultiSelectField.php
blob8d28b59ecbf5e705caa1b93adc3c54e428e7f72e
1 <?php
3 /**
4 * Multi-select field
5 */
6 class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
7 function validate( $value, $alldata ) {
8 $p = parent::validate( $value, $alldata );
10 if ( $p !== true ) {
11 return $p;
14 if ( !is_array( $value ) ) {
15 return false;
18 # If all options are valid, array_intersect of the valid options
19 # and the provided options will return the provided options.
20 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
22 $validValues = array_intersect( $value, $validOptions );
23 if ( count( $validValues ) == count( $value ) ) {
24 return true;
25 } else {
26 return $this->msg( 'htmlform-select-badoption' )->parse();
30 function getInputHTML( $value ) {
31 $value = HTMLFormField::forceToStringRecursive( $value );
32 $html = $this->formatOptions( $this->getOptions(), $value );
34 return $html;
37 function formatOptions( $options, $value ) {
38 $html = '';
40 $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
41 $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
43 foreach ( $options as $label => $info ) {
44 if ( is_array( $info ) ) {
45 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
46 $html .= $this->formatOptions( $info, $value );
47 } else {
48 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
50 // @todo: Make this use checkLabel for consistency purposes
51 $checkbox = Xml::check(
52 $this->mName . '[]',
53 in_array( $info, $value, true ),
54 $attribs + $thisAttribs
56 $checkbox .= '&#160;' . call_user_func( $elementFunc,
57 'label',
58 array( 'for' => "{$this->mID}-$info" ),
59 $label
62 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
63 $checkbox = Html::rawElement(
64 'div',
65 array( 'class' => 'mw-ui-checkbox' ),
66 $checkbox
70 $html .= ' ' . Html::rawElement(
71 'div',
72 array( 'class' => 'mw-htmlform-flatlist-item' ),
73 $checkbox
78 return $html;
81 /**
82 * @param WebRequest $request
84 * @return string
86 function loadDataFromRequest( $request ) {
87 if ( $this->mParent->getMethod() == 'post' ) {
88 if ( $request->wasPosted() ) {
89 # Checkboxes are just not added to the request arrays if they're not checked,
90 # so it's perfectly possible for there not to be an entry at all
91 return $request->getArray( $this->mName, array() );
92 } else {
93 # That's ok, the user has not yet submitted the form, so show the defaults
94 return $this->getDefault();
96 } else {
97 # This is the impossible case: if we look at $_GET and see no data for our
98 # field, is it because the user has not yet submitted the form, or that they
99 # have submitted it with all the options unchecked? We will have to assume the
100 # latter, which basically means that you can't specify 'positive' defaults
101 # for GET forms.
102 # @todo FIXME...
103 return $request->getArray( $this->mName, array() );
107 function getDefault() {
108 if ( isset( $this->mDefault ) ) {
109 return $this->mDefault;
110 } else {
111 return array();
115 function filterDataForSubmit( $data ) {
116 $data = HTMLFormField::forceToStringRecursive( $data );
117 $options = HTMLFormField::flattenOptions( $this->getOptions() );
119 $res = array();
120 foreach ( $options as $opt ) {
121 $res["$opt"] = in_array( $opt, $data, true );
124 return $res;
127 protected function needsLabel() {
128 return false;