2013-11-19 12:55:50 +00:00
|
|
|
<?php
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2024-02-08 15:30:18 +00:00
|
|
|
namespace MediaWiki\HTMLForm\Field;
|
|
|
|
|
|
2023-02-16 19:27:21 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2024-02-08 15:30:18 +00:00
|
|
|
use MediaWiki\HTMLForm\HTMLForm;
|
|
|
|
|
use MediaWiki\HTMLForm\HTMLFormField;
|
|
|
|
|
use MediaWiki\HTMLForm\OOUIHTMLForm;
|
|
|
|
|
use MediaWiki\HTMLForm\VFormHTMLForm;
|
2023-09-07 11:46:15 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2024-05-16 10:52:03 +00:00
|
|
|
use MediaWiki\Xml\Xml;
|
2022-01-06 18:44:56 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
|
|
|
|
* A checkbox field
|
2020-07-10 19:23:59 +00:00
|
|
|
*
|
|
|
|
|
* @stable to extend
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
class HTMLCheckField extends HTMLFormField {
|
2020-07-10 19:23:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
* @stable to override
|
|
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getInputHTML( $value ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( !empty( $this->mParams['invert'] ) ) {
|
|
|
|
|
$value = !$value;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attr = $this->getTooltipAndAccessKey();
|
2013-11-19 13:08:16 +00:00
|
|
|
$attr['id'] = $this->mID;
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$attr += $this->getAttributes( [ 'disabled', 'tabindex' ] );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$attr['class'] = $this->mClass;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$attrLabel = [ 'for' => $this->mID ];
|
2015-06-19 15:45:23 +00:00
|
|
|
if ( isset( $attr['title'] ) ) {
|
|
|
|
|
// propagate tooltip to label
|
|
|
|
|
$attrLabel['title'] = $attr['title'];
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 19:38:08 +00:00
|
|
|
$isVForm = $this->mParent instanceof VFormHTMLForm;
|
|
|
|
|
|
2024-03-06 14:43:50 +00:00
|
|
|
$chkDivider = "\u{00A0}";
|
2015-06-19 18:52:43 +00:00
|
|
|
$chkLabel = Xml::check( $this->mName, $value, $attr ) .
|
2023-06-23 19:38:08 +00:00
|
|
|
$chkDivider .
|
2015-06-19 18:52:43 +00:00
|
|
|
Html::rawElement( 'label', $attrLabel, $this->mLabel );
|
2014-12-24 17:21:32 +00:00
|
|
|
|
2024-03-06 14:43:50 +00:00
|
|
|
if ( $isVForm ) {
|
|
|
|
|
$chkLabelClass = 'mw-ui-checkbox';
|
2014-12-24 17:21:32 +00:00
|
|
|
$chkLabel = Html::rawElement(
|
|
|
|
|
'div',
|
2023-06-23 19:38:08 +00:00
|
|
|
[ 'class' => $chkLabelClass ],
|
2014-12-24 17:21:32 +00:00
|
|
|
$chkLabel
|
|
|
|
|
);
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
2014-12-24 17:21:32 +00:00
|
|
|
|
|
|
|
|
return $chkLabel;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-21 21:03:49 +00:00
|
|
|
/**
|
|
|
|
|
* Get the OOUI version of this field.
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to override
|
2015-04-21 21:03:49 +00:00
|
|
|
* @since 1.26
|
|
|
|
|
* @param string $value
|
2024-02-08 15:30:18 +00:00
|
|
|
* @return \OOUI\CheckboxInputWidget The checkbox widget.
|
2015-04-21 21:03:49 +00:00
|
|
|
*/
|
|
|
|
|
public function getInputOOUI( $value ) {
|
|
|
|
|
if ( !empty( $this->mParams['invert'] ) ) {
|
|
|
|
|
$value = !$value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 19:30:03 +00:00
|
|
|
$attr = $this->getTooltipAndAccessKeyOOUI();
|
2015-04-21 21:03:49 +00:00
|
|
|
$attr['id'] = $this->mID;
|
|
|
|
|
$attr['name'] = $this->mName;
|
|
|
|
|
|
2024-02-08 15:30:18 +00:00
|
|
|
$attr += \OOUI\Element::configFromHtmlAttributes(
|
2016-03-02 19:09:53 +00:00
|
|
|
$this->getAttributes( [ 'disabled', 'tabindex' ] )
|
2015-09-28 11:35:28 +00:00
|
|
|
);
|
2015-04-21 21:03:49 +00:00
|
|
|
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$attr['classes'] = [ $this->mClass ];
|
2015-04-21 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attr['selected'] = $value;
|
|
|
|
|
$attr['value'] = '1'; // Nasty hack, but needed to make this work
|
|
|
|
|
|
2024-02-08 15:30:18 +00:00
|
|
|
return new \OOUI\CheckboxInputWidget( $attr );
|
2015-04-21 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-06 14:43:50 +00:00
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
|
|
|
|
* For a checkbox, the label goes on the right hand side, and is
|
|
|
|
|
* added in getInputHTML(), rather than HTMLFormField::getRow()
|
2015-04-21 21:03:49 +00:00
|
|
|
*
|
|
|
|
|
* ...unless OOUI is being used, in which case we actually return
|
|
|
|
|
* the label here.
|
|
|
|
|
*
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to override
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function getLabel() {
|
2015-04-21 21:03:49 +00:00
|
|
|
if ( $this->mParent instanceof OOUIHTMLForm ) {
|
2023-06-02 19:17:26 +00:00
|
|
|
return $this->mLabel ?? '';
|
2015-04-17 16:56:32 +00:00
|
|
|
} elseif (
|
|
|
|
|
$this->mParent instanceof HTMLForm &&
|
|
|
|
|
$this->mParent->getDisplayFormat() === 'div'
|
|
|
|
|
) {
|
|
|
|
|
return '';
|
2015-04-21 21:03:49 +00:00
|
|
|
} else {
|
2016-12-27 21:14:16 +00:00
|
|
|
return "\u{00A0}";
|
2015-04-21 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get label alignment when generating field for OOUI.
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to override
|
2015-04-21 21:03:49 +00:00
|
|
|
* @return string 'left', 'right', 'top' or 'inline'
|
|
|
|
|
*/
|
|
|
|
|
protected function getLabelAlignOOUI() {
|
|
|
|
|
return 'inline';
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* checkboxes don't need a label.
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to override
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return bool
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
protected function needsLabel() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-25 11:56:52 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function getDefault() {
|
|
|
|
|
return (bool)$this->mDefault;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to override
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param WebRequest $request
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2016-04-23 16:27:02 +00:00
|
|
|
* @return bool
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
2016-11-04 10:40:42 +00:00
|
|
|
public function loadDataFromRequest( $request ) {
|
2015-02-14 07:16:43 +00:00
|
|
|
$invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
// Fetch the value in either one of the two following case:
|
2018-07-18 18:09:39 +00:00
|
|
|
// - 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)
|
2019-03-11 08:40:36 +00:00
|
|
|
if ( $this->isSubmitAttempt( $request ) || $request->getCheck( $this->mName ) ) {
|
2015-02-14 07:16:43 +00:00
|
|
|
return $invert
|
|
|
|
|
? !$request->getBool( $this->mName )
|
|
|
|
|
: $request->getBool( $this->mName );
|
2013-11-19 12:55:50 +00:00
|
|
|
} else {
|
2022-12-25 11:56:52 +00:00
|
|
|
return $this->getDefault();
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|
2024-02-08 15:30:18 +00:00
|
|
|
|
2024-03-07 21:56:58 +00:00
|
|
|
/** @deprecated class alias since 1.42 */
|
2024-02-08 15:30:18 +00:00
|
|
|
class_alias( HTMLCheckField::class, 'HTMLCheckField' );
|