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\HTMLFormField;
|
|
|
|
|
use MediaWiki\HTMLForm\VFormHTMLForm;
|
2024-06-16 18:26:43 +00:00
|
|
|
use MediaWiki\Message\Message;
|
2022-04-25 15:19:41 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
|
|
|
|
* Adds a generic button inline to the form. Does not do anything, you must add
|
|
|
|
|
* click handling code in JavaScript. Use a HTMLSubmitField if you merely
|
|
|
|
|
* wish to add a submit button to a form.
|
|
|
|
|
*
|
2015-12-15 22:12:34 +00:00
|
|
|
* Additional recognized configuration parameters include:
|
2016-04-06 22:22:33 +00:00
|
|
|
* - flags: OOUI flags for the button, see OOUI\FlaggedElement
|
2015-12-15 22:12:34 +00:00
|
|
|
* - buttonlabel-message: Message to use for the button display text, instead
|
|
|
|
|
* of the value from 'default'. Overrides 'buttonlabel' and 'buttonlabel-raw'.
|
|
|
|
|
* - buttonlabel: Text to display for the button display text, instead
|
|
|
|
|
* of the value from 'default'. Overrides 'buttonlabel-raw'.
|
|
|
|
|
* - buttonlabel-raw: HTMLto display for the button display text, instead
|
|
|
|
|
* of the value from 'default'.
|
2016-12-02 17:28:30 +00:00
|
|
|
* - formnovalidate: Set to true if clicking this button should suppress
|
|
|
|
|
* client-side form validation. Used in HTMLFormFieldCloner for add/remove
|
|
|
|
|
* buttons.
|
2015-12-15 22:12:34 +00:00
|
|
|
*
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to extend
|
2013-11-19 12:55:50 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
class HTMLButtonField extends HTMLFormField {
|
2024-09-13 20:19:49 +00:00
|
|
|
/** @var string */
|
2013-11-19 12:55:50 +00:00
|
|
|
protected $buttonType = 'button';
|
2024-09-13 20:19:49 +00:00
|
|
|
/** @var string|null */
|
2015-12-15 22:12:34 +00:00
|
|
|
protected $buttonLabel = null;
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2020-04-07 21:38:17 +00:00
|
|
|
/** @var array Flags to add to OOUI Button widget */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $mFlags = [];
|
2015-06-25 12:56:05 +00:00
|
|
|
|
2024-09-13 20:19:49 +00:00
|
|
|
/** @var bool */
|
2016-12-02 17:28:30 +00:00
|
|
|
protected $mFormnovalidate = false;
|
|
|
|
|
|
2020-07-23 09:41:58 +00:00
|
|
|
/**
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to call
|
2020-07-23 09:41:58 +00:00
|
|
|
* @inheritDoc
|
2020-07-10 19:23:59 +00:00
|
|
|
*/
|
2013-11-19 12:55:50 +00:00
|
|
|
public function __construct( $info ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$info['nodata'] = true;
|
2018-08-08 10:45:04 +00:00
|
|
|
|
|
|
|
|
$this->setShowEmptyLabel( false );
|
|
|
|
|
|
|
|
|
|
parent::__construct( $info );
|
|
|
|
|
|
2015-09-26 20:38:25 +00:00
|
|
|
if ( isset( $info['flags'] ) ) {
|
2015-06-25 12:56:05 +00:00
|
|
|
$this->mFlags = $info['flags'];
|
2015-09-26 20:38:25 +00:00
|
|
|
}
|
2015-12-15 22:12:34 +00:00
|
|
|
|
2016-12-02 17:28:30 +00:00
|
|
|
if ( isset( $info['formnovalidate'] ) ) {
|
|
|
|
|
$this->mFormnovalidate = $info['formnovalidate'];
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-15 22:12:34 +00:00
|
|
|
# Generate the label from a message, if possible
|
|
|
|
|
if ( isset( $info['buttonlabel-message'] ) ) {
|
2016-04-01 12:00:44 +00:00
|
|
|
$this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
|
2015-12-15 22:12:34 +00:00
|
|
|
} elseif ( isset( $info['buttonlabel'] ) ) {
|
2016-12-27 21:14:16 +00:00
|
|
|
if ( $info['buttonlabel'] === ' ' || $info['buttonlabel'] === "\u{00A0}" ) {
|
2015-12-15 22:12:34 +00:00
|
|
|
// Apparently some things set   directly and in an odd format
|
2016-12-27 21:14:16 +00:00
|
|
|
$this->buttonLabel = "\u{00A0}";
|
2015-12-15 22:12:34 +00:00
|
|
|
} else {
|
|
|
|
|
$this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
|
|
|
|
|
}
|
|
|
|
|
} elseif ( isset( $info['buttonlabel-raw'] ) ) {
|
|
|
|
|
$this->buttonLabel = $info['buttonlabel-raw'];
|
|
|
|
|
}
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getInputHTML( $value ) {
|
2015-07-09 17:08:21 +00:00
|
|
|
$flags = '';
|
|
|
|
|
$prefix = 'mw-htmlform-';
|
2024-02-03 00:52:10 +00:00
|
|
|
if ( $this->mParent instanceof VFormHTMLForm ) {
|
2015-07-09 17:08:21 +00:00
|
|
|
$prefix = 'mw-ui-';
|
|
|
|
|
// add mw-ui-button separately, so the descriptor doesn't need to set it
|
2015-09-26 20:32:31 +00:00
|
|
|
$flags .= ' ' . $prefix . 'button';
|
2023-06-23 19:38:08 +00:00
|
|
|
}
|
2015-07-09 17:08:21 +00:00
|
|
|
foreach ( $this->mFlags as $flag ) {
|
|
|
|
|
$flags .= ' ' . $prefix . $flag;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$attr = [
|
2015-07-09 17:08:21 +00:00
|
|
|
'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
|
2013-11-19 12:55:50 +00:00
|
|
|
'id' => $this->mID,
|
2015-12-15 22:12:34 +00:00
|
|
|
'type' => $this->buttonType,
|
|
|
|
|
'name' => $this->mName,
|
2016-04-01 11:49:49 +00:00
|
|
|
'value' => $this->getDefault(),
|
2016-12-02 17:28:30 +00:00
|
|
|
'formnovalidate' => $this->mFormnovalidate,
|
2016-02-17 09:09:32 +00:00
|
|
|
] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2024-03-29 03:00:23 +00:00
|
|
|
return Html::rawElement( 'button', $attr,
|
|
|
|
|
$this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-21 21:03:49 +00:00
|
|
|
/**
|
|
|
|
|
* Get the OOUI widget for this field.
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to override
|
2015-04-21 21:03:49 +00:00
|
|
|
* @param string $value
|
2024-02-08 15:30:18 +00:00
|
|
|
* @return \OOUI\ButtonInputWidget
|
2015-04-21 21:03:49 +00:00
|
|
|
*/
|
|
|
|
|
public function getInputOOUI( $value ) {
|
2024-02-08 15:30:18 +00:00
|
|
|
return new \OOUI\ButtonInputWidget( [
|
2015-04-21 21:03:49 +00:00
|
|
|
'name' => $this->mName,
|
2016-04-01 11:49:49 +00:00
|
|
|
'value' => $this->getDefault(),
|
2024-03-29 03:00:23 +00:00
|
|
|
'label' => $this->buttonLabel
|
2024-02-08 15:30:18 +00:00
|
|
|
? new \OOUI\HtmlSnippet( $this->buttonLabel )
|
2016-04-01 11:49:49 +00:00
|
|
|
: $this->getDefault(),
|
2015-04-21 21:03:49 +00:00
|
|
|
'type' => $this->buttonType,
|
2016-02-17 09:09:32 +00:00
|
|
|
'classes' => [ 'mw-htmlform-submit', $this->mClass ],
|
2015-04-21 21:03:49 +00:00
|
|
|
'id' => $this->mID,
|
2015-06-25 12:56:05 +00:00
|
|
|
'flags' => $this->mFlags,
|
2024-02-08 15:30:18 +00:00
|
|
|
] + \OOUI\Element::configFromHtmlAttributes(
|
2016-03-02 19:09:53 +00:00
|
|
|
$this->getAttributes( [ 'disabled', 'tabindex' ] )
|
|
|
|
|
) );
|
2015-04-21 21:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-06 14:43:50 +00:00
|
|
|
public function getInputCodex( $value, $hasErrors ) {
|
2024-04-04 23:23:01 +00:00
|
|
|
$flags = $this->mFlags;
|
2025-05-16 15:54:07 +00:00
|
|
|
$buttonLabel = $this->buttonLabel ?: $this->getDefault();
|
2024-04-04 23:23:01 +00:00
|
|
|
$buttonClasses = [ 'mw-htmlform-submit', 'cdx-button', $this->mClass ];
|
|
|
|
|
$buttonAttribs = [
|
|
|
|
|
'class' => $buttonClasses,
|
|
|
|
|
'id' => $this->mID,
|
|
|
|
|
'type' => $this->buttonType,
|
|
|
|
|
'name' => $this->mName,
|
|
|
|
|
'value' => $this->getDefault(),
|
|
|
|
|
'formnovalidate' => $this->mFormnovalidate,
|
|
|
|
|
] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
|
|
|
|
|
|
2025-05-16 15:54:07 +00:00
|
|
|
// T394396 - Needs revisiting. Merged with suppress to match sec release.
|
|
|
|
|
// @phan-suppress-next-line SecurityCheck-DoubleEscaped
|
2024-04-04 23:23:01 +00:00
|
|
|
return static::buildCodexComponent(
|
|
|
|
|
$flags,
|
|
|
|
|
$buttonLabel,
|
|
|
|
|
$buttonAttribs
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build the markup of the Codex component
|
|
|
|
|
*
|
|
|
|
|
* @param array $flags The button's flag classes.
|
|
|
|
|
* @param string $buttonLabel The button's label attribute.
|
|
|
|
|
* @param array $attribs The button's list of attributes.
|
|
|
|
|
* @return string Raw HTML.
|
|
|
|
|
*/
|
|
|
|
|
public static function buildCodexComponent(
|
|
|
|
|
$flags,
|
|
|
|
|
$buttonLabel,
|
|
|
|
|
$attribs
|
|
|
|
|
) {
|
|
|
|
|
$flagClasses = [];
|
2024-03-20 21:22:06 +00:00
|
|
|
$flagClassMap = [
|
|
|
|
|
'progressive' => 'cdx-button--action-progressive',
|
|
|
|
|
'destructive' => 'cdx-button--action-destructive',
|
|
|
|
|
'primary' => 'cdx-button--weight-primary',
|
|
|
|
|
'quiet' => 'cdx-button--weight-quiet',
|
|
|
|
|
];
|
|
|
|
|
|
2024-04-04 23:23:01 +00:00
|
|
|
foreach ( $flags as $flag ) {
|
2024-03-20 21:22:06 +00:00
|
|
|
if ( isset( $flagClassMap[$flag] ) ) {
|
2024-04-04 23:23:01 +00:00
|
|
|
$flagClasses[] = $flagClassMap[$flag];
|
2024-03-20 21:22:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 23:23:01 +00:00
|
|
|
$buttonClassesAndFlags = array_merge( $attribs[ 'class' ], $flagClasses );
|
|
|
|
|
$attribs['class'] = $buttonClassesAndFlags;
|
2024-03-06 14:43:50 +00:00
|
|
|
|
2024-04-04 23:23:01 +00:00
|
|
|
$buttonHtml = Html::rawElement(
|
2025-05-16 15:54:07 +00:00
|
|
|
'button', $attribs, htmlspecialchars( $buttonLabel )
|
2024-04-04 23:23:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $buttonHtml;
|
2024-03-06 14:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-10 19:23:59 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
* @stable to override
|
|
|
|
|
*/
|
2013-11-19 12:55:50 +00:00
|
|
|
protected function needsLabel() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Button cannot be invalid
|
2020-07-10 19:23:59 +00:00
|
|
|
* @stable to override
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param string $value
|
|
|
|
|
* @param array $alldata
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2016-11-02 17:13:43 +00:00
|
|
|
* @return bool|string|Message
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public function validate( $value, $alldata ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
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( HTMLButtonField::class, 'HTMLButtonField' );
|