Skip to content

Commit

Permalink
Allow extensions to join address, email and phone tables without limi…
Browse files Browse the repository at this point in the history
…ting to primary fields
  • Loading branch information
seamuslee001 committed Aug 1, 2019
1 parent 46d8739 commit f20106e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
40 changes: 32 additions & 8 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4711,6 +4711,8 @@ public function addPhoneFromClause() {
* Not currently used in core but may be used in override extensions.
*/
protected function joinAddressFromContact($prefix = '', $extra = []) {
$defaults = ['primary_only' => TRUE];
$params = array_merge($defaults, $extra);
$addressTables = ['civicrm_address', 'civicrm_country', 'civicrm_worldregion', 'civicrm_state_province'];
$isJoinRequired = $this->_addressField;
foreach ($addressTables as $addressTable) {
Expand All @@ -4719,11 +4721,15 @@ protected function joinAddressFromContact($prefix = '', $extra = []) {
}
}
if ($isJoinRequired) {
$this->_from .= "
$fromJoin = "
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']}.contact_id)";
if ($params['primary_only']) {
$fromJoin .= " AND
{$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1\n";
}
$this->_from .= $fromJoin;
}
}

Expand All @@ -4738,15 +4744,21 @@ protected function joinAddressFromContact($prefix = '', $extra = []) {
* Not currently used in core but may be used in override extensions.
*/
protected function joinCountryFromAddress($prefix = '', $extra = []) {
$defaults = ['primary_only' => TRUE];
$params = array_merge($defaults, $extra);
// include country field if country column is to be included
if ($this->isTableSelected($prefix . 'civicrm_country') || $this->isTableSelected($prefix . 'civicrm_worldregion')) {
if (empty($this->_aliases[$prefix . 'civicrm_country'])) {
$this->_aliases[$prefix . 'civicrm_country'] = $prefix . '_report_country';
}
$this->_from .= "
$fromJoin = "
LEFT JOIN civicrm_country {$this->_aliases[$prefix . 'civicrm_country']}
ON {$this->_aliases[$prefix . 'civicrm_address']}.country_id = {$this->_aliases[$prefix . 'civicrm_country']}.id AND
ON {$this->_aliases[$prefix . 'civicrm_address']}.country_id = {$this->_aliases[$prefix . 'civicrm_country']}.id";
if ($params['primary_only']) {
$fromJoin .= " AND
{$this->_aliases[$prefix . 'civicrm_address']}.is_primary = 1 ";
}
$this->_from .= $fromJoin;
}
}

Expand All @@ -4761,12 +4773,18 @@ protected function joinCountryFromAddress($prefix = '', $extra = []) {
* Not currently used in core but may be used in override extensions.
*/
protected function joinPhoneFromContact($prefix = '', $extra = []) {
$defaults = ['primary_only' => TRUE];
$params = array_merge($defaults, $extra);
// include phone field if phone column is to be included
if ($this->isTableSelected($prefix . 'civicrm_phone')) {
$this->_from .= "
$fromJoin = "
LEFT JOIN civicrm_phone {$this->_aliases[$prefix . 'civicrm_phone']}
ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_phone']}.contact_id AND
ON {$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_phone']}.contact_id";
if ($params['primary_only']) {
$fromJoin .= "AND
{$this->_aliases[$prefix . 'civicrm_phone']}.is_primary = 1\n";
}
$this->_from .= $fromJoin;
}
}

Expand All @@ -4781,12 +4799,18 @@ protected function joinPhoneFromContact($prefix = '', $extra = []) {
* Not currently used in core but may be used in override extensions.
*/
protected function joinEmailFromContact($prefix = '', $extra = []) {
$defaults = ['primary_only' => TRUE];
$params = array_merge($defaults, $extra);
// include email field if email column is to be included
if ($this->isTableSelected($prefix . 'civicrm_email')) {
$this->_from .= "
$fromJoin = "
LEFT JOIN civicrm_email {$this->_aliases[$prefix . 'civicrm_email']}
ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_email']}.contact_id AND
ON ({$this->_aliases[$prefix . 'civicrm_contact']}.id = {$this->_aliases[$prefix . 'civicrm_email']}.contact_id";
if ($params['primary_only']) {
$fromJoin .= " AND
{$this->_aliases[$prefix . 'civicrm_email']}.is_primary = 1) ";
}
$this->_from .= $fromJoin;
}
}

Expand Down
19 changes: 18 additions & 1 deletion tests/phpunit/CRM/Report/Form/ContactSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function tearDown() {
* Ensure the new Odd/Event street number sort column works correctly
*/
public function testOddEvenStreetNumber() {
$customLocationType = $this->callAPISuccess('LocationType', 'create', [
'name' => 'Custom Location Type',
'display_name' => 'CiviTest Custom Location Type',
'is_active' => 1,
]);
// Create 5 contacts where:
// Contact A - Odd Street number - 3
// Contact B - Odd Street number - 5
Expand Down Expand Up @@ -88,7 +93,13 @@ public function testOddEvenStreetNumber() {
]),
'no_street_number' => $this->individualCreate(),
];

// Create a non primary address to check that we are only outputting primary contact details.
$this->callAPISuccess('Address', 'create', [
'contact_id' => $contactIDs['even_street_number_2'],
'location_type_id' => $customLocationType['id'],
'is_primary' => 0,
'street_number' => 6,
]);
$input = [
'fields' => [
'address_street_number',
Expand Down Expand Up @@ -193,6 +204,12 @@ public function testOddEvenStreetNumber() {
$this->assertEquals($case['expected_contact_ids'], CRM_Utils_Array::collect('civicrm_contact_id', $rows));
// check the order clause
$this->assertEquals(TRUE, !empty(strstr($sql, $case['expected_orderby_clause'])));
// Ensure that we are only fetching primary fields.
foreach ($rows as $row) {
if ($row['civicrm_contact_id'] == $contactIDs['even_street_number_2']) {
$this->assertEquals(4, $row['civicrm_address_address_street_number']);
}
}
}
}

Expand Down

0 comments on commit f20106e

Please sign in to comment.