Cache case count when getting list of cases so query is executed once instead of...
authorMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Sat, 29 Dec 2018 12:15:17 +0000 (12:15 +0000)
committerMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Sat, 29 Dec 2018 12:15:17 +0000 (12:15 +0000)
CRM/Case/BAO/Case.php

index 7e1b83667a84572a05644233bae05972e7537f00..38cf7c48af8d9d28d63364837be60c96743143f8 100644 (file)
@@ -588,11 +588,16 @@ LEFT JOIN civicrm_option_group aog ON aog.name='activity_type'
     $condition = NULL;
     $casesList = array();
 
-    //validate access for own cases.
+    // validate access for own cases.
     if (!self::accessCiviCase()) {
       return $getCount ? 0 : $casesList;
     }
 
+    // Return cached value instead of re-running query
+    if (isset(Civi::$statics[__CLASS__]['totalCount']) && $getCount) {
+      return Civi::$statics[__CLASS__]['totalCount'];
+    }
+
     $type = CRM_Utils_Array::value('type', $params, 'upcoming');
     $userID = CRM_Core_Session::singleton()->get('userID');
 
@@ -610,7 +615,7 @@ LEFT JOIN civicrm_option_group aog ON aog.name='activity_type'
       $caseActivityIDColumn = 'case_recent_activity_id';
     }
 
-    //validate access for all cases.
+    // validate access for all cases.
     if ($allCases && !CRM_Core_Permission::check('access all cases and activities')) {
       $allCases = FALSE;
     }
@@ -631,7 +636,7 @@ LEFT JOIN civicrm_option_group aog ON aog.name='activity_type'
     }
     $condition = implode(' AND ', $whereClauses);
 
-    $totalCount = CRM_Core_DAO::singleValueQuery(self::getCaseActivityCountQuery($type, $userID, $condition));
+    Civi::$statics[__CLASS__]['totalCount'] = $totalCount = CRM_Core_DAO::singleValueQuery(self::getCaseActivityCountQuery($type, $userID, $condition));
     if ($getCount) {
       return $totalCount;
     }