Skip to content

Commit

Permalink
simplify calculation of lifetime memberships in CRM_Price_BAO_PriceSet
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaphoneJon committed Jan 31, 2023
1 parent 129c0d0 commit bd9e389
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions CRM/Price/BAO/PriceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,38 +878,18 @@ public static function buildPriceSet(&$form) {
}

/**
* Check the current Membership having end date null.
* Check for lifetime membership types this contact has that are in this price field.
*
* @param array $options
* @param int $userid
* Probably actually contact ID.
* @param int $contactId
*
* @return bool
*/
public static function checkCurrentMembership(&$options, $userid) {
if (!$userid || empty($options)) {
return FALSE;
}
static $_contact_memberships = [];
$checkLifetime = FALSE;
foreach ($options as $key => $value) {
if (!empty($value['membership_type_id'])) {
if (!isset($_contact_memberships[$userid][$value['membership_type_id']])) {
$_contact_memberships[$userid][$value['membership_type_id']] = CRM_Member_BAO_Membership::getContactMembership($userid, $value['membership_type_id'], FALSE);
}
$currentMembership = $_contact_memberships[$userid][$value['membership_type_id']];
if (!empty($currentMembership) && empty($currentMembership['end_date'])) {
unset($options[$key]);
$checkLifetime = TRUE;
}
}
}
if ($checkLifetime) {
return TRUE;
}
else {
return FALSE;
}
private static function checkCurrentMembership(array $options, int $contactId) : bool {
$contactsLifetimeMemberships = CRM_Member_BAO_Membership::getAllContactMembership($contactId, FALSE, TRUE);
$contactsLifetimeMembershipTypes = array_column($contactsLifetimeMemberships, 'membership_type_id');
$memTypeIdsInPriceField = array_column($options, 'membership_type_id');
return (bool) array_intersect($memTypeIdsInPriceField, $contactsLifetimeMembershipTypes);
}

/**
Expand Down Expand Up @@ -1681,17 +1661,17 @@ protected static function addPriceFieldsToForm(CRM_Core_Form $form, $feeBlock, b
$hideAdminValues = !CRM_Core_Permission::check('edit contributions');
// CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions
$adminFieldVisible = CRM_Core_Permission::check('administer CiviCRM');
$checklifetime = FALSE;
foreach ($feeBlock as $id => $field) {
if (CRM_Utils_Array::value('visibility', $field) == 'public' ||
(CRM_Utils_Array::value('visibility', $field) == 'admin' && $adminFieldVisible == TRUE) ||
!$validFieldsOnly
) {
$options = $field['options'] ?? NULL;
if ($className == 'CRM_Contribute_Form_Contribution_Main' && $component = 'membership') {
$userid = $form->getVar('_membershipContactID');
$checklifetime = self::checkCurrentMembership($options, $userid);
if ($checklifetime) {
$form->assign('ispricelifetime', TRUE);
$contactId = $form->getVar('_membershipContactID');
if ($contactId && $options) {
$checklifetime = $checklifetime ?: self::checkCurrentMembership($options, $contactId);
}
}

Expand Down Expand Up @@ -1719,6 +1699,7 @@ protected static function addPriceFieldsToForm(CRM_Core_Form $form, $feeBlock, b
}
}
}
$form->assign('ispricelifetime', $checklifetime);
}

}

0 comments on commit bd9e389

Please sign in to comment.