CRM-21110 Do not run same/similar query twice when generating Relationships Tab in...
authorSeamus Lee <seamuslee001@gmail.com>
Mon, 28 Aug 2017 05:50:49 +0000 (15:50 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Mon, 28 Aug 2017 05:50:49 +0000 (15:50 +1000)
CRM/Contact/BAO/Relationship.php

index d066665f07c3c5b5c0579663d30c54433494aa8a..ad3adc50281de2b5c3c2a6e85fecb31968f4b766 100644 (file)
@@ -1233,6 +1233,8 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
    * @param bool $permissionedContact
    *   to return only permissioned Contact
    * @param array $params
+   * @param bool $includeTotalCount
+   *   Should we return a count of total accessable relationships
    *
    * @return array|int
    *   relationship records
@@ -1243,7 +1245,7 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
     $count = 0, $relationshipId = 0,
     $links = NULL, $permissionMask = NULL,
     $permissionedContact = FALSE,
-    $params = array()
+    $params = array(), $includeTotalCount = FALSE
   ) {
     $values = array();
     if (!$contactId && !$relationshipId) {
@@ -1277,7 +1279,8 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
     }
 
     // building the query string
-    $queryString = $select1 . $from1 . $where1 . $select2 . $from2 . $where2 . $order . $limit;
+    CRM_Core_DAO::executeQuery("CREATE TEMPORARY TABLE civicrm_contact_relationships " . $select1 . $from1 . $where1 . $select2 . $from2 . $where2);
+    $queryString = "SELECT * FROM civicrm_contact_relationships " . $order . $limit;
 
     $relationship = new CRM_Contact_DAO_Relationship();
 
@@ -1292,6 +1295,10 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
     }
     else {
 
+      if ($includeTotalCount) {
+        $values['total_relationships'] = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_contact_relationships");
+      }
+
       $mask = NULL;
       if ($status != self::INACTIVE) {
         if ($links) {
@@ -2067,20 +2074,13 @@ AND cc.sort_name LIKE '%$name%'";
       $params['rp'], 0, 0,
       $links, $mask,
       $permissionedContacts,
-      $params
+      $params, TRUE
     );
 
     $contactRelationships = array();
-    $params['total'] = 0;
+    $params['total'] = $relationships['total_relationships'];
+    unset($relationships['total_relationships']);
     if (!empty($relationships)) {
-      // FIXME: we cannot directly determine total permissioned relationship, hence re-fire query
-      $permissionedRelationships = CRM_Contact_BAO_Relationship::getRelationship($params['contact_id'],
-        $relationshipStatus,
-        0, 0, 0,
-        NULL, NULL,
-        $permissionedContacts
-      );
-      $params['total'] = count($permissionedRelationships);
 
       // format params
       foreach ($relationships as $relationshipId => $values) {