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