Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] extract getIdsOfMatchingContact #19172

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,16 +542,8 @@ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
$createNewContact = FALSE;
// @todo - it feels like all the rows from here to the end of the IF
// could be removed in favour of a simple check for whether the contact_type & id match
// the call to the deprecated function seems to add no value other that to do an additional
// check for the contact_id & type.
$error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted);
if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
if (is_array($error['error_message']['params'][0])) {
$matchedIDs = $error['error_message']['params'][0];
}
else {
$matchedIDs = explode(',', $error['error_message']['params'][0]);
}
$matchedIDs = $this->getIdsOfMatchingContacts($formatted);
if (!empty($matchedIDs)) {
if (count($matchedIDs) >= 1) {
$updateflag = TRUE;
foreach ($matchedIDs as $contactId) {
Expand Down Expand Up @@ -642,10 +634,6 @@ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) {
$relationship = TRUE;
}
elseif (is_a($error, 'CRM_Core_Error')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't actually possible

$newContact = $error;
$relationship = TRUE;
}
}

//fixed CRM-4148
Expand Down Expand Up @@ -925,7 +913,7 @@ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) {
'contact' => $primaryContactId,
];

list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds);
[$valid, $invalid, $duplicate, $saved, $relationshipIds] = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds);

if ($valid || $duplicate) {
$relationIds['contactTarget'] = $relContactId;
Expand Down Expand Up @@ -1195,7 +1183,7 @@ public static function isErrorInCustomData($params, &$errorMessage, $csType = NU
}
}
if (!empty($relation)) {
list($id, $first, $second) = CRM_Utils_System::explode('_', $relation, 3);
[$id, $first, $second] = CRM_Utils_System::explode('_', $relation, 3);
$direction = "contact_sub_type_$second";
$relationshipType = new CRM_Contact_BAO_RelationshipType();
$relationshipType->id = $id;
Expand Down Expand Up @@ -1609,7 +1597,7 @@ public function createContact(&$formatted, &$contactFields, $onDuplicate, $conta
$formatted['updateBlankLocInfo'] = FALSE;
}

list($data, $contactDetails) = CRM_Contact_BAO_Contact::formatProfileContactParams($formatted, $contactFields, $contactId, NULL, $formatted['contact_type']);
[$data, $contactDetails] = CRM_Contact_BAO_Contact::formatProfileContactParams($formatted, $contactFields, $contactId, NULL, $formatted['contact_type']);

// manage is_opt_out
if (array_key_exists('is_opt_out', $contactFields) && array_key_exists('is_opt_out', $formatted)) {
Expand Down
22 changes: 22 additions & 0 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,26 @@ public static function unserializeCustomValue($customFieldID, $value, $fieldType
return $values;
}

/**
* Get the ids of any contacts that match according to the rule.
*
* @param array $formatted
*
* @return array
*/
protected function getIdsOfMatchingContacts(array $formatted):array {
// the call to the deprecated function seems to add no value other that to do an additional
// check for the contact_id & type.
$error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted);
if (!CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
return [];
}
if (is_array($error['error_message']['params'][0])) {
return $error['error_message']['params'][0];
}
else {
return explode(',', $error['error_message']['params'][0]);
}
}

}
8 changes: 5 additions & 3 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,18 @@ public function testImportParserWtihEmployeeOfRelationship() {
}

/**
* Test that import parser will not fail when same external_identifier found of deleted contact.
* Test that import parser will not fail when same external_identifier found
* of deleted contact.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testImportParserWtihDeletedContactExternalIdentifier() {
public function testImportParserWithDeletedContactExternalIdentifier(): void {
$contactId = $this->individualCreate([
'external_identifier' => 'ext-1',
]);
$this->callAPISuccess('Contact', 'delete', ['id' => $contactId]);
list($originalValues, $result) = $this->setUpBaseContact([
[$originalValues, $result] = $this->setUpBaseContact([
'external_identifier' => 'ext-1',
]);
$originalValues['nick_name'] = 'Old Bill';
Expand Down