FormAction.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Base classes for actions done on pages.
  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
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *
  19. * @file
  20. * @ingroup Actions
  21. */
  22. /**
  23. * An action which shows a form and does something based on the input from the form
  24. *
  25. * @ingroup Actions
  26. */
  27. abstract class FormAction extends Action {
  28. /**
  29. * Get an HTMLForm descriptor array
  30. * @return array
  31. */
  32. protected function getFormFields() {
  33. // Default to an empty form with just a submit button
  34. return [];
  35. }
  36. /**
  37. * Add pre- or post-text to the form
  38. * @return string HTML which will be sent to $form->addPreText()
  39. */
  40. protected function preText() {
  41. return '';
  42. }
  43. /**
  44. * @return string
  45. */
  46. protected function postText() {
  47. return '';
  48. }
  49. /**
  50. * Play with the HTMLForm if you need to more substantially
  51. * @param HTMLForm $form
  52. */
  53. protected function alterForm( HTMLForm $form ) {
  54. }
  55. /**
  56. * Whether the form should use OOUI
  57. * @return bool
  58. */
  59. protected function usesOOUI() {
  60. return false;
  61. }
  62. /**
  63. * Get the HTMLForm to control behavior
  64. * @return HTMLForm|null
  65. */
  66. protected function getForm() {
  67. $this->fields = $this->getFormFields();
  68. // Give hooks a chance to alter the form, adding extra fields or text etc
  69. Hooks::run( 'ActionModifyFormFields', [ $this->getName(), &$this->fields, $this->page ] );
  70. if ( $this->usesOOUI() ) {
  71. $form = HTMLForm::factory( 'ooui', $this->fields, $this->getContext(), $this->getName() );
  72. } else {
  73. $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
  74. }
  75. $form->setSubmitCallback( [ $this, 'onSubmit' ] );
  76. $title = $this->getTitle();
  77. $form->setAction( $title->getLocalURL( [ 'action' => $this->getName() ] ) );
  78. // Retain query parameters (uselang etc)
  79. $params = array_diff_key(
  80. $this->getRequest()->getQueryValues(),
  81. [ 'action' => null, 'title' => null ]
  82. );
  83. if ( $params ) {
  84. $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
  85. }
  86. $form->addPreText( $this->preText() );
  87. $form->addPostText( $this->postText() );
  88. $this->alterForm( $form );
  89. // Give hooks a chance to alter the form, adding extra fields or text etc
  90. Hooks::run( 'ActionBeforeFormDisplay', [ $this->getName(), &$form, $this->page ] );
  91. return $form;
  92. }
  93. /**
  94. * Process the form on POST submission.
  95. *
  96. * If you don't want to do anything with the form, just return false here.
  97. *
  98. * This method will be passed to the HTMLForm as a submit callback (see
  99. * HTMLForm::setSubmitCallback) and must return as documented for HTMLForm::trySubmit.
  100. *
  101. * @see HTMLForm::setSubmitCallback()
  102. * @see HTMLForm::trySubmit()
  103. * @param array $data
  104. * @return bool|string|array|Status Must return as documented for HTMLForm::trySubmit
  105. */
  106. abstract public function onSubmit( $data );
  107. /**
  108. * Do something exciting on successful processing of the form. This might be to show
  109. * a confirmation message (watch, rollback, etc) or to redirect somewhere else (edit,
  110. * protect, etc).
  111. */
  112. abstract public function onSuccess();
  113. /**
  114. * The basic pattern for actions is to display some sort of HTMLForm UI, maybe with
  115. * some stuff underneath (history etc); to do some processing on submission of that
  116. * form (delete, protect, etc) and to do something exciting on 'success', be that
  117. * display something new or redirect to somewhere. Some actions have more exotic
  118. * behavior, but that's what subclassing is for :D
  119. */
  120. public function show() {
  121. $this->setHeaders();
  122. // This will throw exceptions if there's a problem
  123. $this->checkCanExecute( $this->getUser() );
  124. $form = $this->getForm();
  125. if ( $form->show() ) {
  126. $this->onSuccess();
  127. }
  128. }
  129. public function doesWrites() {
  130. return true;
  131. }
  132. }