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