From 3998c71efbbe2de917624dd07e437159f3989aea Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 31 Jan 2023 13:03:09 +1300 Subject: [PATCH] Extract external identifier checking --- CRM/Contact/Import/Parser/Contact.php | 34 +++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 98473619ff..8c02548a12 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -29,7 +29,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { use CRM_Contact_Import_MetadataTrait; - protected $_allExternalIdentifiers = []; + private $externalIdentifiers = []; /** * Array of successfully imported contact id's @@ -507,20 +507,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { } } } - - //check for duplicate external Identifier - $externalID = $params['external_identifier'] ?? NULL; - if ($externalID) { - /* If it's a dupe,external Identifier */ - - if ($externalDupe = CRM_Utils_Array::value($externalID, $this->_allExternalIdentifiers)) { - $errorMessage = ts('External ID conflicts with record %1', [1 => $externalDupe]); - throw new CRM_Core_Exception($errorMessage); - } - //otherwise, count it and move on - // @todo - lineCount is always 0 - $this->_allExternalIdentifiers[$externalID] = $this->_lineCount; - } + $this->checkForDuplicateExternalIdentifiers($params['external_identifier'] ?? ''); //date-format part ends @@ -1733,4 +1720,21 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { ][$outcome] ?? 'ERROR'; } + /** + * Return an error if the csv has more than one row with the same external identifier. + * + * @param string $externalIdentifier + * + * @throws \CRM_Core_Exception + */ + protected function checkForDuplicateExternalIdentifiers(string $externalIdentifier): void { + if ($externalIdentifier) { + $existingRow = array_search($externalIdentifier, $this->externalIdentifiers, TRUE); + if ($existingRow !== FALSE) { + throw new CRM_Core_Exception(ts('External ID conflicts with record %1', [1 => $existingRow + 1])); + } + $this->externalIdentifiers[] = $externalIdentifier; + } + } + } -- 2.25.1