Merge "Add blanket support for mediawiki ui via globals"
[lhc/web/wiklou.git] / includes / htmlform / HTMLCheckField.php
1 <?php
2
3 /**
4 * A checkbox field
5 */
6 class HTMLCheckField extends HTMLFormField {
7 function getInputHTML( $value ) {
8 if ( !empty( $this->mParams['invert'] ) ) {
9 $value = !$value;
10 }
11
12 $attr = $this->getTooltipAndAccessKey();
13 $attr['id'] = $this->mID;
14
15 $attr += $this->getAttributes( array( 'disabled', 'tabindex' ) );
16
17 if ( $this->mClass !== '' ) {
18 $attr['class'] = $this->mClass;
19 }
20
21 if ( $this->mParent->isVForm() ) {
22 // Nest checkbox inside label.
23 return Html::rawElement( 'label',
24 array(
25 'class' => 'mw-ui-checkbox-label'
26 ),
27 Xml::check( $this->mName, $value, $attr ) . $this->mLabel );
28 } else {
29 return Xml::checkLabel( $this->mLabel, $this->mName, $this->mID, $value, $attr );
30 }
31 }
32
33 /**
34 * For a checkbox, the label goes on the right hand side, and is
35 * added in getInputHTML(), rather than HTMLFormField::getRow()
36 * @return string
37 */
38 function getLabel() {
39 return '&#160;';
40 }
41
42 /**
43 * checkboxes don't need a label.
44 * @return bool
45 */
46 protected function needsLabel() {
47 return false;
48 }
49
50 /**
51 * @param WebRequest $request
52 *
53 * @return string
54 */
55 function loadDataFromRequest( $request ) {
56 $invert = false;
57 if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
58 $invert = true;
59 }
60
61 // GetCheck won't work like we want for checks.
62 // Fetch the value in either one of the two following case:
63 // - we have a valid token (form got posted or GET forged by the user)
64 // - checkbox name has a value (false or true), ie is not null
65 if ( $request->getCheck( 'wpEditToken' ) || $request->getVal( $this->mName ) !== null ) {
66 // XOR has the following truth table, which is what we want
67 // INVERT VALUE | OUTPUT
68 // true true | false
69 // false true | true
70 // false false | false
71 // true false | true
72 return $request->getBool( $this->mName ) xor $invert;
73 } else {
74 return $this->getDefault();
75 }
76 }
77 }