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