Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / htmlform / fields / HTMLCheckField.php
blobb080e183ff58c3b8ccaa5b508de985e742987635
1 <?php
3 /**
4 * A checkbox field
5 */
6 class HTMLCheckField extends HTMLFormField {
7 public 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( [ 'disabled', 'tabindex' ] );
19 if ( $this->mClass !== '' ) {
20 $attr['class'] = $this->mClass;
23 $attrLabel = [ '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 [ '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 += OOUI\Element::configFromHtmlAttributes(
60 $this->getAttributes( [ 'disabled', 'tabindex' ] )
63 if ( $this->mClass !== '' ) {
64 $attr['classes'] = [ $this->mClass ];
67 $attr['selected'] = $value;
68 $attr['value'] = '1'; // Nasty hack, but needed to make this work
70 return new OOUI\CheckboxInputWidget( $attr );
73 /**
74 * For a checkbox, the label goes on the right hand side, and is
75 * added in getInputHTML(), rather than HTMLFormField::getRow()
77 * ...unless OOUI is being used, in which case we actually return
78 * the label here.
80 * @return string
82 public function getLabel() {
83 if ( $this->mParent instanceof OOUIHTMLForm ) {
84 return $this->mLabel;
85 } elseif (
86 $this->mParent instanceof HTMLForm &&
87 $this->mParent->getDisplayFormat() === 'div'
88 ) {
89 return '';
90 } else {
91 return '&#160;';
95 /**
96 * Get label alignment when generating field for OOUI.
97 * @return string 'left', 'right', 'top' or 'inline'
99 protected function getLabelAlignOOUI() {
100 return 'inline';
104 * checkboxes don't need a label.
105 * @return bool
107 protected function needsLabel() {
108 return false;
112 * @param WebRequest $request
114 * @return bool
116 public function loadDataFromRequest( $request ) {
117 $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
119 // GetCheck won't work like we want for checks.
120 // Fetch the value in either one of the two following case:
121 // - we have a valid submit attempt (form was just submitted, or a GET URL forged by the user)
122 // - checkbox name has a value (false or true), ie is not null
123 if ( $this->isSubmitAttempt( $request ) || $request->getVal( $this->mName ) !== null ) {
124 return $invert
125 ? !$request->getBool( $this->mName )
126 : $request->getBool( $this->mName );
127 } else {
128 return (bool)$this->getDefault();