MediaWiki  master
HTMLButtonField.php
Go to the documentation of this file.
1 <?php
2 
5 
27  protected $buttonType = 'button';
28  protected $buttonLabel = null;
29 
31  protected $mFlags = [];
32 
33  protected $mFormnovalidate = false;
34 
39  public function __construct( $info ) {
40  $info['nodata'] = true;
41 
42  $this->setShowEmptyLabel( false );
43 
44  parent::__construct( $info );
45 
46  if ( isset( $info['flags'] ) ) {
47  $this->mFlags = $info['flags'];
48  }
49 
50  if ( isset( $info['formnovalidate'] ) ) {
51  $this->mFormnovalidate = $info['formnovalidate'];
52  }
53 
54  # Generate the label from a message, if possible
55  if ( isset( $info['buttonlabel-message'] ) ) {
56  $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
57  } elseif ( isset( $info['buttonlabel'] ) ) {
58  if ( $info['buttonlabel'] === '&#160;' || $info['buttonlabel'] === "\u{00A0}" ) {
59  // Apparently some things set &nbsp directly and in an odd format
60  $this->buttonLabel = "\u{00A0}";
61  } else {
62  $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
63  }
64  } elseif ( isset( $info['buttonlabel-raw'] ) ) {
65  $this->buttonLabel = $info['buttonlabel-raw'];
66  }
67  }
68 
69  public function getInputHTML( $value ) {
70  $flags = '';
71  $prefix = 'mw-htmlform-';
72  if ( $this->mParent instanceof VFormHTMLForm ||
73  $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere )
74  ) {
75  $prefix = 'mw-ui-';
76  // add mw-ui-button separately, so the descriptor doesn't need to set it
77  $flags .= ' ' . $prefix . 'button';
78  }
79  foreach ( $this->mFlags as $flag ) {
80  $flags .= ' ' . $prefix . $flag;
81  }
82  $attr = [
83  'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
84  'id' => $this->mID,
85  'type' => $this->buttonType,
86  'name' => $this->mName,
87  'value' => $this->getDefault(),
88  'formnovalidate' => $this->mFormnovalidate,
89  ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
90 
91  if ( $this->isBadIE() ) {
92  return Html::element( 'input', $attr );
93  } else {
94  return Html::rawElement( 'button', $attr,
95  $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
96  }
97  }
98 
105  public function getInputOOUI( $value ) {
106  return new OOUI\ButtonInputWidget( [
107  'name' => $this->mName,
108  'value' => $this->getDefault(),
109  'label' => !$this->isBadIE() && $this->buttonLabel
110  ? new OOUI\HtmlSnippet( $this->buttonLabel )
111  : $this->getDefault(),
112  'type' => $this->buttonType,
113  'classes' => [ 'mw-htmlform-submit', $this->mClass ],
114  'id' => $this->mID,
115  'flags' => $this->mFlags,
116  'useInputTag' => $this->isBadIE(),
117  ] + OOUI\Element::configFromHtmlAttributes(
118  $this->getAttributes( [ 'disabled', 'tabindex' ] )
119  ) );
120  }
121 
126  protected function needsLabel() {
127  return false;
128  }
129 
139  public function validate( $value, $alldata ) {
140  return true;
141  }
142 
147  private function isBadIE() {
148  $request = $this->mParent
149  ? $this->mParent->getRequest()
150  : RequestContext::getMain()->getRequest();
151  return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
152  }
153 }
Adds a generic button inline to the form.
array $mFlags
Flags to add to OOUI Button widget.
getInputOOUI( $value)
Get the OOUI widget for this field.
validate( $value, $alldata)
Button cannot be invalid.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
The parent class to generate form fields.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getAttributes(array $list)
Returns the given attributes from the parameters.
setShowEmptyLabel( $show)
Tell the field whether to generate a separate label element if its label is blank.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
A class containing constants representing the names of configuration variables.
static getMain()
Get the RequestContext object associated with the main request.
Compact stacked vertical format for forms.