MediaWiki  1.28.0
HTMLMultiSelectField.php
Go to the documentation of this file.
1 <?php
2 
17  public function __construct( $params ) {
18  parent::__construct( $params );
19 
20  // For backwards compatibility, also handle the old way with 'cssclass' => 'mw-chosen'
21  if ( isset( $params['dropdown'] ) || strpos( $this->mClass, 'mw-chosen' ) !== false ) {
22  $this->mClass .= ' mw-htmlform-dropdown';
23  }
24 
25  if ( isset( $params['flatlist'] ) ) {
26  $this->mClass .= ' mw-htmlform-flatlist';
27  }
28  }
29 
30  function validate( $value, $alldata ) {
31  $p = parent::validate( $value, $alldata );
32 
33  if ( $p !== true ) {
34  return $p;
35  }
36 
37  if ( !is_array( $value ) ) {
38  return false;
39  }
40 
41  # If all options are valid, array_intersect of the valid options
42  # and the provided options will return the provided options.
43  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
44 
45  $validValues = array_intersect( $value, $validOptions );
46  if ( count( $validValues ) == count( $value ) ) {
47  return true;
48  } else {
49  return $this->msg( 'htmlform-select-badoption' )->parse();
50  }
51  }
52 
53  function getInputHTML( $value ) {
54  if ( isset( $this->mParams['dropdown'] ) ) {
55  $this->mParent->getOutput()->addModules( 'jquery.chosen' );
56  }
57 
59  $html = $this->formatOptions( $this->getOptions(), $value );
60 
61  return $html;
62  }
63 
64  function formatOptions( $options, $value ) {
65  $html = '';
66 
67  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
68 
69  foreach ( $options as $label => $info ) {
70  if ( is_array( $info ) ) {
71  $html .= Html::rawElement( 'h1', [], $label ) . "\n";
72  $html .= $this->formatOptions( $info, $value );
73  } else {
74  $thisAttribs = [
75  'id' => "{$this->mID}-$info",
76  'value' => $info,
77  ];
78  $checked = in_array( $info, $value, true );
79 
80  $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
81 
82  $html .= ' ' . Html::rawElement(
83  'div',
84  [ 'class' => 'mw-htmlform-flatlist-item' ],
85  $checkbox
86  );
87  }
88  }
89 
90  return $html;
91  }
92 
93  protected function getOneCheckbox( $checked, $attribs, $label ) {
94  if ( $this->mParent instanceof OOUIHTMLForm ) {
95  throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
96  } else {
97  $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
98  $checkbox =
99  Xml::check( "{$this->mName}[]", $checked, $attribs ) .
100  '&#160;' .
101  call_user_func( $elementFunc,
102  'label',
103  [ 'for' => $attribs['id'] ],
104  $label
105  );
106  if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
107  $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
108  $checkbox .
109  Html::closeElement( 'div' );
110  }
111  return $checkbox;
112  }
113  }
114 
122  public function getInputOOUI( $value ) {
123  $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
124 
125  $attr = $this->getTooltipAndAccessKey();
126  $attr['id'] = $this->mID;
127  $attr['name'] = "{$this->mName}[]";
128 
129  $attr['value'] = $value;
130  $attr['options'] = $this->getOptionsOOUI();
131 
132  if ( $this->mOptionsLabelsNotFromMessage ) {
133  foreach ( $attr['options'] as &$option ) {
134  $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
135  }
136  }
137 
138  $attr += OOUI\Element::configFromHtmlAttributes(
139  $this->getAttributes( [ 'disabled', 'tabindex' ] )
140  );
141 
142  if ( $this->mClass !== '' ) {
143  $attr['classes'] = [ $this->mClass ];
144  }
145 
146  return new OOUI\CheckboxMultiselectInputWidget( $attr );
147  }
148 
155  if ( $this->isSubmitAttempt( $request ) ) {
156  // Checkboxes are just not added to the request arrays if they're not checked,
157  // so it's perfectly possible for there not to be an entry at all
158  return $request->getArray( $this->mName, [] );
159  } else {
160  // That's ok, the user has not yet submitted the form, so show the defaults
161  return $this->getDefault();
162  }
163  }
164 
165  function getDefault() {
166  if ( isset( $this->mDefault ) ) {
167  return $this->mDefault;
168  } else {
169  return [];
170  }
171  }
172 
173  function filterDataForSubmit( $data ) {
174  $data = HTMLFormField::forceToStringRecursive( $data );
176 
177  $res = [];
178  foreach ( $options as $opt ) {
179  $res["$opt"] = in_array( $opt, $data, true );
180  }
181 
182  return $res;
183  }
184 
185  protected function needsLabel() {
186  return false;
187  }
188 }
static closeElement($element)
Returns "".
Definition: Html.php:305
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:1936
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:209
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey.
$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:247
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:1046
$res
Definition: database.txt:21
$params
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:1936
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
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
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:2573
The parent class to generate form fields.
getOneCheckbox($checked, $attribs, $label)
formatOptions($options, $value)
Compact stacked vertical format for forms, implemented using OOUI widgets.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
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.
getInputOOUI($value)
Get the OOUI version of this field.