MediaWiki  REL1_23
FormAction.php
Go to the documentation of this file.
00001 <?php
00029 abstract class FormAction extends Action {
00030 
00035     abstract protected function getFormFields();
00036 
00041     protected function preText() {
00042         return '';
00043     }
00044 
00048     protected function postText() {
00049         return '';
00050     }
00051 
00056     protected function alterForm( HTMLForm $form ) {
00057     }
00058 
00063     protected function getForm() {
00064         $this->fields = $this->getFormFields();
00065 
00066         // Give hooks a chance to alter the form, adding extra fields or text etc
00067         wfRunHooks( 'ActionModifyFormFields', array( $this->getName(), &$this->fields, $this->page ) );
00068 
00069         $form = new HTMLForm( $this->fields, $this->getContext(), $this->getName() );
00070         $form->setSubmitCallback( array( $this, 'onSubmit' ) );
00071 
00072         // Retain query parameters (uselang etc)
00073         $form->addHiddenField( 'action', $this->getName() ); // Might not be the same as the query string
00074         $params = array_diff_key(
00075             $this->getRequest()->getQueryValues(),
00076             array( 'action' => null, 'title' => null )
00077         );
00078         $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
00079 
00080         $form->addPreText( $this->preText() );
00081         $form->addPostText( $this->postText() );
00082         $this->alterForm( $form );
00083 
00084         // Give hooks a chance to alter the form, adding extra fields or text etc
00085         wfRunHooks( 'ActionBeforeFormDisplay', array( $this->getName(), &$form, $this->page ) );
00086 
00087         return $form;
00088     }
00089 
00097     abstract public function onSubmit( $data );
00098 
00104     abstract public function onSuccess();
00105 
00113     public function show() {
00114         $this->setHeaders();
00115 
00116         // This will throw exceptions if there's a problem
00117         $this->checkCanExecute( $this->getUser() );
00118 
00119         $form = $this->getForm();
00120         if ( $form->show() ) {
00121             $this->onSuccess();
00122         }
00123     }
00124 
00133     public function execute( array $data = null, $captureErrors = true ) {
00134         try {
00135             // Set a new context so output doesn't leak.
00136             $this->context = clone $this->getContext();
00137 
00138             // This will throw exceptions if there's a problem
00139             $this->checkCanExecute( $this->getUser() );
00140 
00141             $fields = array();
00142             foreach ( $this->fields as $key => $params ) {
00143                 if ( isset( $data[$key] ) ) {
00144                     $fields[$key] = $data[$key];
00145                 } elseif ( isset( $params['default'] ) ) {
00146                     $fields[$key] = $params['default'];
00147                 } else {
00148                     $fields[$key] = null;
00149                 }
00150             }
00151             $status = $this->onSubmit( $fields );
00152             if ( $status === true ) {
00153                 // This might do permanent stuff
00154                 $this->onSuccess();
00155                 return true;
00156             } else {
00157                 return false;
00158             }
00159         }
00160         catch ( ErrorPageError $e ) {
00161             if ( $captureErrors ) {
00162                 return false;
00163             } else {
00164                 throw $e;
00165             }
00166         }
00167     }
00168 }