Skip to content

Commit

Permalink
[REF] Extract & stdise AmountBlockIsActive
Browse files Browse the repository at this point in the history
It took me a bit to figure out what it even meant - so extracted into a function which can provide explanation.

Part of trying to stop passing known values around - see pass to isSeparateMembershipTransaction
- that function should require no input params & be callable with consistent results anywhere
  • Loading branch information
eileenmcnaughton committed Dec 21, 2021
1 parent e28cb6a commit 01a312c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
14 changes: 6 additions & 8 deletions CRM/Contribute/Form/Contribution/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,7 @@ public function buildQuickForm() {
$this->buildCustom($this->_values['honoree_profile_id'], 'honoreeProfileFields', TRUE, 'honor', $fieldTypes);
}
$this->assign('receiptFromEmail', $this->_values['receipt_from_email'] ?? NULL);
$amount_block_is_active = $this->get('amount_block_is_active');
$this->assign('amount_block_is_active', $amount_block_is_active);
$this->assign('amount_block_is_active', $this->isFormSupportsNonMembershipContributions());

// Make a copy of line items array to use for display only
$tplLineItems = $this->_lineItem;
Expand Down Expand Up @@ -1439,9 +1438,9 @@ protected function processMembership($membershipParams, $contactID, $customField
//enabled and contribution amount is not selected. fix for CRM-3010
$isPaidMembership = TRUE;
}
$isProcessSeparateMembershipTransaction = $this->isSeparateMembershipTransaction($this->_id, $this->_values['amount_block_is_active']);
$isProcessSeparateMembershipTransaction = $this->isSeparateMembershipTransaction($this->_id);

if ($this->_values['amount_block_is_active']) {
if ($this->isFormSupportsNonMembershipContributions()) {
$financialTypeID = $this->_values['financial_type_id'];
}
else {
Expand Down Expand Up @@ -1877,13 +1876,12 @@ protected function processSecondaryFinancialTransaction($contactID, &$form, $tem
* transaction AND a membership transaction AND the payment processor supports double financial transactions (ie. NOT doTransferCheckout style)
*
* @param int $formID
* @param bool $amountBlockActiveOnForm
*
* @return bool
*/
public function isSeparateMembershipTransaction($formID, $amountBlockActiveOnForm) {
protected function isSeparateMembershipTransaction($formID): bool {
$memBlockDetails = CRM_Member_BAO_Membership::getMembershipBlock($formID);
if (!empty($memBlockDetails['is_separate_payment']) && $amountBlockActiveOnForm) {
if (!empty($memBlockDetails['is_separate_payment']) && $this->isFormSupportsNonMembershipContributions()) {
return TRUE;
}
return FALSE;
Expand Down Expand Up @@ -2495,7 +2493,7 @@ protected function doMembershipProcessing($contactID, $membershipParams, $premiu
if (!empty($membershipParams['selectMembership'])) {
// CRM-12233
$membershipLineItems = $formLineItems;
if ($this->_separateMembershipPayment && $this->_values['amount_block_is_active']) {
if ($this->_separateMembershipPayment && $this->isFormSupportsNonMembershipContributions()) {
$membershipLineItems = [];
foreach ($this->_values['fee'] as $key => $feeValues) {
if ($feeValues['name'] == 'membership_amount') {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ public static function formRule($fields, $files, $self) {

// CRM-12233
if ($membershipIsActive && empty($self->_membershipBlock['is_required'])
&& $self->_values['amount_block_is_active']
&& $self->isFormSupportsNonMembershipContributions()
) {
$membershipFieldId = $contributionFieldId = $errorKey = $otherFieldId = NULL;
foreach ($self->_values['fee'] as $fieldKey => $fieldValue) {
Expand Down
27 changes: 22 additions & 5 deletions CRM/Contribute/Form/ContributionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,15 @@ public function preProcess() {
// check if one of the (amount , membership) blocks is active or not.
$this->_membershipBlock = $this->get('membershipBlock');

if (!$this->_values['amount_block_is_active'] &&
if (!$this->isFormSupportsNonMembershipContributions() &&
!$this->_membershipBlock['is_active'] &&
!$this->_priceSetId
) {
CRM_Core_Error::statusBounce(ts('The requested online contribution page is missing a required Contribution Amount section or Membership section or Price Set. Please check with the site administrator for assistance.'));
}

if ($this->_values['amount_block_is_active']) {
$this->set('amount_block_is_active', $this->_values['amount_block_is_active']);
}
// This can probably go as nothing it 'getting it' anymore since the values data is loaded
// on every form, rather than being passed from form to form.
$this->set('amount_block_is_active', $this->isFormSupportsNonMembershipContributions());

$this->_contributeMode = $this->get('contributeMode');
$this->assign('contributeMode', $this->_contributeMode);
Expand Down Expand Up @@ -1241,4 +1240,22 @@ public function processAmountAndGetAutoRenew($fields, &$params, &$lineItems, $pr
}
}

/**
* Is payment for (non membership) contributions enabled on this form.
*
* This would be true in a case of contributions only or where both
* memberships and non-membership contributions are enabled (whether they
* are using quick config price sets or explicit price sets).
*
* The value is a database value in the config for the contribution page. It
* is loaded into values in ContributionBase::preProcess (called by this).
*
* @internal function is public to support validate but is for core use only.
*
* @return bool
*/
public function isFormSupportsNonMembershipContributions(): bool {
return (bool) ($this->_values['amount_block_is_active'] ?? FALSE);
}

}

0 comments on commit 01a312c

Please sign in to comment.