From: Jon Goldberg Date: Sun, 29 Jan 2023 22:00:35 +0000 (-0500) Subject: simplify calculation of lifetime memberships in CRM_Price_BAO_PriceSet X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=bd9e3893392eb2df301fafba1c4eaf6e3d7a3485;p=civicrm-core.git simplify calculation of lifetime memberships in CRM_Price_BAO_PriceSet --- diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index bfa8565048..30de2eaf36 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -878,38 +878,18 @@ WHERE id = %1"; } /** - * 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); } /** @@ -1681,6 +1661,7 @@ WHERE ct.id = cp.financial_type_id AND $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) || @@ -1688,10 +1669,9 @@ WHERE ct.id = cp.financial_type_id AND ) { $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); } } @@ -1719,6 +1699,7 @@ WHERE ct.id = cp.financial_type_id AND } } } + $form->assign('ispricelifetime', $checklifetime); } }