From 31fdf2965b4558d3f216f3818107221c953ebdac Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 2 Jun 2022 18:35:51 +1200 Subject: [PATCH] Extract create relationship --- CRM/Contact/Import/Parser/Contact.php | 123 ++++++++++++++------------ 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 6fab0b405a..bcff5e6f60 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -382,63 +382,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { $relContactId = $relatedNewContact->id; $this->_newRelatedContacts[$relContactId] = $relContactId; } - - if (1) { - //fix for CRM-1993.Checks for duplicate related contacts - if (1) { - //if more than one duplicate contact - //found, create relationship with first contact - // now create the relationship record - $relationParams = [ - 'relationship_type_id' => $key, - 'contact_check' => [ - $relContactId => 1, - ], - 'is_active' => 1, - 'skipRecentView' => TRUE, - ]; - - // we only handle related contact success, we ignore failures for now - // at some point wold be nice to have related counts as separate - $relationIds = [ - 'contact' => $primaryContactId, - ]; - - [$valid, $duplicate] = self::legacyCreateMultiple($relationParams, $relationIds); - - if ($valid || $duplicate) { - $relationIds['contactTarget'] = $relContactId; - $action = ($duplicate) ? CRM_Core_Action::UPDATE : CRM_Core_Action::ADD; - CRM_Contact_BAO_Relationship::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action); - } - - //handle current employer, CRM-3532 - if ($valid) { - $allRelationships = CRM_Core_PseudoConstant::relationshipType('name'); - $relationshipTypeId = str_replace([ - '_a_b', - '_b_a', - ], [ - '', - '', - ], $key); - $relationshipType = str_replace($relationshipTypeId . '_', '', $key); - $orgId = $individualId = NULL; - if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') { - $orgId = $relContactId; - $individualId = $primaryContactId; - } - elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') { - $orgId = $primaryContactId; - $individualId = $relContactId; - } - if ($orgId && $individualId) { - $currentEmpParams[$individualId] = $orgId; - CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); - } - } - } - } + $this->createRelationship($key, $relContactId, $primaryContactId); } } if ($this->_updateWithId) { @@ -915,6 +859,71 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { return $errorMessage; } + /** + * @param $key + * @param $relContactId + * @param $primaryContactId + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + protected function createRelationship($key, $relContactId, $primaryContactId): void { + //if more than one duplicate contact + //found, create relationship with first contact + // now create the relationship record + $relationParams = [ + 'relationship_type_id' => $key, + 'contact_check' => [ + $relContactId => 1, + ], + 'is_active' => 1, + 'skipRecentView' => TRUE, + ]; + + // we only handle related contact success, we ignore failures for now + // at some point wold be nice to have related counts as separate + $relationIds = [ + 'contact' => $primaryContactId, + ]; + + [ + $valid, + $duplicate + ] = self::legacyCreateMultiple($relationParams, $relationIds); + + if ($valid || $duplicate) { + $relationIds['contactTarget'] = $relContactId; + $action = ($duplicate) ? CRM_Core_Action::UPDATE : CRM_Core_Action::ADD; + CRM_Contact_BAO_Relationship::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action); + } + + //handle current employer, CRM-3532 + if ($valid) { + $allRelationships = CRM_Core_PseudoConstant::relationshipType('name'); + $relationshipTypeId = str_replace([ + '_a_b', + '_b_a', + ], [ + '', + '', + ], $key); + $relationshipType = str_replace($relationshipTypeId . '_', '', $key); + $orgId = $individualId = NULL; + if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') { + $orgId = $relContactId; + $individualId = $primaryContactId; + } + elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') { + $orgId = $primaryContactId; + $individualId = $relContactId; + } + if ($orgId && $individualId) { + $currentEmpParams[$individualId] = $orgId; + CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); + } + } + } + /** * Method for creating contact. * -- 2.25.1