If you request LogEventList to display the combination of 2 log types, and one of
[lhc/web/wiklou.git] / includes / HTMLForm.php
index 11e477b..5c0d7db 100644 (file)
  *     'help-message'        -- message key for a message to use as a help text.
  *                              can be an array of msg key and then parameters to
  *                              the message.
- *                           Overwrites 'help-messages'.
- *  'help-messages'       -- array of message key. As above, each item can
- *                           be an array of msg key and then parameters.
- *                           Overwrites 'help-message'.
+ *                              Overwrites 'help-messages'.
+ *     'help-messages'       -- array of message key. As above, each item can
+ *                              be an array of msg key and then parameters.
+ *                              Overwrites 'help-message'.
  *     'required'            -- passed through to the object, indicating that it
  *                              is a required field.
  *     'size'                -- the length of text fields
@@ -53,7 +53,7 @@
  *
  * TODO: Document 'section' / 'subsection' stuff
  */
-class HTMLForm {
+class HTMLForm extends ContextSource {
 
        # A mapping of 'type' inputs onto standard HTMLFormField subclasses
        static $typeMappings = array(
@@ -105,7 +105,6 @@ class HTMLForm {
        protected $mSubmitText;
        protected $mSubmitTooltip;
 
-       protected $mContext; // <! IContextSource
        protected $mTitle;
        protected $mMethod = 'post';
 
@@ -114,6 +113,15 @@ class HTMLForm {
        protected $mButtons = array();
 
        protected $mWrapperLegend = false;
+       
+       /**
+        * If true, sections that contain both fields and subsections will
+        * render their subsections before their fields.
+        * 
+        * Subclasses may set this to false to render subsections after fields
+        * instead.
+        */
+       protected $mSubSectionBeforeFields = true;
 
        /**
         * Build a new HTMLForm from an array of field attributes
@@ -124,7 +132,7 @@ class HTMLForm {
         */
        public function __construct( $descriptor, /*IContextSource*/ $context = null, $messagePrefix = '' ) {
                if( $context instanceof IContextSource ){
-                       $this->mContext = $context;
+                       $this->setContext( $context );
                        $this->mTitle = false; // We don't need them to set a title
                        $this->mMessagePrefix = $messagePrefix;
                } else {
@@ -178,7 +186,7 @@ class HTMLForm {
         * done already.
         * @deprecated since 1.18 load modules with ResourceLoader instead
         */
-       static function addJS() { }
+       static function addJS() { wfDeprecated( __METHOD__, '1.18' ); }
 
        /**
         * Initialise a new Object for the field
@@ -310,7 +318,16 @@ class HTMLForm {
         * Set the introductory message, overwriting any existing message.
         * @param $msg String complete text of message to display
         */
-       function setIntro( $msg ) { $this->mPre = $msg; }
+       function setIntro( $msg ) {
+               $this->setPreText( $msg );
+       }
+
+       /**
+        * Set the introductory message, overwriting any existing message.
+        * @since 1.19
+        * @param $msg String complete text of message to display
+        */
+       function setPreText( $msg ) { $this->mPre = $msg; }
 
        /**
         * Add introductory text.
@@ -334,6 +351,20 @@ class HTMLForm {
                }
        }
 
+       /**
+        * Set header text, inside the form.
+        * @since 1.19
+        * @param $msg String complete text of message to display
+        * @param $section The section to add the header to
+        */
+       function setHeaderText( $msg, $section = null ) {
+               if ( is_null( $section ) ) {
+                       $this->mHeader = $msg;
+               } else {
+                       $this->mSectionHeaders[$section] = $msg;
+               }
+       }
+
        /**
         * Add footer text, inside the form.
         * @param $msg String complete text of message to display
@@ -350,12 +381,32 @@ class HTMLForm {
                }
        }
 
+       /**
+        * Set footer text, inside the form.
+        * @since 1.19
+        * @param $msg String complete text of message to display
+        * @param $section string The section to add the footer text to
+        */
+       function setFooterText( $msg, $section = null ) {
+               if ( is_null( $section ) ) {
+                       $this->mFooter = $msg;
+               } else {
+                       $this->mSectionFooters[$section] = $msg;
+               }
+       }
+
        /**
         * Add text to the end of the display.
         * @param $msg String complete text of message to display
         */
        function addPostText( $msg ) { $this->mPost .= $msg; }
 
+       /**
+        * Set text at the end of the display.
+        * @param $msg String complete text of message to display
+        */
+       function setPostText( $msg ) { $this->mPost = $msg; }
+
        /**
         * Add a hidden field to the output
         * @param $name String field name.  This will be used exactly as entered
@@ -442,7 +493,7 @@ class HTMLForm {
 
                $html = '';
                if( $this->getMethod() == 'post' ){
-                       $html .= Html::hidden( 'wpEditToken', $this->getUser()->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
+                       $html .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
                        $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
                }
 
@@ -586,7 +637,7 @@ class HTMLForm {
         * @param $msg String message key
         */
        public function setSubmitTextMsg( $msg ) {
-               return $this->setSubmitText( wfMessage( $msg )->escaped() );
+               return $this->setSubmitText( $this->msg( $msg )->escaped() );
        }
 
        /**
@@ -634,7 +685,7 @@ class HTMLForm {
         * @param $msg String message key
         */
        public function setWrapperLegendMsg( $msg ) {
-               return $this->setWrapperLegend( wfMessage( $msg )->escaped() );
+               return $this->setWrapperLegend( $this->msg( $msg )->escaped() );
        }
 
        /**
@@ -665,36 +716,6 @@ class HTMLForm {
                        : $this->mTitle;
        }
 
-       /**
-        * @return IContextSource
-        */
-       public function getContext(){
-               return $this->mContext instanceof IContextSource
-                       ? $this->mContext
-                       : RequestContext::getMain();
-       }
-
-       /**
-        * @return OutputPage
-        */
-       public function getOutput(){
-               return $this->getContext()->getOutput();
-       }
-
-       /**
-        * @return WebRequest
-        */
-       public function getRequest(){
-               return $this->getContext()->getRequest();
-       }
-
-       /**
-        * @return User
-        */
-       public function getUser(){
-               return $this->getContext()->getUser();
-       }
-
        /**
         * Set the method used to submit the form
         * @param $method String
@@ -763,7 +784,11 @@ class HTMLForm {
                $tableHtml = Html::rawElement( 'table', $attribs,
                        Html::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
 
-               return $subsectionHtml . "\n" . $tableHtml;
+               if ( $this->mSubSectionBeforeFields ) {
+                       return $subsectionHtml . "\n" . $tableHtml;
+               } else {
+                       return $tableHtml . "\n" . $subsectionHtml;
+               }
        }
 
        /**
@@ -866,7 +891,7 @@ abstract class HTMLFormField {
                }
 
                if ( isset( $this->mValidationCallback ) ) {
-                       return call_user_func( $this->mValidationCallback, $value, $alldata );
+                       return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
                }
 
                return true;
@@ -874,7 +899,7 @@ abstract class HTMLFormField {
 
        function filter( $value, $alldata ) {
                if ( isset( $this->mFilterCallback ) ) {
-                       $value = call_user_func( $this->mFilterCallback, $value, $alldata );
+                       $value = call_user_func( $this->mFilterCallback, $value, $alldata, $this->mParent );
                }
 
                return $value;
@@ -1351,7 +1376,10 @@ class HTMLCheckField extends HTMLFormField {
                }
 
                // GetCheck won't work like we want for checks.
-               if ( $request->getCheck( 'wpEditToken' ) || $this->mParent->getMethod() != 'post' ) {
+               // Fetch the value in either one of the two following case:
+               // - we have a valid token (form got posted or GET forged by the user)
+               // - checkbox name has a value (false or true), ie is not null
+               if ( $request->getCheck( 'wpEditToken' ) || $request->getVal( $this->mName )!== null ) {
                        // XOR has the following truth table, which is what we want
                        // INVERT VALUE | OUTPUT
                        // true   true  | false