Merge pull request #4724 from atif-shaikh/CRM-15598
[civicrm-core.git] / CRM / Contact / BAO / GroupContactCache.php
index acda3a755da8734156da24c0611193e78ac998a8..bd0115dc10fcd4013a82122a0107e7559dd5c276 100644 (file)
@@ -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
    *