From: deb.monish Date: Fri, 6 May 2016 14:39:29 +0000 (+0530) Subject: CRM-18490: API does not return Address ID via API X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=66670e4d12e881620f2cdb991910332990fbc3e3;p=civicrm-core.git CRM-18490: API does not return Address ID via API --- diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index d0c25c7517..09129fddb0 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -405,6 +405,7 @@ class CRM_Contact_BAO_Query { * @param bool $smartGroupCache * @param null $displayRelationshipType * @param string $operator + * @param string $apiEntity * * @return \CRM_Contact_BAO_Query */ @@ -413,8 +414,10 @@ class CRM_Contact_BAO_Query { $includeContactIds = FALSE, $strict = FALSE, $mode = 1, $skipPermission = FALSE, $searchDescendentGroups = TRUE, $smartGroupCache = TRUE, $displayRelationshipType = NULL, - $operator = 'AND' + $operator = 'AND', + $apiEntity = NULL ) { + $this->_params = &$params; if ($this->_params == NULL) { $this->_params = array(); @@ -460,13 +463,13 @@ class CRM_Contact_BAO_Query { } // basically do all the work once, and then reuse it - $this->initialize(); + $this->initialize($apiEntity); } /** * Function which actually does all the work for the constructor. */ - public function initialize() { + public function initialize($apiEntity = NULL) { $this->_select = array(); $this->_element = array(); $this->_tables = array(); @@ -495,7 +498,7 @@ class CRM_Contact_BAO_Query { $this->_whereTables = $this->_tables; - $this->selectClause(); + $this->selectClause($apiEntity); $this->_whereClause = $this->whereClause(); if (array_key_exists('civicrm_contribution', $this->_whereTables)) { $component = 'contribution'; @@ -591,12 +594,23 @@ class CRM_Contact_BAO_Query { /** * Some composite fields do not appear in the fields array hack to make them part of the query. */ - public function addSpecialFields() { + public function addSpecialFields($apiEntity) { static $special = array('contact_type', 'contact_sub_type', 'sort_name', 'display_name'); + // if get called via Contact.get API having address_id as return parameter + if ($apiEntity == 'Contact') { + $special[] = 'address_id'; + } foreach ($special as $name) { if (!empty($this->_returnProperties[$name])) { - $this->_select[$name] = "contact_a.{$name} as $name"; - $this->_element[$name] = 1; + if ($name == 'address_id') { + $this->_tables['civicrm_address'] = 1; + $this->_select['address_id'] = 'civicrm_address.id as address_id'; + $this->_element['address_id'] = 1; + } + else { + $this->_select[$name] = "contact_a.{$name} as $name"; + $this->_element[$name] = 1; + } } } } @@ -608,9 +622,9 @@ class CRM_Contact_BAO_Query { * tables, the initial attempt also retrieves all variables used * in the params list */ - public function selectClause() { + public function selectClause($apiEntity = NULL) { - $this->addSpecialFields(); + $this->addSpecialFields($apiEntity); foreach ($this->_fields as $name => $field) { // skip component fields @@ -4263,14 +4277,17 @@ civicrm_relationship.is_permission_a_b = 0 $smartGroupCache = TRUE, $count = FALSE, $skipPermissions = TRUE, - $mode = 1 + $mode = 1, + $apiEntity = NULL ) { $query = new CRM_Contact_BAO_Query( $params, $returnProperties, NULL, TRUE, FALSE, $mode, $skipPermissions, - TRUE, $smartGroupCache + TRUE, $smartGroupCache, + NULL, 'AND', + $apiEntity ); //this should add a check for view deleted if permissions are enabled diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index 1c37dde847..eb0314e60e 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -239,7 +239,7 @@ class CRM_Contribute_BAO_Query { // Adding address_id in a way that is more easily extendable since the above is a bit ... wordy. $supportedBasicReturnValues = array('address_id'); foreach ($supportedBasicReturnValues as $fieldName) { - if (!empty($query->_returnProperties[$fieldName])) { + if (!empty($query->_returnProperties[$fieldName]) && empty($query->_select[$fieldName])) { $query->_select[$fieldName] = "civicrm_contribution.{$fieldName} as $fieldName"; $query->_element[$fieldName] = $query->_tables['civicrm_contribution'] = 1; } diff --git a/api/v3/utils.php b/api/v3/utils.php index 09c0a3dc27..916eae4c04 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -626,7 +626,8 @@ function _civicrm_api3_get_using_query_object($entity, $params, $additional_opti $smartGroupCache, $getCount, $skipPermissions, - $mode + $mode, + $entity ); return $entities; diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 464a9db48f..8ea30e2eb1 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -860,20 +860,23 @@ class api_v3_ContactTest extends CiviUnitTestCase { } /** - * Check that address name is returned if required. + * Check that address name, ID is returned if required. */ - public function testGetReturnAddressName() { + public function testGetReturnAddress() { $contactID = $this->individualCreate(); - $this->callAPISuccess('address', 'create', array( + $result = $this->callAPISuccess('address', 'create', array( 'contact_id' => $contactID, 'address_name' => 'My house', 'location_type_id' => 'Home', 'street_address' => '1 my road', )); + $addressID = $result['id']; + $result = $this->callAPISuccessGetSingle('contact', array( - 'return' => 'address_name, street_address', + 'return' => 'address_name, street_address, address_id', 'id' => $contactID, )); + $this->assertEquals($addressID, $result['address_id']); $this->assertEquals('1 my road', $result['street_address']); $this->assertEquals('My house', $result['address_name']);