From e4992726cc41cbef54aead6dd28d010c1640419f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 8 Mar 2023 09:40:44 +1300 Subject: [PATCH] Extract isMembershipPriceSet (useForMember) --- CRM/Contribute/Form/Contribution/Main.php | 28 ++++---------------- CRM/Contribute/Form/ContributionBase.php | 32 ++++++++++++++++++++--- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index d09ea9c253..755e0b4d78 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -30,8 +30,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu public $_membershipTypeValues; - public $_useForMember; - /** * Array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl * @var array @@ -237,7 +235,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $entityId = $memtypeID = NULL; if ($this->_priceSetId) { - if (($this->_useForMember && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) { + if (($this->isMembershipPriceSet() && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) { $selectedCurrentMemTypes = []; foreach ($this->_priceSet['fields'] as $key => $val) { foreach ($val['options'] as $keys => $values) { @@ -357,24 +355,16 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } //build pledge block. - $this->_useForMember = 0; //don't build membership block when pledge_id is passed if (empty($this->_values['pledge_id']) && empty($this->_ccid)) { $this->_separateMembershipPayment = FALSE; if (CRM_Core_Component::isEnabled('CiviMember')) { - - if ($this->_priceSetId && - (CRM_Core_Component::getComponentID('CiviMember') == CRM_Utils_Array::value('extends', $this->_priceSet)) - ) { - $this->_useForMember = 1; - $this->set('useForMember', $this->_useForMember); - } - $this->_separateMembershipPayment = $this->buildMembershipBlock(); } $this->set('separateMembershipPayment', $this->_separateMembershipPayment); } - $this->assign('useForMember', $this->_useForMember); + + $this->assign('useForMember', (int) $this->isMembershipPriceSet()); // If we configured price set for contribution page // we are not allow membership signup as well as any // other contribution amount field, CRM-5095 @@ -392,14 +382,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } } - if ($this->_priceSetId && empty($this->_ccid)) { - $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config'); - if ($is_quick_config) { - $this->_useForMember = 0; - $this->set('useForMember', $this->_useForMember); - } - } - //we allow premium for pledge during pledge creation only. if (empty($this->_values['pledge_id']) && empty($this->_ccid)) { CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, TRUE); @@ -530,7 +512,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $this->_currentMemberships = []; $membershipTypeIds = $membershipTypes = $radio = $radioOptAttrs = []; - $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember); + $membershipPriceset = (!empty($this->_priceSetId) && $this->isMembershipPriceSet()); $allowAutoRenewMembership = $autoRenewOption = FALSE; $autoRenewMembershipTypeOptions = []; @@ -899,7 +881,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $errors["price_{$otherAmount}"] = ts('Amount is required field.'); } - if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) { + if ($self->isMembershipPriceSet() && !empty($check) && $membershipIsActive) { $priceFieldIDS = []; $priceFieldMemTypes = []; diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index cc1abed649..5d9c97dfdd 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -59,6 +59,13 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { public $_paymentObject = NULL; + /** + * Order object, used to calculate amounts, line items etc. + * + * @var \CRM_Financial_BAO_Order + */ + protected $order; + /** * The membership block for this page * @@ -332,9 +339,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { $this->_bltID = $this->get('bltID'); $this->_paymentProcessor = $this->get('paymentProcessor'); - // This get will ensure it is set for later. Once we are sure all places - // access via the get & not directly, it can go. - $this->getPriceSetID(); + $this->order = new CRM_Financial_BAO_Order(); + $this->order->setPriceSetID($this->getPriceSetID()); $this->_priceSet = $this->get('priceSet'); if (!$this->_values) { @@ -1280,6 +1286,26 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { return $this->_membershipBlock; } + /** + * Is a (non-quick-config) membership price set in use. + * + * @return bool + */ + protected function isMembershipPriceSet(): bool { + if ($this->_useForMember === NULL) { + if (CRM_Core_Component::isEnabled('CiviMember') && + (!$this->isQuickConfig() || !empty($this->_ccid)) && + CRM_Core_Component::getComponentID('CiviMember') === (int) $this->order->getPriceSetMetadata()['extends']) { + $this->_useForMember = 1; + } + else { + $this->_useForMember = 0; + } + $this->set('useForMember', $this->_useForMember); + } + return (bool) $this->_useForMember; + } + /** * Is the contribution page configured for 2 payments, one being membership & one not. * -- 2.25.1