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