From cdfa6649f87d29341cc680e5d42991b23283a238 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 24 Mar 2021 17:29:16 +1300 Subject: [PATCH] Fix for failure to handle label on import This adds generic handling for where label has been submitted and flips to using the api to provide generic handling for names --- CRM/Contact/Import/Parser/Contact.php | 28 ++++++++++++------- api/v3/utils.php | 5 ++++ .../CRM/Contact/Import/Parser/ContactTest.php | 6 ++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 75bbb421d3..3cc2600b25 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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'; @@ -425,6 +428,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { * * @throws \CiviCRM_API3_Exception * @throws \CRM_Core_Exception + * @throws \API_Exception */ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) { $config = CRM_Core_Config::singleton(); @@ -640,16 +644,20 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { //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']; } @@ -1607,8 +1615,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { } } - $contact = CRM_Contact_BAO_Contact::create($data); - $cid = $contact->id; + $contact = civicrm_api3('Contact', 'create', $data); + $cid = $contact['id']; CRM_Core_Config::setPermitCacheFlushMode(TRUE); diff --git a/api/v3/utils.php b/api/v3/utils.php index 88237f41f0..f67ada4890 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -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; diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index e3025cacbb..42a7a6f439 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -461,7 +461,7 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { * @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], @@ -491,9 +491,11 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { /** * 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', -- 2.25.1