Merge "Don't check namespace in SpecialWantedtemplates"
[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 global $wgUseMediaWikiUIEverywhere;
9
10 if ( !empty( $this->mParams['invert'] ) ) {
11 $value = !$value;
12 }
13
14 $attr = $this->getTooltipAndAccessKey();
15 $attr['id'] = $this->mID;
16
17 $attr += $this->getAttributes( array( 'disabled', 'tabindex' ) );
18
19 if ( $this->mClass !== '' ) {
20 $attr['class'] = $this->mClass;
21 }
22
23 $attrLabel = array( 'for' => $this->mID );
24 if ( isset( $attr['title'] ) ) {
25 // propagate tooltip to label
26 $attrLabel['title'] = $attr['title'];
27 }
28
29 $chkLabel = Xml::check( $this->mName, $value, $attr ) .
30 '&#160;' .
31 Html::rawElement( 'label', $attrLabel, $this->mLabel );
32
33 if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
34 $chkLabel = Html::rawElement(
35 'div',
36 array( 'class' => 'mw-ui-checkbox' ),
37 $chkLabel
38 );
39 }
40
41 return $chkLabel;
42 }
43
44 /**
45 * Get the OOUI version of this field.
46 * @since 1.26
47 * @param string $value
48 * @return OOUI\\CheckboxInputWidget The checkbox widget.
49 */
50 public function getInputOOUI( $value ) {
51 if ( !empty( $this->mParams['invert'] ) ) {
52 $value = !$value;
53 }
54
55 $attr = $this->getTooltipAndAccessKey();
56 $attr['id'] = $this->mID;
57 $attr['name'] = $this->mName;
58
59 $attr += $this->getAttributes( array( 'disabled', 'tabindex' ), array( 'tabindex' => 'tabIndex' ) );
60
61 if ( $this->mClass !== '' ) {
62 $attr['classes'] = array( $this->mClass );
63 }
64
65 $attr['selected'] = $value;
66 $attr['value'] = '1'; // Nasty hack, but needed to make this work
67
68 return new OOUI\CheckboxInputWidget( $attr );
69 }
70
71 /**
72 * For a checkbox, the label goes on the right hand side, and is
73 * added in getInputHTML(), rather than HTMLFormField::getRow()
74 *
75 * ...unless OOUI is being used, in which case we actually return
76 * the label here.
77 *
78 * @return string
79 */
80 function getLabel() {
81 if ( $this->mParent instanceof OOUIHTMLForm ) {
82 return $this->mLabel;
83 } elseif (
84 $this->mParent instanceof HTMLForm &&
85 $this->mParent->getDisplayFormat() === 'div'
86 ) {
87 return '';
88 } else {
89 return '&#160;';
90 }
91 }
92
93 /**
94 * Get label alignment when generating field for OOUI.
95 * @return string 'left', 'right', 'top' or 'inline'
96 */
97 protected function getLabelAlignOOUI() {
98 return 'inline';
99 }
100
101 /**
102 * checkboxes don't need a label.
103 * @return bool
104 */
105 protected function needsLabel() {
106 return false;
107 }
108
109 /**
110 * @param WebRequest $request
111 *
112 * @return string
113 */
114 function loadDataFromRequest( $request ) {
115 $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
116
117 // GetCheck won't work like we want for checks.
118 // Fetch the value in either one of the two following case:
119 // - we have a valid token (form got posted or GET forged by the user)
120 // - checkbox name has a value (false or true), ie is not null
121 if ( $request->getCheck( 'wpEditToken' ) || $request->getVal( $this->mName ) !== null ) {
122 return $invert
123 ? !$request->getBool( $this->mName )
124 : $request->getBool( $this->mName );
125 } else {
126 return $this->getDefault();
127 }
128 }
129 }