From 4b2d36d72f3ca0b0cea8b5aa68a54eab45c2ce8a Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 18 Jan 2018 16:28:39 +1300 Subject: [PATCH] CRM-21674 allow lat & long to be pre-calculated for proximity query or search --- CRM/Contact/BAO/ProximityQuery.php | 8 ++++-- CRM/Contact/BAO/Query.php | 2 ++ CRM/Contact/Form/Search/Custom/Proximity.php | 3 ++ .../Contact/Form/Search/Custom/Proximity.tpl | 8 ++++-- tests/phpunit/api/v3/ContactTest.php | 28 +++++++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/CRM/Contact/BAO/ProximityQuery.php b/CRM/Contact/BAO/ProximityQuery.php index 9e971404b6..e41ab733f5 100644 --- a/CRM/Contact/BAO/ProximityQuery.php +++ b/CRM/Contact/BAO/ProximityQuery.php @@ -273,6 +273,8 @@ ACOS( 'state_province' => 0, 'country' => 0, 'distance_unit' => 0, + 'geo_code_1' => 0, + 'geo_code_2' => 0, ); $proximityAddress = array(); @@ -335,8 +337,10 @@ ACOS( $query->_tables['civicrm_address'] = $query->_whereTables['civicrm_address'] = 1; - if (!CRM_Core_BAO_Address::addGeocoderData($proximityAddress)) { - throw new CRM_Core_Exception(ts('Proximity searching requires you to set a valid geocoding provider')); + if (empty($proximityAddress['geo_code_1']) || empty($proximityAddress['geo_code_2'])) { + if (!CRM_Core_BAO_Address::addGeocoderData($proximityAddress)) { + throw new CRM_Core_Exception(ts('Proximity searching requires you to set a valid geocoding provider')); + } } if ( diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 7655e8e6e2..74aabba5fc 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -2002,6 +2002,8 @@ class CRM_Contact_BAO_Query { case 'prox_postal_code': case 'prox_state_province_id': case 'prox_country_id': + case 'prox_geo_code_1': + case 'prox_geo_code_2': // handled by the proximity_distance clause return; diff --git a/CRM/Contact/Form/Search/Custom/Proximity.php b/CRM/Contact/Form/Search/Custom/Proximity.php index 5a86cebdd9..15ab314731 100644 --- a/CRM/Contact/Form/Search/Custom/Proximity.php +++ b/CRM/Contact/Form/Search/Custom/Proximity.php @@ -135,6 +135,9 @@ class CRM_Contact_Form_Search_Custom_Proximity extends CRM_Contact_Form_Search_C $country = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country(); $form->add('select', 'country_id', ts('Country'), $country, TRUE, array('class' => 'crm-select2')); + $form->add('text', 'geo_code_1', ts('Latitude')); + $form->add('text', 'geo_code_2', ts('Longitude')); + $group = array('' => ts('- any group -')) + CRM_Core_PseudoConstant::nestedGroup(); $form->addElement('select', 'group', ts('Group'), $group, array('class' => 'crm-select2 huge')); diff --git a/templates/CRM/Contact/Form/Search/Custom/Proximity.tpl b/templates/CRM/Contact/Form/Search/Custom/Proximity.tpl index 2382577495..309c001202 100644 --- a/templates/CRM/Contact/Form/Search/Custom/Proximity.tpl +++ b/templates/CRM/Contact/Form/Search/Custom/Proximity.tpl @@ -35,13 +35,17 @@
{include file="CRM/common/formButtons.tpl" location="top"}
- + - + + + + +
{$form.distance.label}{$form.distance.html|crmAddClass:four} {$form.prox_distance_unit.html}
FROM...
{ts}FROM...{/ts}
{$form.street_address.label}{$form.street_address.html}
{$form.city.label}{$form.city.html}
{$form.postal_code.label}{$form.postal_code.html}
{$form.country_id.label}{$form.country_id.html}
{$form.state_province_id.label}{$form.state_province_id.html}
AND ...
{ts}OR enter lattitude and longitude if you already know it{/ts}.
{$form.geo_code_1.label}{$form.geo_code_1.html}
{$form.geo_code_2.label}{$form.geo_code_2.html}
{ts}AND ...{/ts}
{ts}Restrict results by ...{/ts}
{$form.group.label}{$form.group.html}
{$form.tag.label}{$form.tag.html}
diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 0e792c74fa..c4d094c82b 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3524,6 +3524,34 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->callAPISuccess('contact', 'delete', array('id' => $created_contact_id, 'skip_undelete' => TRUE)); } + /** + * Test the prox_distance functionality works. + * + * This is primarily testing functionality in the BAO_Query object that 'happens to be' + * accessible via the api. + */ + public function testContactGetProximity() { + CRM_Core_Config::singleton()->geocodeMethod = 'CRM_Utils_MockGeocoder'; + $this->individualCreate(); + $contactID = $this->individualCreate(); + $this->callAPISuccess('Address', 'create', [ + 'contact_id' => $contactID, + 'is_primary' => 1, + 'city' => 'Whangarei', + 'street_address' => 'Dent St', + 'geo_code_1' => '-35.8743325', + 'geo_code_2' => '174.4567136', + 'location_type_id' => 'Home', + ]); + $contact = $this->callAPISuccess('Contact', 'get', [ + 'prox_distance' => 100, + 'prox_geo_code_1' => '-35.72192', + 'prox_geo_code_2' => '174.32034', + ]); + $this->assertEquals(1, $contact['count']); + $this->assertEquals($contactID, $contact['id']); + } + public function testLoggedInUserAPISupportToken() { $description = "Get contact id of the current logged in user"; $subFile = "ContactIDOfLoggedInUserContactAPI"; -- 2.25.1