htmlform: Simplify implementation of loadDataFromRequest()
[lhc/web/wiklou.git] / includes / htmlform / HTMLCheckField.php
index a0dd370..4942327 100644 (file)
@@ -5,6 +5,8 @@
  */
 class HTMLCheckField extends HTMLFormField {
        function getInputHTML( $value ) {
+               global $wgUseMediaWikiUIEverywhere;
+
                if ( !empty( $this->mParams['invert'] ) ) {
                        $value = !$value;
                }
@@ -18,18 +20,19 @@ class HTMLCheckField extends HTMLFormField {
                        $attr['class'] = $this->mClass;
                }
 
-               if ( $this->mParent->isVForm() ) {
-                       // Nest checkbox inside label.
-                       return Html::rawElement( 'label',
-                               array(
-                                       'class' => 'mw-ui-checkbox-label'
-                               ),
-                               Xml::check( $this->mName, $value, $attr ) . $this->mLabel );
-               } else {
-                       return Xml::check( $this->mName, $value, $attr )
-                       . ' '
-                       . Html::rawElement( 'label', array( 'for' => $this->mID ), $this->mLabel );
+               $chkLabel = Xml::check( $this->mName, $value, $attr )
+               . ' '
+               . Html::rawElement( 'label', array( 'for' => $this->mID ), $this->mLabel );
+
+               if ( $wgUseMediaWikiUIEverywhere || $this->mParent instanceof VFormHTMLForm ) {
+                       $chkLabel = Html::rawElement(
+                               'div',
+                               array( 'class' => 'mw-ui-checkbox' ),
+                               $chkLabel
+                       );
                }
+
+               return $chkLabel;
        }
 
        /**
@@ -55,23 +58,16 @@ class HTMLCheckField extends HTMLFormField {
         * @return string
         */
        function loadDataFromRequest( $request ) {
-               $invert = false;
-               if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
-                       $invert = true;
-               }
+               $invert = isset( $this->mParams['invert'] ) && $this->mParams['invert'];
 
                // GetCheck won't work like we want for checks.
                // 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
-                       // false  true  | true
-                       // false  false | false
-                       // true   false | true
-                       return $request->getBool( $this->mName ) xor $invert;
+                       return $invert
+                               ? !$request->getBool( $this->mName )
+                               : $request->getBool( $this->mName );
                } else {
                        return $this->getDefault();
                }