VFormHTMLForm.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * HTML form generation and submission handling, vertical-form style.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Compact stacked vertical format for forms.
  24. */
  25. class VFormHTMLForm extends HTMLForm {
  26. /**
  27. * Wrapper and its legend are never generated in VForm mode.
  28. * @var bool
  29. */
  30. protected $mWrapperLegend = false;
  31. /**
  32. * Symbolic display format name.
  33. * @var string
  34. */
  35. protected $displayFormat = 'vform';
  36. public static function loadInputFromParameters( $fieldname, $descriptor,
  37. HTMLForm $parent = null
  38. ) {
  39. $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
  40. $field->setShowEmptyLabel( false );
  41. return $field;
  42. }
  43. public function getHTML( $submitResult ) {
  44. // This is required for VForm HTMLForms that use that style regardless
  45. // of wgUseMediaWikiUIEverywhere (since they pre-date it).
  46. // When wgUseMediaWikiUIEverywhere is removed, this should be consolidated
  47. // with the addModuleStyles in SpecialPage->setHeaders.
  48. $this->getOutput()->addModuleStyles( [
  49. 'mediawiki.ui',
  50. 'mediawiki.ui.button',
  51. 'mediawiki.ui.input',
  52. 'mediawiki.ui.checkbox',
  53. ] );
  54. return parent::getHTML( $submitResult );
  55. }
  56. protected function getFormAttributes() {
  57. $attribs = parent::getFormAttributes();
  58. $attribs['class'] = [ 'mw-htmlform', 'mw-ui-vform', 'mw-ui-container' ];
  59. return $attribs;
  60. }
  61. public function wrapForm( $html ) {
  62. // Always discard $this->mWrapperLegend
  63. return Html::rawElement( 'form', $this->getFormAttributes(), $html );
  64. }
  65. public function getButtons() {
  66. $buttons = '';
  67. if ( $this->mShowSubmit ) {
  68. $attribs = [];
  69. if ( isset( $this->mSubmitID ) ) {
  70. $attribs['id'] = $this->mSubmitID;
  71. }
  72. if ( isset( $this->mSubmitName ) ) {
  73. $attribs['name'] = $this->mSubmitName;
  74. }
  75. if ( isset( $this->mSubmitTooltip ) ) {
  76. $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
  77. }
  78. $attribs['class'] = [
  79. 'mw-htmlform-submit',
  80. 'mw-ui-button mw-ui-big mw-ui-block',
  81. ];
  82. foreach ( $this->mSubmitFlags as $flag ) {
  83. $attribs['class'][] = 'mw-ui-' . $flag;
  84. }
  85. $buttons .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
  86. }
  87. if ( $this->mShowReset ) {
  88. $buttons .= Html::element(
  89. 'input',
  90. [
  91. 'type' => 'reset',
  92. 'value' => $this->msg( 'htmlform-reset' )->text(),
  93. 'class' => 'mw-ui-button mw-ui-big mw-ui-block',
  94. ]
  95. ) . "\n";
  96. }
  97. if ( $this->mShowCancel ) {
  98. $target = $this->mCancelTarget ?: Title::newMainPage();
  99. if ( $target instanceof Title ) {
  100. $target = $target->getLocalURL();
  101. }
  102. $buttons .= Html::element(
  103. 'a',
  104. [
  105. 'class' => 'mw-ui-button mw-ui-big mw-ui-block',
  106. 'href' => $target,
  107. ],
  108. $this->msg( 'cancel' )->text()
  109. ) . "\n";
  110. }
  111. foreach ( $this->mButtons as $button ) {
  112. $attrs = [
  113. 'type' => 'submit',
  114. 'name' => $button['name'],
  115. 'value' => $button['value']
  116. ];
  117. if ( $button['attribs'] ) {
  118. $attrs += $button['attribs'];
  119. }
  120. if ( isset( $button['id'] ) ) {
  121. $attrs['id'] = $button['id'];
  122. }
  123. $attrs['class'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
  124. $attrs['class'][] = 'mw-ui-button mw-ui-big mw-ui-block';
  125. $buttons .= Html::element( 'input', $attrs ) . "\n";
  126. }
  127. if ( !$buttons ) {
  128. return '';
  129. }
  130. return Html::rawElement( 'div',
  131. [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
  132. }
  133. }