HTMLButtonField.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Adds a generic button inline to the form. Does not do anything, you must add
  4. * click handling code in JavaScript. Use a HTMLSubmitField if you merely
  5. * wish to add a submit button to a form.
  6. *
  7. * Additional recognized configuration parameters include:
  8. * - flags: OOUI flags for the button, see OOUI\FlaggedElement
  9. * - buttonlabel-message: Message to use for the button display text, instead
  10. * of the value from 'default'. Overrides 'buttonlabel' and 'buttonlabel-raw'.
  11. * - buttonlabel: Text to display for the button display text, instead
  12. * of the value from 'default'. Overrides 'buttonlabel-raw'.
  13. * - buttonlabel-raw: HTMLto display for the button display text, instead
  14. * of the value from 'default'.
  15. * - formnovalidate: Set to true if clicking this button should suppress
  16. * client-side form validation. Used in HTMLFormFieldCloner for add/remove
  17. * buttons.
  18. *
  19. * Note that the buttonlabel parameters are not supported on IE6 and IE7 due to
  20. * bugs in those browsers. If detected, they will be served buttons using the
  21. * value of 'default' as the button label.
  22. *
  23. * @since 1.22
  24. */
  25. class HTMLButtonField extends HTMLFormField {
  26. protected $buttonType = 'button';
  27. protected $buttonLabel = null;
  28. /** @var array $mFlags Flags to add to OOUI Button widget */
  29. protected $mFlags = [];
  30. protected $mFormnovalidate = false;
  31. public function __construct( $info ) {
  32. $info['nodata'] = true;
  33. $this->setShowEmptyLabel( false );
  34. parent::__construct( $info );
  35. if ( isset( $info['flags'] ) ) {
  36. $this->mFlags = $info['flags'];
  37. }
  38. if ( isset( $info['formnovalidate'] ) ) {
  39. $this->mFormnovalidate = $info['formnovalidate'];
  40. }
  41. # Generate the label from a message, if possible
  42. if ( isset( $info['buttonlabel-message'] ) ) {
  43. $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
  44. } elseif ( isset( $info['buttonlabel'] ) ) {
  45. if ( $info['buttonlabel'] === '&#160;' || $info['buttonlabel'] === "\u{00A0}" ) {
  46. // Apparently some things set &nbsp directly and in an odd format
  47. $this->buttonLabel = "\u{00A0}";
  48. } else {
  49. $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
  50. }
  51. } elseif ( isset( $info['buttonlabel-raw'] ) ) {
  52. $this->buttonLabel = $info['buttonlabel-raw'];
  53. }
  54. }
  55. public function getInputHTML( $value ) {
  56. $flags = '';
  57. $prefix = 'mw-htmlform-';
  58. if ( $this->mParent instanceof VFormHTMLForm ||
  59. $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' )
  60. ) {
  61. $prefix = 'mw-ui-';
  62. // add mw-ui-button separately, so the descriptor doesn't need to set it
  63. $flags .= ' ' . $prefix . 'button';
  64. }
  65. foreach ( $this->mFlags as $flag ) {
  66. $flags .= ' ' . $prefix . $flag;
  67. }
  68. $attr = [
  69. 'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
  70. 'id' => $this->mID,
  71. 'type' => $this->buttonType,
  72. 'name' => $this->mName,
  73. 'value' => $this->getDefault(),
  74. 'formnovalidate' => $this->mFormnovalidate,
  75. ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
  76. if ( $this->isBadIE() ) {
  77. return Html::element( 'input', $attr );
  78. } else {
  79. return Html::rawElement( 'button', $attr,
  80. $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
  81. }
  82. }
  83. /**
  84. * Get the OOUI widget for this field.
  85. * @param string $value
  86. * @return OOUI\ButtonInputWidget
  87. */
  88. public function getInputOOUI( $value ) {
  89. return new OOUI\ButtonInputWidget( [
  90. 'name' => $this->mName,
  91. 'value' => $this->getDefault(),
  92. 'label' => !$this->isBadIE() && $this->buttonLabel
  93. ? new OOUI\HtmlSnippet( $this->buttonLabel )
  94. : $this->getDefault(),
  95. 'type' => $this->buttonType,
  96. 'classes' => [ 'mw-htmlform-submit', $this->mClass ],
  97. 'id' => $this->mID,
  98. 'flags' => $this->mFlags,
  99. 'useInputTag' => $this->isBadIE(),
  100. ] + OOUI\Element::configFromHtmlAttributes(
  101. $this->getAttributes( [ 'disabled', 'tabindex' ] )
  102. ) );
  103. }
  104. protected function needsLabel() {
  105. return false;
  106. }
  107. /**
  108. * Button cannot be invalid
  109. *
  110. * @param string $value
  111. * @param array $alldata
  112. *
  113. * @return bool|string|Message
  114. */
  115. public function validate( $value, $alldata ) {
  116. return true;
  117. }
  118. /**
  119. * IE<8 has bugs with <button>, so we'll need to avoid them.
  120. * @return bool Whether the request is from a bad version of IE
  121. */
  122. private function isBadIE() {
  123. $request = $this->mParent
  124. ? $this->mParent->getRequest()
  125. : RequestContext::getMain()->getRequest();
  126. return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );
  127. }
  128. }