MediaWiki  1.30.0
HTMLCheckMatrix.php
Go to the documentation of this file.
1 <?php
2 
25  static private $requiredParams = [
26  // Required by underlying HTMLFormField
27  'fieldname',
28  // Required by HTMLCheckMatrix
29  'rows',
30  'columns'
31  ];
32 
33  public function __construct( $params ) {
34  $missing = array_diff( self::$requiredParams, array_keys( $params ) );
35  if ( $missing ) {
36  throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
37  }
38  parent::__construct( $params );
39  }
40 
41  public function validate( $value, $alldata ) {
42  $rows = $this->mParams['rows'];
43  $columns = $this->mParams['columns'];
44 
45  // Make sure user-defined validation callback is run
46  $p = parent::validate( $value, $alldata );
47  if ( $p !== true ) {
48  return $p;
49  }
50 
51  // Make sure submitted value is an array
52  if ( !is_array( $value ) ) {
53  return false;
54  }
55 
56  // If all options are valid, array_intersect of the valid options
57  // and the provided options will return the provided options.
58  $validOptions = [];
59  foreach ( $rows as $rowTag ) {
60  foreach ( $columns as $columnTag ) {
61  $validOptions[] = $columnTag . '-' . $rowTag;
62  }
63  }
64  $validValues = array_intersect( $value, $validOptions );
65  if ( count( $validValues ) == count( $value ) ) {
66  return true;
67  } else {
68  return $this->msg( 'htmlform-select-badoption' );
69  }
70  }
71 
82  public function getInputHTML( $value ) {
83  $html = '';
84  $tableContents = '';
85  $rows = $this->mParams['rows'];
86  $columns = $this->mParams['columns'];
87 
88  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
89 
90  // Build the column headers
91  $headerContents = Html::rawElement( 'td', [], '&#160;' );
92  foreach ( $columns as $columnLabel => $columnTag ) {
93  $headerContents .= Html::rawElement( 'td', [], $columnLabel );
94  }
95  $tableContents .= Html::rawElement( 'tr', [], "\n$headerContents\n" );
96 
97  $tooltipClass = 'mw-icon-question';
98  if ( isset( $this->mParams['tooltip-class'] ) ) {
99  $tooltipClass = $this->mParams['tooltip-class'];
100  }
101 
102  // Build the options matrix
103  foreach ( $rows as $rowLabel => $rowTag ) {
104  // Append tooltip if configured
105  if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
106  $tooltipAttribs = [
107  'class' => "mw-htmlform-tooltip $tooltipClass",
108  'title' => $this->mParams['tooltips'][$rowLabel],
109  ];
110  $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
111  }
112  $rowContents = Html::rawElement( 'td', [], $rowLabel );
113  foreach ( $columns as $columnTag ) {
114  $thisTag = "$columnTag-$rowTag";
115  // Construct the checkbox
116  $thisAttribs = [
117  'id' => "{$this->mID}-$thisTag",
118  'value' => $thisTag,
119  ];
120  $checked = in_array( $thisTag, (array)$value, true );
121  if ( $this->isTagForcedOff( $thisTag ) ) {
122  $checked = false;
123  $thisAttribs['disabled'] = 1;
124  } elseif ( $this->isTagForcedOn( $thisTag ) ) {
125  $checked = true;
126  $thisAttribs['disabled'] = 1;
127  }
128 
129  $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs );
130 
131  $rowContents .= Html::rawElement(
132  'td',
133  [],
134  $checkbox
135  );
136  }
137  $tableContents .= Html::rawElement( 'tr', [], "\n$rowContents\n" );
138  }
139 
140  // Put it all in a table
141  $html .= Html::rawElement( 'table',
142  [ 'class' => 'mw-htmlform-matrix' ],
143  Html::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n";
144 
145  return $html;
146  }
147 
148  protected function getOneCheckbox( $checked, $attribs ) {
149  if ( $this->mParent instanceof OOUIHTMLForm ) {
150  return new OOUI\CheckboxInputWidget( [
151  'name' => "{$this->mName}[]",
152  'selected' => $checked,
153  ] + OOUI\Element::configFromHtmlAttributes(
154  $attribs
155  ) );
156  } else {
157  $checkbox = Xml::check( "{$this->mName}[]", $checked, $attribs );
158  if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
159  $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
160  $checkbox .
161  Html::element( 'label', [ 'for' => $attribs['id'] ] ) .
162  Html::closeElement( 'div' );
163  }
164  return $checkbox;
165  }
166  }
167 
168  protected function isTagForcedOff( $tag ) {
169  return isset( $this->mParams['force-options-off'] )
170  && in_array( $tag, $this->mParams['force-options-off'] );
171  }
172 
173  protected function isTagForcedOn( $tag ) {
174  return isset( $this->mParams['force-options-on'] )
175  && in_array( $tag, $this->mParams['force-options-on'] );
176  }
177 
189  public function getTableRow( $value ) {
190  list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
191  $inputHtml = $this->getInputHTML( $value );
192  $fieldType = static::class;
193  $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
194  $cellAttributes = [ 'colspan' => 2 ];
195 
196  $hideClass = '';
197  $hideAttributes = [];
198  if ( $this->mHideIf ) {
199  $hideAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
200  $hideClass = 'mw-htmlform-hide-if';
201  }
202 
203  $label = $this->getLabelHtml( $cellAttributes );
204 
205  $field = Html::rawElement(
206  'td',
207  [ 'class' => 'mw-input' ] + $cellAttributes,
208  $inputHtml . "\n$errors"
209  );
210 
211  $html = Html::rawElement( 'tr',
212  [ 'class' => "mw-htmlform-vertical-label $hideClass" ] + $hideAttributes,
213  $label );
214  $html .= Html::rawElement( 'tr',
215  [ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $hideClass" ] +
216  $hideAttributes,
217  $field );
218 
219  return $html . $helptext;
220  }
221 
227  public function loadDataFromRequest( $request ) {
228  if ( $this->isSubmitAttempt( $request ) ) {
229  // Checkboxes are just not added to the request arrays if they're not checked,
230  // so it's perfectly possible for there not to be an entry at all
231  return $request->getArray( $this->mName, [] );
232  } else {
233  // That's ok, the user has not yet submitted the form, so show the defaults
234  return $this->getDefault();
235  }
236  }
237 
238  public function getDefault() {
239  if ( isset( $this->mDefault ) ) {
240  return $this->mDefault;
241  } else {
242  return [];
243  }
244  }
245 
246  public function filterDataForSubmit( $data ) {
247  $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
248  $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
249  $res = [];
250  foreach ( $columns as $column ) {
251  foreach ( $rows as $row ) {
252  // Make sure option hasn't been forced
253  $thisTag = "$column-$row";
254  if ( $this->isTagForcedOff( $thisTag ) ) {
255  $res[$thisTag] = false;
256  } elseif ( $this->isTagForcedOn( $thisTag ) ) {
257  $res[$thisTag] = true;
258  } else {
259  $res[$thisTag] = in_array( $thisTag, $data );
260  }
261  }
262  }
263 
264  return $res;
265  }
266 }
captcha-old.count
count
Definition: captcha-old.py:249
HTMLFormField\getErrorsAndErrorClass
getErrorsAndErrorClass( $value)
Determine form errors to display and their classes.
Definition: HTMLFormField.php:862
$params
$params
Definition: styleTest.css.php:40
HTMLCheckMatrix\getDefault
getDefault()
Definition: HTMLCheckMatrix.php:238
$res
$res
Definition: database.txt:21
HTMLCheckMatrix\isTagForcedOff
isTagForcedOff( $tag)
Definition: HTMLCheckMatrix.php:168
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
OOUIHTMLForm
Compact stacked vertical format for forms, implemented using OOUI widgets.
Definition: OOUIHTMLForm.php:27
HTMLFormField\getLabelHtml
getLabelHtml( $cellAttributes=[])
Definition: HTMLFormField.php:933
HTMLFormField\getHelpTextHtmlTable
getHelpTextHtmlTable( $helptext)
Generate help text HTML in table format.
Definition: HTMLFormField.php:763
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:309
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:127
HTMLFormField\getHelpText
getHelpText()
Determine the help text to display.
Definition: HTMLFormField.php:828
$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:1965
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:1965
HTMLFormField\$mDefault
$mDefault
Definition: HTMLFormField.php:19
HTMLCheckMatrix\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLCheckMatrix.php:41
HTMLCheckMatrix\getTableRow
getTableRow( $value)
Get the complete table row for the input, including help text, labels, and whatever.
Definition: HTMLCheckMatrix.php:189
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2581
HTMLCheckMatrix\filterDataForSubmit
filterDataForSubmit( $data)
Support for seperating multi-option preferences into multiple preferences Due to lack of array suppor...
Definition: HTMLCheckMatrix.php:246
$value
$value
Definition: styleTest.css.php:45
HTMLCheckMatrix\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLCheckMatrix.php:227
HTMLFormFieldRequiredOptionsException
Definition: HTMLFormFieldRequiredOptionsException.php:3
HTMLFormField\msg
msg()
Get a translated interface message.
Definition: HTMLFormField.php:77
HTMLCheckMatrix\$requiredParams
static $requiredParams
Definition: HTMLCheckMatrix.php:25
HTMLCheckMatrix\getInputHTML
getInputHTML( $value)
Build a table containing a matrix of checkbox options.
Definition: HTMLCheckMatrix.php:82
HTMLCheckMatrix
A checkbox matrix Operates similarly to HTMLMultiSelectField, but instead of using an array of option...
Definition: HTMLCheckMatrix.php:24
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2581
HTMLNestedFilterable
Definition: HTMLNestedFilterable.php:3
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
HTMLCheckMatrix\__construct
__construct( $params)
Initialise the object.
Definition: HTMLCheckMatrix.php:33
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
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:1110
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
HTMLCheckMatrix\isTagForcedOn
isTagForcedOn( $tag)
Definition: HTMLCheckMatrix.php:173
HTMLCheckMatrix\getOneCheckbox
getOneCheckbox( $checked, $attribs)
Definition: HTMLCheckMatrix.php:148
array
the array() calling protocol came about after MediaWiki 1.4rc1.
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:1015