EBernhardson (WMF) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/61163


Change subject: HTMLCheckMatrix support for forcing options on/off
......................................................................

HTMLCheckMatrix support for forcing options on/off

Bug: 47743
Renamed remove-options parameter of HTMLCheckMatrix to force-options-on
and added additional force-options-off. Minor refactor of
PreferencesForm::filterDataForSubmit to move class specific code into their
respective classes.

Change-Id: I61a6b2bcce3102e2350088912ee77620a9f678f9
---
M includes/HTMLForm.php
M includes/Preferences.php
2 files changed, 72 insertions(+), 45 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/63/61163/1

diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php
index b7bdcfd..69fd13d 100644
--- a/includes/HTMLForm.php
+++ b/includes/HTMLForm.php
@@ -1068,6 +1068,7 @@
         * @return
         */
        function filterDataForSubmit( $data ) {
+
                return $data;
        }
 
@@ -1809,7 +1810,7 @@
  * options, uses an array of rows and an array of columns to dynamically
  * construct a matrix of options.
  */
-class HTMLCheckMatrix extends HTMLFormField {
+class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
 
        function validate( $value, $alldata ) {
                $rows = $this->mParams['rows'];
@@ -1874,22 +1875,24 @@
                        $rowContents = Html::rawElement( 'td', array(), 
$rowLabel );
                        foreach ( $columns as $columnTag ) {
                                // Knock out any options that are not wanted
-                               if ( isset( $this->mParams['remove-options'] )
-                                       && in_array( "$columnTag-$rowTag", 
$this->mParams['remove-options'] ) )
-                               {
-                                       $rowContents .= Html::rawElement( 'td', 
array(), ' ' );
-                               } else {
-                                       // Construct the checkbox
-                                       $thisAttribs = array(
-                                               'id' => 
"{$this->mID}-$columnTag-$rowTag",
-                                               'value' => $columnTag . '-' . 
$rowTag
-                                       );
-                                       $checkbox = Xml::check(
-                                               $this->mName . '[]',
-                                               in_array( $columnTag . '-' . 
$rowTag, (array)$value, true ),
-                                               $attribs + $thisAttribs );
-                                       $rowContents .= Html::rawElement( 'td', 
array(), $checkbox );
+                               $thisTag = "$columnTag-$rowTag";
+                               // Construct the checkbox
+                               $thisAttribs = array(
+                                       'id' => "{$this->mID}-$thisTag",
+                                       'value' => $thisTag,
+                               );
+                               $checked = in_array( $thisTag, (array)$value, 
true);
+                               if ( $this->isTagForcedOff( $thisTag ) ) {
+                                       $checked = false;
+                                       $thisAttribs['disabled'] = 1;
+                               } elseif ( $this->isTagForcedOn( $thisTag ) ) {
+                                       $checked = true;
+                                       $thisAttribs['disabled'] = 1;
                                }
+                               $rowContents .= Html::rawElement( 
+                                       'td', 
+                                       array(), 
+                                       Xml::check( "{$this->mName}[]", 
$checked, $attribs + $thisAttribs ) );
                        }
                        $tableContents .= Html::rawElement( 'tr', array(), 
"\n$rowContents\n" );
                }
@@ -1899,6 +1902,16 @@
                        Html::rawElement( 'tbody', array(), 
"\n$tableContents\n" ) ) . "\n";
 
                return $html;
+       }
+
+       protected function isTagForcedOff( $tag ) {
+               return isset( $this->mParams['force-options-off'] )
+                       && in_array( $tag, $this->mParams['force-options-off'] 
);
+       }
+
+       protected function isTagForcedOn( $tag ) {
+               return isset( $this->mParams['force-options-on'] )
+                       && in_array( $tag, $this->mParams['force-options-on'] );
        }
 
        /**
@@ -1964,6 +1977,23 @@
                } else {
                        return array();
                }
+       }
+
+       function filterDataForSubmit( $data ) {
+               $columns = HTMLFormField::flattenOptions( 
$this->mParams['columns'] );
+               $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
+               $res = array();
+               foreach ( $columns as $column ) {
+                       foreach ( $rows as $row ) {
+                               // Make sure option hasn't been forced
+                               $thisTag = "$column-$row";
+                               if ( !$this->isTagForcedOff( $thisTag ) && 
!$this->isTagForcedOn( $thisTag ) ) {
+                                       $res[$thisTag] = in_array( $thisTag, 
$data );
+                               }
+                       }
+               }
+
+               return $res;
        }
 }
 
@@ -2106,7 +2136,7 @@
 /**
  * Multi-select field
  */
