From 034b5cb65179c3f00393bb428a8390a508058e11 Mon Sep 17 00:00:00 2001 From: Kurund Jalmi Date: Fri, 17 Jul 2015 17:08:58 +0530 Subject: [PATCH] fixes to allow overwriting of existing membership defaults via url and code cleanup --- CRM/Contribute/Form/Contribution/Main.php | 34 +++++++++++------------ CRM/Price/BAO/PriceSet.php | 34 ++++++++++++++--------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 646397bf03..e56ad218d6 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -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); } } } diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index c7acad678b..9176d4ccd6 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -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 * -- 2.25.1