Skip to content
Snippets Groups Projects
Unverified Commit b3c3e52b authored by colemanw's avatar colemanw Committed by GitHub
Browse files

Merge pull request #24848 from eileenmcnaughton/import_box

#3850 Fix checkboxes handling for custom fields in contact import
parents 67e9abdd e99a2a1a
No related branches found
No related tags found
No related merge requests found
...@@ -812,9 +812,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { ...@@ -812,9 +812,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
$data[$blockName][$loc]['is_primary'] = 1; $data[$blockName][$loc]['is_primary'] = 1;
} }
if (0) { if (1) {
}
else {
if ($fieldName === 'state_province') { if ($fieldName === 'state_province') {
// CRM-3393 // CRM-3393
if (is_numeric($value) && ((int ) $value) >= 1000) { if (is_numeric($value) && ((int ) $value) >= 1000) {
...@@ -844,72 +842,27 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { ...@@ -844,72 +842,27 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
} }
} }
} }
else { if (TRUE) {
if (($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key))) { // @todo - meaningless IF - left to keep whitespace clean in PR.
// for autocomplete transfer hidden value instead of label if ($key === 'location') {
if ($params[$key] && isset($params[$key . '_id'])) { foreach ($value as $locationTypeId => $field) {
$value = $params[$key . '_id']; foreach ($field as $block => $val) {
} if ($block === 'address' && array_key_exists('address_name', $val)) {
$value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name'];
// 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']);
} }
} }
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; continue;
} }
else { else {
if ($key === 'location') { $data[$key] = $value;
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;
}
} }
} }
} }
......
First Name,Last name,Colour
Bob,Smith,"Red,Yellow,Blue"
Mary,Smith,"1,2,3"
...@@ -854,6 +854,49 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { ...@@ -854,6 +854,49 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
$this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]); $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 * Test importing in the Preferred Language Field
* *
...@@ -2095,6 +2138,8 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { ...@@ -2095,6 +2138,8 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
* e.g [['first_name']['email', 1]]. * e.g [['first_name']['email', 1]].
* {@see \CRM_Contact_Import_Parser_Contact::getMappingFieldFromMapperInput} * {@see \CRM_Contact_Import_Parser_Contact::getMappingFieldFromMapperInput}
* @param array $submittedValues * @param array $submittedValues
*
* @throws \CRM_Core_Exception
*/ */
protected function importCSV(string $csv, array $mapper, array $submittedValues = []): void { protected function importCSV(string $csv, array $mapper, array $submittedValues = []): void {
$submittedValues = array_merge([ $submittedValues = array_merge([
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment