MediaWiki  1.27.2
HTMLMultiSelectField.php
Go to the documentation of this file.
1 <?php
2 
7  function validate( $value, $alldata ) {
8  $p = parent::validate( $value, $alldata );
9 
10  if ( $p !== true ) {
11  return $p;
12  }
13 
14  if ( !is_array( $value ) ) {
15  return false;
16  }
17 
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() );
21 
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();
27  }
28  }
29 
30  function getInputHTML( $value ) {
32  $html = $this->formatOptions( $this->getOptions(), $value );
33 
34  return $html;
35  }
36 
37  function formatOptions( $options, $value ) {
38  $html = '';
39 
40  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
41 
42  foreach ( $options as $label => $info ) {
43  if ( is_array( $info ) ) {
44  $html .= Html::rawElement( 'h1', [], $label ) . "\n";
45  $html .= $this->formatOptions( $info, $value );
46  } else {
47  $thisAttribs = [
48  'id' => "{$this->mID}-$info",
49  'value' => $info,
50  ];
51  $checked = in_array( $info, $value, true );
52 
53  $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
54 
55  $html .= ' ' . Html::rawElement(
56  'div',
57  [ 'class' => 'mw-htmlform-flatlist-item' ],
58  $checkbox
59  );
60  }
61  }
62 
63  return $html;
64  }
65 
66  protected function getOneCheckbox( $checked, $attribs, $label ) {
67  if ( $this->mParent instanceof OOUIHTMLForm ) {
68  if ( $this->mOptionsLabelsNotFromMessage ) {
69  $label = new OOUI\HtmlSnippet( $label );
70  }
71  return new OOUI\FieldLayout(
72  new OOUI\CheckboxInputWidget( [
73  'name' => "{$this->mName}[]",
74  'selected' => $checked,
75  ] + OOUI\Element::configFromHtmlAttributes(
76  $attribs
77  ) ),
78  [
79  'label' => $label,
80  'align' => 'inline',
81  ]
82  );
83  } else {
84  $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
85  $checkbox =
86  Xml::check( "{$this->mName}[]", $checked, $attribs ) .
87  '&#160;' .
88  call_user_func( $elementFunc,
89  'label',
90  [ 'for' => $attribs['id'] ],
91  $label
92  );
93  if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
94  $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
95  $checkbox .
96  Html::closeElement( 'div' );
97  }
98  return $checkbox;
99  }
100  }
101 
108  if ( $this->mParent->getMethod() == 'post' ) {
109  if ( $request->wasPosted() ) {
110  # Checkboxes are just not added to the request arrays if they're not checked,
111  # so it's perfectly possible for there not to be an entry at all
112  return $request->getArray( $this->mName, [] );
113  } else {
114  # That's ok, the user has not yet submitted the form, so show the defaults
115  return $this->getDefault();
116  }
117  } else {
118  # This is the impossible case: if we look at $_GET and see no data for our
119  # field, is it because the user has not yet submitted the form, or that they
120  # have submitted it with all the options unchecked? We will have to assume the
121  # latter, which basically means that you can't specify 'positive' defaults
122  # for GET forms.
123  # @todo FIXME...
124  return $request->getArray( $this->mName, [] );
125  }
126  }
127 
128  function getDefault() {
129  if ( isset( $this->mDefault ) ) {
130  return $this->mDefault;
131  } else {
132  return [];
133  }
134  }
135 
136  function filterDataForSubmit( $data ) {
137  $data = HTMLFormField::forceToStringRecursive( $data );
139 
140  $res = [];
141  foreach ( $options as $opt ) {
142  $res["$opt"] = in_array( $opt, $data, true );
143  }
144 
145  return $res;
146  }
147 
148  protected function needsLabel() {
149  return false;
150  }
151 }
static closeElement($element)
Returns "".
Definition: Html.php:306
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1798
getOptions()
Fetch the array of options from the field's parameters.
msg()
Get a translated interface message.
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:210
$value
static check($name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition: Xml.php:324
static forceToStringRecursive($array)
Recursively forces values in an array to strings, because issues arise with integer 0 as a value...
filterDataForSubmit($data)
Support for seperating multi-option preferences into multiple preferences Due to lack of array suppor...
Multi-select field.
static openElement($element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:248
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1004
$res
Definition: database.txt:21
validate($value, $alldata)
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2418
The parent class to generate form fields.
getOneCheckbox($checked, $attribs, $label)
formatOptions($options, $value)
Compact stacked vertical format for forms, implemented using OOUI widgets.
static flattenOptions($options)
flatten an array of options to a single array, for instance, a set of "" inside "...
getAttributes(array $list)
Returns the given attributes from the parameters.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1798