From f5d4a76c548050cec4c61dcf5bb0ec35e1c4c487 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 12 May 2022 12:12:29 +1200 Subject: [PATCH] [REF] [Import] Minor code cleanup This chunk of code effectively constructs an array 'value' which holds the location fields from the mapping. If the array is not empty then use it as an array, otherwise use just the value --- CRM/Contact/Import/Parser/Contact.php | 42 +++++++-------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index ff4290fc244f..6a139e622ead 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -2451,42 +2451,17 @@ private function getParams(array $values): array { $relatedContactType = $this->getRelatedContactType($mappedField['relationship_type_id'], $mappedField['relationship_direction']); $relatedContactLocationTypeID = $relatedContactKey ? $mappedField['location_type_id'] : NULL; $relatedContactWebsiteTypeID = $relatedContactKey ? $mappedField['website_type_id'] : NULL; - $relatedContactIMProviderID = $relatedContactKey ? $mappedField['im_provider_id'] : NULL; + $relatedContactIMProviderID = $relatedContactKey ? $mappedField['provider_id'] : NULL; $relatedContactPhoneTypeID = $relatedContactKey ? $mappedField['phone_type_id'] : NULL; - $locationTypeID = $relatedContactKey ? NULL : $mappedField['location_type_id']; - $phoneTypeID = $relatedContactKey ? NULL : $mappedField['phone_type_id']; - $imProviderID = $relatedContactKey ? NULL : $mappedField['im_provider_id']; - $websiteTypeID = $relatedContactKey ? NULL : $mappedField['website_type_id']; - + $locationFields = ['location_type_id', 'phone_type_id', 'provider_id', 'website_type_id']; + $value = array_filter(array_intersect_key($mappedField, array_fill_keys($locationFields, 1))); if (!$relatedContactKey) { - if (isset($locationTypeID)) { + if (!empty($value)) { if (!isset($params[$fieldName])) { $params[$fieldName] = []; } - - $value = [ - $fieldName => $importedValue, - 'location_type_id' => $locationTypeID, - ]; - - if (isset($phoneTypeID)) { - $value['phone_type_id'] = $phoneTypeID; - } - - // get IM service Provider type id - if (isset($imProviderID)) { - $value['provider_id'] = $imProviderID; - } - - $params[$fieldName][] = $value; - } - elseif (isset($websiteTypeID)) { - $value = [ - $fieldName => $importedValue, - 'website_type_id' => $websiteTypeID, - ]; - + $value[$fieldName] = $importedValue; $params[$fieldName][] = $value; } @@ -3249,6 +3224,7 @@ public function validateValues(array $values): void { * * This is the same format as saved in civicrm_mapping_field except * that location_type_id = 'Primary' rather than empty where relevant. + * Also 'im_provider_id' is mapped to the 'real' field name 'provider_id' * * @return array * @throws \API_Exception @@ -3260,8 +3236,12 @@ protected function getFieldMappings(): array { if (!$mappedField['location_type_id'] && !empty($this->importableFieldsMetadata[$mappedField['name']]['hasLocationType'])) { $mappedField['location_type_id'] = 'Primary'; } - // Just for clarity since 0 is a pseudovalue + // Just for clarity since 0 is a pseudo-value unset($mappedField['mapping_id']); + // Annoyingly the civicrm_mapping_field name for this differs from civicrm_im. + // Test cover in `CRM_Contact_Import_Parser_ContactTest::testMapFields` + $mappedField['provider_id'] = $mappedField['im_provider_id']; + unset($mappedField['im_provider_id']); $mappedFields[] = $mappedField; } return $mappedFields;