simplify calculation of lifetime memberships in CRM_Price_BAO_PriceSet
authorJon Goldberg <jon@megaphonetech.com>
Sun, 29 Jan 2023 22:00:35 +0000 (17:00 -0500)
committerJon Goldberg <jon@megaphonetech.com>
Tue, 31 Jan 2023 15:13:36 +0000 (10:13 -0500)
CRM/Price/BAO/PriceSet.php

index bfa8565048203e8c8130bf8e8ddfe8633bb3939b..30de2eaf3686ed0b943f2712d6281966e266115c 100644 (file)
@@ -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);
   }
 
 }