MediaWiki  master
HTMLButtonField.php
Go to the documentation of this file.
1 <?php
2 
23  protected $buttonType = 'button';
24  protected $buttonLabel = null;
25 
27  protected $mFlags = [];
28 
29  protected $mFormnovalidate = false;
30 
31  public function __construct( $info ) {
32  $info['nodata'] = true;
33 
34  $this->setShowEmptyLabel( false );
35 
36  parent::__construct( $info );
37 
38  if ( isset( $info['flags'] ) ) {
39  $this->mFlags = $info['flags'];
40  }
41 
42  if ( isset( $info['formnovalidate'] ) ) {
43  $this->mFormnovalidate = $info['formnovalidate'];
44  }
45 
46  # Generate the label from a message, if possible
47  if ( isset( $info['buttonlabel-message'] ) ) {
48  $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
49  } elseif ( isset( $info['buttonlabel'] ) ) {
50  if ( $info['buttonlabel'] === '&#160;' || $info['buttonlabel'] === "\u{00A0}" ) {
51  // Apparently some things set &nbsp directly and in an odd format
52  $this->buttonLabel = "\u{00A0}";
53  } else {
54  $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
55  }
56  } elseif ( isset( $info['buttonlabel-raw'] ) ) {
57  $this->buttonLabel = $info['buttonlabel-raw'];
58  }
59  }
60 
61  public function getInputHTML( $value ) {
62  $flags = '';
63  $prefix = 'mw-htmlform-';
64  if ( $this->mParent instanceof VFormHTMLForm ||
65  $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' )
66  ) {
67  $prefix = 'mw-ui-';
68  // add mw-ui-button separately, so the descriptor doesn't need to set it
69  $flags .= ' ' . $prefix . 'button';
70  }
71  foreach ( $this->mFlags as $flag ) {
72  $flags .= ' ' . $prefix . $flag;
73  }
74  $attr = [
75  'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
76  'id' => $this->mID,
77  'type' => $this->buttonType,
78  'name' => $this->mName,
79  'value' => $this->getDefault(),
80  'formnovalidate' => $this->mFormnovalidate,
81  ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
82 
83  if ( $this->isBadIE() ) {
84  return Html::element( 'input', $attr );
85  } else {
86  return Html::rawElement( 'button', $attr,
87  $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
88  }
89  }
90 
96  public function getInputOOUI( $value ) {
97  return new OOUI\ButtonInputWidget( [
98  'name' => $this->mName,
99  'value' => $this->getDefault(),
100  'label' => !$this->isBadIE() && $this->buttonLabel
101  ? new OOUI\HtmlSnippet( $this->buttonLabel )
102  : $this->getDefault(),
103  'type' => $this->buttonType,
104  'classes' => [ 'mw-htmlform-submit', $this->mClass ],
105  'id' => $this->mID,
106  'flags' => $this->mFlags,
107  'useInputTag' => $this->isBadIE(),
108  ] + OOUI\Element::configFromHtmlAttributes(
109  $this->getAttributes( [ 'disabled', 'tabindex' ] )
110  ) );
111  }
112 
113  protected function needsLabel() {
114  return false;
115  }
116 
125  public function validate( $value, $alldata ) {
126  return true;
127  }
128 
133  private function isBadIE() {
134  $request = $this->mParent
135  ? $this->mParent->getRequest()
136  : RequestContext::getMain()->getRequest();
137  return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
138  }
139 }
HTMLButtonField\needsLabel
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
Definition: HTMLButtonField.php:113
HTMLButtonField\__construct
__construct( $info)
Initialise the object.
Definition: HTMLButtonField.php:31
HTMLButtonField\validate
validate( $value, $alldata)
Button cannot be invalid.
Definition: HTMLButtonField.php:125
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
HTMLButtonField\getInputOOUI
getInputOOUI( $value)
Get the OOUI widget for this field.
Definition: HTMLButtonField.php:96
HTMLFormField\setShowEmptyLabel
setShowEmptyLabel( $show)
Tell the field whether to generate a separate label element if its label is blank.
Definition: HTMLFormField.php:348
HTMLButtonField\$mFormnovalidate
$mFormnovalidate
Definition: HTMLButtonField.php:29
VFormHTMLForm
Compact stacked vertical format for forms.
Definition: VFormHTMLForm.php:27
HTMLButtonField
Adds a generic button inline to the form.
Definition: HTMLButtonField.php:22
HTMLFormField\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLFormField.php:1155
HTMLButtonField\$buttonLabel
$buttonLabel
Definition: HTMLButtonField.php:24
HTMLFormField\$mName
$mName
Definition: HTMLFormField.php:13
HTMLButtonField\isBadIE
isBadIE()
IE<8 has bugs with <button>, so we'll need to avoid them.
Definition: HTMLButtonField.php:133
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:7
HTMLFormField\$mID
$mID
Definition: HTMLFormField.php:16
HTMLButtonField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLButtonField.php:61
HTMLFormField\getDefault
getDefault()
Definition: HTMLFormField.php:960
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
HTMLButtonField\$buttonType
$buttonType
Definition: HTMLButtonField.php:23
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
HTMLButtonField\$mFlags
array $mFlags
Flags to add to OOUI Button widget.
Definition: HTMLButtonField.php:27
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:999