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