From be40742b430244952b890a25371f243e558f4806 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 15 Apr 2020 16:50:01 -0400 Subject: [PATCH] Import - extract duplicate code to function FIXME: Extracting this was a first step, but there's also still lots of inconsistency and duplication with how the various import classes handle custom data. --- CRM/Contribute/Import/Parser/Contribution.php | 19 +--------- CRM/Event/Import/Parser/Participant.php | 19 +--------- CRM/Import/Parser.php | 37 +++++++++++++++++++ CRM/Member/Import/Parser/Membership.php | 19 +--------- CRM/Utils/DeprecatedUtils.php | 19 +--------- 5 files changed, 41 insertions(+), 72 deletions(-) diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index 0ab29840850a..41fe4b8e4b61 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -653,24 +653,7 @@ private function deprecatedFormatParams($params, &$values, $create = FALSE, $onD $values[$key] = $value; $type = $customFields[$customFieldID]['html_type']; if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { - $mulValues = explode(',', $value); - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - $values[$key] = []; - foreach ($mulValues as $v1) { - foreach ($customOption as $customValueID => $customLabel) { - $customValue = $customLabel['value']; - if ((strtolower($customLabel['label']) == strtolower(trim($v1))) || - (strtolower($customValue) == strtolower(trim($v1))) - ) { - if ($type == 'CheckBox') { - $values[$key][$customValue] = 1; - } - else { - $values[$key][] = $customValue; - } - } - } - } + $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type); } elseif ($type == 'Select' || $type == 'Radio' || ($type == 'Autocomplete-Select' && diff --git a/CRM/Event/Import/Parser/Participant.php b/CRM/Event/Import/Parser/Participant.php index a3f5142787fc..bb1703c83544 100644 --- a/CRM/Event/Import/Parser/Participant.php +++ b/CRM/Event/Import/Parser/Participant.php @@ -513,24 +513,7 @@ protected function formatValues(&$values, $params) { $values[$key] = $value; $type = $customFields[$customFieldID]['html_type']; if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { - $mulValues = explode(',', $value); - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - $values[$key] = []; - foreach ($mulValues as $v1) { - foreach ($customOption as $customValueID => $customLabel) { - $customValue = $customLabel['value']; - if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || - (strtolower(trim($customValue)) == strtolower(trim($v1))) - ) { - if ($type == 'CheckBox') { - $values[$key][$customValue] = 1; - } - else { - $values[$key][] = $customValue; - } - } - } - } + $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type); } elseif ($type == 'Select' || $type == 'Radio') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index bfe559191dc5..957cb9b48699 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -567,4 +567,41 @@ protected function parsePseudoConstantField($submittedValue, $fieldSpec) { return ''; } + /** + * This is code extracted from 4 places where this exact snippet was being duplicated. + * + * FIXME: Extracting this was a first step, but there's also + * 1. Inconsistency in the way other select options are handled. + * Contribution adds handling for Select/Radio/Autocomplete + * Participant/Activity only handles Select/Radio and misses Autocomplete + * Membership is missing all of it + * 2. Inconsistency with the way this works vs. how it's implemented in Contact import. + * + * @param $customFieldID + * @param $value + * @param $fieldType + * @return array + */ + public static function unserializeCustomValue($customFieldID, $value, $fieldType) { + $mulValues = explode(',', $value); + $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); + $values = []; + foreach ($mulValues as $v1) { + foreach ($customOption as $customValueID => $customLabel) { + $customValue = $customLabel['value']; + if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || + (strtolower(trim($customValue)) == strtolower(trim($v1))) + ) { + if ($fieldType == 'CheckBox') { + $values[$customValue] = 1; + } + else { + $values[] = $customValue; + } + } + } + } + return $values; + } + } diff --git a/CRM/Member/Import/Parser/Membership.php b/CRM/Member/Import/Parser/Membership.php index abd91e7e9c63..b43b507705f7 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -631,24 +631,7 @@ public function membership_format_params($params, &$values, $create = FALSE) { $values[$key] = $value; $type = $customFields[$customFieldID]['html_type']; if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { - $mulValues = explode(',', $value); - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - $values[$key] = []; - foreach ($mulValues as $v1) { - foreach ($customOption as $customValueID => $customLabel) { - $customValue = $customLabel['value']; - if ((strtolower($customLabel['label']) == strtolower(trim($v1))) || - (strtolower($customValue) == strtolower(trim($v1))) - ) { - if ($type == 'CheckBox') { - $values[$key][$customValue] = 1; - } - else { - $values[$key][] = $customValue; - } - } - } - } + $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type); } } diff --git a/CRM/Utils/DeprecatedUtils.php b/CRM/Utils/DeprecatedUtils.php index ebde5c37542a..af1b411902e2 100644 --- a/CRM/Utils/DeprecatedUtils.php +++ b/CRM/Utils/DeprecatedUtils.php @@ -135,24 +135,7 @@ function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $ $values[$key] = $value; $type = $customFields[$customFieldID]['html_type']; if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { - $mulValues = explode(',', $value); - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - $values[$key] = []; - foreach ($mulValues as $v1) { - foreach ($customOption as $customValueID => $customLabel) { - $customValue = $customLabel['value']; - if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || - (strtolower(trim($customValue)) == strtolower(trim($v1))) - ) { - if ($type == 'CheckBox') { - $values[$key][$customValue] = 1; - } - else { - $values[$key][] = $customValue; - } - } - } - } + $values[$key] = CRM_Import_Parser::unserializeCustomValue($customFieldID, $value, $type); } elseif ($type == 'Select' || $type == 'Radio') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);