Skip to content

Commit

Permalink
CRM-21677 - reduce unnecessary address joins on activity & contact de…
Browse files Browse the repository at this point in the history
…tail reports
  • Loading branch information
eileenmcnaughton committed Mar 22, 2018
1 parent 8dd36c0 commit e3c74b6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 72 deletions.
87 changes: 87 additions & 0 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4334,6 +4334,93 @@ public function addPhoneFromClause() {
}
}

/**
* Add Address into From Table if required.
*
* Prefix will be added to both tables as
* it is assumed you are using it to get address of a secondary contact.
*
* @param string $prefix
* @param array $extra Additional options.
* Not currently used in core but may be used in override extensions.
*/
protected function joinAddressFromContact($prefix = '', $extra = array()) {
$addressTables = ['civicrm_address', 'civicrm_country', 'civicrm_worldregion', 'civicrm_state_province'];
$isJoinRequired = $this->_addressField;
foreach ($addressTables as $addressTable) {
if ($this->isTableSelected($prefix . $addressTable)) {
$isJoinRequired = TRUE;
}
}
if ($isJoinRequired) {
$this->_from .= "
LEFT JOIN civicrm_address {$this->_aliases[$prefix . 'civicrm_address']}
ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id =
{$this->_aliases[$prefix . 'civicrm_address']}.contact_id) AND
{$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1\n";
}
}

/**
* Add Country into From Table if required.
*
* Prefix will be added to both tables as
* it is assumed you are using it to get address of a secondary contact.
*
* @param string $prefix
* @param array $extra Additional options.
* Not currently used in core but may be used in override extensions.
*/
protected function joinCountryFromAddress($prefix = '', $extra = array()) {
// include country field if country column is to be included
if ($this->isTableSelected($prefix . 'civicrm_country')) {
$this->_from .= "
LEFT JOIN civicrm_country {$this->_aliases[$prefix . 'civicrm_country']}
ON {$this->_aliases[$prefix . 'civicrm_address']}.country_id = {$this->_aliases[$prefix . 'civicrm_country']}.id AND
{$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1 ";
}
}

/**
* Add Phone into From Table if required.
*
* Prefix will be added to both tables as
* it is assumed you are using it to get address of a secondary contact.
*
* @param string $prefix
* @param array $extra Additional options.
* Not currently used in core but may be used in override extensions.
*/
protected function joinPhoneFromContact($prefix = '', $extra = array()) {
// include phone field if phone column is to be included
if ($this->isTableSelected($prefix . 'civicrm_phone')) {
$this->_from .= "
LEFT JOIN civicrm_phone {$this->_aliases[$prefix . 'civicrm_phone']}
ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_phone']}.contact_id AND
{$this->_aliases[$prefix . 'civicrm_phone']}.is_primary = 1\n";
}
}

/**
* Add Email into From Table if required.
*
* Prefix will be added to both tables as
* it is assumed you are using it to get address of a secondary contact.
*
* @param string $prefix
* @param array $extra Additional options.
* Not currently used in core but may be used in override extensions.
*/
protected function joinEmailFromContact($prefix = '', $extra = array()) {
// include email field if email column is to be included
if ($this->isTableSelected($prefix . 'civicrm_email')) {
$this->_from .= "
LEFT JOIN civicrm_email {$this->_aliases[$prefix . 'civicrm_email']}
ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_email']}.contact_id AND
{$this->_aliases[$prefix . 'civicrm_email']}.is_primary = 1) ";
}
}

/**
* Add Financial Transaction into From Table if required.
*/
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public function from($recordType = 'target') {
$this->_aliases['civicrm_contact'] = 'civicrm_contact_source';
}

$this->addAddressFromClause();
$this->joinAddressFromContact();
}

/**
Expand Down
14 changes: 2 additions & 12 deletions CRM/Report/Form/ActivitySummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,7 @@ public function from($durationMode = FALSE) {
LEFT JOIN civicrm_case_contact
ON civicrm_case_contact.case_id = civicrm_case.id ";

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 ";
}
$this->joinPhoneFromContact();
}
else {
$this->_from = "
Expand All @@ -355,12 +350,7 @@ public function from($durationMode = FALSE) {
{$this->_aclFrom}";
}

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 ";
}
$this->joinEmailFromContact();
}

/**
Expand Down
33 changes: 4 additions & 29 deletions CRM/Report/Form/Contact/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,35 +457,10 @@ public function from() {
$this->_from = "
FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom}";

if ($this->isTableSelected('civicrm_country') ||
$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 ) ";
}

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

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

if ($this->isTableSelected('civicrm_country')) {
$this->_from .= "
LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
ON {$this->_aliases['civicrm_address']}.country_id = {$this->_aliases['civicrm_country']}.id AND
{$this->_aliases['civicrm_address']}.is_primary = 1 ";
}
$this->joinAddressFromContact();
$this->joinCountryFromAddress();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();

$this->_from .= "{$group}";

Expand Down
33 changes: 3 additions & 30 deletions CRM/Report/Form/Contact/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,6 @@ public function select() {
if (!empty($field['required']) ||
!empty($this->_params['fields'][$fieldName])
) {
if ($tableName == 'civicrm_email') {
$this->_emailField = TRUE;
}
elseif ($tableName == 'civicrm_phone') {
$this->_phoneField = TRUE;
}
elseif ($tableName == 'civicrm_country') {
$this->_countryField = TRUE;
}

$alias = "{$tableName}_{$fieldName}";
$select[] = "{$field['dbAlias']} as {$alias}";
Expand Down Expand Up @@ -192,27 +183,9 @@ public function 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 ) ";

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

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

if ($this->isTableSelected('civicrm_country')) {
$this->_from .= "
LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
ON {$this->_aliases['civicrm_address']}.country_id = {$this->_aliases['civicrm_country']}.id AND
{$this->_aliases['civicrm_address']}.is_primary = 1 ";
}
$this->joinPhoneFromContact();
$this->joinEmailFromContact();
$this->joinCountryFromAddress();
}

public function postProcess() {
Expand Down

0 comments on commit e3c74b6

Please sign in to comment.