MediaWiki  REL1_24
FormAction.php
Go to the documentation of this file.
00001 <?php
00028 abstract class FormAction extends Action {
00029 
00034     abstract protected function getFormFields();
00035 
00040     protected function preText() {
00041         return '';
00042     }
00043 
00047     protected function postText() {
00048         return '';
00049     }
00050 
00055     protected function alterForm( HTMLForm $form ) {
00056     }
00057 
00062     protected function getForm() {
00063         $this->fields = $this->getFormFields();
00064 
00065         // Give hooks a chance to alter the form, adding extra fields or text etc
00066         wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields, $this->page ) );
00067 
00068         $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
00069         $form->setSubmitCallback( array( $this, 'onSubmit' ) );
00070 
00071         // Retain query parameters (uselang etc)
00072         $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
00073         $params = array_diff_key(
00074             $this->getRequest()->getQueryValues(),
00075             array( 'action' => null, 'title' => null )
00076         );
00077         $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
00078 
00079         $form->addPreText( $this->preText() );
00080         $form->addPostText( $this->postText() );
00081         $this->alterForm( $form );
00082 
00083         // Give hooks a chance to alter the form, adding extra fields or text etc
00084         wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page ) );
00085 
00086         return $form;
00087     }
00088 
00096     abstract public function onSubmit( $data );
00097 
00103     abstract public function onSuccess();
00104 
00112     public function show() {
00113         $this->setHeaders();
00114 
00115         // This will throw exceptions if there's a problem
00116         $this->checkCanExecute( $this->getUser() );
00117 
00118         $form = $this->getForm();
00119         if ( $form->show() ) {
00120             $this->onSuccess();
00121         }
00122     }
00123 }