From 853c52a1e80a92e6fdc53d64a9cb20294387fc74 Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Wed, 2 Nov 2016 14:23:41 +0530 Subject: [PATCH] CRM-19589: Search for contacts in Smart Groups based on group status shows incorrect results --- CRM/Contact/BAO/Query.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index fc7f4862e2..147cb3b17c 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -2931,7 +2931,14 @@ class CRM_Contact_BAO_Query { $statii[] = '"Added"'; } - $ssClause = $this->addGroupContactCache($value, NULL, "contact_a", $op); + //CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table + $ssClause = NULL; + if (in_array("'Added'", $statii) || // if both Added and Removed statuses are selected + (count($statii) == 1 && $statii[0] == 'Removed') // if only Removed status is selected + ) { + $ssClause = $this->addGroupContactCache($value, NULL, "contact_a", $op); + } + $isSmart = (!$ssClause) ? FALSE : TRUE; if (!is_array($value) && count($statii) == 1 && @@ -2944,7 +2951,7 @@ class CRM_Contact_BAO_Query { } $groupClause = NULL; - if (!$isSmart) { + if (!$isSmart || in_array("'Added'", $statii)) { $groupIds = implode(',', (array) $value); $gcTable = "`civicrm_group_contact-{$groupIds}`"; $joinClause = array("contact_a.id = {$gcTable}.contact_id"); @@ -2978,6 +2985,7 @@ class CRM_Contact_BAO_Query { if (strpos($op, 'NULL') === FALSE) { $this->_qill[$grouping][] = ts("Group Status %1", array(1 => implode(' ' . ts('or') . ' ', $statii))); } + if ($groupClause) { $this->_where[$grouping][] = $groupClause; } -- 2.25.1