From 449fda1c48b1c21fe58cd5f806fff9e0f7d4901d Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 9 Jul 2019 12:58:16 +1200 Subject: [PATCH] Extract saveMapping Field Basic extraction. Tangental to export mapping cleanup --- CRM/Contact/Import/Form/MapField.php | 115 +++++++++++++++------------ 1 file changed, 66 insertions(+), 49 deletions(-) diff --git a/CRM/Contact/Import/Form/MapField.php b/CRM/Contact/Import/Form/MapField.php index fa24b12165..e2710bee6f 100644 --- a/CRM/Contact/Import/Form/MapField.php +++ b/CRM/Contact/Import/Form/MapField.php @@ -713,6 +713,7 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { * @param $mapperKeys * * @return \CRM_Contact_Import_Parser_Contact + * @throws \CiviCRM_API3_Exception */ public function submit($params, $mapperKeys) { $mapper = $mapperKeysMain = $locations = array(); @@ -893,57 +894,11 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { $cType = 'Organization'; } + $mappingID = NULL; for ($i = 0; $i < $this->_columnCount; $i++) { - $saveMappingFields = new CRM_Core_DAO_MappingField(); - $saveMappingFields->mapping_id = $saveMapping['id']; - $saveMappingFields->contact_type = $cType; - $saveMappingFields->column_number = $i; - - $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3); - $id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL; - $first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL; - $second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL; - if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { - $saveMappingFields->name = ucwords(str_replace("_", " ", $mapperKeys[$i][1])); - $saveMappingFields->relationship_type_id = $id; - $saveMappingFields->relationship_direction = "{$first}_{$second}"; - // to get phoneType id and provider id separately - // before saving mappingFields of phone and IM for related contact, CRM-3140 - if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'url') { - $saveMappingFields->website_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; - } - else { - if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'phone') { - $saveMappingFields->phone_type_id = isset($mapperKeys[$i][3]) ? $mapperKeys[$i][3] : NULL; - } - elseif (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'im') { - $saveMappingFields->im_provider_id = isset($mapperKeys[$i][3]) ? $mapperKeys[$i][3] : NULL; - } - $saveMappingFields->location_type_id = (isset($mapperKeys[$i][2]) && $mapperKeys[$i][2] !== 'Primary') ? $mapperKeys[$i][2] : NULL; - } - } - else { - $saveMappingFields->name = $mapper[$i]; - $locationTypeID = $parserParameters['mapperLocType'][$i]; - // to get phoneType id and provider id separately - // before saving mappingFields of phone and IM, CRM-3140 - if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') { - $saveMappingFields->website_type_id = isset($mapperKeys[$i][1]) ? $mapperKeys[$i][1] : NULL; - } - else { - if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'phone') { - $saveMappingFields->phone_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; - } - elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') { - $saveMappingFields->im_provider_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; - } - $saveMappingFields->location_type_id = is_numeric($locationTypeID) ? $locationTypeID : NULL; - } - $saveMappingFields->relationship_type_id = NULL; - } - $saveMappingFields->save(); + $mappingID = $this->saveMappingField($mapperKeys, $saveMapping, $cType, $i, $mapper, $parserParameters); } - $this->set('savedMapping', $saveMappingFields->mapping_id); + $this->set('savedMapping', $mappingID); } $parser = new CRM_Contact_Import_Parser_Contact($mapperKeysMain, $parserParameters['mapperLocType'], $parserParameters['mapperPhoneType'], @@ -970,4 +925,66 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField { return $parser; } + /** + * @param $mapperKeys + * @param array $saveMapping + * @param string $cType + * @param int $i + * @param array $mapper + * @param array $parserParameters + * + * @return int + */ + protected function saveMappingField($mapperKeys, array $saveMapping, string $cType, int $i, array $mapper, array $parserParameters): int { + $saveMappingFields = new CRM_Core_DAO_MappingField(); + $saveMappingFields->mapping_id = $saveMapping['id']; + $saveMappingFields->contact_type = $cType; + $saveMappingFields->column_number = $i; + + $mapperKeyParts = explode('_', $mapperKeys[$i][0], 3); + $id = isset($mapperKeyParts[0]) ? $mapperKeyParts[0] : NULL; + $first = isset($mapperKeyParts[1]) ? $mapperKeyParts[1] : NULL; + $second = isset($mapperKeyParts[2]) ? $mapperKeyParts[2] : NULL; + if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { + $saveMappingFields->name = ucwords(str_replace("_", " ", $mapperKeys[$i][1])); + $saveMappingFields->relationship_type_id = $id; + $saveMappingFields->relationship_direction = "{$first}_{$second}"; + // to get phoneType id and provider id separately + // before saving mappingFields of phone and IM for related contact, CRM-3140 + if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'url') { + $saveMappingFields->website_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; + } + else { + if (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'phone') { + $saveMappingFields->phone_type_id = isset($mapperKeys[$i][3]) ? $mapperKeys[$i][3] : NULL; + } + elseif (CRM_Utils_Array::value('1', $mapperKeys[$i]) == 'im') { + $saveMappingFields->im_provider_id = isset($mapperKeys[$i][3]) ? $mapperKeys[$i][3] : NULL; + } + $saveMappingFields->location_type_id = (isset($mapperKeys[$i][2]) && $mapperKeys[$i][2] !== 'Primary') ? $mapperKeys[$i][2] : NULL; + } + } + else { + $saveMappingFields->name = $mapper[$i]; + $locationTypeID = $parserParameters['mapperLocType'][$i]; + // to get phoneType id and provider id separately + // before saving mappingFields of phone and IM, CRM-3140 + if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'url') { + $saveMappingFields->website_type_id = isset($mapperKeys[$i][1]) ? $mapperKeys[$i][1] : NULL; + } + else { + if (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'phone') { + $saveMappingFields->phone_type_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; + } + elseif (CRM_Utils_Array::value('0', $mapperKeys[$i]) == 'im') { + $saveMappingFields->im_provider_id = isset($mapperKeys[$i][2]) ? $mapperKeys[$i][2] : NULL; + } + $saveMappingFields->location_type_id = is_numeric($locationTypeID) ? $locationTypeID : NULL; + } + $saveMappingFields->relationship_type_id = NULL; + } + $saveMappingFields->save(); + return $saveMappingFields->mapping_id; + } + } -- 2.25.1