Skip to content

Commit

Permalink
CRM-21677 reduce unnecessary joins in membership reports
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Mar 23, 2018
1 parent b94eea5 commit 183af10
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 89 deletions.
34 changes: 3 additions & 31 deletions CRM/Report/Form/Member/ContributionDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form {
protected $_addressField = FALSE;

protected $_emailField = FALSE;

protected $_summary = NULL;

Expand Down Expand Up @@ -411,12 +408,6 @@ public function select() {
if (!empty($field['required']) ||
!empty($this->_params['fields'][$fieldName])
) {
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
}
if ($tableName == 'civicrm_email') {
$this->_emailField = TRUE;
}

// only include statistics columns if set
if (!empty($field['statistics'])) {
Expand Down Expand Up @@ -509,12 +500,6 @@ public function from() {

}

if (!empty($this->_params['fields']['phone'])) {
$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)";
}
//for contribution batches
if (!empty($this->_params['fields']['batch_id']) ||
!empty($this->_params['bid_value'])
Expand All @@ -528,22 +513,9 @@ public function from() {
AND {$this->_aliases['civicrm_batch']}.entity_table = 'civicrm_financial_trxn')";
}

if ($this->_addressField OR
(!empty($this->_params['state_province_id_value']) OR
!empty($this->_params['country_id_value']))
) {
$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";
}
$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();
}

/**
Expand Down
25 changes: 3 additions & 22 deletions CRM/Report/Form/Member/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,10 @@ public function from() {
ON {$this->_aliases['civicrm_membership_status']}.id =
{$this->_aliases['civicrm_membership']}.status_id ";

if ($this->isTableSelected('civicrm_address')) {
$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->joinPhoneFromContact();
$this->joinEmailFromContact();

if ($this->isTableSelected('civicrm_email')) {
$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";
}
//used when phone field is selected
if ($this->isTableSelected('civicrm_phone')) {
$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";
}
//used when contribution field is selected.
if ($this->isTableSelected('civicrm_contribution')) {
$this->_from .= "
Expand Down
39 changes: 3 additions & 36 deletions CRM/Report/Form/Member/Lapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
class CRM_Report_Form_Member_Lapse extends CRM_Report_Form {

protected $_summary = NULL;
protected $_addressField = FALSE;
protected $_emailField = FALSE;
protected $_phoneField = FALSE;
protected $_charts = array('' => 'Tabular');
protected $_customGroupExtends = array(
'Membership',
Expand Down Expand Up @@ -212,16 +209,6 @@ public function select() {
if (!empty($field['required']) ||
!empty($this->_params['fields'][$fieldName])
) {
// to include optional columns address ,email and phone only if checked
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}"]['type'] = CRM_Utils_Array::value('type', $field);
$this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
Expand All @@ -248,8 +235,6 @@ public static function formRule($fields, $files, $self) {
}

public function from() {
$this->_from = NULL;

$this->_from = "
FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}
INNER JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
Expand All @@ -262,27 +247,9 @@ public function from() {
ON {$this->_aliases['civicrm_membership']}.membership_type_id =
{$this->_aliases['civicrm_membership_type']}.id";

// 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";
}

// include phone field if phone column is to be included
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() {
Expand Down

0 comments on commit 183af10

Please sign in to comment.