Skip to content

Commit

Permalink
Fix issues with smart group search
Browse files Browse the repository at this point in the history
  • Loading branch information
Jitendra Purohit committed Jun 26, 2018
1 parent 06bd476 commit 2b231e0
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3048,9 +3048,25 @@ public function group($values) {
}

//CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table
if (count($smartGroupIDs)) {
$groupContactCacheClause = $this->addGroupContactCache($smartGroupIDs, NULL, "contact_a", $op);
$groupContactCacheClause = '';
if (count($smartGroupIDs) || empty($value)) {
$isNullOp = (strpos($op, 'NULL') !== FALSE);
$gccTableAlias = "`civicrm_group_contact_cache_";
$gccTableAlias .= ($isNullOp) ? "a`" : implode(',', $smartGroupIDs) . "`";
$groupContactCacheClause = $this->addGroupContactCache($smartGroupIDs, $gccTableAlias, "contact_a", $op);
if (!empty($groupContactCacheClause)) {
if ($isNotOp) {
$groupIds = implode(',', (array) $smartGroupIDs);
$gcTable = "`civicrm_group_contact-{$groupIds}`";
$joinClause = array("contact_a.id = {$gcTable}.contact_id");
$this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
if (strpos($op, 'IN') !== FALSE) {
$groupClause[] = "{$gcTable}.group_id $op ( $groupIds ) AND {$gccTableAlias}.group_id IS NULL";
}
else {
$groupClause[] = "{$gcTable}.group_id $op $groupIds AND {$gccTableAlias}.group_id IS NULL";
}
}
$groupClause[] = " ( {$groupContactCacheClause} ) ";
}
}
Expand Down Expand Up @@ -3131,16 +3147,19 @@ public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "
CRM_Contact_BAO_GroupContactCache::load($group);
}
}
if ($group->N == 0) {
if ($group->N == 0 && $op != 'NOT IN') {
return NULL;
}

if (!$tableAlias) {
$tableAlias = "`civicrm_group_contact_cache_";
$tableAlias .= ($isNullOp) ? "a`" : implode(',', (array) $groupsIds) . "`";
}

$this->_tables[$tableAlias] = $this->_whereTables[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.{$joinColumn} = {$tableAlias}.contact_id ";

if ($op == 'NOT IN') {
return "{$tableAlias}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( " . implode(',', (array) $groupsIds) . " ) )";
}
return self::buildClause("{$tableAlias}.group_id", $op, $groups, 'Int');
}

Expand Down

0 comments on commit 2b231e0

Please sign in to comment.