X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FContact%2FBAO%2FGroupContactCache.php;h=9c79144121e0ad78730d66a8b3ac101da666e47a;hb=21dfd5f567e7996ad92cf2a7d0fbe6f5fc205a5c;hp=a88d317325675d0aa44f7623b0036b4d1ed86331;hpb=845bc17a66f2ef2ca7c819fbe99ec646f0d42634;p=civicrm-core.git diff --git a/CRM/Contact/BAO/GroupContactCache.php b/CRM/Contact/BAO/GroupContactCache.php index a88d317325..9c79144121 100644 --- a/CRM/Contact/BAO/GroupContactCache.php +++ b/CRM/Contact/BAO/GroupContactCache.php @@ -1,7 +1,7 @@ array($groupID, 'Integer')); @@ -117,16 +118,17 @@ AND ( g.cache_date IS NULL OR * * @param int/array $groupID groupID of group that we are checking against * if empty, all groups are checked - * @param int $limit limits the number of groups we evaluate + * @param int $limit + * Limits the number of groups we evaluate. * * @return boolean true if we did not regenerate, false if we did */ - static function loadAll($groupIDs = null, $limit = 0) { + public static function loadAll($groupIDs = null, $limit = 0) { // ensure that all the smart groups are loaded // this function is expensive and should be sparingly used if groupIDs is empty if (empty($groupIDs)) { $groupIDClause = null; - $groupIDs = array( ); + $groupIDs = array(); } else { if (!is_array($groupIDs)) { @@ -193,16 +195,16 @@ AND g.refresh_date IS NULL /** * FIXME: This function should not be needed, because the cache table should not be getting truncated */ - static function fillIfEmpty() { + public static function fillIfEmpty() { if (!CRM_Core_DAO::singleValueQuery("SELECT COUNT(id) FROM civicrm_group_contact_cache")) { self::loadAll(); } } /** - * @param $groupID + * @param int $groupID */ - static function add($groupID) { + public static function add($groupID) { // first delete the current cache self::remove($groupID); if (!is_array($groupID)) { @@ -218,10 +220,10 @@ AND g.refresh_date IS NULL } /** - * @param $groupID + * @param int $groupID * @param $values */ - static function store(&$groupID, &$values) { + public static function store(&$groupID, &$values) { $processed = FALSE; // sort the values so we put group IDs in front and hence optimize @@ -242,10 +244,12 @@ AND g.refresh_date IS NULL /** * Change the cache_date * - * @param $groupID array(int) - * @param $processed bool, whether the cache data was recently modified + * @param $groupID + * Array(int). + * @param $processed + * Bool, whether the cache data was recently modified. */ - static function updateCacheTime($groupID, $processed) { + public static function updateCacheTime($groupID, $processed) { // only update cache entry if we had any values if ($processed) { // also update the group with cache date information @@ -275,14 +279,15 @@ WHERE id IN ( $groupIDs ) * cache date, i.e. the removal is not done if the group was recently * loaded into the cache. * - * @param $groupID int the groupID to delete cache entries, NULL for all groups - * @param $onceOnly boolean run the function exactly once for all groups. + * @param $groupID + * Int the groupID to delete cache entries, NULL for all groups. + * @param $onceOnly + * Boolean run the function exactly once for all groups. * - * @public * @return void * @static */ - static function remove($groupID = NULL, $onceOnly = TRUE) { + public static function remove($groupID = NULL, $onceOnly = TRUE) { static $invoked = FALSE; // typically this needs to happy only once per instance @@ -300,11 +305,13 @@ WHERE id IN ( $groupIDs ) if ($groupID == NULL) { $invoked = TRUE; - } else if (is_array($groupID)) { + } + elseif (is_array($groupID)) { foreach ($groupID as $gid) { unset(self::$_alreadyLoaded[$gid]); } - } else if ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) { + } + elseif ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) { unset(self::$_alreadyLoaded[$groupID]); } @@ -387,13 +394,39 @@ WHERE id = %1 } /** - * load the smart group cache for a saved search + * Removes one or more contacts from the smart group cache + * @param int|array $cid + * @param int $groupId + * @return bool - true if successful + */ + public 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 * - * @param object $group - the smart group that needs to be loaded - * @param boolean $force - should we force a search through + * @param object $group + * The smart group that needs to be loaded. + * @param bool $force + * Should we force a search through. * */ - static function load(&$group, $force = FALSE) { + public static function load(&$group, $force = FALSE) { $groupID = $group->id; $savedSearchID = $group->saved_search_id; if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) { @@ -553,7 +586,7 @@ AND civicrm_group_contact.group_id = $groupID "; /** * @return int */ - static function smartGroupCacheTimeout() { + public static function smartGroupCacheTimeout() { $config = CRM_Core_Config::singleton(); if ( @@ -572,12 +605,13 @@ AND civicrm_group_contact.group_id = $groupID "; * Note that this could potentially be a super slow function since * it ensure that all contact groups are loaded in the cache * - * @param int $contactID - * @param boolean $showHidden - hidden groups are shown only if this flag is set + * @param int $contactID + * @param bool $showHidden + * Hidden groups are shown only if this flag is set. * * @return array an array of groups that this contact belongs to */ - static function contactGroup($contactID, $showHidden = FALSE) { + public static function contactGroup($contactID, $showHidden = FALSE) { if (empty($contactID)) { return; } @@ -619,7 +653,7 @@ ORDER BY gc.contact_id, g.children $prevContactID = $dao->contact_id; if (!array_key_exists($dao->contact_id, $contactGroup)) { $contactGroup[$dao->contact_id] = - array( 'group' => array(), 'groupTitle' => array()); + array('group' => array(), 'groupTitle' => array()); } $contactGroup[$dao->contact_id]['group'][] = @@ -627,7 +661,7 @@ ORDER BY gc.contact_id, g.children 'id' => $dao->group_id, 'title' => $dao->title, 'description' => $dao->description, - 'children' => $dao->children + 'children' => $dao->children, ); $contactGroup[$dao->contact_id]['groupTitle'][] = $dao->title; } @@ -645,4 +679,3 @@ ORDER BY gc.contact_id, g.children } } -