From e46a19ec3dd0828aa15a8b9497325b9b2878346f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 7 Jan 2022 13:32:58 +1300 Subject: [PATCH] Simplify determination of organizationID --- CRM/Contact/BAO/Contact.php | 2 +- CRM/Contact/BAO/Contact/Utils.php | 23 +++++++---------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 9cbbb6567b..d851d6ebad 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -206,7 +206,7 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact implements Civi\Te $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : $params['current_employer'] ?? NULL; $newContact = empty($params['contact_id']); - if ($newEmployer) { + if (!CRM_Utils_System::isNull($newEmployer)) { CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact); } elseif ($employerId) { diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index f109f032c5..97f2c50aeb 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\Contact; + /** * * @package CRM @@ -245,9 +247,10 @@ WHERE id IN ( $idString ) * Contact id of the individual. * @param $organization * (id or name). - * @param int $previousEmployerID + * @param null $previousEmployerID * @param bool $newContact * + * @throws \API_Exception * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ @@ -260,23 +263,11 @@ WHERE id IN ( $idString ) } if (!is_numeric($organization)) { $dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts(['organization_name' => $organization], 'Organization', 'Unsupervised', [], FALSE); - - if (is_array($dupeIDs) && !empty($dupeIDs)) { - // we should create relationship only w/ first org CRM-4193 - foreach ($dupeIDs as $orgId) { - $organization = $orgId; - break; - } - } - else { - //create new organization - $newOrg = [ + $organization = reset($dupeIDs) ?: Contact::create(FALSE) + ->setValues([ 'contact_type' => 'Organization', 'organization_name' => $organization, - ]; - $org = CRM_Contact_BAO_Contact::create($newOrg); - $organization = $org->id; - } + ])->execute()->first()['id']; } if (is_numeric($organization)) { -- 2.25.1