From bae894d85af1c37f2737ac7847e5cf74198b0283 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Wed, 18 Aug 2021 18:50:00 +0100 Subject: [PATCH] Handle checking smartgroups when generating list of groups to unsubscribe --- CRM/Mailing/Event/BAO/Unsubscribe.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/CRM/Mailing/Event/BAO/Unsubscribe.php b/CRM/Mailing/Event/BAO/Unsubscribe.php index 73f61f549c..f416648822 100644 --- a/CRM/Mailing/Event/BAO/Unsubscribe.php +++ b/CRM/Mailing/Event/BAO/Unsubscribe.php @@ -226,23 +226,33 @@ WHERE email = %2 $groupIdClause = ''; if ($groupIds || $baseGroupIds) { $groupIdClause = "AND grp.id IN (" . implode(', ', array_merge($groupIds, $baseGroupIds)) . ")"; + // Check that groupcontactcache is up to date so we can get smartgroups + CRM_Contact_BAO_GroupContactCache::check(array_merge($groupIds, $baseGroupIds)); } - $do = CRM_Core_DAO::executeQuery(" + + $groupsSQL = " SELECT grp.id as group_id, grp.title as title, grp.frontend_title as frontend_title, grp.frontend_description as frontend_description, - grp.description as description + grp.description as description, + grp.saved_search_id as saved_search_id FROM civicrm_group grp LEFT JOIN civicrm_group_contact gc ON gc.group_id = grp.id + LEFT JOIN civicrm_group_contact_cache gcc + ON gcc.group_id = grp.id WHERE grp.is_hidden = 0 $groupIdClause - AND (grp.saved_search_id is not null - OR (gc.contact_id = $contact_id + AND ((grp.saved_search_id is not null AND gcc.contact_id = %1) + OR (gc.contact_id = %1 AND gc.status = 'Added') $baseGroupClause - )"); + ) GROUP BY grp.id"; + $groupsParams = [ + 1 => [$contact_id, 'Positive'], + ]; + $do = CRM_Core_DAO::executeQuery($groupsSQL, $groupsParams); if ($return) { $returnGroups = []; -- 2.25.1