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