From 811a3ea4f2c2fafe88c516285f6768c5451e81c0 Mon Sep 17 00:00:00 2001 From: "Donald A. Lobo" Date: Thu, 2 Jan 2014 10:25:16 +0530 Subject: [PATCH] CRM-13606 - Include / Exclude custom Search does not work properly http://issues.civicrm.org/jira/browse/CRM-13606 --- CRM/Contact/Form/Search/Custom/Group.php | 41 ++++++++++++++---------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/CRM/Contact/Form/Search/Custom/Group.php b/CRM/Contact/Form/Search/Custom/Group.php index b40d55f122..8982da93c2 100644 --- a/CRM/Contact/Form/Search/Custom/Group.php +++ b/CRM/Contact/Form/Search/Custom/Group.php @@ -303,16 +303,19 @@ class CRM_Contact_Form_Search_Custom_Group //search for smart group contacts foreach ($this->_excludeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { - $ssId = CRM_Utils_Array::key($values, $smartGroup); - - $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); - - $smartSql = $smartSql . " AND contact_a.id NOT IN ( - SELECT contact_id FROM civicrm_group_contact - WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; - + $ssGroup = new CRM_Contact_DAO_Group(); + $ssGroup->id = $values; + if (!$ssGroup->find(TRUE)) { + CRM_Core_Error::fatal(); + } + CRM_Contact_BAO_GroupContactCache::load($ssGroup); + + $smartSql = " +SELECT gcc.contact_id +FROM civicrm_group_contact_cache gcc +WHERE gcc.group_id = {$ssGroup->id} +"; $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) $smartSql"; - CRM_Core_DAO::executeQuery($smartGroupQuery); } } @@ -366,18 +369,22 @@ class CRM_Contact_Form_Search_Custom_Group foreach ($this->_includeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { + $ssGroup = new CRM_Contact_DAO_Group(); + $ssGroup->id = $values; + if (!$ssGroup->find(TRUE)) { + CRM_Core_Error::fatal(); + } + CRM_Contact_BAO_GroupContactCache::load($ssGroup); - $ssId = CRM_Utils_Array::key($values, $smartGroup); - - $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); - - $smartSql .= " AND contact_a.id NOT IN ( - SELECT contact_id FROM civicrm_group_contact - WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; + $smartSql = " +SELECT gcc.contact_id +FROM civicrm_group_contact_cache gcc +WHERE gcc.group_id = {$ssGroup->id} +"; //used only when exclude group is selected if ($xGroups != 0) { - $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})"; + $smartSql .= " AND gcc.contact_id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})"; } $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id) -- 2.25.1