Allow extra buttons and hidden fields to be included in HTMLForm. Accessible either...
authorHappy-melon <happy-melon@users.mediawiki.org>
Tue, 22 Sep 2009 20:05:28 +0000 (20:05 +0000)
committerHappy-melon <happy-melon@users.mediawiki.org>
Tue, 22 Sep 2009 20:05:28 +0000 (20:05 +0000)
includes/HTMLForm.php
includes/specials/SpecialCreateAccount.php
includes/specials/SpecialUserlogin.php

index 1764f6e..25067ed 100644 (file)
@@ -60,6 +60,9 @@ class HTMLForm {
                'float' => 'HTMLFloatField',
                'info' => 'HTMLInfoField',
                'selectorother' => 'HTMLSelectOrOtherField',
+               'submit' => 'HTMLSubmitField',
+               'hidden' => 'HTMLHiddenField',
+       
                # HTMLTextField will output the correct type="" attribute automagically.
                # There are about four zillion other HTML 5 input types, like url, but
                # we don't use those at the moment, so no point in adding all of them.
@@ -78,6 +81,9 @@ class HTMLForm {
        protected $mSubmitID;
        protected $mSubmitText;
        protected $mTitle;
+       
+       protected $mHiddenFields = array();
+       protected $mButtons = array();
 
        /**
         * Build a new HTMLForm from an array of field attributes
@@ -249,6 +255,19 @@ class HTMLForm {
        function setIntro( $msg ) {
                $this->mIntro = $msg;
        }
+       
+       /**
+        * Add a hidden field to the output
+        * @param $name String field name
+        * @param $value String field value
+        */
+       public function addHiddenField( $name, $value ){
+               $this->mHiddenFields[ $name ] = $value;
+       }
+       
+       public function addButton( $name, $value, $id=null ){
+               $this->mButtons[] = compact( 'name', 'value', 'id' );
+       }
 
        /**
         * Display the form (sending to wgOut), with an appropriate error 
@@ -305,6 +324,10 @@ class HTMLForm {
 
                $html .= Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
                $html .= Html::hidden( 'title', $this->getTitle() ) . "\n";
+               
+               foreach( $this->mHiddenFields as $name => $value ){
+                       $html .= Html::hidden( $name, $value ) . "\n";
+               }
 
                return $html;
        }
@@ -335,6 +358,17 @@ class HTMLForm {
                        ) . "\n";
                }
                
+               foreach( $this->mButtons as $button ){
+                       $attrs = array(
+                               'type'  => 'submit',
+                               'name'  => $button['name'],
+                               'value' => $button['value']
+                       );
+                       if( isset( $button['id'] ) )
+                               $attrs['id'] = $button['id'];
+                       $html .= Html::element( 'input', $attrs );
+               }
+               
                return $html;
        }
 
@@ -554,7 +588,7 @@ abstract class HTMLFormField {
         * that the user-defined callback mValidationCallback is still run
         * @param $value String the value the field was submitted with
         * @param $alldata $all the data collected from the form
-        * @return Bool is the input valid
+        * @return Mixed Bool true on success, or String error to display.
         */
        function validate( $value, $alldata ) {
                if ( isset( $this->mValidationCallback ) ) {
@@ -1188,3 +1222,30 @@ class HTMLInfoField extends HTMLFormField {
                return false;
        }
 }
+
+class HTMLHiddenField extends HTMLFormField {
+       
+       public function getTableRow( $value ){
+               $this->mParent->addHiddenField( 
+                       $this->mParams['name'],
+                       $this->mParams['default']
+               );
+               return '';
+       }
+
+       public function getInputHTML( $value ){ return ''; }
+}
+
+class HTMLSubmitField extends HTMLFormField {
+       
+       public function getTableRow( $value ){
+               $this->mParent->addButton(
+                       $this->mParams['name'],
+                       $this->mParams['default'],
+                       isset($this->mParams['id']) ? $this->mParams['id'] : null 
+               );
+       }
+       
+       public function getInputHTML( $value ){ return ''; }
+}
+
index 6e05f5f..2d59f2b 100644 (file)
@@ -357,21 +357,7 @@ class SpecialCreateAccount extends SpecialPage {
                                array( 'id' => 'languagelinks' ),
                                SpecialUserLogin::makeLanguageSelector( $this->getTitle(), $this->mReturnTo ) )
                        : '';
-               
-               # Add a  'send password by email' button if available
-               $buttons = '';
-               if( $wgEnableEmail && $wgUser->isLoggedIn() ){
-                       $buttons = Html::element(
-                               'input',
-                               array( 
-                                       'type'  => 'submit',
-                                       'name'  => 'wpCreateaccountMail',
-                                       'value' => wfMsg( 'createaccountmail' ),
-                                       'id'    => 'wpCreateaccountMail',
-                               ) 
-                       );
-               }
-               
+                               
                # Give authentication and captcha plugins a chance to 
                # modify the form, by hook or by using $wgAuth
                $wgAuth->modifyUITemplate( $this, 'new' );
@@ -419,6 +405,17 @@ class SpecialCreateAccount extends SpecialPage {
                $form->setSubmitId( 'wpCreateaccount' );
                $form->suppressReset();
                $form->loadData();
+               $form->addHiddenField( 'returnto', $this->mReturnTo );
+               $form->addHiddenField( 'returntoquery', $this->mReturnToQuery );
+               
+               # Add a  'send password by email' button if available
+               if( $wgEnableEmail && $wgUser->isLoggedIn() ){
+                       $form->addButton(
+                               'wpCreateaccountMail',
+                               wfMsg( 'createaccountmail' ),
+                               'wpCreateaccountMail'
+                       );
+               }
                
                $formContents = '' 
                        . Html::rawElement( 'p', array( 'id' => 'userloginlink' ),
@@ -426,10 +423,8 @@ class SpecialCreateAccount extends SpecialPage {
                        . $this->mFormHeader
                        . $langSelector
                        . $form->getBody() 
+                       . $form->getHiddenFields()
                        . $form->getButtons()
-                       . $buttons
-                       . Xml::hidden( 'returnto', $this->mReturnTo )
-                       . Xml::hidden( 'returntoquery', $this->mReturnToQuery )
                ;
 
                $wgOut->setPageTitle( wfMsg( 'createaccount' ) );
index a9b57d9..6be4038 100644 (file)
@@ -192,20 +192,6 @@ class SpecialUserLogin extends SpecialPage {
                                array( 'id' => 'languagelinks' ),
                                self::makeLanguageSelector( $this->getTitle(), $this->mReturnTo ) )
                        : '';
-               
-               # Add a  'mail reset' button if available
-               $buttons = '';
-               if( $wgEnableEmail && $wgAuth->allowPasswordChange() ){
-                       $buttons = Html::element(
-                               'input',
-                               array( 
-                                       'type'  => 'submit',
-                                       'name'  => 'wpMailmypassword',
-                                       'value' => wfMsg( 'mailmypassword' ),
-                                       'id'    => 'wpMailmypassword',
-                               ) 
-                       );
-               }
 
                # Give authentication and captcha plugins a chance to 
                # modify the form, by hook or by using $wgAuth
@@ -237,6 +223,18 @@ class SpecialUserLogin extends SpecialPage {
                $form->setSubmitId( 'wpLoginAttempt' );
                $form->suppressReset();
                $form->loadData();
+               $form->addHiddenField( 'returnto', $this->mReturnTo );
+               $form->addHiddenField( 'returntoquery', $this->mReturnToQuery );
+               
+               # Add a  'mail reset' button if available
+               $buttons = '';
+               if( $wgEnableEmail && $wgAuth->allowPasswordChange() ){
+                       $form->addButton(
+                               'wpMailmypassword',
+                               wfMsg( 'mailmypassword' ),
+                               'wpMailmypassword'
+                       );
+               }
                
                $formContents = '' 
                        . Html::rawElement( 'p', array( 'id' => 'userloginlink' ),
@@ -246,10 +244,8 @@ class SpecialUserLogin extends SpecialPage {
                        . $this->mFormHeader
                        . $langSelector
                        . $form->getBody() 
+                       . $form->getHiddenFields()
                        . $form->getButtons()
-                       . $buttons
-                       . Xml::hidden( 'returnto', $this->mReturnTo )
-                       . Xml::hidden( 'returntoquery', $this->mReturnToQuery )
                ;
 
                $wgOut->setPageTitle( wfMsg( 'login' ) );