Simplify determination of organizationID
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 7 Jan 2022 00:32:58 +0000 (13:32 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 7 Jan 2022 21:14:34 +0000 (10:14 +1300)
CRM/Contact/BAO/Contact.php
CRM/Contact/BAO/Contact/Utils.php

index 9cbbb6567ba0725bbef0bf87e214c8971a718324..d851d6ebad5bbd3d5f896ce2c2cc9f11b67a5254 100644 (file)
@@ -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) {
index f109f032c5d576bc9f584e8a294616924cc441d9..97f2c50aebd4cb219841d7cab867ea9f4f8384f8 100644 (file)
@@ -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)) {