diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index d9de18c08526..98b0ed3e0e65 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -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. */ diff --git a/CRM/Report/Form/Activity.php b/CRM/Report/Form/Activity.php index dd52e784aab9..8532ae97171b 100644 --- a/CRM/Report/Form/Activity.php +++ b/CRM/Report/Form/Activity.php @@ -582,7 +582,7 @@ public function from($recordType = 'target') { $this->_aliases['civicrm_contact'] = 'civicrm_contact_source'; } - $this->addAddressFromClause(); + $this->joinAddressFromContact(); } /** diff --git a/CRM/Report/Form/ActivitySummary.php b/CRM/Report/Form/ActivitySummary.php index 9ce524b30f8f..29960096afe1 100644 --- a/CRM/Report/Form/ActivitySummary.php +++ b/CRM/Report/Form/ActivitySummary.php @@ -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 = " @@ -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(); } /** diff --git a/CRM/Report/Form/Contact/Detail.php b/CRM/Report/Form/Contact/Detail.php index 8ddef41d967d..c16977d5c784 100644 --- a/CRM/Report/Form/Contact/Detail.php +++ b/CRM/Report/Form/Contact/Detail.php @@ -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}"; diff --git a/CRM/Report/Form/Contact/Summary.php b/CRM/Report/Form/Contact/Summary.php index a37ce608d5a3..706d992981e5 100644 --- a/CRM/Report/Form/Contact/Summary.php +++ b/CRM/Report/Form/Contact/Summary.php @@ -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}"; @@ -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() {