Skip to content

Commit

Permalink
[REF] extract getIdsOfMatchingContact
Browse files Browse the repository at this point in the history
It seems this really needs sorting to be shared between the parser classes
to fix up the error handling
  • Loading branch information
eileenmcnaughton committed Dec 11, 2020
1 parent 7194e56 commit a8ea392
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
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')) {
$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

0 comments on commit a8ea392

Please sign in to comment.