2013-11-19 12:55:50 +00:00
|
|
|
<?php
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2022-04-25 15:19:41 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
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 ) {
|
2022-04-29 19:04:55 +00:00
|
|
|
$useMediaWikiUIEverywhere = $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere );
|
2014-08-24 01:12:26 +00:00
|
|
|
|
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'];
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-19 18:52:43 +00:00
|
|
|
$chkLabel = Xml::check( $this->mName, $value, $attr ) .
|
2016-12-27 21:14:16 +00:00
|
|
|
"\u{00A0}" .
|
2015-06-19 18:52:43 +00:00
|
|
|
Html::rawElement( 'label', $attrLabel, $this->mLabel );
|
2014-12-24 17:21:32 +00:00
|
|
|
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( $useMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
|
2014-12-24 17:21:32 +00:00
|
|
|
$chkLabel = Html::rawElement(
|
|
|
|
|
'div',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'class' => 'mw-ui-checkbox' ],
|
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
|
2015-11-23 22:53:38 +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;
|
|
|
|
|
|
2016-03-02 19:09:53 +00:00
|
|
|
$attr += OOUI\Element::configFromHtmlAttributes(
|
|
|
|
|
$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
|
|
|
|
|
|
|
|
|
|
return new OOUI\CheckboxInputWidget( $attr );
|
|
|
|
|
}
|
|
|
|
|
|
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 ) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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 {
|
2016-04-23 16:27:02 +00:00
|
|
|
return (bool)$this->getDefault();
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|