From 257195f949b98c7853a043e0452c64f5d1310222 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 9 Jul 2019 20:23:38 +1200 Subject: [PATCH] Extract mapping converter function, kinda brutally --- CRM/Core/BAO/Mapping.php | 138 ++++++++++++++++++++++----------------- 1 file changed, 78 insertions(+), 60 deletions(-) diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index 768b997f97..df7ed0671a 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -990,6 +990,80 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { return $compArray; } + /** + * Get the parameters for a mapping field in a saveable format from the quickform mapping format. + * + * @param array $params + * @param int $mappingId + * @param array $v + * @param string $k + * @param string $key + * @param int $colCnt + * + * @return array + */ + protected static function getMappingParams($params, $mappingId, $v, $k, $key, $colCnt) { + $locationTypeId = NULL; + $phoneTypeId = NULL; + $saveMappingFields = new CRM_Core_DAO_MappingField(); + + $saveMappingFields->mapping_id = $mappingId; + $saveMappingFields->name = CRM_Utils_Array::value('1', $v); + $saveMappingFields->contact_type = CRM_Utils_Array::value('0', $v); + $locationId = CRM_Utils_Array::value('2', $v); + $saveMappingFields->location_type_id = is_numeric($locationId) ? $locationId : NULL; + + if ($v[1] == 'phone') { + $saveMappingFields->phone_type_id = CRM_Utils_Array::value('3', $v); + } + elseif ($v[1] == 'im') { + $saveMappingFields->im_provider_id = CRM_Utils_Array::value('3', $v); + } + + if (!empty($params['operator'])) { + $saveMappingFields->operator = CRM_Utils_Array::value($k, $params['operator'][$key]); + } + if (!empty($params['value'])) { + $saveMappingFields->value = CRM_Utils_Array::value($k, $params['value'][$key]); + } + // Handle mapping for 'related contact' fields + if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) { + list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v)); + if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { + + if (!empty($v['2'])) { + $saveMappingFields->name = CRM_Utils_Array::value('2', $v); + } + elseif (!empty($v['4'])) { + $saveMappingFields->name = CRM_Utils_Array::value('4', $v); + } + + if (is_numeric(CRM_Utils_Array::value('3', $v))) { + $locationTypeId = CRM_Utils_Array::value('3', $v); + } + elseif (is_numeric(CRM_Utils_Array::value('5', $v))) { + $locationTypeId = CRM_Utils_Array::value('5', $v); + } + + if (is_numeric(CRM_Utils_Array::value('4', $v))) { + $phoneTypeId = CRM_Utils_Array::value('4', $v); + } + elseif (is_numeric(CRM_Utils_Array::value('6', $v))) { + $phoneTypeId = CRM_Utils_Array::value('6', $v); + } + + $saveMappingFields->location_type_id = is_numeric($locationTypeId) ? $locationTypeId : NULL; + $saveMappingFields->phone_type_id = is_numeric($phoneTypeId) ? $phoneTypeId : NULL; + $saveMappingFields->relationship_type_id = $id; + $saveMappingFields->relationship_direction = "{$first}_{$second}"; + } + } + + $saveMappingFields->grouping = $key; + $saveMappingFields->column_number = $colCnt; + return (array) $saveMappingFields; + } + /** * Function returns all custom fields with group title and * field label @@ -1222,69 +1296,13 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { foreach ($params['mapper'] as $key => $value) { $colCnt = 0; foreach ($value as $k => $v) { - $locationTypeid = NULL; - $phoneTypeid = NULL; if (!empty($v['1'])) { - $saveMappingFields = new CRM_Core_DAO_MappingField(); - - $saveMappingFields->mapping_id = $mappingId; - $saveMappingFields->name = CRM_Utils_Array::value('1', $v); - $saveMappingFields->contact_type = CRM_Utils_Array::value('0', $v); - $locationId = CRM_Utils_Array::value('2', $v); - $saveMappingFields->location_type_id = is_numeric($locationId) ? $locationId : NULL; - - if ($v[1] == 'phone') { - $saveMappingFields->phone_type_id = CRM_Utils_Array::value('3', $v); - } - elseif ($v[1] == 'im') { - $saveMappingFields->im_provider_id = CRM_Utils_Array::value('3', $v); - } - - if (!empty($params['operator'])) { - $saveMappingFields->operator = CRM_Utils_Array::value($k, $params['operator'][$key]); - } - if (!empty($params['value'])) { - $saveMappingFields->value = CRM_Utils_Array::value($k, $params['value'][$key]); - } - // Handle mapping for 'related contact' fields - if (count(explode('_', CRM_Utils_Array::value('1', $v))) > 2) { - list($id, $first, $second) = explode('_', CRM_Utils_Array::value('1', $v)); - if (($first == 'a' && $second == 'b') || ($first == 'b' && $second == 'a')) { - - if (!empty($v['2'])) { - $saveMappingFields->name = CRM_Utils_Array::value('2', $v); - } - elseif (!empty($v['4'])) { - $saveMappingFields->name = CRM_Utils_Array::value('4', $v); - } - - if (is_numeric(CRM_Utils_Array::value('3', $v))) { - $locationTypeid = CRM_Utils_Array::value('3', $v); - } - elseif (is_numeric(CRM_Utils_Array::value('5', $v))) { - $locationTypeid = CRM_Utils_Array::value('5', $v); - } - - if (is_numeric(CRM_Utils_Array::value('4', $v))) { - $phoneTypeid = CRM_Utils_Array::value('4', $v); - } - elseif (is_numeric(CRM_Utils_Array::value('6', $v))) { - $phoneTypeid = CRM_Utils_Array::value('6', $v); - } - - $saveMappingFields->location_type_id = is_numeric($locationTypeid) ? $locationTypeid : NULL; - $saveMappingFields->phone_type_id = is_numeric($phoneTypeid) ? $phoneTypeid : NULL; - $saveMappingFields->relationship_type_id = $id; - $saveMappingFields->relationship_direction = "{$first}_{$second}"; - } - } - - $saveMappingFields->grouping = $key; - $saveMappingFields->column_number = $colCnt; - $saveMappingFields->save(); + $saveMappingParams = self::getMappingParams($params, $mappingId, $v, $k, $key, $colCnt); + $saveMappingField = new CRM_Core_DAO_MappingField(); + $saveMappingField->copyValues($saveMappingParams, TRUE); + $saveMappingField->save(); $colCnt++; - $locationTypeid = $phoneTypeid = NULL; } } } -- 2.25.1