Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-07-15-32-51
[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 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 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 static function add($groupID) {
192 // first delete the current cache
193 self::remove($groupID);
194 if (!is_array($groupID)) {
195 $groupID = array($groupID);
196 }
197
198 $returnProperties = array('contact_id');
199 foreach ($groupID as $gid) {
200 $params = array(array('group', 'IN', array($gid => 1), 0, 0));
201 // the below call updates the cache table as a byproduct of the query
202 CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, 0, FALSE);
203 }
204 }
205
206 static function store(&$groupID, &$values) {
207 $processed = FALSE;
208
209 // sort the values so we put group IDs in front and hence optimize
210 // mysql storage (or so we think) CRM-9493
211 sort($values);
212
213 // to avoid long strings, lets do BULK_INSERT_COUNT values at a time
214 while (!empty($values)) {
215 $processed = TRUE;
216 $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT);
217 $str = implode(',', $input);
218 $sql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) VALUES $str;";
219 CRM_Core_DAO::executeQuery($sql);
220 }
221 self::updateCacheTime($groupID, $processed);
222 }
223
224 /**
225 * Change the cache_date
226 *
227 * @param $groupID array(int)
228 * @param $processed bool, whether the cache data was recently modified
229 */
230 static function updateCacheTime($groupID, $processed) {
231 // only update cache entry if we had any values
232 if ($processed) {
233 // also update the group with cache date information
234 //make sure to give original timezone settings again.
235 $now = CRM_Utils_Date::getUTCTime();
236 $refresh = 'null';
237 }
238 else {
239 $now = 'null';
240 $refresh = 'null';
241 }
242
243 $groupIDs = implode(',', $groupID);
244 $sql = "
245 UPDATE civicrm_group
246 SET cache_date = $now, refresh_date = $refresh
247 WHERE id IN ( $groupIDs )
248 ";
249 CRM_Core_DAO::executeQuery($sql);
250 }
251
252 /**
253 * Removes all the cache entries pertaining to a specific group
254 * If no groupID is passed in, removes cache entries for all groups
255 * Has an optimization to bypass repeated invocations of this function.
256 * Note that this function is an advisory, i.e. the removal respects the
257 * cache date, i.e. the removal is not done if the group was recently
258 * loaded into the cache.
259 *
260 * @param $groupID int the groupID to delete cache entries, NULL for all groups
261 * @param $onceOnly boolean run the function exactly once for all groups.
262 *
263 * @public
264 * @return void
265 * @static
266 */
267 static function remove($groupID = NULL, $onceOnly = TRUE) {
268 static $invoked = FALSE;
269
270 // typically this needs to happy only once per instance
271 // this is especially true in import, where we dont need
272 // to do this all the time
273 // this optimization is done only when no groupID is passed
274 // i.e. cache is reset for all groups
275 if (
276 $onceOnly &&
277 $invoked &&
278 $groupID == NULL
279 ) {
280 return;
281 }
282
283 if ($groupID == NULL) {
284 $invoked = TRUE;
285 } else if (is_array($groupID)) {
286 foreach ($groupID as $gid) {
287 unset(self::$_alreadyLoaded[$gid]);
288 }
289 } else if ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) {
290 unset(self::$_alreadyLoaded[$groupID]);
291 }
292
293 $refresh = null;
294 $params = array();
295 $smartGroupCacheTimeout = self::smartGroupCacheTimeout();
296
297 $now = CRM_Utils_Date::getUTCTime();
298 $refreshTime = CRM_Utils_Date::getUTCTime($smartGroupCacheTimeout * 60);
299
300 if (!isset($groupID)) {
301 if ($smartGroupCacheTimeout == 0) {
302 $query = "
303 TRUNCATE civicrm_group_contact_cache
304 ";
305 $update = "
306 UPDATE civicrm_group g
307 SET cache_date = null,
308 refresh_date = null
309 ";
310 }
311 else {
312 $query = "
313 DELETE gc
314 FROM civicrm_group_contact_cache gc
315 INNER JOIN civicrm_group g ON g.id = gc.group_id
316 WHERE TIMESTAMPDIFF(MINUTE, g.cache_date, $now) >= $smartGroupCacheTimeout
317 ";
318 $update = "
319 UPDATE civicrm_group g
320 SET cache_date = null,
321 refresh_date = null
322 WHERE TIMESTAMPDIFF(MINUTE, cache_date, $now) >= $smartGroupCacheTimeout
323 ";
324 $refresh = "
325 UPDATE civicrm_group g
326 SET refresh_date = $refreshTime
327 WHERE TIMESTAMPDIFF(MINUTE, cache_date, $now) < $smartGroupCacheTimeout
328 AND refresh_date IS NULL
329 ";
330 }
331 }
332 elseif (is_array($groupID)) {
333 $groupIDs = implode(', ', $groupID);
334 $query = "
335 DELETE g
336 FROM civicrm_group_contact_cache g
337 WHERE g.group_id IN ( $groupIDs )
338 ";
339 $update = "
340 UPDATE civicrm_group g
341 SET cache_date = null,
342 refresh_date = null
343 WHERE id IN ( $groupIDs )
344 ";
345 }
346 else {
347 $query = "
348 DELETE g
349 FROM civicrm_group_contact_cache g
350 WHERE g.group_id = %1
351 ";
352 $update = "
353 UPDATE civicrm_group g
354 SET cache_date = null,
355 refresh_date = null
356 WHERE id = %1
357 ";
358 $params = array(1 => array($groupID, 'Integer'));
359 }
360
361 CRM_Core_DAO::executeQuery($query, $params);
362
363 if ($refresh) {
364 CRM_Core_DAO::executeQuery($refresh, $params);
365 }
366
367 // also update the cache_date for these groups
368 CRM_Core_DAO::executeQuery($update, $params);
369 }
370
371 /**
372 * load the smart group cache for a saved search
373 *
374 * @param object $group - the smart group that needs to be loaded
375 * @param boolean $force - should we force a search through
376 *
377 */
378 static function load(&$group, $force = FALSE) {
379 $groupID = $group->id;
380 $savedSearchID = $group->saved_search_id;
381 if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) {
382 return;
383 }
384
385 // grab a lock so other processes dont compete and do the same query
386 $lockName = "civicrm.group.{$groupID}";
387 $lock = new CRM_Core_Lock($lockName);
388 if (!$lock->isAcquired()) {
389 // this can cause inconsistent results since we dont know if the other process
390 // will fill up the cache before our calling routine needs it.
391 // however this routine does not return the status either, so basically
392 // its a "lets return and hope for the best"
393 return;
394 }
395
396 self::$_alreadyLoaded[$groupID] = 1;
397
398 // we now have the lock, but some other proces could have actually done the work
399 // before we got here, so before we do any work, lets ensure that work needs to be
400 // done
401 // we allow hidden groups here since we dont know if the caller wants to evaluate an
402 // hidden group
403 if (!$force && !self::shouldGroupBeRefreshed($groupID, TRUE)) {
404 $lock->release();
405 return;
406 }
407
408 $sql = NULL;
409 $idName = 'id';
410 $customClass = NULL;
411 if ($savedSearchID) {
412 $ssParams = CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID);
413
414 // rectify params to what proximity search expects if there is a value for prox_distance
415 // CRM-7021
416 if (!empty($ssParams)) {
417 CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams);
418 }
419
420
421 $returnProperties = array();
422 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) {
423 $fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
424 $returnProperties = CRM_Core_BAO_Mapping::returnProperties($fv);
425 }
426
427 if (isset($ssParams['customSearchID'])) {
428 // if custom search
429
430 // we split it up and store custom class
431 // so temp tables are not destroyed if they are used
432 // hence customClass is defined above at top of function
433 $customClass =
434 CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID);
435 $searchSQL = $customClass->contactIDs();
436 $idName = 'contact_id';
437 }
438 else {
439 $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
440
441 $query =
442 new CRM_Contact_BAO_Query(
443 $ssParams, $returnProperties, NULL,
444 FALSE, FALSE, 1,
445 TRUE, TRUE,
446 FALSE,
447 CRM_Utils_Array::value('display_relationship_type', $formValues),
448 CRM_Utils_Array::value('operator', $formValues, 'AND')
449 );
450 $query->_useDistinct = FALSE;
451 $query->_useGroupBy = FALSE;
452 $searchSQL =
453 $query->searchQuery(
454 0, 0, NULL,
455 FALSE, FALSE,
456 FALSE, TRUE,
457 TRUE,
458 NULL, NULL, NULL,
459 TRUE
460 );
461 }
462 $groupID = CRM_Utils_Type::escape($groupID, 'Integer');
463 $sql = $searchSQL . " AND contact_a.id NOT IN (
464 SELECT contact_id FROM civicrm_group_contact
465 WHERE civicrm_group_contact.status = 'Removed'
466 AND civicrm_group_contact.group_id = $groupID ) ";
467 }
468
469 if ($sql) {
470 $sql = preg_replace("/^\s*SELECT/", "SELECT $groupID as group_id, ", $sql);
471 }
472
473 // lets also store the records that are explicitly added to the group
474 // this allows us to skip the group contact LEFT JOIN
475 $sqlB = "
476 SELECT $groupID as group_id, contact_id as $idName
477 FROM civicrm_group_contact
478 WHERE civicrm_group_contact.status = 'Added'
479 AND civicrm_group_contact.group_id = $groupID ";
480
481 $groupIDs = array($groupID);
482 self::remove($groupIDs);
483 $processed = FALSE;
484 $tempTable = 'civicrm_temp_group_contact_cache' . rand(0,2000);
485 foreach (array($sql, $sqlB) as $selectSql) {
486 if (!$selectSql) {
487 continue;
488 }
489 $insertSql = "CREATE TEMPORARY TABLE $tempTable ($selectSql);";
490 $processed = TRUE;
491 $result = CRM_Core_DAO::executeQuery($insertSql);
492 CRM_Core_DAO::executeQuery(
493 "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
494 SELECT DISTINCT $idName, group_id FROM $tempTable
495 ");
496 CRM_Core_DAO::executeQuery(" DROP TABLE $tempTable");
497 }
498
499 self::updateCacheTime($groupIDs, $processed);
500
501 if ($group->children) {
502
503 //Store a list of contacts who are removed from the parent group
504 $sql = "
505 SELECT contact_id
506 FROM civicrm_group_contact
507 WHERE civicrm_group_contact.status = 'Removed'
508 AND civicrm_group_contact.group_id = $groupID ";
509 $dao = CRM_Core_DAO::executeQuery($sql);
510 $removed_contacts = array();
511 while ($dao->fetch()) {
512 $removed_contacts[] = $dao->contact_id;
513 }
514
515 $childrenIDs = explode(',', $group->children);
516 foreach ($childrenIDs as $childID) {
517 $contactIDs = CRM_Contact_BAO_Group::getMember($childID, FALSE);
518 //Unset each contact that is removed from the parent group
519 foreach ($removed_contacts as $removed_contact) {
520 unset($contactIDs[$removed_contact]);
521 }
522 $values = array();
523 foreach ($contactIDs as $contactID => $dontCare) {
524 $values[] = "({$groupID},{$contactID})";
525 }
526
527 self::store($groupIDs, $values);
528 }
529 }
530
531 $lock->release();
532 }
533
534 static function smartGroupCacheTimeout() {
535 $config = CRM_Core_Config::singleton();
536
537 if (
538 isset($config->smartGroupCacheTimeout) &&
539 is_numeric($config->smartGroupCacheTimeout) &&
540 $config->smartGroupCacheTimeout > 0) {
541 return $config->smartGroupCacheTimeout;
542 }
543
544 // lets have a min cache time of 5 mins if not set
545 return 5;
546 }
547
548 /**
549 * Get all the smart groups that this contact belongs to
550 * Note that this could potentially be a super slow function since
551 * it ensure that all contact groups are loaded in the cache
552 *
553 * @param int $contactID
554 * @param boolean $showHidden - hidden groups are shown only if this flag is set
555 *
556 * @return array an array of groups that this contact belongs to
557 */
558 static function contactGroup($contactID, $showHidden = FALSE) {
559 if (empty($contactID)) {
560 return;
561 }
562
563 if (is_array($contactID)) {
564 $contactIDs = $contactID;
565 }
566 else {
567 $contactIDs = array($contactID);
568 }
569
570 self::loadAll();
571
572 $hiddenClause = '';
573 if (!$showHidden) {
574 $hiddenClause = ' AND (g.is_hidden = 0 OR g.is_hidden IS NULL) ';
575 }
576
577 $contactIDString = CRM_Core_DAO::escapeString(implode(', ', $contactIDs));
578 $sql = "
579 SELECT gc.group_id, gc.contact_id, g.title, g.children, g.description
580 FROM civicrm_group_contact_cache gc
581 INNER JOIN civicrm_group g ON g.id = gc.group_id
582 WHERE gc.contact_id IN ($contactIDString)
583 $hiddenClause
584 ORDER BY gc.contact_id, g.children
585 ";
586
587 $dao = CRM_Core_DAO::executeQuery($sql);
588 $contactGroup = array();
589 $prevContactID = null;
590 while ($dao->fetch()) {
591 if (
592 $prevContactID &&
593 $prevContactID != $dao->contact_id
594 ) {
595 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
596 }
597 $prevContactID = $dao->contact_id;
598 if (!array_key_exists($dao->contact_id, $contactGroup)) {
599 $contactGroup[$dao->contact_id] =
600 array( 'group' => array(), 'groupTitle' => array());
601 }
602
603 $contactGroup[$dao->contact_id]['group'][] =
604 array(
605 'id' => $dao->group_id,
606 'title' => $dao->title,
607 'description' => $dao->description,
608 'children' => $dao->children
609 );
610 $contactGroup[$dao->contact_id]['groupTitle'][] = $dao->title;
611 }
612
613 if ($prevContactID) {
614 $contactGroup[$prevContactID]['groupTitle'] = implode(', ', $contactGroup[$prevContactID]['groupTitle']);
615 }
616
617 if (is_numeric($contactID)) {
618 return $contactGroup[$contactID];
619 }
620 else {
621 return $contactGroup;
622 }
623 }
624
625 }
626