From 2c6bbd0646494e3ce48ea7e87f88c90dfaf5f64a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 10 Dec 2014 17:30:37 -0500 Subject: [PATCH] CRM-15711 - Improve efficiency of deleting a contact --- CRM/Contact/BAO/Contact.php | 9 +++++++-- CRM/Contact/BAO/GroupContactCache.php | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 0ced7cfa72..9bba46e9c2 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -860,8 +860,13 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); //delete the contact id from recently view CRM_Utils_Recent::delContact($id); - // reset the group contact cache for this group - CRM_Contact_BAO_GroupContactCache::remove(); + // Update the group contact cache + if ($restore) { + CRM_Contact_BAO_GroupContactCache::remove(); + } + else { + CRM_Contact_BAO_GroupContactCache::removeContact($id); + } // delete any dupe cache entry CRM_Core_BAO_PrevNextCache::deleteItem($id); diff --git a/CRM/Contact/BAO/GroupContactCache.php b/CRM/Contact/BAO/GroupContactCache.php index acda3a755d..bd0115dc10 100644 --- a/CRM/Contact/BAO/GroupContactCache.php +++ b/CRM/Contact/BAO/GroupContactCache.php @@ -384,6 +384,30 @@ WHERE id = %1 CRM_Core_DAO::executeQuery($update, $params); } + /** + * Removes one or more contacts from the smart group cache + * @param int|array $cid + * @param int $groupId + * @return bool - true if successful + */ + static function removeContact($cid, $groupId = NULL) { + $cids = array(); + // sanitize input + foreach ((array) $cid as $c) { + $cids[] = CRM_Utils_Type::escape($c, 'Integer'); + } + if ($cids) { + $condition = count($cids) == 1 ? "= {$cids[0]}" : "IN (" . implode(',', $cids) . ")"; + if ($groupId) { + $condition .= " AND group_id = " . CRM_Utils_Type::escape($groupId, 'Integer'); + } + $sql = "DELETE FROM civicrm_group_contact_cache WHERE contact_id $condition"; + CRM_Core_DAO::executeQuery($sql); + return TRUE; + } + return FALSE; + } + /** * Load the smart group cache for a saved search * -- 2.25.1