Merge "Import: Handle uploads with sha1 starting with 0 properly"
[mediawiki.git] / includes / htmlform / HTMLCheckField.php
blob5ef4f79fda6449b0cfcd2b31dbc3e7fa79d30298
1 <?php
3 /**
4 * A checkbox field
5 */
6 class HTMLCheckField extends HTMLFormField {
7 function getInputHTML( $value ) {
8 global $wgUseMediaWikiUIEverywhere;
10 if ( !empty( $this->mParams['invert'] ) ) {
11 $value = !$value;
14 $attr = $this->getTooltipAndAccessKey();
15 $attr['id'] = $this->mID;
17 $attr += $this->getAttributes( array( 'disabled', 'tabindex' ) );
19 if ( $this->mClass !== '' ) {
20 $attr['class'] = $this->mClass;
23 $attrLabel = array( 'for' => $this->mID );
24 if ( isset( $attr['title'] ) ) {
25 // propagate tooltip to label
26 $attrLabel['title'] = $attr['title'];
29 $chkLabel = Xml::check( $this->mName, $value, $attr ) .
30 '&#160;' .
31 Html::rawElement( 'label', $attrLabel, $this->mLabel );
33 if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
34 $chkLabel = Html::rawElement(
35 'div',
36 array( 'class' => 'mw-ui-checkbox' ),
37 $chkLabel
41 return $chkLabel;
44 /**
45 * Get the OOUI version of this field.
46 * @since 1.26
47 * @param string $value
48 * @return OOUI\CheckboxInputWidget The checkbox widget.
50 public function getInputOOUI( $value ) {
51 if ( !empty( $this->mParams['invert'] ) ) {
52 $value = !$value;
55 $attr = $this->getTooltipAndAccessKey();
56 $attr['id'] = $this->mID;
57 $attr['name'] = $this->mName;
59 $attr += $this->getAttributes(
60 array( 'disabled', 'tabindex' ),
61 array( 'tabindex' => 'tabIndex' )
64 if ( $this->mClass !== '' ) {
65 $attr['classes'] = array( $this->mClass );
68 $attr['selected'] = $value;
69 $attr['value'] = '1'; // Nasty hack, but needed to make this work
71 return new OOUI\CheckboxInputWidget( $attr );
74 /**
75 * For a checkbox, the label goes on the right hand side, and is
76 * added in getInputHTML(), rather than HTMLFormField::getRow()
78 * ...unless OOUI is being used, in which case we actually return
79 * the label here.
81 * @return string
83 function getLabel() {
84 if ( $this->mParent instanceof OOUIHTMLForm ) {
85 return $this->mLabel;
86 } elseif (
87 $this->mParent instanceof HTMLForm &&
88 $this->mParent->getDisplayFormat() === 'div'
89 ) {
90 return '';
91 } else {
92 return '&#160;';
96 /**
97 * Get label alignment when generating field for OOUI.
98 * @return string 'left', 'right', 'top' or 'inline'
100 protected function getLabelAlignOOUI() {
101 return 'inline';
105 * checkboxes don't need a label.
106 * @return bool
108 protected function needsLabel() {
109 return false;
113 * @param WebRequest $request
115 * @return string
117 function loadDataFromRequest( $request ) {
118 $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
120 // GetCheck won't work like we want for checks.
121 // Fetch the value in either one of the two following case:
122 // - we have a valid token (form got posted or GET forged by the user)
123 // - checkbox name has a value (false or true), ie is not null
124 if ( $request->getCheck( 'wpEditToken' ) || $request->getVal( $this->mName ) !== null ) {
125 return $invert
126 ? !$request->getBool( $this->mName )
127 : $request->getBool( $this->mName );
128 } else {
129 return $this->getDefault();