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