3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Contact_BAO_GroupContactCache
extends CRM_Contact_DAO_GroupContactCache
{
37 static $_alreadyLoaded = array();
40 * Check to see if we have cache entries for this group
41 * if not, regenerate, else return
45 * @internal param int $groupID groupID of group that we are checking against
47 * @return boolean true if we did not regenerate, false if we did
49 static function check($groupIDs) {
50 if (empty($groupIDs)) {
54 return self
::loadAll($groupIDs);
58 * Common function that formulates the query to see which groups needs to be refreshed
59 * based on their cache date and the smartGroupCacheTimeOut
61 * @param string $groupIDClause the clause which limits which groups we need to evaluate
62 * @param boolean $includeHiddenGroups hidden groups are excluded by default
64 * @return string the sql query which lists the groups that need to be refreshed
68 static function groupRefreshedClause($groupIDClause = null, $includeHiddenGroups = FALSE) {
69 $smartGroupCacheTimeout = self
::smartGroupCacheTimeout();
70 $now = CRM_Utils_Date
::getUTCTime();
75 WHERE ( g.saved_search_id IS NOT NULL OR g.children IS NOT NULL )
77 AND ( g.cache_date IS NULL OR
78 ( TIMESTAMPDIFF(MINUTE, g.cache_date, $now) >= $smartGroupCacheTimeout ) OR
79 ( $now >= g.refresh_date )
83 if (!$includeHiddenGroups) {
84 $query .= "AND (g.is_hidden = 0 OR g.is_hidden IS NULL)";
87 if (!empty($groupIDClause)) {
88 $query .= " AND ( $groupIDClause ) ";
95 * Checks to see if a group has been refreshed recently. This is primarily used
96 * in a locking scenario when some other process might have refreshed things underneath
99 * @param int $groupID the group ID
100 * @param boolean $includeHiddenGroups hidden groups are excluded by default
102 * @return string the sql query which lists the groups that need to be refreshed
106 static function shouldGroupBeRefreshed($groupID, $includeHiddenGroups = FALSE) {
107 $query = self
::groupRefreshedClause("g.id = %1", $includeHiddenGroups);
108 $params = array(1 => array($groupID, 'Integer'));
110 // if the query returns the group ID, it means the group is a valid candidate for refreshing
111 return CRM_Core_DAO
::singleValueQuery($query, $params);
115 * Check to see if we have cache entries for this group
116 * if not, regenerate, else return
118 * @param int/array $groupID groupID of group that we are checking against
119 * if empty, all groups are checked
120 * @param int $limit limits the number of groups we evaluate
122 * @return boolean true if we did not regenerate, false if we did
124 static function loadAll($groupIDs = null, $limit = 0) {
125 // ensure that all the smart groups are loaded
126 // this function is expensive and should be sparingly used if groupIDs is empty
127 if (empty($groupIDs)) {
128 $groupIDClause = null;
129 $groupIDs = array( );
132 if (!is_array($groupIDs)) {
133 $groupIDs = array($groupIDs);
136 // note escapeString is a must here and we can't send the imploded value as second arguement to
137 // the executeQuery(), since that would put single quote around the string and such a string
138 // of comma separated integers would not work.
139 $groupIDString = CRM_Core_DAO
::escapeString(implode(', ', $groupIDs));
141 $groupIDClause = "g.id IN ({$groupIDString})";
144 $query = self
::groupRefreshedClause($groupIDClause);
146 $limitClause = $orderClause = NULL;
148 $limitClause = " LIMIT 0, $limit";
149 $orderClause = " ORDER BY g.cache_date, g.refresh_date";
151 // We ignore hidden groups and disabled groups
157 $dao = CRM_Core_DAO
::executeQuery($query);
158 $processGroupIDs = array();
159 $refreshGroupIDs = $groupIDs;
160 while ($dao->fetch()) {
161 $processGroupIDs[] = $dao->id
;
163 // remove this id from refreshGroupIDs
164 foreach ($refreshGroupIDs as $idx => $gid) {
165 if ($gid == $dao->id
) {
166 unset($refreshGroupIDs[$idx]);
172 if (!empty($refreshGroupIDs)) {
173 $refreshGroupIDString = CRM_Core_DAO
::escapeString(implode(', ', $refreshGroupIDs));
174 $time = CRM_Utils_Date
::getUTCTime(self
::smartGroupCacheTimeout() * 60);
176 UPDATE civicrm_group g
177 SET g.refresh_date = $time
178 WHERE g.id IN ( {$refreshGroupIDString} )
179 AND g.refresh_date IS NULL
181 CRM_Core_DAO
::executeQuery($query);
184 if (empty($processGroupIDs)) {
188 self
::add($processGroupIDs);
194 * FIXME: This function should not be needed, because the cache table should not be getting truncated
196 static function fillIfEmpty() {
197 if (!CRM_Core_DAO
::singleValueQuery("SELECT COUNT(id) FROM civicrm_group_contact_cache")) {
205 static function add($groupID) {
206 // first delete the current cache
207 self
::remove($groupID);
208 if (!is_array($groupID)) {
209 $groupID = array($groupID);
212 $returnProperties = array('contact_id');
213 foreach ($groupID as $gid) {
214 $params = array(array('group', 'IN', array($gid => 1), 0, 0));
215 // the below call updates the cache table as a byproduct of the query
216 CRM_Contact_BAO_Query
::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, FALSE);
224 static function store(&$groupID, &$values) {
227 // sort the values so we put group IDs in front and hence optimize
228 // mysql storage (or so we think) CRM-9493
231 // to avoid long strings, lets do BULK_INSERT_COUNT values at a time
232 while (!empty($values)) {
234 $input = array_splice($values, 0, CRM_Core_DAO
::BULK_INSERT_COUNT
);
235 $str = implode(',', $input);
236 $sql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) VALUES $str;";
237 CRM_Core_DAO
::executeQuery($sql);
239 self
::updateCacheTime($groupID, $processed);
243 * Change the cache_date
245 * @param $groupID array(int)
246 * @param $processed bool, whether the cache data was recently modified
248 static function updateCacheTime($groupID, $processed) {
249 // only update cache entry if we had any values
251 // also update the group with cache date information
252 //make sure to give original timezone settings again.
253 $now = CRM_Utils_Date
::getUTCTime();
261 $groupIDs = implode(',', $groupID);
264 SET cache_date = $now, refresh_date = $refresh
265 WHERE id IN ( $groupIDs )
267 CRM_Core_DAO
::executeQuery($sql);
271 * Removes all the cache entries pertaining to a specific group
272 * If no groupID is passed in, removes cache entries for all groups
273 * Has an optimization to bypass repeated invocations of this function.
274 * Note that this function is an advisory, i.e. the removal respects the
275 * cache date, i.e. the removal is not done if the group was recently
276 * loaded into the cache.
278 * @param $groupID int the groupID to delete cache entries, NULL for all groups
279 * @param $onceOnly boolean run the function exactly once for all groups.
285 static function remove($groupID = NULL, $onceOnly = TRUE) {
286 static $invoked = FALSE;
288 // typically this needs to happy only once per instance
289 // this is especially true in import, where we dont need
290 // to do this all the time
291 // this optimization is done only when no groupID is passed
292 // i.e. cache is reset for all groups
301 if ($groupID == NULL) {
303 } else if (is_array($groupID)) {
304 foreach ($groupID as $gid) {
305 unset(self
::$_alreadyLoaded[$gid]);
307 } else if ($groupID && array_key_exists($groupID, self
::$_alreadyLoaded)) {
308 unset(self
::$_alreadyLoaded[$groupID]);
313 $smartGroupCacheTimeout = self
::smartGroupCacheTimeout();
315 $now = CRM_Utils_Date
::getUTCTime();
316 $refreshTime = CRM_Utils_Date
::getUTCTime($smartGroupCacheTimeout * 60);
318 if (!isset($groupID)) {
319 if ($smartGroupCacheTimeout == 0) {
321 TRUNCATE civicrm_group_contact_cache
324 UPDATE civicrm_group g
325 SET cache_date = null,
332 FROM civicrm_group_contact_cache gc
333 INNER JOIN civicrm_group g ON g.id = gc.group_id
334 WHERE TIMESTAMPDIFF(MINUTE, g.cache_date, $now) >= $smartGroupCacheTimeout
337 UPDATE civicrm_group g
338 SET cache_date = null,
340 WHERE TIMESTAMPDIFF(MINUTE, cache_date, $now) >= $smartGroupCacheTimeout
343 UPDATE civicrm_group g
344 SET refresh_date = $refreshTime
345 WHERE TIMESTAMPDIFF(MINUTE, cache_date, $now) < $smartGroupCacheTimeout
346 AND refresh_date IS NULL
350 elseif (is_array($groupID)) {
351 $groupIDs = implode(', ', $groupID);
354 FROM civicrm_group_contact_cache g
355 WHERE g.group_id IN ( $groupIDs )
358 UPDATE civicrm_group g
359 SET cache_date = null,
361 WHERE id IN ( $groupIDs )
367 FROM civicrm_group_contact_cache g
368 WHERE g.group_id = %1
371 UPDATE civicrm_group g
372 SET cache_date = null,
376 $params = array(1 => array($groupID, 'Integer'));
379 CRM_Core_DAO
::executeQuery($query, $params);
382 CRM_Core_DAO
::executeQuery($refresh, $params);
385 // also update the cache_date for these groups
386 CRM_Core_DAO
::executeQuery($update, $params);
390 * load the smart group cache for a saved search
392 * @param object $group - the smart group that needs to be loaded
393 * @param boolean $force - should we force a search through
396 static function load(&$group, $force = FALSE) {
397 $groupID = $group->id
;
398 $savedSearchID = $group->saved_search_id
;
399 if (array_key_exists($groupID, self
::$_alreadyLoaded) && !$force) {
403 // grab a lock so other processes dont compete and do the same query
404 $lockName = "civicrm.group.{$groupID}";
405 $lock = new CRM_Core_Lock($lockName);
406 if (!$lock->isAcquired()) {
407 // this can cause inconsistent results since we dont know if the other process
408 // will fill up the cache before our calling routine needs it.
409 // however this routine does not return the status either, so basically
410 // its a "lets return and hope for the best"
414 self
::$_alreadyLoaded[$groupID] = 1;
416 // we now have the lock, but some other proces could have actually done the work
417 // before we got here, so before we do any work, lets ensure that work needs to be
419 // we allow hidden groups here since we dont know if the caller wants to evaluate an
421 if (!$force && !self
::shouldGroupBeRefreshed($groupID, TRUE)) {
429 if ($savedSearchID) {
430 $ssParams = CRM_Contact_BAO_SavedSearch
::getSearchParams($savedSearchID);
432 // rectify params to what proximity search expects if there is a value for prox_distance
434 if (!empty($ssParams)) {
435 CRM_Contact_BAO_ProximityQuery
::fixInputParams($ssParams);
439 $returnProperties = array();
440 if (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) {
441 $fv = CRM_Contact_BAO_SavedSearch
::getFormValues($savedSearchID);
442 $returnProperties = CRM_Core_BAO_Mapping
::returnProperties($fv);
445 if (isset($ssParams['customSearchID'])) {
448 // we split it up and store custom class
449 // so temp tables are not destroyed if they are used
450 // hence customClass is defined above at top of function
452 CRM_Contact_BAO_SearchCustom
::customClass($ssParams['customSearchID'], $savedSearchID);
453 $searchSQL = $customClass->contactIDs();
454 $searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL);
455 $idName = 'contact_id';
458 $formValues = CRM_Contact_BAO_SavedSearch
::getFormValues($savedSearchID);
461 new CRM_Contact_BAO_Query(
462 $ssParams, $returnProperties, NULL,
466 CRM_Utils_Array
::value('display_relationship_type', $formValues),
467 CRM_Utils_Array
::value('operator', $formValues, 'AND')
469 $query->_useDistinct
= FALSE;
470 $query->_useGroupBy
= FALSE;
481 $groupID = CRM_Utils_Type
::escape($groupID, 'Integer');
482 $sql = $searchSQL . " AND contact_a.id NOT IN (
483 SELECT contact_id FROM civicrm_group_contact
484 WHERE civicrm_group_contact.status = 'Removed'
485 AND civicrm_group_contact.group_id = $groupID ) ";
489 $sql = preg_replace("/^\s*SELECT/", "SELECT $groupID as group_id, ", $sql);
492 // lets also store the records that are explicitly added to the group
493 // this allows us to skip the group contact LEFT JOIN
495 SELECT $groupID as group_id, contact_id as $idName
496 FROM civicrm_group_contact
497 WHERE civicrm_group_contact.status = 'Added'
498 AND civicrm_group_contact.group_id = $groupID ";
500 $groupIDs = array($groupID);
501 self
::remove($groupIDs);
503 $tempTable = 'civicrm_temp_group_contact_cache' . rand(0,2000);
504 foreach (array($sql, $sqlB) as $selectSql) {
508 $insertSql = "CREATE TEMPORARY TABLE $tempTable ($selectSql);";
510 $result = CRM_Core_DAO
::executeQuery($insertSql);
511 CRM_Core_DAO
::executeQuery(
512 "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
513 SELECT DISTINCT $idName, group_id FROM $tempTable
515 CRM_Core_DAO
::executeQuery(" DROP TABLE $tempTable");
518 self
::updateCacheTime($groupIDs, $processed);
520 if ($group->children
) {
522 //Store a list of contacts who are removed from the parent group
525 FROM civicrm_group_contact
526 WHERE civicrm_group_contact.status = 'Removed'
527 AND civicrm_group_contact.group_id = $groupID ";
528 $dao = CRM_Core_DAO
::executeQuery($sql);
529 $removed_contacts = array();
530 while ($dao->fetch()) {
531 $removed_contacts[] = $dao->contact_id
;
534 $childrenIDs = explode(',', $group->children
);
535 foreach ($childrenIDs as $childID) {
536 $contactIDs = CRM_Contact_BAO_Group
::getMember($childID, FALSE);
537 //Unset each contact that is removed from the parent group
538 foreach ($removed_contacts as $removed_contact) {
539 unset($contactIDs[$removed_contact]);
542 foreach ($contactIDs as $contactID => $dontCare) {
543 $values[] = "({$groupID},{$contactID})";
546 self
::store($groupIDs, $values);
556 static function smartGroupCacheTimeout() {
557 $config = CRM_Core_Config
::singleton();
560 isset($config->smartGroupCacheTimeout
) &&
561 is_numeric($config->smartGroupCacheTimeout
) &&
562 $config->smartGroupCacheTimeout
> 0) {
563 return $config->smartGroupCacheTimeout
;
566 // lets have a min cache time of 5 mins if not set
571 * Get all the smart groups that this contact belongs to
572 * Note that this could potentially be a super slow function since
573 * it ensure that all contact groups are loaded in the cache
575 * @param int $contactID
576 * @param boolean $showHidden - hidden groups are shown only if this flag is set
578 * @return array an array of groups that this contact belongs to
580 static function contactGroup($contactID, $showHidden = FALSE) {
581 if (empty($contactID)) {
585 if (is_array($contactID)) {
586 $contactIDs = $contactID;
589 $contactIDs = array($contactID);
596 $hiddenClause = ' AND (g.is_hidden = 0 OR g.is_hidden IS NULL) ';
599 $contactIDString = CRM_Core_DAO
::escapeString(implode(', ', $contactIDs));
601 SELECT gc.group_id, gc.contact_id, g.title, g.children, g.description
602 FROM civicrm_group_contact_cache gc
603 INNER JOIN civicrm_group g ON g.id = gc.group_id
604 WHERE gc.contact_id IN ($contactIDString)
606 ORDER BY gc.contact_id, g.children
609 $dao = CRM_Core_DAO
::executeQuery($sql);
610 $contactGroup = array();
611 $prevContactID = null;
612 while ($dao->fetch()) {
615 $prevContactID != $dao->contact_id
617 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
619 $prevContactID = $dao->contact_id
;
620 if (!array_key_exists($dao->contact_id
, $contactGroup)) {
621 $contactGroup[$dao->contact_id
] =
622 array( 'group' => array(), 'groupTitle' => array());
625 $contactGroup[$dao->contact_id
]['group'][] =
627 'id' => $dao->group_id
,
628 'title' => $dao->title
,
629 'description' => $dao->description
,
630 'children' => $dao->children
632 $contactGroup[$dao->contact_id
]['groupTitle'][] = $dao->title
;
635 if ($prevContactID) {
636 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
639 if ((!empty($contactGroup[$contactID]) && is_numeric($contactID))) {
640 return $contactGroup[$contactID];
643 return $contactGroup;