Add PreferencesFormPreSave hook
[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 if ( !empty( $this->mParams['disabled'] ) ) {
16 $attr['disabled'] = 'disabled';
17 }
18
19 if ( $this->mClass !== '' ) {
20 $attr['class'] = $this->mClass;
21 }
22
23 if ( $this->mParent->isVForm() ) {
24 // Nest checkbox inside label.
25 return Html::rawElement( 'label',
26 array(
27 'class' => 'mw-ui-checkbox-label'
28 ),
29 Xml::check( $this->mName, $value, $attr ) . // Html:rawElement doesn't escape contents.
30 htmlspecialchars( $this->mLabel ) );
31 } else {
32 return Xml::check( $this->mName, $value, $attr )
33 . '&#160;'
34 . Html::rawElement( 'label', array( 'for' => $this->mID ), $this->mLabel );
35 }
36 }
37
38 /**
39 * For a checkbox, the label goes on the right hand side, and is
40 * added in getInputHTML(), rather than HTMLFormField::getRow()
41 * @return String
42 */
43 function getLabel() {
44 return '&#160;';
45 }
46
47 /**
48 * checkboxes don't need a label.
49 */
50 protected function needsLabel() {
51 return false;
52 }
53
54 /**
55 * @param $request WebRequest
56 *
57 * @return String
58 */
59 function loadDataFromRequest( $request ) {
60 $invert = false;
61 if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
62 $invert = true;
63 }
64
65 // GetCheck won't work like we want for checks.
66 // Fetch the value in either one of the two following case:
67 // - we have a valid token (form got posted or GET forged by the user)
68 // - checkbox name has a value (false or true), ie is not null
69 if ( $request->getCheck( 'wpEditToken' ) || $request->getVal( $this->mName ) !== null ) {
70 // XOR has the following truth table, which is what we want
71 // INVERT VALUE | OUTPUT
72 // true true | false
73 // false true | true
74 // false false | false
75 // true false | true
76 return $request->getBool( $this->mName ) xor $invert;
77 } else {
78 return $this->getDefault();
79 }
80 }
81 }