From 53aba436edd9fca0e83edd004e26b8ccb2864f97 Mon Sep 17 00:00:00 2001 From: larssandergreen Date: Tue, 13 Jun 2023 19:31:19 -0600 Subject: [PATCH] Prevent erroneous GroupContact hooks for Contact already in group --- CRM/Contact/BAO/GroupContact.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/CRM/Contact/BAO/GroupContact.php b/CRM/Contact/BAO/GroupContact.php index 2c2bfd50f9..114eba98e5 100644 --- a/CRM/Contact/BAO/GroupContact.php +++ b/CRM/Contact/BAO/GroupContact.php @@ -130,15 +130,10 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact implemen if (empty($contactIds) || empty($groupId)) { return []; } - - CRM_Utils_Hook::pre('create', 'GroupContact', $groupId, $contactIds); - $result = self::bulkAddContactsToGroup($contactIds, $groupId, $method, $status, $tracking); CRM_Contact_BAO_GroupContactCache::invalidateGroupContactCache($groupId); CRM_Contact_BAO_Contact_Utils::clearContactCaches(); - CRM_Utils_Hook::post('create', 'GroupContact', $groupId, $contactIds); - return [count($contactIds), $result['count_added'], $result['count_not_added']]; } @@ -711,23 +706,28 @@ AND contact_id IN ( $contactStr ) } $gcValues = $shValues = []; - foreach ($input as $cid) { + foreach ($input as $key => $cid) { if (isset($presentIDs[$cid])) { + unset($input[$key]); $numContactsNotAdded++; - continue; } - - $gcValues[] = "( $groupID, $cid, '$status' )"; - $shValues[] = "( $groupID, $cid, '$date', '$method', '$status', '$tracking' )"; - $numContactsAdded++; + else { + $gcValues[] = "( $groupID, $cid, '$status' )"; + $shValues[] = "( $groupID, $cid, '$date', '$method', '$status', '$tracking' )"; + $numContactsAdded++; + } } if (!empty($gcValues)) { + CRM_Utils_Hook::pre('create', 'GroupContact', $groupID, $input); + $cgSQL = $contactGroupSQL . implode(",\n", $gcValues); CRM_Core_DAO::executeQuery($cgSQL); $shSQL = $subscriptioHistorySQL . implode(",\n", $shValues); CRM_Core_DAO::executeQuery($shSQL); + + CRM_Utils_Hook::post('create', 'GroupContact', $groupID, $input); } } -- 2.25.1