-class HTMLMultiSelectField extends HTMLFormField {
+class HTMLMultiSelectField extends HTMLFormField implements 
HTMLNestedFilterable {
 
        function validate( $value, $alldata ) {
                $p = parent::validate( $value, $alldata );
@@ -2197,6 +2227,17 @@
                } else {
                        return array();
                }
+       }
+
+       function filterDataForSubmit( $data ) {
+        $options = HTMLFormField::flattenOptions( $this->mParams['options'] );
+
+               $res = array();
+        foreach ( $options as $opt ) {
+            $res["$opt"] = in_array( $opt, $data );
+        }
+
+               return $res;
        }
 
        protected function needsLabel() {
@@ -2638,3 +2679,12 @@
                return '';
        }
 }
+
+interface HTMLNestedFilterable {
+       /**
+        * Support for seperating multi-option preferences into multiple 
preferences
+        * Due to lack of array support.
+     * @param $data array
+        */
+       function filterDataForSubmit( $data );
+}
diff --git a/includes/Preferences.php b/includes/Preferences.php
index 44c87f0..48c9ac6 100644
--- a/includes/Preferences.php
+++ b/includes/Preferences.php
@@ -1562,37 +1562,14 @@
         * @return array
         */
        function filterDataForSubmit( $data ) {
-               // Support for separating multi-option preferences into 
multiple preferences
-               // Due to lack of array support.
                foreach ( $this->mFlatFields as $fieldname => $field ) {
-                       $info = $field->mParams;
-
-                       if ( $field instanceof HTMLMultiSelectField ) {
-                               $options = HTMLFormField::flattenOptions( 
$info['options'] );
+                       if ( $field instanceof HTMLNestedFilterable ) {
+                               $info = $field->mParams;
                                $prefix = isset( $info['prefix'] ) ? 
$info['prefix'] : $fieldname;
-
-                               foreach ( $options as $opt ) {
-                                       $data["$prefix$opt"] = in_array( $opt, 
$data[$fieldname] );
+                               foreach 
($field->filterDataForSubmit($data[$fieldname]) as $key => $value) {
+                                       $data["$prefix-$key"] = $value;
                                }
-
-                               unset( $data[$fieldname] );
-
-                       } elseif ( $field instanceof HTMLCheckMatrix ) {
-                               $columns = HTMLFormField::flattenOptions( 
$info['columns'] );
-                               $rows = HTMLFormField::flattenOptions( 
$info['rows'] );
-                               $prefix = isset( $info['prefix'] ) ? 
$info['prefix'] : $fieldname;
-                               foreach ( $columns as $column ) {
-                                       foreach ( $rows as $row ) {
-                                               // Make sure option hasn't been 
removed
-                                               if ( !isset( 
$info['remove-options'] )
-                                                       || !in_array( 
"$column-$row", $info['remove-options'] ) )
-                                               {
-                                                       
$data["$prefix-$column-$row"] = in_array( "$column-$row", $data[$fieldname] );
-                                               }
-                                       }
-                               }
-
-                               unset( $data[$fieldname] );
+                               unset($data[$fieldname]);
                        }
                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/61163
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I61a6b2bcce3102e2350088912ee77620a9f678f9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: EBernhardson (WMF) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to