From eaedbd919153ca861b5fc15f61d082dcca383d35 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 31 May 2022 10:07:49 +1200 Subject: [PATCH 1/4] Remove legacy calls to civicrm_error, unpack return The function can only return an array or a BAO object --- CRM/Contact/Import/Parser/Contact.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 9ddeb885a6a6..c7b592369d70 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -419,7 +419,7 @@ public function import($onDuplicate, &$values) { $matchedIDs = []; // To update/fill contact, get the matching contact Ids if duplicate contact found // otherwise get contact Id from object of related contact - if (is_array($relatedNewContact) && civicrm_error($relatedNewContact)) { + if (is_array($relatedNewContact)) { if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { $matchedIDs = $relatedNewContact['error_message']['params'][0]; if (!is_array($matchedIDs)) { @@ -543,7 +543,7 @@ public function import($onDuplicate, &$values) { return $this->processMessage($values, $this->_retCode); } //dupe checking - if (is_array($newContact) && civicrm_error($newContact)) { + if (is_array($newContact)) { $code = NULL; if (($code = CRM_Utils_Array::value('code', $newContact['error_message'])) && ($code == CRM_Core_Error::DUPLICATE_CONTACT)) { @@ -1141,7 +1141,8 @@ public static function addToErrorMsg($errorName, &$errorMessage) { * @param bool $requiredCheck * @param int $dedupeRuleGroupID * - * @return array|bool|\CRM_Contact_BAO_Contact|\CRM_Core_Error|null + * @return array|\CRM_Contact_BAO_Contact + * If a duplicate is found an array is returned, otherwise CRM_Contact_BAO_Contact */ public function createContact(&$formatted, &$contactFields, $onDuplicate, $contactId = NULL, $requiredCheck = TRUE, $dedupeRuleGroupID = NULL) { $dupeCheck = FALSE; @@ -1784,7 +1785,7 @@ private function handleDuplicateError(array $newContact, array $values, int $onD $newContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); } - if (civicrm_error($newContact)) { + if (is_array($newContact)) { if (empty($newContact['error_message']['params'])) { // different kind of error other than DUPLICATE $errorMessage = $newContact['error_message']; From 66f7ef7aca1da9431e497f6a9e09c6ce3c84acaa Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 31 May 2022 10:11:51 +1200 Subject: [PATCH 2/4] Remove unreachable code --- CRM/Contact/Import/Parser/Contact.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index c7b592369d70..0b62f0618a5f 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -1786,14 +1786,6 @@ private function handleDuplicateError(array $newContact, array $values, int $onD } if (is_array($newContact)) { - if (empty($newContact['error_message']['params'])) { - // different kind of error other than DUPLICATE - $errorMessage = $newContact['error_message']; - array_unshift($values, $errorMessage); - $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage); - return CRM_Import_Parser::ERROR; - } - $contactID = $newContact['error_message']['params'][0]; if (is_array($contactID)) { $contactID = array_pop($contactID); From 8fbfda98703327933577a429c654a5c31f7919d2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 31 May 2022 10:21:11 +1200 Subject: [PATCH 3/4] Remove more checks that can be is_array --- CRM/Contact/Import/Parser/Contact.php | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 0b62f0618a5f..155578ad7d38 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -331,7 +331,7 @@ public function import($onDuplicate, &$values) { $this->_retCode = CRM_Import_Parser::VALID; } } - elseif (CRM_Core_Error::isAPIError($newContact, CRM_Core_Error::DUPLICATE_CONTACT)) { + elseif (is_array($newContact)) { // if duplicate, no need of further processing if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { $errorMessage = "Skipping duplicate record"; @@ -372,7 +372,7 @@ public function import($onDuplicate, &$values) { } $primaryContactId = NULL; - if (CRM_Core_Error::isAPIError($newContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { + if (is_array($newContact)) { if ($dupeCount == 1 && CRM_Utils_Rule::integer($contactID)) { $primaryContactId = $contactID; } @@ -381,7 +381,7 @@ public function import($onDuplicate, &$values) { $primaryContactId = $newContact->id; } - if ((CRM_Core_Error::isAPIError($newContact, CRM_Core_ERROR::DUPLICATE_CONTACT) || is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) { + if ((is_array($newContact) || is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) { //relationship contact insert foreach ($this->getRelatedContactsParams($params) as $key => $field) { @@ -420,17 +420,9 @@ public function import($onDuplicate, &$values) { // To update/fill contact, get the matching contact Ids if duplicate contact found // otherwise get contact Id from object of related contact if (is_array($relatedNewContact)) { - if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { - $matchedIDs = $relatedNewContact['error_message']['params'][0]; - if (!is_array($matchedIDs)) { - $matchedIDs = explode(',', $matchedIDs); - } - } - else { - $errorMessage = $relatedNewContact['error_message']; - array_unshift($values, $errorMessage); - $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage); - return CRM_Import_Parser::ERROR; + $matchedIDs = $relatedNewContact['error_message']['params'][0]; + if (!is_array($matchedIDs)) { + $matchedIDs = explode(',', $matchedIDs); } } else { @@ -454,11 +446,11 @@ public function import($onDuplicate, &$values) { return CRM_Import_Parser::NO_MATCH; } else { - $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]); + $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]); } } static $relativeContact = []; - if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { + if (is_array($relatedNewContact)) { if (count($matchedIDs) >= 1) { $relContactId = $matchedIDs[0]; //add relative contact to count during update & fill mode. @@ -480,7 +472,7 @@ public function import($onDuplicate, &$values) { $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; } - if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT) || ($relatedNewContact instanceof CRM_Contact_BAO_Contact)) { + if (is_array($relatedNewContact) || ($relatedNewContact instanceof CRM_Contact_BAO_Contact)) { //fix for CRM-1993.Checks for duplicate related contacts if (count($matchedIDs) >= 1) { //if more than one duplicate contact From 085927a84357104e17bf09ceb9a235014090f4e8 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 31 May 2022 10:27:08 +1200 Subject: [PATCH 4/4] Another unreachable place - code is always duplicate if it is an array --- CRM/Contact/Import/Parser/Contact.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 155578ad7d38..e3c450d4f763 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -536,17 +536,7 @@ public function import($onDuplicate, &$values) { } //dupe checking if (is_array($newContact)) { - $code = NULL; - - if (($code = CRM_Utils_Array::value('code', $newContact['error_message'])) && ($code == CRM_Core_Error::DUPLICATE_CONTACT)) { - return $this->handleDuplicateError($newContact, $values, $onDuplicate, $formatted, $contactFields); - } - // Not a dupe, so we had an error - $errorMessage = $newContact['error_message']; - array_unshift($values, $errorMessage); - $this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage); - return CRM_Import_Parser::ERROR; - + return $this->handleDuplicateError($newContact, $values, $onDuplicate, $formatted, $contactFields); } if (empty($this->_unparsedStreetAddressContacts)) {