Skip to content

Commit

Permalink
Merge pull request civicrm#19886 from eileenmcnaughton/import
Browse files Browse the repository at this point in the history
Fix for importing preferred language (and other fields) by label
  • Loading branch information
seamuslee001 authored Apr 16, 2021
2 parents b92bae9 + 91cd966 commit 33ce2de
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
28 changes: 18 additions & 10 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

use Civi\Api4\Contact;

require_once 'CRM/Utils/DeprecatedUtils.php';
require_once 'api/v3/utils.php';

Expand Down Expand Up @@ -425,6 +428,7 @@ public function getAllFields() {
*
* @throws \CiviCRM_API3_Exception
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
$config = CRM_Core_Config::singleton();
Expand Down Expand Up @@ -640,16 +644,20 @@ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
//now we create new contact in update/fill mode also.
$contactID = NULL;
if ($createNewContact || ($this->_retCode != CRM_Import_Parser::NO_MATCH && $this->_updateWithId)) {

//CRM-4430, don't carry if not submitted.
foreach (['prefix_id', 'suffix_id', 'gender_id'] as $name) {
if (!empty($formatted[$name])) {
$options = CRM_Contact_BAO_Contact::buildOptions($name, 'get');
if (!isset($options[$formatted[$name]])) {
$formatted[$name] = CRM_Utils_Array::key((string) $formatted[$name], $options);
}
// @todo - there are multiple places where formatting is done that need consolidation.
// This handles where the label has been passed in and it has gotten this far.
// probably a bunch of hard-coded stuff could be removed to rely on this.
$fields = Contact::getFields(FALSE)
->addWhere('options', '=', TRUE)
->setLoadOptions(TRUE)
->execute()->indexBy('name');
foreach ($fields as $fieldName => $fieldSpec) {
if (!empty($formatted[$fieldName])
&& empty($fieldSpec['options'][$formatted[$fieldName]])) {
$formatted[$fieldName] = array_search($formatted[$fieldName], $fieldSpec['options'], TRUE) ?? $formatted[$fieldName];
}
}
//CRM-4430, don't carry if not submitted.
if ($this->_updateWithId && !empty($params['id'])) {
$contactID = $params['id'];
}
Expand Down Expand Up @@ -1607,8 +1615,8 @@ public function createContact(&$formatted, &$contactFields, $onDuplicate, $conta
}
}

$contact = CRM_Contact_BAO_Contact::create($data);
$cid = $contact->id;
$contact = civicrm_api3('Contact', 'create', $data);
$cid = $contact['id'];

CRM_Core_Config::setPermitCacheFlushMode(TRUE);

Expand Down
2 changes: 0 additions & 2 deletions CRM/Member/Import/Parser/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,6 @@ public function membership_format_params($params, &$values, $create = FALSE) {
}
}

_civicrm_api3_custom_format_params($params, $values, 'Membership');

if ($create) {
// CRM_Member_BAO_Membership::create() handles membership_start_date, membership_join_date,
// membership_end_date and membership_source. So, if $values contains
Expand Down
5 changes: 5 additions & 0 deletions api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,11 @@ function _civicrm_api3_object_to_array_unique_fields(&$dao, &$values) {
* ID of entity per $extends.
*/
function _civicrm_api3_custom_format_params($params, &$values, $extends, $entityId = NULL) {
if (!empty($params['custom'])) {
// The Import class does the formatting first - ideally it wouldn't but this early return
// provides transitional support.
return;
}
$values['custom'] = [];
$checkCheckBoxField = FALSE;
$entity = $extends;
Expand Down
22 changes: 20 additions & 2 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public function testGenderLabel() {
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testPrefixLabel() {
public function testPrefixLabel(): void {
$this->callAPISuccess('OptionValue', 'create', ['option_group_id' => 'individual_prefix', 'name' => 'new_one', 'label' => 'special', 'value' => 70]);
$mapping = [
['name' => 'first_name', 'column_number' => 0],
Expand Down Expand Up @@ -484,9 +484,11 @@ public function testPrefixLabel() {
/**
* Test that labels work for importing custom data.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testCustomDataLabel() {
public function testCustomDataLabel(): void {
$this->createCustomGroupWithFieldOfType([], 'select');
$contactValues = [
'first_name' => 'Bill',
Expand Down Expand Up @@ -519,6 +521,22 @@ public function testCustomDataName() {
$this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]);
}

/**
* Test importing in the Preferred Language Field
*
* @throws \CRM_Core_Exception
*/
public function testPreferredLanguageImport() {
$contactValues = [
'first_name' => 'Bill',
'last_name' => 'Gates',
'email' => '[email protected]',
'nick_name' => 'Billy-boy',
'preferred_language' => 'English (Australia)',
];
$this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [NULL, NULL, 'Primary', NULL, NULL]);
}

/**
* Test that the import parser adds the address to the primary location.
*
Expand Down
5 changes: 3 additions & 2 deletions tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function tearDown(): void {
* Test Import.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testImport() {
$this->individualCreate();
Expand Down Expand Up @@ -334,9 +335,9 @@ protected function createImportObject(array $fields): \CRM_Member_Import_Parser_
* Test importing to a custom field.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
*/
public function testImportCustomData() {
public function testImportCustomData(): void {
$donaldDuckID = $this->individualCreate(['first_name' => 'Donald', 'last_name' => 'Duck']);
$this->createCustomGroupWithFieldsOfAllTypes(['extends' => 'Membership']);
$membershipImporter = $this->createImportObject([
Expand Down

0 comments on commit 33ce2de

Please sign in to comment.