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
index 8dbea8e2106b0cae01e34ace49e612749a64b649..9e13d05bd805d8ea577e1d9c1497513e3f7a5706 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -169,7 +169,7 @@ AND     ( g.cache_date IS NULL OR
 
     if (!empty($refreshGroupIDs)) {
       $refreshGroupIDString = CRM_Core_DAO::escapeString(implode(', ', $refreshGroupIDs));
-      $time  = CRM_Utils_Date::getUTCTime($smartGroupCacheTimeout * 60);
+      $time  = CRM_Utils_Date::getUTCTime(self::smartGroupCacheTimeout() * 60);
       $query = "
 UPDATE civicrm_group g
 SET    g.refresh_date = $time
@@ -249,6 +249,21 @@ WHERE  id IN ( $groupIDs )
     CRM_Core_DAO::executeQuery($sql);
   }
 
+  /**
+   * Removes all the cache entries pertaining to a specific group
+   * If no groupID is passed in, removes cache entries for all groups
+   * Has an optimization to bypass repeated invocations of this function.
+   * Note that this function is an advisory, i.e. the removal respects the
+   * cache date, i.e. the removal is not done if the group was recently
+   * loaded into the cache.
+   *
+   * @param $groupID  int the groupID to delete cache entries, NULL for all groups
+   * @param $onceOnly boolean run the function exactly once for all groups.
+   *
+   * @public
+   * @return void
+   * @static
+   */
   static function remove($groupID = NULL, $onceOnly = TRUE) {
     static $invoked = FALSE;
 
@@ -385,8 +400,7 @@ WHERE  id = %1
     // done
     // we allow hidden groups here since we dont know if the caller wants to evaluate an
     // hidden group
-    // note that we ignore the force option in this case and rely on someone else having done it
-    if (! self::shouldGroupBeRefreshed($groupID, FALSE, TRUE)) {
+    if (!$force && !self::shouldGroupBeRefreshed($groupID, TRUE)) {
       $lock->release();
       return;
     }
@@ -466,16 +480,22 @@ WHERE  civicrm_group_contact.status = 'Added'
 
     $groupIDs = array($groupID);
     self::remove($groupIDs);
-
     $processed = FALSE;
+    $tempTable = 'civicrm_temp_group_contact_cache' . rand(0,2000);
     foreach (array($sql, $sqlB) as $selectSql) {
       if (!$selectSql) {
         continue;
       }
-      $insertSql = "INSERT IGNORE INTO civicrm_group_contact_cache (group_id,contact_id) ($selectSql);";
+      $insertSql = "CREATE TEMPORARY TABLE $tempTable ($selectSql);";
       $processed = TRUE;
       $result = CRM_Core_DAO::executeQuery($insertSql);
+      CRM_Core_DAO::executeQuery(
+        "INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)
+        SELECT DISTINCT $idName, group_id FROM $tempTable
+      ");
+      CRM_Core_DAO::executeQuery(" DROP TABLE $tempTable");
     }
+
     self::updateCacheTime($groupIDs, $processed);
 
     if ($group->children) {