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