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  $isCodexForm = $this->mParent instanceof CodexHTMLForm;
73  if ( $this->mParent instanceof VFormHTMLForm ||
74  ( !$isCodexForm && $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere ) )
75  ) {
76  $prefix = 'mw-ui-';
77  // add mw-ui-button separately, so the descriptor doesn't need to set it
78  $flags .= ' ' . $prefix . 'button';
79  }
80  if ( $isCodexForm ) {
81  $flags .= ' cdx-button cdx-button--action-progressive cdx-button--weight-primary';
82  }
83  foreach ( $this->mFlags as $flag ) {
84  $flags .= ' ' . $prefix . $flag;
85  }
86  $attr = [
87  'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
88  'id' => $this->mID,
89  'type' => $this->buttonType,
90  'name' => $this->mName,
91  'value' => $this->getDefault(),
92  'formnovalidate' => $this->mFormnovalidate,
93  ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
94 
95  if ( $this->isBadIE() ) {
96  return Html::element( 'input', $attr );
97  } else {
98  return Html::rawElement( 'button', $attr,
99  $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
100  }
101  }
102 
109  public function getInputOOUI( $value ) {
110  return new OOUI\ButtonInputWidget( [
111  'name' => $this->mName,
112  'value' => $this->getDefault(),
113  'label' => !$this->isBadIE() && $this->buttonLabel
114  ? new OOUI\HtmlSnippet( $this->buttonLabel )
115  : $this->getDefault(),
116  'type' => $this->buttonType,
117  'classes' => [ 'mw-htmlform-submit', $this->mClass ],
118  'id' => $this->mID,
119  'flags' => $this->mFlags,
120  'useInputTag' => $this->isBadIE(),
121  ] + OOUI\Element::configFromHtmlAttributes(
122  $this->getAttributes( [ 'disabled', 'tabindex' ] )
123  ) );
124  }
125 
130  protected function needsLabel() {
131  return false;
132  }
133 
143  public function validate( $value, $alldata ) {
144  return true;
145  }
146 
151  private function isBadIE() {
152  $request = $this->mParent
153  ? $this->mParent->getRequest()
154  : RequestContext::getMain()->getRequest();
155  return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
156  }
157 }
Codex based HTML form.
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:57
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.