]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/htmlform/fields/HTMLButtonField.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / htmlform / fields / 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  * - formnovalidate: Set to true if clicking this button should suppress
17  *   client-side form validation. Used in HTMLFormFieldCloner for add/remove
18  *   buttons.
19  *
20  * Note that the buttonlabel parameters are not supported on IE6 and IE7 due to
21  * bugs in those browsers. If detected, they will be served buttons using the
22  * value of 'default' as the button label.
23  *
24  * @since 1.22
25  */
26 class HTMLButtonField extends HTMLFormField {
27         protected $buttonType = 'button';
28         protected $buttonLabel = null;
29
30         /** @var array $mFlags Flags to add to OOUI Button widget */
31         protected $mFlags = [];
32
33         protected $mFormnovalidate = false;
34
35         public function __construct( $info ) {
36                 $info['nodata'] = true;
37                 if ( isset( $info['flags'] ) ) {
38                         $this->mFlags = $info['flags'];
39                 }
40
41                 if ( isset( $info['formnovalidate'] ) ) {
42                         $this->mFormnovalidate = $info['formnovalidate'];
43                 }
44
45                 # Generate the label from a message, if possible
46                 if ( isset( $info['buttonlabel-message'] ) ) {
47                         $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->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' => $this->getDefault(),
83                         'formnovalidate' => $this->mFormnovalidate,
84                 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
85
86                 if ( $this->isBadIE() ) {
87                         return Html::element( 'input', $attr );
88                 } else {
89                         return Html::rawElement( 'button', $attr,
90                                 $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
91                 }
92         }
93
94         /**
95          * Get the OOUI widget for this field.
96          * @param string $value
97          * @return OOUI\ButtonInputWidget
98          */
99         public function getInputOOUI( $value ) {
100                 return new OOUI\ButtonInputWidget( [
101                         'name' => $this->mName,
102                         'value' => $this->getDefault(),
103                         'label' => !$this->isBadIE() && $this->buttonLabel
104                                 ? new OOUI\HtmlSnippet( $this->buttonLabel )
105                                 : $this->getDefault(),
106                         'type' => $this->buttonType,
107                         'classes' => [ 'mw-htmlform-submit', $this->mClass ],
108                         'id' => $this->mID,
109                         'flags' => $this->mFlags,
110                         'useInputTag' => $this->isBadIE(),
111                 ] + OOUI\Element::configFromHtmlAttributes(
112                         $this->getAttributes( [ 'disabled', 'tabindex' ] )
113                 ) );
114         }
115
116         protected function needsLabel() {
117                 return false;
118         }
119
120         /**
121          * Button cannot be invalid
122          *
123          * @param string $value
124          * @param array $alldata
125          *
126          * @return bool|string|Message
127          */
128         public function validate( $value, $alldata ) {
129                 return true;
130         }
131
132         /**
133          * IE<8 has bugs with <button>, so we'll need to avoid them.
134          * @return bool Whether the request is from a bad version of IE
135          */
136         private function isBadIE() {
137                 $request = $this->mParent
138                         ? $this->mParent->getRequest()
139                         : RequestContext::getMain()->getRequest();
140                 return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
141         }
142 }