CRM-20955 - Allow contact's second membership to inherit when created in back end...
authorDave Jenkins <davej+git@circle-interactive.co.uk>
Mon, 24 Jul 2017 17:01:20 +0000 (18:01 +0100)
committerDave Jenkins <davej+git@circle-interactive.co.uk>
Mon, 24 Jul 2017 17:01:20 +0000 (18:01 +0100)
CRM/Member/BAO/Membership.php

index 6d206aa137b10365629e19600d18df5dc846214a..a5bea509fcf686d7ff9459f7d8ef17b4d72f764e 100644 (file)
@@ -1344,9 +1344,11 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
    * @throws \CRM_Core_Exception
    */
   public static function createRelatedMemberships(&$params, &$dao, $reset = FALSE) {
+    // CRM-4213 check for loops, using static variable to record contacts already processed.
     static $relatedContactIds = array();
     if ($reset) {
-      // not sure why a static var is in use here - we need a way to reset it from the test suite
+      // We need a way to reset this static variable from the test suite.
+      // @todo consider replacing with Civi::$statics but note reset now used elsewhere: CRM-17723.
       $relatedContactIds = array();
       return FALSE;
     }
@@ -1388,12 +1390,12 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
       );
     }
 
-    // check for loops. CRM-4213
-    // remove repeated related contacts, which already inherited membership.
-    $relatedContactIds[$membership->contact_id] = TRUE;
+    // CRM-4213, CRM-19735 check for loops, using static variable to record contacts already processed.
+    // Remove repeated related contacts, which already inherited membership of this type.
+    $relatedContactIds[$membership->contact_id][$membership->membership_type_id] = TRUE;
     foreach ($allRelatedContacts as $cid => $status) {
-      if (empty($relatedContactIds[$cid])) {
-        $relatedContactIds[$cid] = TRUE;
+      if (empty($relatedContactIds[$cid]) || empty($relatedContactIds[$cid][$membership->membership_type_id])) {
+        $relatedContactIds[$cid][$membership->membership_type_id] = TRUE;
 
         //don't create membership again for owner contact.
         $nestedRelationship = FALSE;