CRM-21677 reduce unnecessary joins in pledge reports
authoreileen <emcnaughton@wikimedia.org>
Mon, 26 Mar 2018 04:37:29 +0000 (17:37 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 26 Mar 2018 04:38:09 +0000 (17:38 +1300)
And walklist report:

Pledge/Detail
pledge/pbnp
pledge/summary
walklist/walklist

CRM/Report/Form/Pledge/Detail.php
CRM/Report/Form/Pledge/Pbnp.php
CRM/Report/Form/Pledge/Summary.php
CRM/Report/Form/Walklist/Walklist.php

index 85a485caa551b4c2a1d0defadb4baa3b503145ad..7c2c1da2a56a794d671acb3639872274aa3bfb44 100644 (file)
@@ -286,16 +286,9 @@ class CRM_Report_Form_Pledge_Detail extends CRM_Report_Form {
       ";
     }
 
-    $this->addPhoneFromClause();
-    $this->addAddressFromClause();
-    // include email field if email column is to be included
-    if ($this->_emailField) {
-      $this->_from .= "
-                 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
-                           ON ({$this->_aliases['civicrm_contact']}.id =
-                               {$this->_aliases['civicrm_email']}.contact_id) AND
-                               {$this->_aliases['civicrm_email']}.is_primary = 1\n";
-    }
+    $this->joinPhoneFromContact();
+    $this->joinAddressFromContact();
+    $this->joinEmailFromContact();
   }
 
   /**
index 596d91dd1b1dfad490bf7573ed8203147fd807a4..01294d59c62836a9845b945187129f4388d69f41 100644 (file)
@@ -230,14 +230,6 @@ class CRM_Report_Form_Pledge_Pbnp extends CRM_Report_Form {
           if (!empty($field['required']) ||
             !empty($this->_params['fields'][$fieldName])
           ) {
-            // to include optional columns address and email, only if checked
-            if ($tableName == 'civicrm_address') {
-              $this->_addressField = TRUE;
-              $this->_emailField = TRUE;
-            }
-            elseif ($tableName == 'civicrm_email') {
-              $this->_emailField = TRUE;
-            }
             $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
             $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
             $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
@@ -279,23 +271,9 @@ class CRM_Report_Form_Pledge_Pbnp extends CRM_Report_Form {
                         ON ({$this->_aliases['civicrm_pledge']}.id =
                             {$this->_aliases['civicrm_pledge_payment']}.pledge_id AND  {$this->_aliases['civicrm_pledge_payment']}.status_id = {$pendingStatus} ) ";
 
-    // include address field if address column is to be included
-    if ($this->_addressField) {
-      $this->_from .= "
-             LEFT  JOIN civicrm_address {$this->_aliases['civicrm_address']}
-                        ON ({$this->_aliases['civicrm_contact']}.id =
-                            {$this->_aliases['civicrm_address']}.contact_id) AND
-                            {$this->_aliases['civicrm_address']}.is_primary = 1\n";
-    }
+    $this->joinAddressFromContact();
+    $this->joinEmailFromContact();
 
-    // include email field if email column is to be included
-    if ($this->_emailField) {
-      $this->_from .= "
-            LEFT  JOIN civicrm_email {$this->_aliases['civicrm_email']}
-                       ON ({$this->_aliases['civicrm_contact']}.id =
-                           {$this->_aliases['civicrm_email']}.contact_id) AND
-                           {$this->_aliases['civicrm_email']}.is_primary = 1\n";
-    }
   }
 
   public function groupBy() {
index 39749ef8048eeab5172737740d85e85f241a5f43..b5905c5c8dfe539e03ccc9843a907dd4b475de85 100644 (file)
@@ -38,8 +38,6 @@ class CRM_Report_Form_Pledge_Summary extends CRM_Report_Form {
   protected $_totalPaid = FALSE;
   protected $_customGroupExtends = array('Pledge', 'Individual');
   protected $_customGroupGroupBy = TRUE;
-  protected $_addressField = FALSE;
-  protected $_emailField = FALSE;
 
   /**
    * This report has not been optimised for group filtering.
@@ -216,23 +214,8 @@ class CRM_Report_Form_Pledge_Summary extends CRM_Report_Form {
                           {$this->_aliases['civicrm_pledge']}.contact_id )
                  {$this->_aclFrom} ";
 
-    // include address field if address column is to be included
-    if ($this->_addressField) {
-      $this->_from .= "
-                 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
-                           ON ({$this->_aliases['civicrm_contact']}.id =
-                               {$this->_aliases['civicrm_address']}.contact_id) AND
-                               {$this->_aliases['civicrm_address']}.is_primary = 1\n";
-    }
-
-    // include email field if email column is to be included
-    if ($this->_emailField) {
-      $this->_from .= "
-                 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
-                           ON ({$this->_aliases['civicrm_contact']}.id =
-                               {$this->_aliases['civicrm_email']}.contact_id) AND
-                               {$this->_aliases['civicrm_email']}.is_primary = 1\n";
-    }
+    $this->joinAddressFromContact();
+    $this->joinEmailFromContact();
 
     if (!empty($this->_params['fields']['total_paid'])) {
       $this->_from .= "
index 98e51413fe419437cbab3f29d520008e78007345..25e92d7027bbbe08a9330185c9c0ab688b98c107 100644 (file)
  * @copyright CiviCRM LLC (c) 2004-2018
  */
 class CRM_Report_Form_Walklist_Walklist extends CRM_Report_Form {
-  protected $_addressField = FALSE;
-
-  protected $_emailField = FALSE;
-
-  protected $_phoneField = FALSE;
 
   protected $_summary = NULL;
 
@@ -149,6 +144,7 @@ class CRM_Report_Form_Walklist_Walklist extends CRM_Report_Form {
   }
 
   public function select() {
+    // @todo remove this function & use parent.
     $select = array();
 
     $this->_columnHeaders = array();
@@ -157,15 +153,6 @@ class CRM_Report_Form_Walklist_Walklist extends CRM_Report_Form {
         if (!empty($field['required']) ||
           !empty($this->_params['fields'][$fieldName])
         ) {
-          if ($tableName == 'civicrm_address') {
-            $this->_addressField = TRUE;
-          }
-          elseif ($tableName == 'civicrm_email') {
-            $this->_emailField = TRUE;
-          }
-          elseif ($tableName == 'civicrm_phone') {
-            $this->_phoneField = TRUE;
-          }
 
           $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
           $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
@@ -183,17 +170,10 @@ class CRM_Report_Form_Walklist_Walklist extends CRM_Report_Form {
     $this->_from = "
 FROM       civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
 ";
-    if ($this->_addressField) {
-      $this->_from .= "LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
-    }
-
-    if ($this->_emailField) {
-      $this->_from .= "LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
-    }
 
-    if ($this->_phoneField) {
-      $this->_from .= "LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
-    }
+    $this->joinAddressFromContact();
+    $this->joinPhoneFromContact();
+    $this->joinEmailFromContact();
   }
 
   public function where() {