From 71c79f0d0465f9789111981ab4b15a36f8aacbe6 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 19 Jul 2015 21:31:15 +1200 Subject: [PATCH] CRM-16867 fix membership-renew check to get membership types from price fields --- CRM/Contribute/BAO/ContributionPage.php | 3 -- .../Form/ContributionPage/Amount.php | 22 ++++++--- .../Form/ContributionPage/Custom.php | 5 +- CRM/Price/BAO/PriceSet.php | 48 +++++++++++++++++++ 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionPage.php b/CRM/Contribute/BAO/ContributionPage.php index 3c0a4efd34..96caefc1f0 100644 --- a/CRM/Contribute/BAO/ContributionPage.php +++ b/CRM/Contribute/BAO/ContributionPage.php @@ -26,11 +26,8 @@ */ /** - * * @package CRM * @copyright CiviCRM LLC (c) 2004-2015 - * $Id$ - * */ /** diff --git a/CRM/Contribute/Form/ContributionPage/Amount.php b/CRM/Contribute/Form/ContributionPage/Amount.php index 5c40c35de8..6830b6899a 100644 --- a/CRM/Contribute/Form/ContributionPage/Amount.php +++ b/CRM/Contribute/Form/ContributionPage/Amount.php @@ -299,12 +299,20 @@ SELECT id $membershipBlock->entity_id = $self->_id; $membershipBlock->is_active = 1; $hasMembershipBlk = FALSE; + if ($membershipBlock->find(TRUE)) { + $setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $self->_id, NULL, NULL); if (!empty($fields['amount_block_is_active']) && - ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $self->_id, NULL, 1)) + // I don't think setID could ever not be set. + ($setID) ) { - $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends'); - if ($extends && $extends == CRM_Core_Component::getComponentID('CiviMember')) { + // If we are using quick config for the contribution part we need quick config for the membership part. + // Note that having 2 price sets against a page may allow our code to be more sensible when managing separate payments. + // The quick config is intended to make it possible not to force people to use price sets until the price set UI + // is easier to use, but should have no effect on form processing but the separate membership payment complicates this + // as there is no linking of the contribution price set to the page. + $priceSet = civicrm_api3('PriceSet', 'getsingle', array('id' => $setID)); + if (empty($priceSet['is_quick_config']) && CRM_Utils_Array::value('extends', $priceSet) == CRM_Core_Component::getComponentID('CiviMember')) { $errors['amount_block_is_active'] = ts('You cannot use a Membership Price Set when the Contribution Amounts section is enabled. Click the Memberships tab above, and select your Membership Price Set on that form. Membership Price Sets may include additional fields for non-membership options that require an additional fee (e.g. magazine subscription) or an additional voluntary contribution.'); return $errors; } @@ -314,13 +322,13 @@ SELECT id $errors['amount_block_is_active'] = ts('To disable Contribution Amounts section you need to first disable Separate Membership Payment option from Membership Settings.'); } - //CRM-16165, Don't allow reccuring contribution if membership block contain any renewable membership option - $membershipTypes = unserialize($membershipBlock->membership_types); - if (!empty($fields['is_recur']) && !empty($membershipTypes)) { + //CRM-16165, Don't allow recurring contribution if membership block contain any renewable membership option + $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($setID); + if (!empty($fields['is_recur']) && !empty($membershipTypes['autorenew'])) { if (!$membershipBlock->is_separate_payment) { $errors['is_recur'] = ts('You need to enable Separate Membership Payment when online contribution page is configured for both Membership and Recurring Contribution.'); } - elseif (count(array_filter($membershipTypes)) != 0) { + else { $errors['is_recur'] = ts('You cannot enable both Recurring Contributions and Auto-renew memberships on the same online contribution page.'); } } diff --git a/CRM/Contribute/Form/ContributionPage/Custom.php b/CRM/Contribute/Form/ContributionPage/Custom.php index ea730d09f2..41af24bfe0 100644 --- a/CRM/Contribute/Form/ContributionPage/Custom.php +++ b/CRM/Contribute/Form/ContributionPage/Custom.php @@ -26,15 +26,12 @@ */ /** - * * @package CRM * @copyright CiviCRM LLC (c) 2004-2015 - * $Id$ - * */ /** - * form to process actions on the group aspect of Custom Data + * Form to process actions on the group aspect of Custom Data. */ class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage { diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index e79e9bdad5..3d563f77bd 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -1382,6 +1382,54 @@ WHERE ps.id = %1 return FALSE; } + /** + * Get an array of the membership types in a price set. + * + * @param int $id + * + * @return array( + * Membership types in the price set + */ + static function getMembershipTypesFromPriceSet($id) { + $query + = "SELECT pfv.id, pfv.price_field_id, pfv.name, pfv.membership_type_id, pf.html_type, mt.auto_renew +FROM civicrm_price_field_value pfv +LEFT JOIN civicrm_price_field pf ON pf.id = pfv.price_field_id +LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id +LEFT JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id +WHERE ps.id = %1 +"; + + $params = array(1 => array($id, 'Integer')); + $dao = CRM_Core_DAO::executeQuery($query, $params); + + $membershipTypes = array( + 'all' => array(), + 'autorenew' => array(), + 'autorenew_required' => array(), + 'autorenew_optional' => array(), + ); + while ($dao->fetch()) { + if (empty($dao->membership_type_id)) { + continue; + } + $membershipTypes['all'][] = $dao->membership_type_id; + if (!empty($dao->auto_renew)) { + $membershipTypes['autorenew'][] = $dao->membership_type_id; + if ($dao->auto_renew == 2) { + $membershipTypes['autorenew_required'][] = $dao->membership_type_id; + } + else { + $membershipTypes['autorenew_optional'][] = $dao->membership_type_id; + } + } + else { + $membershipTypes['non_renew'][] = $dao->membership_type_id; + } + } + return $membershipTypes; + } + /** * Copy priceSet when event/contibution page is copied * -- 2.25.1