Merge pull request #8346 from sqweets/auto-renew-filter-changes
[civicrm-core.git] / CRM / Contact / BAO / GroupContactCache.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33class CRM_Contact_BAO_GroupContactCache extends CRM_Contact_DAO_GroupContactCache {
34
35 static $_alreadyLoaded = array();
36
a8eb1fe6
TO
37 /**
38 * Get a list of caching modes.
39 *
40 * @return array
41 */
42 public static function getModes() {
43 return array(
44 // Flush expired caches in response to user actions.
45 'opportunistic' => ts('Opportunistic Flush'),
46
47 // Flush expired caches via background cron jobs.
48 'deterministic' => ts('Cron Flush'),
49 );
50 }
51
6a488035 52 /**
67d19299 53 * Check to see if we have cache entries for this group.
54 *
55 * If not, regenerate, else return.
6a488035 56 *
adf28ffd 57 * @param array $groupIDs
77c5b619 58 * Of group that we are checking against.
6a488035 59 *
5396af74 60 * @return bool
a6c01b45 61 * TRUE if we did not regenerate, FALSE if we did
6a488035 62 */
00be9182 63 public static function check($groupIDs) {
6a488035
TO
64 if (empty($groupIDs)) {
65 return TRUE;
66 }
67
68 return self::loadAll($groupIDs);
69 }
70
abdb2607 71 /**
adf28ffd 72 * Formulate the query to see which groups needs to be refreshed.
73 *
74 * The calculation is based on their cache date and the smartGroupCacheTimeOut
abdb2607 75 *
77c5b619
TO
76 * @param string $groupIDClause
77 * The clause which limits which groups we need to evaluate.
78 * @param bool $includeHiddenGroups
79 * Hidden groups are excluded by default.
abdb2607 80 *
a6c01b45
CW
81 * @return string
82 * the sql query which lists the groups that need to be refreshed
abdb2607 83 */
e60f24eb 84 public static function groupRefreshedClause($groupIDClause = NULL, $includeHiddenGroups = FALSE) {
4052239b 85 $smartGroupCacheTimeoutDateTime = self::getCacheInvalidDateTime();
abdb2607
DL
86
87 $query = "
88SELECT g.id
89FROM civicrm_group g
90WHERE ( g.saved_search_id IS NOT NULL OR g.children IS NOT NULL )
91AND g.is_active = 1
4052239b 92AND (
93 g.cache_date IS NULL
94 OR cache_date <= $smartGroupCacheTimeoutDateTime
95 OR NOW() >= g.refresh_date
96)";
abdb2607
DL
97
98 if (!$includeHiddenGroups) {
99 $query .= "AND (g.is_hidden = 0 OR g.is_hidden IS NULL)";
100 }
101
102 if (!empty($groupIDClause)) {
103 $query .= " AND ( $groupIDClause ) ";
104 }
105
106 return $query;
107 }
108
109 /**
adf28ffd 110 * Check to see if a group has been refreshed recently.
111 *
112 * This is primarily used in a locking scenario when some other process might have refreshed things underneath
abdb2607
DL
113 * this process
114 *
77c5b619
TO
115 * @param int $groupID
116 * The group ID.
117 * @param bool $includeHiddenGroups
118 * Hidden groups are excluded by default.
abdb2607 119 *
a6c01b45
CW
120 * @return string
121 * the sql query which lists the groups that need to be refreshed
abdb2607 122 */
00be9182 123 public static function shouldGroupBeRefreshed($groupID, $includeHiddenGroups = FALSE) {
abdb2607
DL
124 $query = self::groupRefreshedClause("g.id = %1", $includeHiddenGroups);
125 $params = array(1 => array($groupID, 'Integer'));
126
4f53db5a 127 // if the query returns the group ID, it means the group is a valid candidate for refreshing
abdb2607
DL
128 return CRM_Core_DAO::singleValueQuery($query, $params);
129 }
130
6a488035 131 /**
adf28ffd 132 * Check to see if we have cache entries for this group.
133 *
6a488035
TO
134 * if not, regenerate, else return
135 *
adf28ffd 136 * @param int|array $groupIDs groupIDs of group that we are checking against
6a488035 137 * if empty, all groups are checked
77c5b619
TO
138 * @param int $limit
139 * Limits the number of groups we evaluate.
6a488035 140 *
5396af74 141 * @return bool
a6c01b45 142 * TRUE if we did not regenerate, FALSE if we did
6a488035 143 */
e60f24eb 144 public static function loadAll($groupIDs = NULL, $limit = 0) {
6a488035
TO
145 // ensure that all the smart groups are loaded
146 // this function is expensive and should be sparingly used if groupIDs is empty
6a488035 147 if (empty($groupIDs)) {
e60f24eb 148 $groupIDClause = NULL;
481a74f4 149 $groupIDs = array();
6a488035
TO
150 }
151 else {
152 if (!is_array($groupIDs)) {
153 $groupIDs = array($groupIDs);
154 }
155
b44e3f84 156 // note escapeString is a must here and we can't send the imploded value as second argument to
6a488035
TO
157 // the executeQuery(), since that would put single quote around the string and such a string
158 // of comma separated integers would not work.
0e4e5a49 159 $groupIDString = CRM_Core_DAO::escapeString(implode(', ', $groupIDs));
6a488035 160
abdb2607 161 $groupIDClause = "g.id IN ({$groupIDString})";
6a488035
TO
162 }
163
abdb2607 164 $query = self::groupRefreshedClause($groupIDClause);
6a488035
TO
165
166 $limitClause = $orderClause = NULL;
167 if ($limit > 0) {
168 $limitClause = " LIMIT 0, $limit";
169 $orderClause = " ORDER BY g.cache_date, g.refresh_date";
170 }
e2422b8f 171 // We ignore hidden groups and disabled groups
abdb2607 172 $query .= "
6a488035 173 $orderClause
f9e16e9a 174 $limitClause
6a488035
TO
175";
176
177 $dao = CRM_Core_DAO::executeQuery($query);
178 $processGroupIDs = array();
179 $refreshGroupIDs = $groupIDs;
180 while ($dao->fetch()) {
181 $processGroupIDs[] = $dao->id;
182
183 // remove this id from refreshGroupIDs
184 foreach ($refreshGroupIDs as $idx => $gid) {
185 if ($gid == $dao->id) {
186 unset($refreshGroupIDs[$idx]);
187 break;
188 }
189 }
190 }
191
192 if (!empty($refreshGroupIDs)) {
82a837e9 193 $refreshGroupIDString = CRM_Core_DAO::escapeString(implode(', ', $refreshGroupIDs));
a986056d 194 $time = self::getRefreshDateTime();
6a488035
TO
195 $query = "
196UPDATE civicrm_group g
197SET g.refresh_date = $time
198WHERE g.id IN ( {$refreshGroupIDString} )
199AND g.refresh_date IS NULL
200";
82a837e9 201 CRM_Core_DAO::executeQuery($query);
6a488035
TO
202 }
203
204 if (empty($processGroupIDs)) {
205 return TRUE;
206 }
207 else {
208 self::add($processGroupIDs);
209 return FALSE;
210 }
211 }
212
520bca8e 213 /**
adf28ffd 214 * Fill the group contact cache if it is empty.
215 *
216 * Do this by the expensive operation of loading all groups. Call sparingly.
520bca8e 217 */
00be9182 218 public static function fillIfEmpty() {
520bca8e
CW
219 if (!CRM_Core_DAO::singleValueQuery("SELECT COUNT(id) FROM civicrm_group_contact_cache")) {
220 self::loadAll();
221 }
222 }
223
86538308 224 /**
adf28ffd 225 * Build the smart group cache for a given group.
226 *
100fef9d 227 * @param int $groupID
86538308 228 */
00be9182 229 public static function add($groupID) {
6a488035
TO
230 // first delete the current cache
231 self::remove($groupID);
232 if (!is_array($groupID)) {
233 $groupID = array($groupID);
234 }
235
236 $returnProperties = array('contact_id');
237 foreach ($groupID as $gid) {
1dbdc161 238 $params = array(array('group', 'IN', array($gid), 0, 0));
abdb2607 239 // the below call updates the cache table as a byproduct of the query
6a488035
TO
240 CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, FALSE);
241 }
242 }
243
86538308 244 /**
adf28ffd 245 * Store values into the group contact cache.
246 *
247 * @todo review use of INSERT IGNORE. This function appears to be slower that inserting
248 * with a left join. Also, 200 at once seems too little.
249 *
100fef9d 250 * @param int $groupID
adf28ffd 251 * @param array $values
86538308 252 */
00be9182 253 public static function store(&$groupID, &$values) {
6a488035
TO
254 $processed = FALSE;
255
256 // sort the values so we put group IDs in front and hence optimize
257 // mysql storage (or so we think) CRM-9493
258 sort($values);
eb917190 259
6a488035
TO
260 // to avoid long strings, lets do BULK_INSERT_COUNT values at a time
261 while (!empty($values)) {
262 $processed = TRUE;
353ffa53
TO
263 $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
264 $str = implode(',', $input);
265 $sql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) VALUES $str;";
6a488035
TO
266 CRM_Core_DAO::executeQuery($sql);
267 }
268 self::updateCacheTime($groupID, $processed);
269 }
270
271 /**
fe482240 272 * Change the cache_date.
6a488035 273 *
5a4f6742
CW
274 * @param array $groupID
275 * @param bool $processed
276 * Whether the cache data was recently modified.
6a488035 277 */
00be9182 278 public static function updateCacheTime($groupID, $processed) {
6a488035
TO
279 // only update cache entry if we had any values
280 if ($processed) {
281 // also update the group with cache date information
a986056d 282 $now = date('YmdHis');
6a488035
TO
283 $refresh = 'null';
284 }
285 else {
353ffa53 286 $now = 'null';
6a488035
TO
287 $refresh = 'null';
288 }
289
290 $groupIDs = implode(',', $groupID);
291 $sql = "
292UPDATE civicrm_group
293SET cache_date = $now, refresh_date = $refresh
294WHERE id IN ( $groupIDs )
295";
296 CRM_Core_DAO::executeQuery($sql);
297 }
298
cee4f6a1 299 /**
fe482240 300 * Removes all the cache entries pertaining to a specific group.
adf28ffd 301 *
cee4f6a1
DL
302 * If no groupID is passed in, removes cache entries for all groups
303 * Has an optimization to bypass repeated invocations of this function.
304 * Note that this function is an advisory, i.e. the removal respects the
305 * cache date, i.e. the removal is not done if the group was recently
306 * loaded into the cache.
307 *
cc7b1cd9 308 * In fact it turned out there is little overlap between the code when group is passed in
309 * and group is not so it makes more sense as separate functions.
310 *
311 * @todo remove last call to this function from outside the class then make function protected,
312 * enforce groupID as an array & remove non group handling.
313 *
5a4f6742
CW
314 * @param int $groupID
315 * the groupID to delete cache entries, NULL for all groups.
316 * @param bool $onceOnly
317 * run the function exactly once for all groups.
cee4f6a1 318 */
00be9182 319 public static function remove($groupID = NULL, $onceOnly = TRUE) {
6a488035
TO
320 static $invoked = FALSE;
321
322 // typically this needs to happy only once per instance
4052239b 323 // this is especially TRUE in import, where we don't need
6a488035
TO
324 // to do this all the time
325 // this optimization is done only when no groupID is passed
326 // i.e. cache is reset for all groups
abdb2607
DL
327 if (
328 $onceOnly &&
6a488035
TO
329 $invoked &&
330 $groupID == NULL
331 ) {
332 return;
333 }
334
335 if ($groupID == NULL) {
336 $invoked = TRUE;
0db6c3e1 337 }
4c9b6178 338 elseif (is_array($groupID)) {
abdb2607 339 foreach ($groupID as $gid) {
6a488035 340 unset(self::$_alreadyLoaded[$gid]);
abdb2607 341 }
0db6c3e1 342 }
4c9b6178 343 elseif ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) {
6a488035
TO
344 unset(self::$_alreadyLoaded[$groupID]);
345 }
346
e60f24eb 347 $refresh = NULL;
6a488035 348 $smartGroupCacheTimeout = self::smartGroupCacheTimeout();
4052239b 349 $params = array(
350 1 => array(self::getCacheInvalidDateTime(), 'String'),
351 2 => array(self::getRefreshDateTime(), 'String'),
352 );
6a488035
TO
353
354 if (!isset($groupID)) {
355 if ($smartGroupCacheTimeout == 0) {
356 $query = "
357TRUNCATE civicrm_group_contact_cache
358";
359 $update = "
360UPDATE civicrm_group g
361SET cache_date = null,
362 refresh_date = null
363";
364 }
365 else {
a4f518e9 366
6a488035
TO
367 $query = "
368DELETE gc
369FROM civicrm_group_contact_cache gc
370INNER JOIN civicrm_group g ON g.id = gc.group_id
9fd7ef4f 371WHERE g.cache_date <= %1
6a488035
TO
372";
373 $update = "
374UPDATE civicrm_group g
375SET cache_date = null,
376 refresh_date = null
9fd7ef4f 377WHERE g.cache_date <= %1
6a488035
TO
378";
379 $refresh = "
380UPDATE civicrm_group g
4052239b 381SET refresh_date = %2
382WHERE g.cache_date < %1
6a488035
TO
383AND refresh_date IS NULL
384";
385 }
386 }
387 elseif (is_array($groupID)) {
388 $groupIDs = implode(', ', $groupID);
389 $query = "
390DELETE g
391FROM civicrm_group_contact_cache g
392WHERE g.group_id IN ( $groupIDs )
393";
394 $update = "
395UPDATE civicrm_group g
396SET cache_date = null,
397 refresh_date = null
398WHERE id IN ( $groupIDs )
399";
400 }
401 else {
402 $query = "
403DELETE g
404FROM civicrm_group_contact_cache g
405WHERE g.group_id = %1
406";
407 $update = "
408UPDATE civicrm_group g
409SET cache_date = null,
410 refresh_date = null
411WHERE id = %1
412";
413 $params = array(1 => array($groupID, 'Integer'));
414 }
415
416 CRM_Core_DAO::executeQuery($query, $params);
417
418 if ($refresh) {
419 CRM_Core_DAO::executeQuery($refresh, $params);
420 }
421
422 // also update the cache_date for these groups
423 CRM_Core_DAO::executeQuery($update, $params);
424 }
425
801bafd7 426 /**
427 * Refresh the smart group cache tables.
428 *
429 * This involves clearing out any aged entries (based on the site timeout setting) and resetting the time outs.
430 *
431 * This function should be called via the opportunistic or deterministic cache refresh function to make the intent
432 * clear.
433 */
2b68a50c 434 protected static function flushCaches() {
cc7b1cd9 435 try {
436 $lock = self::getLockForRefresh();
437 }
438 catch (CRM_Core_Exception $e) {
439 // Someone else is kindly doing the refresh for us right now.
801bafd7 440 return;
441 }
801bafd7 442 $params = array(1 => array(self::getCacheInvalidDateTime(), 'String'));
cc7b1cd9 443 // @todo this is consistent with previous behaviour but as the first query could take several seconds the second
444 // could become inaccurate. It seems to make more sense to fetch them first & delete from an array (which would
445 // also reduce joins). If we do this we should also consider how best to iterate the groups. If we do them one at
446 // a time we could call a hook, allowing people to manage the frequency on their groups, or possibly custom searches
447 // might do that too. However, for 2000 groups that's 2000 iterations. If we do all once we potentially create a
448 // slow query. It's worth noting the speed issue generally relates to the size of the group but if one slow group
449 // is in a query with 500 fast ones all 500 get locked. One approach might be to calculate group size or the
450 // number of groups & then process all at once or many query runs depending on what is found. Of course those
451 // preliminary queries would need speed testing.
801bafd7 452 CRM_Core_DAO::executeQuery(
453 "
454 DELETE gc
455 FROM civicrm_group_contact_cache gc
456 INNER JOIN civicrm_group g ON g.id = gc.group_id
457 WHERE g.cache_date <= %1
458 ",
459 $params
460 );
461
cc7b1cd9 462 // Clear these out without resetting them because we are not building caches here, only clearing them,
463 // so the state is 'as if they had never been built'.
801bafd7 464 CRM_Core_DAO::executeQuery(
465 "
466 UPDATE civicrm_group g
cc7b1cd9 467 SET cache_date = NULL,
468 refresh_date = NULL
801bafd7 469 WHERE g.cache_date <= %1
470 ",
471 $params
472 );
cc7b1cd9 473 $lock->release();
801bafd7 474 }
475
476 /**
477 * Check if the refresh is already initiated.
478 *
479 * We have 2 imperfect methods for this:
480 * 1) a static variable in the function. This works fine within a request
481 * 2) a mysql lock. This works fine as long as CiviMail is not running, or if mysql is version 5.7+
482 *
483 * Where these 2 locks fail we get 2 processes running at the same time, but we have at least minimised that.
cc7b1cd9 484 *
485 * @return \Civi\Core\Lock\LockInterface
486 * @throws \CRM_Core_Exception
801bafd7 487 */
cc7b1cd9 488 protected static function getLockForRefresh() {
489 if (!isset(Civi::$statics[__CLASS__])) {
490 Civi::$statics[__CLASS__] = array('is_refresh_init' => FALSE);
491 }
492
493 if (Civi::$statics[__CLASS__]['is_refresh_init']) {
494 throw new CRM_Core_Exception('A refresh has already run in this process');
801bafd7 495 }
496 $lock = Civi::lockManager()->acquire('data.core.group.refresh');
cc7b1cd9 497 if ($lock->isAcquired()) {
498 Civi::$statics[__CLASS__]['is_refresh_init'] = TRUE;
499 return $lock;
801bafd7 500 }
cc7b1cd9 501 throw new CRM_Core_Exception('Mysql lock unavailable');
801bafd7 502 }
503
504 /**
505 * Do an opportunistic cache refresh if the site is configured for these.
506 *
507 * Sites that do not run the smart group clearing cron job should refresh the caches under an opportunistic mode, akin
508 * to a poor man's cron. The user session will be forced to wait on this so it is less desirable.
801bafd7 509 */
2b68a50c 510 public static function opportunisticCacheFlush() {
cc7b1cd9 511 if (Civi::settings()->get('smart_group_cache_refresh_mode') == 'opportunistic') {
2b68a50c 512 self::flushCaches();
801bafd7 513 }
514 }
515
516 /**
517 * Do a forced cache refresh.
518 *
519 * This function is appropriate to be called by system jobs & non-user sessions.
801bafd7 520 */
2b68a50c 521 public static function deterministicCacheFlush() {
801bafd7 522 if (self::smartGroupCacheTimeout() == 0) {
523 CRM_Core_DAO::executeQuery("TRUNCATE civicrm_group_contact_cache");
524 CRM_Core_DAO::executeQuery("
525 UPDATE civicrm_group g
526 SET cache_date = null, refresh_date = null");
527 }
528 else {
2b68a50c 529 self::flushCaches();
801bafd7 530 }
531 }
532
2c6bbd06 533 /**
adf28ffd 534 * Remove one or more contacts from the smart group cache.
535 *
2c6bbd06
CW
536 * @param int|array $cid
537 * @param int $groupId
adf28ffd 538 *
4eeb9a5b
TO
539 * @return bool
540 * TRUE if successful.
2c6bbd06 541 */
00be9182 542 public static function removeContact($cid, $groupId = NULL) {
2c6bbd06
CW
543 $cids = array();
544 // sanitize input
545 foreach ((array) $cid as $c) {
546 $cids[] = CRM_Utils_Type::escape($c, 'Integer');
547 }
548 if ($cids) {
549 $condition = count($cids) == 1 ? "= {$cids[0]}" : "IN (" . implode(',', $cids) . ")";
550 if ($groupId) {
551 $condition .= " AND group_id = " . CRM_Utils_Type::escape($groupId, 'Integer');
552 }
553 $sql = "DELETE FROM civicrm_group_contact_cache WHERE contact_id $condition";
554 CRM_Core_DAO::executeQuery($sql);
555 return TRUE;
556 }
557 return FALSE;
558 }
559
6a488035 560 /**
fe482240 561 * Load the smart group cache for a saved search.
abdb2607 562 *
77c5b619
TO
563 * @param object $group
564 * The smart group that needs to be loaded.
565 * @param bool $force
566 * Should we force a search through.
6a488035 567 */
00be9182 568 public static function load(&$group, $force = FALSE) {
6a488035
TO
569 $groupID = $group->id;
570 $savedSearchID = $group->saved_search_id;
abdb2607 571 if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) {
6a488035
TO
572 return;
573 }
cc13551d 574
4052239b 575 // grab a lock so other processes don't compete and do the same query
83617886 576 $lock = Civi::lockManager()->acquire("data.core.group.{$groupID}");
cc13551d 577 if (!$lock->isAcquired()) {
4052239b 578 // this can cause inconsistent results since we don't know if the other process
cc13551d
DL
579 // will fill up the cache before our calling routine needs it.
580 // however this routine does not return the status either, so basically
581 // its a "lets return and hope for the best"
582 return;
583 }
584
6a488035 585 self::$_alreadyLoaded[$groupID] = 1;
abdb2607 586
b44e3f84 587 // we now have the lock, but some other process could have actually done the work
abdb2607
DL
588 // before we got here, so before we do any work, lets ensure that work needs to be
589 // done
590 // we allow hidden groups here since we dont know if the caller wants to evaluate an
591 // hidden group
3cd86345 592 if (!$force && !self::shouldGroupBeRefreshed($groupID, TRUE)) {
abdb2607
DL
593 $lock->release();
594 return;
595 }
596
353ffa53
TO
597 $sql = NULL;
598 $idName = 'id';
6a488035
TO
599 $customClass = NULL;
600 if ($savedSearchID) {
601 $ssParams = CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID);
602
603 // rectify params to what proximity search expects if there is a value for prox_distance
604 // CRM-7021
605 if (!empty($ssParams)) {
606 CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams);
607 }
608
6a488035
TO
609 $returnProperties = array();
610 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) {
611 $fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
612 $returnProperties = CRM_Core_BAO_Mapping::returnProperties($fv);
613 }
614
615 if (isset($ssParams['customSearchID'])) {
616 // if custom search
617
618 // we split it up and store custom class
619 // so temp tables are not destroyed if they are used
620 // hence customClass is defined above at top of function
5396af74 621 $customClass = CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID);
6a488035 622 $searchSQL = $customClass->contactIDs();
26a9b6ab 623 $searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL);
d02c030f 624 if (!strstr($searchSQL, 'WHERE')) {
625 $searchSQL .= " WHERE ( 1 ) ";
626 }
6a488035
TO
627 $idName = 'contact_id';
628 }
629 else {
630 $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
beccb90f 631 // CRM-17075 using the formValues in this way imposes extra logic and complexity.
632 // we have the where_clause and where tables stored in the saved_search table
633 // and should use these rather than re-processing the form criteria (which over-works
634 // the link between the form layer & the query layer too).
635 // It's hard to think of when you would want to use anything other than return
636 // properties = array('contact_id' => 1) here as the point would appear to be to
637 // generate the list of contact ids in the group.
638 // @todo review this to use values in saved_search table (preferably for 4.8).
5396af74 639 $query
640 = new CRM_Contact_BAO_Query(
6a488035 641 $ssParams, $returnProperties, NULL,
cc13551d
DL
642 FALSE, FALSE, 1,
643 TRUE, TRUE,
644 FALSE,
6a488035
TO
645 CRM_Utils_Array::value('display_relationship_type', $formValues),
646 CRM_Utils_Array::value('operator', $formValues, 'AND')
353ffa53 647 );
6a488035 648 $query->_useDistinct = FALSE;
353ffa53 649 $query->_useGroupBy = FALSE;
5396af74 650 $searchSQL
651 = $query->searchQuery(
6a488035 652 0, 0, NULL,
353ffa53
TO
653 FALSE, FALSE,
654 FALSE, TRUE,
655 TRUE,
656 NULL, NULL, NULL,
657 TRUE
658 );
6a488035
TO
659 }
660 $groupID = CRM_Utils_Type::escape($groupID, 'Integer');
661 $sql = $searchSQL . " AND contact_a.id NOT IN (
662 SELECT contact_id FROM civicrm_group_contact
663 WHERE civicrm_group_contact.status = 'Removed'
664 AND civicrm_group_contact.group_id = $groupID ) ";
665 }
666
667 if ($sql) {
668 $sql = preg_replace("/^\s*SELECT/", "SELECT $groupID as group_id, ", $sql);
669 }
670
671 // lets also store the records that are explicitly added to the group
672 // this allows us to skip the group contact LEFT JOIN
673 $sqlB = "
674SELECT $groupID as group_id, contact_id as $idName
675FROM civicrm_group_contact
676WHERE civicrm_group_contact.status = 'Added'
677 AND civicrm_group_contact.group_id = $groupID ";
678
679 $groupIDs = array($groupID);
680 self::remove($groupIDs);
abdb2607 681 $processed = FALSE;
353ffa53 682 $tempTable = 'civicrm_temp_group_contact_cache' . rand(0, 2000);
6a488035
TO
683 foreach (array($sql, $sqlB) as $selectSql) {
684 if (!$selectSql) {
685 continue;
686 }
b8eae4bd 687 $insertSql = "CREATE TEMPORARY TABLE $tempTable ($selectSql);";
eb917190 688 $processed = TRUE;
801bafd7 689 CRM_Core_DAO::executeQuery($insertSql);
b8eae4bd 690 CRM_Core_DAO::executeQuery(
691 "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
eab13399 692 SELECT DISTINCT $idName, group_id FROM $tempTable
b8eae4bd 693 ");
5467c9fb 694 CRM_Core_DAO::executeQuery(" DROP TEMPORARY TABLE $tempTable");
6a488035 695 }
b8eae4bd 696
6a488035
TO
697 self::updateCacheTime($groupIDs, $processed);
698
699 if ($group->children) {
700
701 //Store a list of contacts who are removed from the parent group
702 $sql = "
703SELECT contact_id
704FROM civicrm_group_contact
705WHERE civicrm_group_contact.status = 'Removed'
706AND civicrm_group_contact.group_id = $groupID ";
707 $dao = CRM_Core_DAO::executeQuery($sql);
708 $removed_contacts = array();
709 while ($dao->fetch()) {
710 $removed_contacts[] = $dao->contact_id;
711 }
712
713 $childrenIDs = explode(',', $group->children);
714 foreach ($childrenIDs as $childID) {
715 $contactIDs = CRM_Contact_BAO_Group::getMember($childID, FALSE);
716 //Unset each contact that is removed from the parent group
717 foreach ($removed_contacts as $removed_contact) {
718 unset($contactIDs[$removed_contact]);
719 }
720 $values = array();
721 foreach ($contactIDs as $contactID => $dontCare) {
722 $values[] = "({$groupID},{$contactID})";
723 }
724
725 self::store($groupIDs, $values);
726 }
727 }
cc13551d
DL
728
729 $lock->release();
6a488035
TO
730 }
731
86538308 732 /**
adf28ffd 733 * Retrieve the smart group cache timeout in minutes.
734 *
735 * This checks if a timeout has been configured. If one has then smart groups should not
736 * be refreshed more frequently than the time out. If a group was recently refreshed it should not
737 * refresh again within that period.
738 *
86538308
EM
739 * @return int
740 */
00be9182 741 public static function smartGroupCacheTimeout() {
6a488035
TO
742 $config = CRM_Core_Config::singleton();
743
744 if (
745 isset($config->smartGroupCacheTimeout) &&
9bdcddd7 746 is_numeric($config->smartGroupCacheTimeout)
353ffa53 747 ) {
6a488035
TO
748 return $config->smartGroupCacheTimeout;
749 }
750
adf28ffd 751 // Default to 5 minutes.
6a488035
TO
752 return 5;
753 }
754
9be31c7a 755 /**
fe482240 756 * Get all the smart groups that this contact belongs to.
adf28ffd 757 *
9be31c7a
DL
758 * Note that this could potentially be a super slow function since
759 * it ensure that all contact groups are loaded in the cache
760 *
77c5b619
TO
761 * @param int $contactID
762 * @param bool $showHidden
763 * Hidden groups are shown only if this flag is set.
9be31c7a 764 *
a6c01b45
CW
765 * @return array
766 * an array of groups that this contact belongs to
9be31c7a 767 */
00be9182 768 public static function contactGroup($contactID, $showHidden = FALSE) {
6a488035 769 if (empty($contactID)) {
5396af74 770 return NULL;
6a488035
TO
771 }
772
773 if (is_array($contactID)) {
774 $contactIDs = $contactID;
775 }
776 else {
777 $contactIDs = array($contactID);
778 }
779
780 self::loadAll();
781
9be31c7a
DL
782 $hiddenClause = '';
783 if (!$showHidden) {
784 $hiddenClause = ' AND (g.is_hidden = 0 OR g.is_hidden IS NULL) ';
785 }
786
6a488035
TO
787 $contactIDString = CRM_Core_DAO::escapeString(implode(', ', $contactIDs));
788 $sql = "
789SELECT gc.group_id, gc.contact_id, g.title, g.children, g.description
790FROM civicrm_group_contact_cache gc
791INNER JOIN civicrm_group g ON g.id = gc.group_id
792WHERE gc.contact_id IN ($contactIDString)
9be31c7a 793 $hiddenClause
6a488035
TO
794ORDER BY gc.contact_id, g.children
795";
796
797 $dao = CRM_Core_DAO::executeQuery($sql);
798 $contactGroup = array();
e60f24eb 799 $prevContactID = NULL;
6a488035
TO
800 while ($dao->fetch()) {
801 if (
802 $prevContactID &&
803 $prevContactID != $dao->contact_id
804 ) {
805 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
806 }
807 $prevContactID = $dao->contact_id;
808 if (!array_key_exists($dao->contact_id, $contactGroup)) {
5396af74 809 $contactGroup[$dao->contact_id]
810 = array('group' => array(), 'groupTitle' => array());
6a488035
TO
811 }
812
5396af74 813 $contactGroup[$dao->contact_id]['group'][]
814 = array(
6a488035
TO
815 'id' => $dao->group_id,
816 'title' => $dao->title,
817 'description' => $dao->description,
21dfd5f5 818 'children' => $dao->children,
6a488035
TO
819 );
820 $contactGroup[$dao->contact_id]['groupTitle'][] = $dao->title;
821 }
822
823 if ($prevContactID) {
824 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
825 }
826
13982f7b 827 if ((!empty($contactGroup[$contactID]) && is_numeric($contactID))) {
6a488035
TO
828 return $contactGroup[$contactID];
829 }
830 else {
831 return $contactGroup;
832 }
833 }
834
4052239b 835 /**
836 * Get the datetime from which the cache should be considered invalid.
837 *
838 * Ie if the smartgroup cache timeout is 5 minutes ago then the cache is invalid if it was
839 * refreshed 6 minutes ago, but not if it was refreshed 4 minutes ago.
840 *
841 * @return string
842 */
843 public static function getCacheInvalidDateTime() {
8b6074a7 844 return date('YmdHis', strtotime("-" . self::smartGroupCacheTimeout() . " Minutes"));
4052239b 845 }
846
a986056d 847 /**
848 * Get the date when the cache should be refreshed from.
849 *
850 * Ie. now + the offset & we will delete anything prior to then.
851 *
852 * @return string
853 */
4052239b 854 public static function getRefreshDateTime() {
8b6074a7 855 return date('YmdHis', strtotime("+ " . self::smartGroupCacheTimeout() . " Minutes"));
a986056d 856 }
857
6a488035 858}