0e7155f0d1c6910e82fa88a80b5c15b9069d84f2
[civicrm-core.git] / CRM / Contact / BAO / GroupContactCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Contact_BAO_GroupContactCache extends CRM_Contact_DAO_GroupContactCache {
36
37 static $_alreadyLoaded = array();
38
39 /**
40 * Check to see if we have cache entries for this group
41 * if not, regenerate, else return
42 *
43 * @param $groupIDs of group that we are checking against
44 *
45 * @return boolean true if we did not regenerate, false if we did
46 */
47 public static function check($groupIDs) {
48 if (empty($groupIDs)) {
49 return TRUE;
50 }
51
52 return self::loadAll($groupIDs);
53 }
54
55 /**
56 * Common function that formulates the query to see which groups needs to be refreshed
57 * based on their cache date and the smartGroupCacheTimeOut
58 *
59 * @param string $groupIDClause the clause which limits which groups we need to evaluate
60 * @param boolean $includeHiddenGroups hidden groups are excluded by default
61 *
62 * @return string the sql query which lists the groups that need to be refreshed
63 * @static
64 */
65 public static function groupRefreshedClause($groupIDClause = null, $includeHiddenGroups = FALSE) {
66 $smartGroupCacheTimeout = self::smartGroupCacheTimeout();
67 $now = CRM_Utils_Date::getUTCTime();
68
69 $query = "
70 SELECT g.id
71 FROM civicrm_group g
72 WHERE ( g.saved_search_id IS NOT NULL OR g.children IS NOT NULL )
73 AND g.is_active = 1
74 AND ( g.cache_date IS NULL OR
75 ( TIMESTAMPDIFF(MINUTE, g.cache_date, $now) >= $smartGroupCacheTimeout ) OR
76 ( $now >= g.refresh_date )
77 )
78 ";
79
80 if (!$includeHiddenGroups) {
81 $query .= "AND (g.is_hidden = 0 OR g.is_hidden IS NULL)";
82 }
83
84 if (!empty($groupIDClause)) {
85 $query .= " AND ( $groupIDClause ) ";
86 }
87
88 return $query;
89 }
90
91 /**
92 * Checks to see if a group has been refreshed recently. This is primarily used
93 * in a locking scenario when some other process might have refreshed things underneath
94 * this process
95 *
96 * @param int $groupID the group ID
97 * @param boolean $includeHiddenGroups hidden groups are excluded by default
98 *
99 * @return string the sql query which lists the groups that need to be refreshed
100 * @static
101 */
102 public static function shouldGroupBeRefreshed($groupID, $includeHiddenGroups = FALSE) {
103 $query = self::groupRefreshedClause("g.id = %1", $includeHiddenGroups);
104 $params = array(1 => array($groupID, 'Integer'));
105
106 // if the query returns the group ID, it means the group is a valid candidate for refreshing
107 return CRM_Core_DAO::singleValueQuery($query, $params);
108 }
109
110 /**
111 * Check to see if we have cache entries for this group
112 * if not, regenerate, else return
113 *
114 * @param int/array $groupID groupID of group that we are checking against
115 * if empty, all groups are checked
116 * @param int $limit limits the number of groups we evaluate
117 *
118 * @return boolean true if we did not regenerate, false if we did
119 */
120 public static function loadAll($groupIDs = null, $limit = 0) {
121 // ensure that all the smart groups are loaded
122 // this function is expensive and should be sparingly used if groupIDs is empty
123 if (empty($groupIDs)) {
124 $groupIDClause = null;
125 $groupIDs = array( );
126 }
127 else {
128 if (!is_array($groupIDs)) {
129 $groupIDs = array($groupIDs);
130 }
131
132 // note escapeString is a must here and we can't send the imploded value as second arguement to
133 // the executeQuery(), since that would put single quote around the string and such a string
134 // of comma separated integers would not work.
135 $groupIDString = CRM_Core_DAO::escapeString(implode(', ', $groupIDs));
136
137 $groupIDClause = "g.id IN ({$groupIDString})";
138 }
139
140 $query = self::groupRefreshedClause($groupIDClause);
141
142 $limitClause = $orderClause = NULL;
143 if ($limit > 0) {
144 $limitClause = " LIMIT 0, $limit";
145 $orderClause = " ORDER BY g.cache_date, g.refresh_date";
146 }
147 // We ignore hidden groups and disabled groups
148 $query .= "
149 $orderClause
150 $limitClause
151 ";
152
153 $dao = CRM_Core_DAO::executeQuery($query);
154 $processGroupIDs = array();
155 $refreshGroupIDs = $groupIDs;
156 while ($dao->fetch()) {
157 $processGroupIDs[] = $dao->id;
158
159 // remove this id from refreshGroupIDs
160 foreach ($refreshGroupIDs as $idx => $gid) {
161 if ($gid == $dao->id) {
162 unset($refreshGroupIDs[$idx]);
163 break;
164 }
165 }
166 }
167
168 if (!empty($refreshGroupIDs)) {
169 $refreshGroupIDString = CRM_Core_DAO::escapeString(implode(', ', $refreshGroupIDs));
170 $time = CRM_Utils_Date::getUTCTime(self::smartGroupCacheTimeout() * 60);
171 $query = "
172 UPDATE civicrm_group g
173 SET g.refresh_date = $time
174 WHERE g.id IN ( {$refreshGroupIDString} )
175 AND g.refresh_date IS NULL
176 ";
177 CRM_Core_DAO::executeQuery($query);
178 }
179
180 if (empty($processGroupIDs)) {
181 return TRUE;
182 }
183 else {
184 self::add($processGroupIDs);
185 return FALSE;
186 }
187 }
188
189 /**
190 * FIXME: This function should not be needed, because the cache table should not be getting truncated
191 */
192 public static function fillIfEmpty() {
193 if (!CRM_Core_DAO::singleValueQuery("SELECT COUNT(id) FROM civicrm_group_contact_cache")) {
194 self::loadAll();
195 }
196 }
197
198 /**
199 * @param int $groupID
200 */
201 public static function add($groupID) {
202 // first delete the current cache
203 self::remove($groupID);
204 if (!is_array($groupID)) {
205 $groupID = array($groupID);
206 }
207
208 $returnProperties = array('contact_id');
209 foreach ($groupID as $gid) {
210 $params = array(array('group', 'IN', array($gid => 1), 0, 0));
211 // the below call updates the cache table as a byproduct of the query
212 CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, FALSE);
213 }
214 }
215
216 /**
217 * @param int $groupID
218 * @param $values
219 */
220 public static function store(&$groupID, &$values) {
221 $processed = FALSE;
222
223 // sort the values so we put group IDs in front and hence optimize
224 // mysql storage (or so we think) CRM-9493
225 sort($values);
226
227 // to avoid long strings, lets do BULK_INSERT_COUNT values at a time
228 while (!empty($values)) {
229 $processed = TRUE;
230 $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
231 $str = implode(',', $input);
232 $sql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) VALUES $str;";
233 CRM_Core_DAO::executeQuery($sql);
234 }
235 self::updateCacheTime($groupID, $processed);
236 }
237
238 /**
239 * Change the cache_date
240 *
241 * @param $groupID array(int)
242 * @param $processed bool, whether the cache data was recently modified
243 */
244 public static function updateCacheTime($groupID, $processed) {
245 // only update cache entry if we had any values
246 if ($processed) {
247 // also update the group with cache date information
248 //make sure to give original timezone settings again.
249 $now = CRM_Utils_Date::getUTCTime();
250 $refresh = 'null';
251 }
252 else {
253 $now = 'null';
254 $refresh = 'null';
255 }
256
257 $groupIDs = implode(',', $groupID);
258 $sql = "
259 UPDATE civicrm_group
260 SET cache_date = $now, refresh_date = $refresh
261 WHERE id IN ( $groupIDs )
262 ";
263 CRM_Core_DAO::executeQuery($sql);
264 }
265
266 /**
267 * Removes all the cache entries pertaining to a specific group
268 * If no groupID is passed in, removes cache entries for all groups
269 * Has an optimization to bypass repeated invocations of this function.
270 * Note that this function is an advisory, i.e. the removal respects the
271 * cache date, i.e. the removal is not done if the group was recently
272 * loaded into the cache.
273 *
274 * @param $groupID int the groupID to delete cache entries, NULL for all groups
275 * @param $onceOnly boolean run the function exactly once for all groups.
276 *
277 * @return void
278 * @static
279 */
280 public static function remove($groupID = NULL, $onceOnly = TRUE) {
281 static $invoked = FALSE;
282
283 // typically this needs to happy only once per instance
284 // this is especially true in import, where we dont need
285 // to do this all the time
286 // this optimization is done only when no groupID is passed
287 // i.e. cache is reset for all groups
288 if (
289 $onceOnly &&
290 $invoked &&
291 $groupID == NULL
292 ) {
293 return;
294 }
295
296 if ($groupID == NULL) {
297 $invoked = TRUE;
298 } else if (is_array($groupID)) {
299 foreach ($groupID as $gid) {
300 unset(self::$_alreadyLoaded[$gid]);
301 }
302 } else if ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) {
303 unset(self::$_alreadyLoaded[$groupID]);
304 }
305
306 $refresh = null;
307 $params = array();
308 $smartGroupCacheTimeout = self::smartGroupCacheTimeout();
309
310 $now = CRM_Utils_Date::getUTCTime();
311 $refreshTime = CRM_Utils_Date::getUTCTime($smartGroupCacheTimeout * 60);
312
313 if (!isset($groupID)) {
314 if ($smartGroupCacheTimeout == 0) {
315 $query = "
316 TRUNCATE civicrm_group_contact_cache
317 ";
318 $update = "
319 UPDATE civicrm_group g
320 SET cache_date = null,
321 refresh_date = null
322 ";
323 }
324 else {
325 $query = "
326 DELETE gc
327 FROM civicrm_group_contact_cache gc
328 INNER JOIN civicrm_group g ON g.id = gc.group_id
329 WHERE TIMESTAMPDIFF(MINUTE, g.cache_date, $now) >= $smartGroupCacheTimeout
330 ";
331 $update = "
332 UPDATE civicrm_group g
333 SET cache_date = null,
334 refresh_date = null
335 WHERE TIMESTAMPDIFF(MINUTE, cache_date, $now) >= $smartGroupCacheTimeout
336 ";
337 $refresh = "
338 UPDATE civicrm_group g
339 SET refresh_date = $refreshTime
340 WHERE TIMESTAMPDIFF(MINUTE, cache_date, $now) < $smartGroupCacheTimeout
341 AND refresh_date IS NULL
342 ";
343 }
344 }
345 elseif (is_array($groupID)) {
346 $groupIDs = implode(', ', $groupID);
347 $query = "
348 DELETE g
349 FROM civicrm_group_contact_cache g
350 WHERE g.group_id IN ( $groupIDs )
351 ";
352 $update = "
353 UPDATE civicrm_group g
354 SET cache_date = null,
355 refresh_date = null
356 WHERE id IN ( $groupIDs )
357 ";
358 }
359 else {
360 $query = "
361 DELETE g
362 FROM civicrm_group_contact_cache g
363 WHERE g.group_id = %1
364 ";
365 $update = "
366 UPDATE civicrm_group g
367 SET cache_date = null,
368 refresh_date = null
369 WHERE id = %1
370 ";
371 $params = array(1 => array($groupID, 'Integer'));
372 }
373
374 CRM_Core_DAO::executeQuery($query, $params);
375
376 if ($refresh) {
377 CRM_Core_DAO::executeQuery($refresh, $params);
378 }
379
380 // also update the cache_date for these groups
381 CRM_Core_DAO::executeQuery($update, $params);
382 }
383
384 /**
385 * Removes one or more contacts from the smart group cache
386 * @param int|array $cid
387 * @param int $groupId
388 * @return bool - true if successful
389 */
390 public static function removeContact($cid, $groupId = NULL) {
391 $cids = array();
392 // sanitize input
393 foreach ((array) $cid as $c) {
394 $cids[] = CRM_Utils_Type::escape($c, 'Integer');
395 }
396 if ($cids) {
397 $condition = count($cids) == 1 ? "= {$cids[0]}" : "IN (" . implode(',', $cids) . ")";
398 if ($groupId) {
399 $condition .= " AND group_id = " . CRM_Utils_Type::escape($groupId, 'Integer');
400 }
401 $sql = "DELETE FROM civicrm_group_contact_cache WHERE contact_id $condition";
402 CRM_Core_DAO::executeQuery($sql);
403 return TRUE;
404 }
405 return FALSE;
406 }
407
408 /**
409 * Load the smart group cache for a saved search
410 *
411 * @param object $group - the smart group that needs to be loaded
412 * @param boolean $force - should we force a search through
413 *
414 */
415 public static function load(&$group, $force = FALSE) {
416 $groupID = $group->id;
417 $savedSearchID = $group->saved_search_id;
418 if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) {
419 return;
420 }
421
422 // grab a lock so other processes dont compete and do the same query
423 $lockName = "civicrm.group.{$groupID}";
424 $lock = new CRM_Core_Lock($lockName);
425 if (!$lock->isAcquired()) {
426 // this can cause inconsistent results since we dont know if the other process
427 // will fill up the cache before our calling routine needs it.
428 // however this routine does not return the status either, so basically
429 // its a "lets return and hope for the best"
430 return;
431 }
432
433 self::$_alreadyLoaded[$groupID] = 1;
434
435 // we now have the lock, but some other proces could have actually done the work
436 // before we got here, so before we do any work, lets ensure that work needs to be
437 // done
438 // we allow hidden groups here since we dont know if the caller wants to evaluate an
439 // hidden group
440 if (!$force && !self::shouldGroupBeRefreshed($groupID, TRUE)) {
441 $lock->release();
442 return;
443 }
444
445 $sql = NULL;
446 $idName = 'id';
447 $customClass = NULL;
448 if ($savedSearchID) {
449 $ssParams = CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID);
450
451 // rectify params to what proximity search expects if there is a value for prox_distance
452 // CRM-7021
453 if (!empty($ssParams)) {
454 CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams);
455 }
456
457
458 $returnProperties = array();
459 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) {
460 $fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
461 $returnProperties = CRM_Core_BAO_Mapping::returnProperties($fv);
462 }
463
464 if (isset($ssParams['customSearchID'])) {
465 // if custom search
466
467 // we split it up and store custom class
468 // so temp tables are not destroyed if they are used
469 // hence customClass is defined above at top of function
470 $customClass =
471 CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID);
472 $searchSQL = $customClass->contactIDs();
473 $searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL);
474 $idName = 'contact_id';
475 }
476 else {
477 $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
478
479 $query =
480 new CRM_Contact_BAO_Query(
481 $ssParams, $returnProperties, NULL,
482 FALSE, FALSE, 1,
483 TRUE, TRUE,
484 FALSE,
485 CRM_Utils_Array::value('display_relationship_type', $formValues),
486 CRM_Utils_Array::value('operator', $formValues, 'AND')
487 );
488 $query->_useDistinct = FALSE;
489 $query->_useGroupBy = FALSE;
490 $searchSQL =
491 $query->searchQuery(
492 0, 0, NULL,
493 FALSE, FALSE,
494 FALSE, TRUE,
495 TRUE,
496 NULL, NULL, NULL,
497 TRUE
498 );
499 }
500 $groupID = CRM_Utils_Type::escape($groupID, 'Integer');
501 $sql = $searchSQL . " AND contact_a.id NOT IN (
502 SELECT contact_id FROM civicrm_group_contact
503 WHERE civicrm_group_contact.status = 'Removed'
504 AND civicrm_group_contact.group_id = $groupID ) ";
505 }
506
507 if ($sql) {
508 $sql = preg_replace("/^\s*SELECT/", "SELECT $groupID as group_id, ", $sql);
509 }
510
511 // lets also store the records that are explicitly added to the group
512 // this allows us to skip the group contact LEFT JOIN
513 $sqlB = "
514 SELECT $groupID as group_id, contact_id as $idName
515 FROM civicrm_group_contact
516 WHERE civicrm_group_contact.status = 'Added'
517 AND civicrm_group_contact.group_id = $groupID ";
518
519 $groupIDs = array($groupID);
520 self::remove($groupIDs);
521 $processed = FALSE;
522 $tempTable = 'civicrm_temp_group_contact_cache' . rand(0,2000);
523 foreach (array($sql, $sqlB) as $selectSql) {
524 if (!$selectSql) {
525 continue;
526 }
527 $insertSql = "CREATE TEMPORARY TABLE $tempTable ($selectSql);";
528 $processed = TRUE;
529 $result = CRM_Core_DAO::executeQuery($insertSql);
530 CRM_Core_DAO::executeQuery(
531 "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
532 SELECT DISTINCT $idName, group_id FROM $tempTable
533 ");
534 CRM_Core_DAO::executeQuery(" DROP TABLE $tempTable");
535 }
536
537 self::updateCacheTime($groupIDs, $processed);
538
539 if ($group->children) {
540
541 //Store a list of contacts who are removed from the parent group
542 $sql = "
543 SELECT contact_id
544 FROM civicrm_group_contact
545 WHERE civicrm_group_contact.status = 'Removed'
546 AND civicrm_group_contact.group_id = $groupID ";
547 $dao = CRM_Core_DAO::executeQuery($sql);
548 $removed_contacts = array();
549 while ($dao->fetch()) {
550 $removed_contacts[] = $dao->contact_id;
551 }
552
553 $childrenIDs = explode(',', $group->children);
554 foreach ($childrenIDs as $childID) {
555 $contactIDs = CRM_Contact_BAO_Group::getMember($childID, FALSE);
556 //Unset each contact that is removed from the parent group
557 foreach ($removed_contacts as $removed_contact) {
558 unset($contactIDs[$removed_contact]);
559 }
560 $values = array();
561 foreach ($contactIDs as $contactID => $dontCare) {
562 $values[] = "({$groupID},{$contactID})";
563 }
564
565 self::store($groupIDs, $values);
566 }
567 }
568
569 $lock->release();
570 }
571
572 /**
573 * @return int
574 */
575 public static function smartGroupCacheTimeout() {
576 $config = CRM_Core_Config::singleton();
577
578 if (
579 isset($config->smartGroupCacheTimeout) &&
580 is_numeric($config->smartGroupCacheTimeout) &&
581 $config->smartGroupCacheTimeout > 0) {
582 return $config->smartGroupCacheTimeout;
583 }
584
585 // lets have a min cache time of 5 mins if not set
586 return 5;
587 }
588
589 /**
590 * Get all the smart groups that this contact belongs to
591 * Note that this could potentially be a super slow function since
592 * it ensure that all contact groups are loaded in the cache
593 *
594 * @param int $contactID
595 * @param boolean $showHidden - hidden groups are shown only if this flag is set
596 *
597 * @return array an array of groups that this contact belongs to
598 */
599 public static function contactGroup($contactID, $showHidden = FALSE) {
600 if (empty($contactID)) {
601 return;
602 }
603
604 if (is_array($contactID)) {
605 $contactIDs = $contactID;
606 }
607 else {
608 $contactIDs = array($contactID);
609 }
610
611 self::loadAll();
612
613 $hiddenClause = '';
614 if (!$showHidden) {
615 $hiddenClause = ' AND (g.is_hidden = 0 OR g.is_hidden IS NULL) ';
616 }
617
618 $contactIDString = CRM_Core_DAO::escapeString(implode(', ', $contactIDs));
619 $sql = "
620 SELECT gc.group_id, gc.contact_id, g.title, g.children, g.description
621 FROM civicrm_group_contact_cache gc
622 INNER JOIN civicrm_group g ON g.id = gc.group_id
623 WHERE gc.contact_id IN ($contactIDString)
624 $hiddenClause
625 ORDER BY gc.contact_id, g.children
626 ";
627
628 $dao = CRM_Core_DAO::executeQuery($sql);
629 $contactGroup = array();
630 $prevContactID = null;
631 while ($dao->fetch()) {
632 if (
633 $prevContactID &&
634 $prevContactID != $dao->contact_id
635 ) {
636 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
637 }
638 $prevContactID = $dao->contact_id;
639 if (!array_key_exists($dao->contact_id, $contactGroup)) {
640 $contactGroup[$dao->contact_id] =
641 array( 'group' => array(), 'groupTitle' => array());
642 }
643
644 $contactGroup[$dao->contact_id]['group'][] =
645 array(
646 'id' => $dao->group_id,
647 'title' => $dao->title,
648 'description' => $dao->description,
649 'children' => $dao->children
650 );
651 $contactGroup[$dao->contact_id]['groupTitle'][] = $dao->title;
652 }
653
654 if ($prevContactID) {
655 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
656 }
657
658 if ((!empty($contactGroup[$contactID]) && is_numeric($contactID))) {
659 return $contactGroup[$contactID];
660 }
661 else {
662 return $contactGroup;
663 }
664 }
665
666 }