Skip to content

Commit

Permalink
Merge pull request #17080 from colemanw/importExtract
Browse files Browse the repository at this point in the history
[REF] Import - extract duplicate code to function
  • Loading branch information
colemanw authored Apr 19, 2020
2 parents e3e2d18 + be40742 commit 82ef5c9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 72 deletions.
19 changes: 1 addition & 18 deletions CRM/Contribute/Import/Parser/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand Down
19 changes: 1 addition & 18 deletions CRM/Event/Import/Parser/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
19 changes: 1 addition & 18 deletions CRM/Member/Import/Parser/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
19 changes: 1 addition & 18 deletions CRM/Utils/DeprecatedUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 82ef5c9

Please sign in to comment.