Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify calculation of lifetime memberships in CRM_Price_BAO_PriceSet #25456

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}