Completed
Branch master (939199)
by
unknown
39:35
created

includes/htmlform/fields/HTMLButtonField.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Adds a generic button inline to the form. Does not do anything, you must add
5
 * click handling code in JavaScript. Use a HTMLSubmitField if you merely
6
 * wish to add a submit button to a form.
7
 *
8
 * Additional recognized configuration parameters include:
9
 * - flags: OOUI flags for the button, see OOUI\FlaggedElement
10
 * - buttonlabel-message: Message to use for the button display text, instead
11
 *   of the value from 'default'. Overrides 'buttonlabel' and 'buttonlabel-raw'.
12
 * - buttonlabel: Text to display for the button display text, instead
13
 *   of the value from 'default'. Overrides 'buttonlabel-raw'.
14
 * - buttonlabel-raw: HTMLto display for the button display text, instead
15
 *   of the value from 'default'.
16
 *
17
 * Note that the buttonlabel parameters are not supported on IE6 and IE7 due to
18
 * bugs in those browsers. If detected, they will be served buttons using the
19
 * value of 'default' as the button label.
20
 *
21
 * @since 1.22
22
 */
23
class HTMLButtonField extends HTMLFormField {
24
	protected $buttonType = 'button';
25
	protected $buttonLabel = null;
26
27
	/** @var array $mFlags Flags to add to OOUI Button widget */
28
	protected $mFlags = [];
29
30
	public function __construct( $info ) {
31
		$info['nodata'] = true;
32
		if ( isset( $info['flags'] ) ) {
33
			$this->mFlags = $info['flags'];
0 ignored issues
show
Documentation Bug introduced by
It seems like $info['flags'] of type boolean is incompatible with the declared type array of property $mFlags.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
		}
35
36
		# Generate the label from a message, if possible
37
		if ( isset( $info['buttonlabel-message'] ) ) {
38
			$this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
39
		} elseif ( isset( $info['buttonlabel'] ) ) {
40
			if ( $info['buttonlabel'] === '&#160;' ) {
41
				// Apparently some things set &nbsp directly and in an odd format
42
				$this->buttonLabel = '&#160;';
43
			} else {
44
				$this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
45
			}
46
		} elseif ( isset( $info['buttonlabel-raw'] ) ) {
47
			$this->buttonLabel = $info['buttonlabel-raw'];
48
		}
49
50
		$this->setShowEmptyLabel( false );
51
52
		parent::__construct( $info );
53
	}
54
55
	public function getInputHTML( $value ) {
56
		$flags = '';
57
		$prefix = 'mw-htmlform-';
58
		if ( $this->mParent instanceof VFormHTMLForm ||
59
			$this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' )
60
		) {
61
			$prefix = 'mw-ui-';
62
			// add mw-ui-button separately, so the descriptor doesn't need to set it
63
			$flags .= ' ' . $prefix . 'button';
64
		}
65
		foreach ( $this->mFlags as $flag ) {
66
			$flags .= ' ' . $prefix . $flag;
67
		}
68
		$attr = [
69
			'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
70
			'id' => $this->mID,
71
			'type' => $this->buttonType,
72
			'name' => $this->mName,
73
			'value' => $this->getDefault(),
74
		] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
75
76
		if ( $this->isBadIE() ) {
77
			return Html::element( 'input', $attr );
78
		} else {
79
			return Html::rawElement( 'button', $attr,
80
				$this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
81
		}
82
	}
83
84
	/**
85
	 * Get the OOUI widget for this field.
86
	 * @param string $value
87
	 * @return OOUI\ButtonInputWidget
88
	 */
89
	public function getInputOOUI( $value ) {
90
		return new OOUI\ButtonInputWidget( [
91
			'name' => $this->mName,
92
			'value' => $this->getDefault(),
93
			'label' => !$this->isBadIE() && $this->buttonLabel
94
				? new OOUI\HtmlSnippet( $this->buttonLabel )
95
				: $this->getDefault(),
96
			'type' => $this->buttonType,
97
			'classes' => [ 'mw-htmlform-submit', $this->mClass ],
98
			'id' => $this->mID,
99
			'flags' => $this->mFlags,
100
			'useInputTag' => $this->isBadIE(),
101
		] + OOUI\Element::configFromHtmlAttributes(
102
			$this->getAttributes( [ 'disabled', 'tabindex' ] )
103
		) );
104
	}
105
106
	protected function needsLabel() {
107
		return false;
108
	}
109
110
	/**
111
	 * Button cannot be invalid
112
	 *
113
	 * @param string $value
114
	 * @param array $alldata
115
	 *
116
	 * @return bool
117
	 */
118
	public function validate( $value, $alldata ) {
119
		return true;
120
	}
121
122
	/**
123
	 * IE<8 has bugs with <button>, so we'll need to avoid them.
124
	 * @return bool Whether the request is from a bad version of IE
125
	 */
126
	private function isBadIE() {
127
		$request = $this->mParent
128
			? $this->mParent->getRequest()
129
			: RequestContext::getMain()->getRequest();
130
		return preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
131
	}
132
}
133