File: HTMLCheckField.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (204 lines) | stat: -rw-r--r-- 5,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php

namespace MediaWiki\HTMLForm\Field;

use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\HTMLForm\HTMLFormField;
use MediaWiki\HTMLForm\OOUIHTMLForm;
use MediaWiki\HTMLForm\VFormHTMLForm;
use MediaWiki\Request\WebRequest;
use MediaWiki\Xml\Xml;

/**
 * A checkbox field
 *
 * @stable to extend
 */
class HTMLCheckField extends HTMLFormField {

	/**
	 * @inheritDoc
	 * @stable to override
	 */
	public function getInputHTML( $value ) {
		if ( !empty( $this->mParams['invert'] ) ) {
			$value = !$value;
		}

		$attr = $this->getTooltipAndAccessKey();
		$attr['id'] = $this->mID;

		$attr += $this->getAttributes( [ 'disabled', 'tabindex' ] );

		if ( $this->mClass !== '' ) {
			$attr['class'] = $this->mClass;
		}

		$attrLabel = [ 'for' => $this->mID ];
		if ( isset( $attr['title'] ) ) {
			// propagate tooltip to label
			$attrLabel['title'] = $attr['title'];
		}

		$isVForm = $this->mParent instanceof VFormHTMLForm;

		$chkDivider = "\u{00A0}";
		$chkLabel = Xml::check( $this->mName, $value, $attr ) .
			$chkDivider .
			Html::rawElement( 'label', $attrLabel, $this->mLabel );

		if ( $isVForm ) {
			$chkLabelClass = 'mw-ui-checkbox';
			$chkLabel = Html::rawElement(
				'div',
				[ 'class' => $chkLabelClass ],
				$chkLabel
			);
		}

		return $chkLabel;
	}

	/**
	 * Get the OOUI version of this field.
	 * @stable to override
	 * @since 1.26
	 * @param string $value
	 * @return \OOUI\CheckboxInputWidget The checkbox widget.
	 */
	public function getInputOOUI( $value ) {
		if ( !empty( $this->mParams['invert'] ) ) {
			$value = !$value;
		}

		$attr = $this->getTooltipAndAccessKeyOOUI();
		$attr['id'] = $this->mID;
		$attr['name'] = $this->mName;

		$attr += \OOUI\Element::configFromHtmlAttributes(
			$this->getAttributes( [ 'disabled', 'tabindex' ] )
		);

		if ( $this->mClass !== '' ) {
			$attr['classes'] = [ $this->mClass ];
		}

		$attr['selected'] = $value;
		$attr['value'] = '1'; // Nasty hack, but needed to make this work

		return new \OOUI\CheckboxInputWidget( $attr );
	}

	public function getInputCodex( $value, $hasErrors ) {
		if ( !empty( $this->mParams['invert'] ) ) {
			$value = !$value;
		}

		// Attributes for the <input> element.
		$attribs = $this->getTooltipAndAccessKey();
		$attribs['id'] = $this->mID;
		$attribs += $this->getAttributes( [ 'disabled', 'tabindex' ] );

		// The Xml class doesn't support an array of classes, so we have to provide a string.
		$inputClass = $this->mClass ?? '';
		$attribs['class'] = $inputClass . ' cdx-checkbox__input';

		// Attributes for the <label> element.
		$labelAttribs = [ 'for' => $this->mID ];
		$labelAttribs['class'] = [ 'cdx-checkbox__label' ];

		// Attributes for the wrapper <div>.
		$wrapperAttribs = [ 'class' => [ 'cdx-checkbox' ] ];
		if ( $hasErrors ) {
			$wrapperAttribs['class'][] = 'cdx-checkbox--status-error';
		}
		if ( isset( $attribs['title'] ) ) {
			// Propagate tooltip to the entire component (including the label).
			$wrapperAttribs['title'] = $attribs['title'];
		}

		// Construct the component.
		$checkIcon = "<span class=\"cdx-checkbox__icon\">\u{00A0}</span>";
		$innerContent = Xml::check( $this->mName, $value, $attribs ) .
			$checkIcon .
			Html::rawElement( 'label', $labelAttribs, $this->mLabel );
		return Html::rawElement(
			'div',
			$wrapperAttribs,
			$innerContent
		);
	}

	/**
	 * For a checkbox, the label goes on the right hand side, and is
	 * added in getInputHTML(), rather than HTMLFormField::getRow()
	 *
	 * ...unless OOUI is being used, in which case we actually return
	 * the label here.
	 *
	 * @stable to override
	 * @return string
	 */
	public function getLabel() {
		if ( $this->mParent instanceof OOUIHTMLForm ) {
			return $this->mLabel ?? '';
		} elseif (
			$this->mParent instanceof HTMLForm &&
			$this->mParent->getDisplayFormat() === 'div'
		) {
			return '';
		} else {
			return "\u{00A0}";
		}
	}

	/**
	 * Get label alignment when generating field for OOUI.
	 * @stable to override
	 * @return string 'left', 'right', 'top' or 'inline'
	 */
	protected function getLabelAlignOOUI() {
		return 'inline';
	}

	/**
	 * checkboxes don't need a label.
	 * @stable to override
	 * @return bool
	 */
	protected function needsLabel() {
		return false;
	}

	/**
	 * @return bool
	 */
	public function getDefault() {
		return (bool)$this->mDefault;
	}

	/**
	 * @stable to override
	 * @param WebRequest $request
	 *
	 * @return bool
	 */
	public function loadDataFromRequest( $request ) {
		$invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];

		// Fetch the value in either one of the two following case:
		// - we have a valid submit attempt (form was just submitted)
		// - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
		if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
			return $invert
				? !$request->getBool( $this->mName )
				: $request->getBool( $this->mName );
		} else {
			return $this->getDefault();
		}
	}
}

/** @deprecated class alias since 1.42 */
class_alias( HTMLCheckField::class, 'HTMLCheckField' );