MediaWiki  1.29.1
HTMLMultiSelectField.php
Go to the documentation of this file.
1 <?php
2 
17  public function __construct( $params ) {
18  parent::__construct( $params );
19 
20  // If the disabled-options parameter is not provided, use an empty array
21  if ( isset( $this->mParams['disabled-options'] ) === false ) {
22  $this->mParams['disabled-options'] = [];
23  }
24 
25  // For backwards compatibility, also handle the old way with 'cssclass' => 'mw-chosen'
26  if ( isset( $params['dropdown'] ) || strpos( $this->mClass, 'mw-chosen' ) !== false ) {
27  $this->mClass .= ' mw-htmlform-dropdown';
28  }
29 
30  if ( isset( $params['flatlist'] ) ) {
31  $this->mClass .= ' mw-htmlform-flatlist';
32  }
33  }
34 
35  public function validate( $value, $alldata ) {
36  $p = parent::validate( $value, $alldata );
37 
38  if ( $p !== true ) {
39  return $p;
40  }
41 
42  if ( !is_array( $value ) ) {
43  return false;
44  }
45 
46  # If all options are valid, array_intersect of the valid options
47  # and the provided options will return the provided options.
48  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
49 
50  $validValues = array_intersect( $value, $validOptions );
51  if ( count( $validValues ) == count( $value ) ) {
52  return true;
53  } else {
54  return $this->msg( 'htmlform-select-badoption' );
55  }
56  }
57 
58  public function getInputHTML( $value ) {
59  if ( isset( $this->mParams['dropdown'] ) ) {
60  $this->mParent->getOutput()->addModules( 'jquery.chosen' );
61  }
62 
64  $html = $this->formatOptions( $this->getOptions(), $value );
65 
66  return $html;
67  }
68 
69  public function formatOptions( $options, $value ) {
70  $html = '';
71 
72  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
73 
74  foreach ( $options as $label => $info ) {
75  if ( is_array( $info ) ) {
76  $html .= Html::rawElement( 'h1', [], $label ) . "\n";
77  $html .= $this->formatOptions( $info, $value );
78  } else {
79  $thisAttribs = [
80  'id' => "{$this->mID}-$info",
81  'value' => $info,
82  ];
83  if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
84  $thisAttribs['disabled'] = 'disabled';
85  }
86  $checked = in_array( $info, $value, true );
87 
88  $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
89 
90  $html .= ' ' . Html::rawElement(
91  'div',
92  [ 'class' => 'mw-htmlform-flatlist-item' ],
93  $checkbox
94  );
95  }
96  }
97 
98  return $html;
99  }
100 
101  protected function getOneCheckbox( $checked, $attribs, $label ) {
102  if ( $this->mParent instanceof OOUIHTMLForm ) {
103  throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
104  } else {
105  $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
106  $checkbox =
107  Xml::check( "{$this->mName}[]", $checked, $attribs ) .
108  '&#160;' .
109  call_user_func( $elementFunc,
110  'label',
111  [ 'for' => $attribs['id'] ],
112  $label
113  );
114  if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
115  $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
116  $checkbox .
117  Html::closeElement( 'div' );
118  }
119  return $checkbox;
120  }
121  }
122 
127  public function getOptionsOOUI() {
128  $options = parent::getOptionsOOUI();
129  foreach ( $options as &$option ) {
130  $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
131  }
132  return $options;
133  }
134 
142  public function getInputOOUI( $value ) {
143  $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
144 
145  $attr = $this->getTooltipAndAccessKey();
146  $attr['id'] = $this->mID;
147  $attr['name'] = "{$this->mName}[]";
148 
149  $attr['value'] = $value;
150  $attr['options'] = $this->getOptionsOOUI();
151 
152  if ( $this->mOptionsLabelsNotFromMessage ) {
153  foreach ( $attr['options'] as &$option ) {
154  $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
155  }
156  }
157 
158  $attr += OOUI\Element::configFromHtmlAttributes(
159  $this->getAttributes( [ 'disabled', 'tabindex' ] )
160  );
161 
162  if ( $this->mClass !== '' ) {
163  $attr['classes'] = [ $this->mClass ];
164  }
165 
166  return new OOUI\CheckboxMultiselectInputWidget( $attr );
167  }
168 
174  public function loadDataFromRequest( $request ) {
175  if ( $this->isSubmitAttempt( $request ) ) {
176  // Checkboxes are just not added to the request arrays if they're not checked,
177  // so it's perfectly possible for there not to be an entry at all
178  return $request->getArray( $this->mName, [] );
179  } else {
180  // That's ok, the user has not yet submitted the form, so show the defaults
181  return $this->getDefault();
182  }
183  }
184 
185  public function getDefault() {
186  if ( isset( $this->mDefault ) ) {
187  return $this->mDefault;
188  } else {
189  return [];
190  }
191  }
192 
193  public function filterDataForSubmit( $data ) {
194  $data = HTMLFormField::forceToStringRecursive( $data );
196 
197  $res = [];
198  foreach ( $options as $opt ) {
199  $res["$opt"] = in_array( $opt, $data, true );
200  }
201 
202  return $res;
203  }
204 
205  protected function needsLabel() {
206  return false;
207  }
208 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1053
HTMLMultiSelectField\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLMultiSelectField.php:174
$request
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2612
HTMLMultiSelectField\needsLabel
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
Definition: HTMLMultiSelectField.php:205
HTMLMultiSelectField\getDefault
getDefault()
Definition: HTMLMultiSelectField.php:185
HTMLMultiSelectField\getInputOOUI
getInputOOUI( $value)
Get the OOUI version of this field.
Definition: HTMLMultiSelectField.php:142
$opt
$opt
Definition: postprocess-phan.php:115
captcha-old.count
count
Definition: captcha-old.py:225
$params
$params
Definition: styleTest.css.php:40
$res
$res
Definition: database.txt:21
HTMLFormField\$mClass
$mClass
Definition: HTMLFormField.php:16
php
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
HTMLMultiSelectField\filterDataForSubmit
filterDataForSubmit( $data)
Support for seperating multi-option preferences into multiple preferences Due to lack of array suppor...
Definition: HTMLMultiSelectField.php:193
OOUIHTMLForm
Compact stacked vertical format for forms, implemented using OOUI widgets.
Definition: OOUIHTMLForm.php:27
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:309
HTMLMultiSelectField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLMultiSelectField.php:35
$html
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:1956
MWException
MediaWiki exception.
Definition: MWException.php:26
Xml\check
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition: Xml.php:323
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:7
$attribs
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:1956
HTMLFormField\$mDefault
$mDefault
Definition: HTMLFormField.php:19
HTMLFormField\$mID
$mID
Definition: HTMLFormField.php:15
$value
$value
Definition: styleTest.css.php:45
HTMLFormField\msg
msg()
Get a translated interface message.
Definition: HTMLFormField.php:77
HTMLMultiSelectField\getOneCheckbox
getOneCheckbox( $checked, $attribs, $label)
Definition: HTMLMultiSelectField.php:101
HTMLMultiSelectField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLMultiSelectField.php:58
HTMLNestedFilterable
Definition: HTMLNestedFilterable.php:3
HTMLMultiSelectField
Multi-select field.
Definition: HTMLMultiSelectField.php:6
as
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
Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:251
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
HTMLFormField\forceToStringRecursive
static forceToStringRecursive( $array)
Recursively forces values in an array to strings, because issues arise with integer 0 as a value.
Definition: HTMLFormField.php:1039
HTMLFormField\isSubmitAttempt
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
Definition: HTMLFormField.php:362
HTMLMultiSelectField\formatOptions
formatOptions( $options, $value)
Definition: HTMLMultiSelectField.php:69
HTMLMultiSelectField\__construct
__construct( $params)
Definition: HTMLMultiSelectField.php:17
HTMLFormField\getTooltipAndAccessKey
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey.
Definition: HTMLFormField.php:983
HTMLFormField\flattenOptions
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
Definition: HTMLFormField.php:1126
HTMLMultiSelectField\getOptionsOOUI
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
Definition: HTMLMultiSelectField.php:127
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:997