Merge "Added Range support to FileBackend::streamFile()"
[lhc/web/wiklou.git] / includes / htmlform / HTMLForm.php
index d671029..8ac4cf2 100644 (file)
@@ -169,6 +169,8 @@ class HTMLForm extends ContextSource {
        protected $mShowReset = false;
        protected $mShowSubmit = true;
        protected $mSubmitFlags = [ 'constructive', 'primary' ];
+       protected $mShowCancel = false;
+       protected $mCancelTarget;
 
        protected $mSubmitCallback;
        protected $mValidationErrorMessage;
@@ -569,7 +571,7 @@ class HTMLForm extends ContextSource {
 
                # Check for cancelled submission
                foreach ( $this->mFlatFields as $fieldname => $field ) {
-                       if ( !empty( $field->mParams['nodata'] ) ) {
+                       if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
                                continue;
                        }
                        if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
@@ -580,7 +582,7 @@ class HTMLForm extends ContextSource {
 
                # Check for validation
                foreach ( $this->mFlatFields as $fieldname => $field ) {
-                       if ( !empty( $field->mParams['nodata'] ) ) {
+                       if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
                                continue;
                        }
                        if ( $field->isHidden( $this->mFieldData ) ) {
@@ -894,6 +896,7 @@ class HTMLForm extends ContextSource {
         *  - id: (string, optional) DOM id for the button.
         *  - attribs: (array, optional) Additional HTML attributes.
         *  - flags: (string|string[], optional) OOUI flags.
+        *  - framed: (boolean=true, optional) OOUI framed attribute.
         * @return HTMLForm $this for chaining calls (since 1.20)
         */
        public function addButton( $data ) {
@@ -922,6 +925,7 @@ class HTMLForm extends ContextSource {
                        'id' => null,
                        'attribs' => null,
                        'flags' => null,
+                       'framed' => true,
                ];
 
                return $this;
@@ -1106,6 +1110,21 @@ class HTMLForm extends ContextSource {
                        ) . "\n";
                }
 
+               if ( $this->mShowCancel ) {
+                       $target = $this->mCancelTarget ?: Title::newMainPage();
+                       if ( $target instanceof Title ) {
+                               $target = $target->getLocalURL();
+                       }
+                       $buttons .= Html::element(
+                                       'a',
+                                       [
+                                               'class' => $useMediaWikiUIEverywhere ? 'mw-ui-button' : null,
+                                               'href' => $target,
+                                       ],
+                                       $this->msg( 'cancel' )->text()
+                               ) . "\n";
+               }
+
                // IE<8 has bugs with <button>, so we'll need to avoid them.
                $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
 
@@ -1117,7 +1136,7 @@ class HTMLForm extends ContextSource {
                        ];
 
                        if ( isset( $button['label-message'] ) ) {
-                               $label = $this->msg( $button['label-message'] )->parse();
+                               $label = $this->getMessage( $button['label-message'] )->parse();
                        } elseif ( isset( $button['label'] ) ) {
                                $label = htmlspecialchars( $button['label'] );
                        } elseif ( isset( $button['label-raw'] ) ) {
@@ -1198,17 +1217,10 @@ class HTMLForm extends ContextSource {
                $errorstr = '';
 
                foreach ( $errors as $error ) {
-                       if ( is_array( $error ) ) {
-                               $msg = array_shift( $error );
-                       } else {
-                               $msg = $error;
-                               $error = [];
-                       }
-
                        $errorstr .= Html::rawElement(
                                'li',
                                [],
-                               $this->msg( $msg, $error )->parse()
+                               $this->getMessage( $error )->parse()
                        );
                }
 
@@ -1233,17 +1245,25 @@ class HTMLForm extends ContextSource {
        /**
         * Identify that the submit button in the form has a destructive action
         * @since 1.24
+        *
+        * @return HTMLForm $this for chaining calls (since 1.28)
         */
        public function setSubmitDestructive() {
                $this->mSubmitFlags = [ 'destructive', 'primary' ];
+
+               return $this;
        }
 
        /**
         * Identify that the submit button in the form has a progressive action
         * @since 1.25
+        *
+        * @return HTMLForm $this for chaining calls (since 1.28)
         */
        public function setSubmitProgressive() {
                $this->mSubmitFlags = [ 'progressive', 'primary' ];
+
+               return $this;
        }
 
        /**
@@ -1323,6 +1343,28 @@ class HTMLForm extends ContextSource {
                return $this;
        }
 
+       /**
+        * Show a cancel button (or prevent it). The button is not shown by default.
+        * @param bool $show
+        * @return HTMLForm $this for chaining calls
+        * @since 1.27
+        */
+       public function showCancel( $show = true ) {
+               $this->mShowCancel = $show;
+               return $this;
+       }
+
+       /**
+        * Sets the target where the user is redirected to after clicking cancel.
+        * @param Title|string $target Target as a Title object or an URL
+        * @return HTMLForm $this for chaining calls
+        * @since 1.27
+        */
+       public function setCancelTarget( $target ) {
+               $this->mCancelTarget = $target;
+               return $this;
+       }
+
        /**
         * Set the id of the \<table\> or outermost \<div\> element.
         *
@@ -1725,4 +1767,14 @@ class HTMLForm extends ContextSource {
 
                return $this;
        }
+
+       /**
+        * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
+        * name + parameters array) into a Message.
+        * @param mixed $value
+        * @return Message
+        */
+       protected function getMessage( $value ) {
+               return Message::newFromSpecifier( $value )->setContext( $this );
+       }
 }