fixes to allow overwriting of existing membership defaults via url and code cleanup
authorKurund Jalmi <kurund@yahoo.com>
Fri, 17 Jul 2015 11:38:58 +0000 (17:08 +0530)
committerKurund Jalmi <kurund@yahoo.com>
Fri, 17 Jul 2015 11:38:58 +0000 (17:08 +0530)
CRM/Contribute/Form/Contribution/Main.php
CRM/Price/BAO/PriceSet.php

index 646397bf03f4b013aae26fc28e685c2e96ba7b13..e56ad218d6f1e6ee69ba6279f3c8869a2cf375a4 100644 (file)
@@ -333,30 +333,30 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
         foreach ($this->_priceSet['fields'] as $key => $val) {
           foreach ($val['options'] as $keys => $values) {
             $opMemTypeId = CRM_Utils_Array::value('membership_type_id', $values);
-            if ($opMemTypeId &&
+            $priceFieldName = 'price_' . $values['price_field_id'];
+            $priceFieldValue = CRM_Price_BAO_PriceSet::getPriceFieldValueFromURL($this, $priceFieldName);
+            if (!empty($priceFieldValue)) {
+              CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $this->_defaults);
+              // break here to prevent overwriting of default due to 'is_default'
+              // option configuration or setting of current membership or
+              // membership for related organization.
+              // The value sent via URL get's higher priority.
+              break;
+            }
+            elseif ($opMemTypeId &&
               in_array($opMemTypeId, $this->_currentMemberships) &&
               !in_array($opMemTypeId, $selectedCurrentMemTypes)
             ) {
-              if ($val['html_type'] == 'CheckBox') {
-                $this->_defaults["price_{$key}"][$keys] = 1;
-              }
-              else {
-                $this->_defaults["price_{$key}"] = $keys;
-              }
+              CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
               $selectedCurrentMemTypes[] = $values['membership_type_id'];
             }
             elseif (!empty($values['is_default']) &&
               !$opMemTypeId &&
-              (!isset($this->_defaults["price_{$key}"]) ||
-                ($val['html_type'] == 'CheckBox' && !isset($this->_defaults["price_{$key}"][$keys]))
-              )
-            ) {
-              if ($val['html_type'] == 'CheckBox') {
-                $this->_defaults["price_{$key}"][$keys] = 1;
-              }
-              else {
-                $this->_defaults["price_{$key}"] = $keys;
-              }
+              (!isset($this->_defaults[$priceFieldName]) ||
+                ($val['html_type'] == 'CheckBox' &&
+                !isset($this->_defaults[$priceFieldName][$keys]))
+              ) ) {
+              CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
             }
           }
         }
index c7acad678b2789cf6e5e7c9ff747b42abaf4d42d..9176d4ccd6cab4bbfbf146ed16d7ebe21c653eb7 100644 (file)
@@ -1046,30 +1046,38 @@ WHERE  id = %1";
         $priceFieldName = 'price_' . $values['price_field_id'];
         $priceFieldValue = self::getPriceFieldValueFromURL($form, $priceFieldName);
         if (!empty($priceFieldValue)) {
-          if ($val['html_type'] == 'CheckBox') {
-            $defaults[$priceFieldName][$priceFieldValue] = 1;
-          }
-          else {
-            $defaults[$priceFieldName] = $priceFieldValue;
-          }
-
+          self::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $defaults);
           // break here to prevent overwriting of default due to 'is_default'
           // option configuration. The value sent via URL get's higher priority.
           break;
         }
         elseif ($values['is_default']) {
-          if ($val['html_type'] == 'CheckBox') {
-            $defaults[$priceFieldName][$keys] = 1;
-          }
-          else {
-            $defaults[$priceFieldName] = $keys;
-          }
+          self::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $defaults);
         }
       }
     }
     return $defaults;
   }
 
+  /**
+   * Get the value of price field if passed via url
+   *
+   * @param string $priceFieldName
+   * @param string $priceFieldValue
+   * @param string $priceFieldType
+   * @param array $defaults
+   *
+   * @return void
+   */
+  public static function setDefaultPriceSetField($priceFieldName, $priceFieldValue, $priceFieldType, &$defaults) {
+    if ($priceFieldType == 'CheckBox') {
+      $defaults[$priceFieldName][$priceFieldValue] = 1;
+    }
+    else {
+      $defaults[$priceFieldName] = $priceFieldValue;
+    }
+  }
+
   /**
    * Get the value of price field if passed via url
    *