diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 1980d227d4dff755922838b922d000fb1e72ad67..5c96f7c0e4c236c90e2e61426ed9ef8a429a3789 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -812,9 +812,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { $data[$blockName][$loc]['is_primary'] = 1; } - if (0) { - } - else { + if (1) { if ($fieldName === 'state_province') { // CRM-3393 if (is_numeric($value) && ((int ) $value) >= 1000) { @@ -844,72 +842,27 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { } } } - else { - if (($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key))) { - // for autocomplete transfer hidden value instead of label - if ($params[$key] && isset($params[$key . '_id'])) { - $value = $params[$key . '_id']; - } - - // we need to append time with date - if ($params[$key] && isset($params[$key . '_time'])) { - $value .= ' ' . $params[$key . '_time']; - } - - // if auth source is not checksum / login && $value is blank, do not proceed - CRM-10128 - if (($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0 && - ($value == '' || !isset($value)) - ) { - continue; - } - - $valueId = NULL; - - //CRM-13596 - check for contact_sub_type_hidden first - if (array_key_exists('contact_sub_type_hidden', $params)) { - $type = $params['contact_sub_type_hidden']; - } - else { - $type = $data['contact_type']; - if (!empty($data['contact_sub_type'])) { - $type = CRM_Utils_Array::explodePadded($data['contact_sub_type']); + if (TRUE) { + // @todo - meaningless IF - left to keep whitespace clean in PR. + if ($key === 'location') { + foreach ($value as $locationTypeId => $field) { + foreach ($field as $block => $val) { + if ($block === 'address' && array_key_exists('address_name', $val)) { + $value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name']; + } } } - - CRM_Core_BAO_CustomField::formatCustomField($customFieldId, - $data['custom'], - $value, - $type, - $valueId, - $contactID, - FALSE, - FALSE - ); } - elseif ($key === 'edit') { + if (in_array($key, ['nick_name', 'job_title', 'middle_name', 'birth_date', 'gender_id', 'current_employer', 'prefix_id', 'suffix_id']) + && ($value == '' || !isset($value)) && + ($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0 || + ($key === 'current_employer' && empty($params['current_employer']))) { + // CRM-10128: if auth source is not checksum / login && $value is blank, do not fill $data with empty value + // to avoid update with empty values continue; } else { - if ($key === 'location') { - foreach ($value as $locationTypeId => $field) { - foreach ($field as $block => $val) { - if ($block === 'address' && array_key_exists('address_name', $val)) { - $value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name']; - } - } - } - } - if (in_array($key, ['nick_name', 'job_title', 'middle_name', 'birth_date', 'gender_id', 'current_employer', 'prefix_id', 'suffix_id']) - && ($value == '' || !isset($value)) && - ($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0 || - ($key === 'current_employer' && empty($params['current_employer']))) { - // CRM-10128: if auth source is not checksum / login && $value is blank, do not fill $data with empty value - // to avoid update with empty values - continue; - } - else { - $data[$key] = $value; - } + $data[$key] = $value; } } } diff --git a/tests/phpunit/CRM/Contact/Import/Form/data/individual_with_custom_checkbox_field.csv b/tests/phpunit/CRM/Contact/Import/Form/data/individual_with_custom_checkbox_field.csv new file mode 100644 index 0000000000000000000000000000000000000000..62d17c04fd9208f39bae84a7ad5b209025b8becd --- /dev/null +++ b/tests/phpunit/CRM/Contact/Import/Form/data/individual_with_custom_checkbox_field.csv @@ -0,0 +1,3 @@ +First Name,Last name,Colour +Bob,Smith,"Red,Yellow,Blue" +Mary,Smith,"1,2,3" diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index b78b6c7456a1537bd9488539ed9b19307062c491..7d1f215ac8414926b24ca87fb832e33793e46d20 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -854,6 +854,49 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]); } + /** + * Test values import correctly when they are numbers. + * + * https://lab.civicrm.org/dev/core/-/issues/3850 + * @throws \CRM_Core_Exception + */ + public function testCustomCheckboxNumericValues(): void { + $this->createCustomGroupWithFieldOfType([], 'checkbox', '', [ + 'option_values' => [ + [ + 'label' => 'Red', + 'value' => '1', + 'weight' => 1, + 'is_active' => 1, + ], + [ + 'label' => 'Yellow', + 'value' => '2', + 'weight' => 2, + 'is_active' => 1, + ], + [ + 'label' => 'Blue', + 'value' => '3', + 'weight' => 3, + 'is_active' => 1, + ], + ], + ]); + $this->importCSV('individual_with_custom_checkbox_field.csv', [ + [0 => 'first_name'], + [0 => 'last_name'], + [0 => $this->getCustomFieldName('checkbox')], + ]); + $contacts = Contact::get()->addWhere('last_name', '=', 'Smith') + ->addSelect($this->getCustomFieldName('checkbox', 4)) + ->execute(); + $this->assertCount(2, $contacts); + foreach ($contacts as $contact) { + $this->assertEquals([1, 2, 3], $contact[$this->getCustomFieldName('checkbox', 4)]); + } + } + /** * Test importing in the Preferred Language Field * @@ -2095,6 +2138,8 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { * e.g [['first_name']['email', 1]]. * {@see \CRM_Contact_Import_Parser_Contact::getMappingFieldFromMapperInput} * @param array $submittedValues + * + * @throws \CRM_Core_Exception */ protected function importCSV(string $csv, array $mapper, array $submittedValues = []): void { $submittedValues = array_merge([