MediaWiki  1.23.2
HTMLCheckMatrix.php
Go to the documentation of this file.
1 <?php
2 
25  static private $requiredParams = array(
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  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 = array();
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' )->parse();
69  }
70  }
71 
82  function getInputHTML( $value ) {
83  $html = '';
84  $tableContents = '';
85  $rows = $this->mParams['rows'];
86  $columns = $this->mParams['columns'];
87 
88  $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
89 
90  // Build the column headers
91  $headerContents = Html::rawElement( 'td', array(), '&#160;' );
92  foreach ( $columns as $columnLabel => $columnTag ) {
93  $headerContents .= Html::rawElement( 'td', array(), $columnLabel );
94  }
95  $tableContents .= Html::rawElement( 'tr', array(), "\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 = array(
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', array(), $rowLabel );
113  foreach ( $columns as $columnTag ) {
114  $thisTag = "$columnTag-$rowTag";
115  // Construct the checkbox
116  $thisAttribs = array(
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  $rowContents .= Html::rawElement(
129  'td',
130  array(),
131  Xml::check( "{$this->mName}[]", $checked, $attribs + $thisAttribs )
132  );
133  }
134  $tableContents .= Html::rawElement( 'tr', array(), "\n$rowContents\n" );
135  }
136 
137  // Put it all in a table
138  $html .= Html::rawElement( 'table',
139  array( 'class' => 'mw-htmlform-matrix' ),
140  Html::rawElement( 'tbody', array(), "\n$tableContents\n" ) ) . "\n";
141 
142  return $html;
143  }
144 
145  protected function isTagForcedOff( $tag ) {
146  return isset( $this->mParams['force-options-off'] )
147  && in_array( $tag, $this->mParams['force-options-off'] );
148  }
149 
150  protected function isTagForcedOn( $tag ) {
151  return isset( $this->mParams['force-options-on'] )
152  && in_array( $tag, $this->mParams['force-options-on'] );
153  }
154 
166  function getTableRow( $value ) {
167  list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
168  $inputHtml = $this->getInputHTML( $value );
169  $fieldType = get_class( $this );
170  $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
171  $cellAttributes = array( 'colspan' => 2 );
172 
173  $label = $this->getLabelHtml( $cellAttributes );
174 
175  $field = Html::rawElement(
176  'td',
177  array( 'class' => 'mw-input' ) + $cellAttributes,
178  $inputHtml . "\n$errors"
179  );
180 
181  $html = Html::rawElement( 'tr', array( 'class' => 'mw-htmlform-vertical-label' ), $label );
182  $html .= Html::rawElement( 'tr',
183  array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
184  $field );
185 
186  return $html . $helptext;
187  }
188 
194  function loadDataFromRequest( $request ) {
195  if ( $this->mParent->getMethod() == 'post' ) {
196  if ( $request->wasPosted() ) {
197  // Checkboxes are not added to the request arrays if they're not checked,
198  // so it's perfectly possible for there not to be an entry at all
199  return $request->getArray( $this->mName, array() );
200  } else {
201  // That's ok, the user has not yet submitted the form, so show the defaults
202  return $this->getDefault();
203  }
204  } else {
205  // This is the impossible case: if we look at $_GET and see no data for our
206  // field, is it because the user has not yet submitted the form, or that they
207  // have submitted it with all the options unchecked. We will have to assume the
208  // latter, which basically means that you can't specify 'positive' defaults
209  // for GET forms.
210  return $request->getArray( $this->mName, array() );
211  }
212  }
213 
214  function getDefault() {
215  if ( isset( $this->mDefault ) ) {
216  return $this->mDefault;
217  } else {
218  return array();
219  }
220  }
221 
222  function filterDataForSubmit( $data ) {
223  $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
224  $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
225  $res = array();
226  foreach ( $columns as $column ) {
227  foreach ( $rows as $row ) {
228  // Make sure option hasn't been forced
229  $thisTag = "$column-$row";
230  if ( $this->isTagForcedOff( $thisTag ) ) {
231  $res[$thisTag] = false;
232  } elseif ( $this->isTagForcedOn( $thisTag ) ) {
233  $res[$thisTag] = true;
234  } else {
235  $res[$thisTag] = in_array( $thisTag, $data );
236  }
237  }
238  }
239 
240  return $res;
241  }
242 }
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$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:1530
HTMLFormField\getErrorsAndErrorClass
getErrorsAndErrorClass( $value)
Determine form errors to display and their classes.
Definition: HTMLFormField.php:409
$params
$params
Definition: styleTest.css.php:40
HTMLCheckMatrix\getDefault
getDefault()
Definition: HTMLCheckMatrix.php:214
HTMLCheckMatrix\isTagForcedOff
isTagForcedOff( $tag)
Definition: HTMLCheckMatrix.php:145
HTMLFormField\getHelpTextHtmlTable
getHelpTextHtmlTable( $helptext)
Generate help text HTML in table format.
Definition: HTMLFormField.php:329
HTMLFormField\getHelpText
getHelpText()
Determine the help text to display.
Definition: HTMLFormField.php:374
Html\element
static element( $element, $attribs=array(), $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:148
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:7
HTMLFormField\$mDefault
$mDefault
Definition: HTMLFormField.php:16
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
HTMLCheckMatrix\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLCheckMatrix.php:41
$columns
if(! $in) $columns
Definition: Utf8Test.php:50
HTMLCheckMatrix\getTableRow
getTableRow( $value)
Get the complete table row for the input, including help text, labels, and whatever.
Definition: HTMLCheckMatrix.php:166
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
HTMLCheckMatrix\filterDataForSubmit
filterDataForSubmit( $data)
Support for seperating multi-option preferences into multiple preferences Due to lack of array suppor...
Definition: HTMLCheckMatrix.php:222
HTMLFormField\getLabelHtml
getLabelHtml( $cellAttributes=array())
Definition: HTMLFormField.php:429
$value
$value
Definition: styleTest.css.php:45
Xml\check
static check( $name, $checked=false, $attribs=array())
Convenience function to build an HTML checkbox.
Definition: Xml.php:339
HTMLCheckMatrix\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLCheckMatrix.php:194
HTMLFormFieldRequiredOptionsException
Definition: HTMLFormFieldRequiredOptionsException.php:3
HTMLFormField\msg
msg()
Get a translated interface message.
Definition: HTMLFormField.php:51
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
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
HTMLCheckMatrix\__construct
__construct( $params)
Initialise the object.
Definition: HTMLCheckMatrix.php:33
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:601
Html\rawElement
static rawElement( $element, $attribs=array(), $contents='')
Returns an HTML element in a string.
Definition: Html.php:124
$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:1530
HTMLCheckMatrix\isTagForcedOn
isTagForcedOn( $tag)
Definition: HTMLCheckMatrix.php:150
$res
$res
Definition: database.txt:21
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:493