| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm\Field; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\HTMLForm\HTMLForm; |
| 7 |
use MediaWiki\HTMLForm\HTMLFormField; |
| 8 |
use MediaWiki\HTMLForm\OOUIHTMLForm; |
| 9 |
use MediaWiki\HTMLForm\VFormHTMLForm; |
| 10 |
use MediaWiki\Request\WebRequest; |
| 11 |
use MediaWiki\Xml\Xml; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class HTMLCheckField extends HTMLFormField { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function getInputHTML( $value ) { |
| 25 |
if ( !empty( $this->mParams['invert'] ) ) { |
| 26 |
$value = !$value; |
| 27 |
} |
| 28 |
|
| 29 |
$attr = $this->getTooltipAndAccessKey(); |
| 30 |
$attr['id'] = $this->mID; |
| 31 |
|
| 32 |
$attr += $this->getAttributes( [ 'disabled', 'tabindex' ] ); |
| 33 |
|
| 34 |
if ( $this->mClass !== '' ) { |
| 35 |
$attr['class'] = $this->mClass; |
| 36 |
} |
| 37 |
|
| 38 |
$attrLabel = [ 'for' => $this->mID ]; |
| 39 |
if ( isset( $attr['title'] ) ) { |
| 40 |
|
| 41 |
$attrLabel['title'] = $attr['title']; |
| 42 |
} |
| 43 |
|
| 44 |
$isVForm = $this->mParent instanceof VFormHTMLForm; |
| 45 |
|
| 46 |
$chkDivider = "\u{00A0}"; |
| 47 |
$chkLabel = Xml::check( $this->mName, $value, $attr ) . |
| 48 |
$chkDivider . |
| 49 |
Html::rawElement( 'label', $attrLabel, $this->mLabel ); |
| 50 |
|
| 51 |
if ( $isVForm ) { |
| 52 |
$chkLabelClass = 'mw-ui-checkbox'; |
| 53 |
$chkLabel = Html::rawElement( |
| 54 |
'div', |
| 55 |
[ 'class' => $chkLabelClass ], |
| 56 |
$chkLabel |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
return $chkLabel; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function getInputOOUI( $value ) { |
| 71 |
if ( !empty( $this->mParams['invert'] ) ) { |
| 72 |
$value = !$value; |
| 73 |
} |
| 74 |
|
| 75 |
$attr = $this->getTooltipAndAccessKeyOOUI(); |
| 76 |
$attr['id'] = $this->mID; |
| 77 |
$attr['name'] = $this->mName; |
| 78 |
|
| 79 |
$attr += \OOUI\Element::configFromHtmlAttributes( |
| 80 |
$this->getAttributes( [ 'disabled', 'tabindex' ] ) |
| 81 |
); |
| 82 |
|
| 83 |
if ( $this->mClass !== '' ) { |
| 84 |
$attr['classes'] = [ $this->mClass ]; |
| 85 |
} |
| 86 |
|
| 87 |
$attr['selected'] = $value; |
| 88 |
$attr['value'] = '1'; |
| 89 |
|
| 90 |
return new \OOUI\CheckboxInputWidget( $attr ); |
| 91 |
} |
| 92 |
|
| 93 |
public function getInputCodex( $value, $hasErrors ) { |
| 94 |
if ( !empty( $this->mParams['invert'] ) ) { |
| 95 |
$value = !$value; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
$attribs = $this->getTooltipAndAccessKey(); |
| 100 |
$attribs['id'] = $this->mID; |
| 101 |
$attribs += $this->getAttributes( [ 'disabled', 'tabindex' ] ); |
| 102 |
|
| 103 |
|
| 104 |
$inputClass = $this->mClass ?? ''; |
| 105 |
$attribs['class'] = $inputClass . ' cdx-checkbox__input'; |
| 106 |
|
| 107 |
|
| 108 |
$labelAttribs = [ 'for' => $this->mID ]; |
| 109 |
$labelAttribs['class'] = [ 'cdx-checkbox__label' ]; |
| 110 |
|
| 111 |
|
| 112 |
$wrapperAttribs = [ 'class' => [ 'cdx-checkbox' ] ]; |
| 113 |
if ( $hasErrors ) { |
| 114 |
$wrapperAttribs['class'][] = 'cdx-checkbox--status-error'; |
| 115 |
} |
| 116 |
if ( isset( $attribs['title'] ) ) { |
| 117 |
|
| 118 |
$wrapperAttribs['title'] = $attribs['title']; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
$checkIcon = "<span class=\"cdx-checkbox__icon\">\u{00A0}</span>"; |
| 123 |
$innerContent = Xml::check( $this->mName, $value, $attribs ) . |
| 124 |
$checkIcon . |
| 125 |
Html::rawElement( 'label', $labelAttribs, $this->mLabel ); |
| 126 |
return Html::rawElement( |
| 127 |
'div', |
| 128 |
$wrapperAttribs, |
| 129 |
$innerContent |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function getLabel() { |
| 144 |
if ( $this->mParent instanceof OOUIHTMLForm ) { |
| 145 |
return $this->mLabel ?? ''; |
| 146 |
} elseif ( |
| 147 |
$this->mParent instanceof HTMLForm && |
| 148 |
$this->mParent->getDisplayFormat() === 'div' |
| 149 |
) { |
| 150 |
return ''; |
| 151 |
} else { |
| 152 |
return "\u{00A0}"; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
protected function getLabelAlignOOUI() { |
| 162 |
return 'inline'; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
protected function needsLabel() { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function getDefault() { |
| 178 |
return (bool)$this->mDefault; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function loadDataFromRequest( $request ) { |
| 188 |
$invert = isset( $this->mParams['invert'] ) && $this->mParams['invert']; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) { |
| 194 |
return $invert |
| 195 |
? !$request->getBool( $this->mName ) |
| 196 |
: $request->getBool( $this->mName ); |
| 197 |
} else { |
| 198 |
return $this->getDefault(); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
class_alias( HTMLCheckField::class, 'HTMLCheckField' ); |