MediaWiki  master
HTMLButtonField.php
Go to the documentation of this file.
1 <?php
2 
4 
26  protected $buttonType = 'button';
27  protected $buttonLabel = null;
28 
30  protected $mFlags = [];
31 
32  protected $mFormnovalidate = false;
33 
38  public function __construct( $info ) {
39  $info['nodata'] = true;
40 
41  $this->setShowEmptyLabel( false );
42 
43  parent::__construct( $info );
44 
45  if ( isset( $info['flags'] ) ) {
46  $this->mFlags = $info['flags'];
47  }
48 
49  if ( isset( $info['formnovalidate'] ) ) {
50  $this->mFormnovalidate = $info['formnovalidate'];
51  }
52 
53  # Generate the label from a message, if possible
54  if ( isset( $info['buttonlabel-message'] ) ) {
55  $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
56  } elseif ( isset( $info['buttonlabel'] ) ) {
57  if ( $info['buttonlabel'] === '&#160;' || $info['buttonlabel'] === "\u{00A0}" ) {
58  // Apparently some things set &nbsp directly and in an odd format
59  $this->buttonLabel = "\u{00A0}";
60  } else {
61  $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
62  }
63  } elseif ( isset( $info['buttonlabel-raw'] ) ) {
64  $this->buttonLabel = $info['buttonlabel-raw'];
65  }
66  }
67 
68  public function getInputHTML( $value ) {
69  $flags = '';
70  $prefix = 'mw-htmlform-';
71  if ( $this->mParent instanceof VFormHTMLForm ||
72  $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere )
73  ) {
74  $prefix = 'mw-ui-';
75  // add mw-ui-button separately, so the descriptor doesn't need to set it
76  $flags .= ' ' . $prefix . 'button';
77  }
78  foreach ( $this->mFlags as $flag ) {
79  $flags .= ' ' . $prefix . $flag;
80  }
81  $attr = [
82  'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
83  'id' => $this->mID,
84  'type' => $this->buttonType,
85  'name' => $this->mName,
86  'value' => $this->getDefault(),
87  'formnovalidate' => $this->mFormnovalidate,
88  ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
89 
90  if ( $this->isBadIE() ) {
91  return Html::element( 'input', $attr );
92  } else {
93  return Html::rawElement( 'button', $attr,
94  $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
95  }
96  }
97 
104  public function getInputOOUI( $value ) {
105  return new OOUI\ButtonInputWidget( [
106  'name' => $this->mName,
107  'value' => $this->getDefault(),
108  'label' => !$this->isBadIE() && $this->buttonLabel
109  ? new OOUI\HtmlSnippet( $this->buttonLabel )
110  : $this->getDefault(),
111  'type' => $this->buttonType,
112  'classes' => [ 'mw-htmlform-submit', $this->mClass ],
113  'id' => $this->mID,
114  'flags' => $this->mFlags,
115  'useInputTag' => $this->isBadIE(),
116  ] + OOUI\Element::configFromHtmlAttributes(
117  $this->getAttributes( [ 'disabled', 'tabindex' ] )
118  ) );
119  }
120 
125  protected function needsLabel() {
126  return false;
127  }
128 
138  public function validate( $value, $alldata ) {
139  return true;
140  }
141 
146  private function isBadIE() {
147  $request = $this->mParent
148  ? $this->mParent->getRequest()
149  : RequestContext::getMain()->getRequest();
150  return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
151  }
152 }
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.
isBadIE()
IE<8 has bugs with <button>, so we'll need to avoid them.
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.
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
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.