2013-11-19 12:55:50 +00:00
|
|
|
<?php
|
2013-11-19 13:08:16 +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:
|
|
|
|
|
* - flags: OOUI flags for the button, see OOUI\\FlaggedElement
|
|
|
|
|
* - 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'.
|
|
|
|
|
*
|
|
|
|
|
* Note that the buttonlabel parameters are not supported on IE6 and IE7 due to
|
|
|
|
|
* bugs in those browsers. If detected, they will be served buttons using the
|
|
|
|
|
* value of 'default' as the button label.
|
|
|
|
|
*
|
2013-11-19 12:55:50 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
class HTMLButtonField extends HTMLFormField {
|
|
|
|
|
protected $buttonType = 'button';
|
2015-12-15 22:12:34 +00:00
|
|
|
protected $buttonLabel = null;
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2015-06-25 12:56:05 +00:00
|
|
|
/** @var array $mFlags Flags to add to OOUI Button widget */
|
|
|
|
|
protected $mFlags = array();
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
public function __construct( $info ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$info['nodata'] = true;
|
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
|
|
|
|
|
|
|
|
# Generate the label from a message, if possible
|
|
|
|
|
if ( isset( $info['buttonlabel-message'] ) ) {
|
|
|
|
|
$msgInfo = $info['buttonlabel-message'];
|
|
|
|
|
|
|
|
|
|
if ( is_array( $msgInfo ) ) {
|
|
|
|
|
$msg = array_shift( $msgInfo );
|
|
|
|
|
} else {
|
|
|
|
|
$msg = $msgInfo;
|
|
|
|
|
$msgInfo = array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->buttonLabel = $this->msg( $msg, $msgInfo )->parse();
|
|
|
|
|
} elseif ( isset( $info['buttonlabel'] ) ) {
|
|
|
|
|
if ( $info['buttonlabel'] === ' ' ) {
|
|
|
|
|
// Apparently some things set   directly and in an odd format
|
|
|
|
|
$this->buttonLabel = ' ';
|
|
|
|
|
} else {
|
|
|
|
|
$this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
|
|
|
|
|
}
|
|
|
|
|
} elseif ( isset( $info['buttonlabel-raw'] ) ) {
|
|
|
|
|
$this->buttonLabel = $info['buttonlabel-raw'];
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
parent::__construct( $info );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getInputHTML( $value ) {
|
2015-07-09 17:08:21 +00:00
|
|
|
$flags = '';
|
|
|
|
|
$prefix = 'mw-htmlform-';
|
|
|
|
|
if ( $this->mParent instanceof VFormHTMLForm ||
|
|
|
|
|
$this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' )
|
|
|
|
|
) {
|
|
|
|
|
$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';
|
2015-07-09 17:08:21 +00:00
|
|
|
}
|
|
|
|
|
foreach ( $this->mFlags as $flag ) {
|
|
|
|
|
$flags .= ' ' . $prefix . $flag;
|
|
|
|
|
}
|
2013-11-19 12:55:50 +00:00
|
|
|
$attr = array(
|
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,
|
|
|
|
|
'value' => $value,
|
2013-05-02 18:27:44 +00:00
|
|
|
) + $this->getAttributes( array( 'disabled', 'tabindex' ) );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2015-12-15 22:12:34 +00:00
|
|
|
if ( $this->isBadIE() ) {
|
|
|
|
|
return Html::element( 'input', $attr );
|
|
|
|
|
} else {
|
|
|
|
|
return Html::rawElement( 'button', $attr, $this->buttonLabel ?: htmlspecialchars( $value ) );
|
|
|
|
|
}
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-21 21:03:49 +00:00
|
|
|
/**
|
|
|
|
|
* Get the OOUI widget for this field.
|
|
|
|
|
* @param string $value
|
2015-11-23 22:53:38 +00:00
|
|
|
* @return OOUI\ButtonInputWidget
|
2015-04-21 21:03:49 +00:00
|
|
|
*/
|
|
|
|
|
public function getInputOOUI( $value ) {
|
|
|
|
|
return new OOUI\ButtonInputWidget( array(
|
|
|
|
|
'name' => $this->mName,
|
|
|
|
|
'value' => $value,
|
2015-12-15 22:12:34 +00:00
|
|
|
'label' => !$this->isBadIE() && $this->buttonLabel
|
|
|
|
|
? new OOUI\HtmlSnippet( $this->buttonLabel )
|
|
|
|
|
: $value,
|
2015-04-21 21:03:49 +00:00
|
|
|
'type' => $this->buttonType,
|
|
|
|
|
'classes' => array( 'mw-htmlform-submit', $this->mClass ),
|
|
|
|
|
'id' => $this->mID,
|
2015-06-25 12:56:05 +00:00
|
|
|
'flags' => $this->mFlags,
|
2015-12-15 22:12:34 +00:00
|
|
|
'useInputTag' => $this->isBadIE(),
|
2015-04-21 21:03:49 +00:00
|
|
|
) + $this->getAttributes( array( 'disabled', 'tabindex' ), array( 'tabindex' => 'tabIndex' ) ) );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
protected function needsLabel() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Button cannot be invalid
|
|
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @param string $value
|
|
|
|
|
* @param array $alldata
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return bool
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public function validate( $value, $alldata ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-12-15 22:12:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* IE<8 has bugs with <button>, so we'll need to avoid them.
|
|
|
|
|
* @return bool Whether the request is from a bad version of IE
|
|
|
|
|
*/
|
|
|
|
|
private function isBadIE() {
|
|
|
|
|
$request = $this->mParent
|
|
|
|
|
? $this->mParent->getRequest()
|
|
|
|
|
: RequestContext::getMain()->getRequest();
|
|
|
|
|
return preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|