From ab0d2051c5d4ef74e3a98852cc98e7577c2c4f21 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Thu, 4 May 2023 16:10:31 -0400 Subject: [PATCH] Import fill on email/phone respects location type --- CRM/Contact/Import/Parser/Contact.php | 66 ++++++++++--------- .../CRM/Contact/Import/Parser/ContactTest.php | 29 ++++++++ 2 files changed, 64 insertions(+), 31 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 1e5dd16620..e18c460c2d 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -885,48 +885,52 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { $contact = get_object_vars($contactObj); foreach ($params as $key => $value) { - if ($key === 'id' || $key === 'contact_type' || $key === 'address') { + if (in_array($key, ['id', 'contact_type'])) { continue; } - - if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) { - $custom_params = ['id' => $contact['id'], 'return' => $key]; - $getValue = civicrm_api3('Contact', 'getvalue', $custom_params); - if (empty($getValue)) { - unset($getValue); + // These values must be handled differently because we need to account for location type. + $checkLocationType = in_array($key, ['address', 'phone', 'email']); + + if (!$checkLocationType) { + if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) { + $custom_params = ['id' => $contact['id'], 'return' => $key]; + $getValue = civicrm_api3('Contact', 'getvalue', $custom_params); + if (empty($getValue)) { + unset($getValue); + } + } + else { + $getValue = CRM_Utils_Array::retrieveValueRecursive($contact, $key); } - } - else { - $getValue = CRM_Utils_Array::retrieveValueRecursive($contact, $key); - } - if ($modeFill && isset($getValue)) { - unset($params[$key]); - if ($customFieldId) { - // Extra values must be unset to ensure the values are not - // imported. - unset($params['custom'][$customFieldId]); + if ($modeFill && isset($getValue)) { + unset($params[$key]); + if ($customFieldId) { + // Extra values must be unset to ensure the values are not + // imported. + unset($params['custom'][$customFieldId]); + } } } - } - - if (isset($params['address']) && is_array($params['address'])) { - foreach ($params['address'] as $key => $value) { - if ($modeFill) { - $getValue = CRM_Utils_Array::retrieveValueRecursive($contact, 'address'); - - if (isset($getValue)) { - foreach ($getValue as $cnt => $values) { - if ((!empty($getValue[$cnt]['location_type_id']) && !empty($params['address'][$key]['location_type_id'])) && $getValue[$cnt]['location_type_id'] == $params['address'][$key]['location_type_id']) { - unset($params['address'][$key]); + else { + if (is_array($params[$key]) ?? FALSE) { + foreach ($params[$key] as $innerKey => $value) { + if ($modeFill) { + $getValue = CRM_Utils_Array::retrieveValueRecursive($contact, $key); + if (isset($getValue)) { + foreach ($getValue as $cnt => $values) { + if ((!empty($getValue[$cnt]['location_type_id']) && !empty($params[$key][$innerKey]['location_type_id'])) && $getValue[$cnt]['location_type_id'] == $params[$key][$innerKey]['location_type_id']) { + unset($params[$key][$innerKey]); + } + } } } } + if (count($params[$key]) == 0) { + unset($params[$key]); + } } } - if (count($params['address']) == 0) { - unset($params['address']); - } } } diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 732d00bbec..e6edd920e9 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -396,6 +396,35 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $this->assertEquals('212-555-1212', $anthonysPhone['phone']); } + /** + * Test that importing a phone/email with "Fill" strategy respects location type. + * + * @throws \Exception + */ + public function testImportFillWithLocationType(): void { + $anthony = $this->individualCreate(); + Phone::create() + ->addValue('contact_id', $anthony) + ->addValue('location_type_id:label', 'Home') + ->addValue('phone', '123-456-7890') + ->execute(); + $homeLocationTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', 'Home'); + $workLocationTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', 'Work'); + $phoneTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', 'Phone'); + $fieldMapping = [ + ['name' => 'id'], + ['name' => 'phone', 'location_type_id' => $workLocationTypeID, 'phone_type_id' => $phoneTypeID], + ]; + $this->runImport([ + 'id' => $anthony, + 'phone' => '212-555-1212', + ], CRM_Import_Parser::DUPLICATE_FILL, FALSE, $fieldMapping); + $homePhone = $this->callAPISuccessGetSingle('Phone', ['contact_id' => $anthony, 'location_type_id' => $homeLocationTypeID]); + $workPhone = $this->callAPISuccessGetSingle('Phone', ['contact_id' => $anthony, 'location_type_id' => $workLocationTypeID]); + $this->assertEquals('123-456-7890', $homePhone['phone']); + $this->assertEquals('212-555-1212', $workPhone['phone']); + } + /** * Test import parser will fallback to external identifier. * -- 2.25.1