Merge pull request #22599 from civicrm/5.46
[civicrm-core.git] / CRM / Dedupe / Merger.php
index 92b7e8c6d30c7097451f54c1d377fda5750445f7..48e000f2a8cace9352699647acfec8fbf5c4c5f8 100644 (file)
@@ -683,11 +683,11 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
    * @throws \CiviCRM_API3_Exception
    * @throws \Civi\API\Exception\UnauthorizedException
    */
-  protected static function createContact($contactID, $params) {
+  protected static function updateContact(int $contactID, $params): void {
     // This parameter causes blank fields to be be emptied out.
     // We can probably remove.
     $params['updateBlankLocInfo'] = TRUE;
-    $data = self::formatProfileContactParams($params, [], $contactID);
+    $data = self::formatProfileContactParams($params, $contactID);
     CRM_Contact_BAO_Contact::create($data);
   }
 
@@ -700,51 +700,23 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
    * eliminate a toxic function.
    *
    * @param array $params
-   * @param $fields
    * @param int $contactID
-   * @param int $ufGroupId
-   * @param null $ctype
    *
    * @return array
    */
   private static function formatProfileContactParams(
-    &$params,
-    $fields,
-    $contactID = NULL,
-    $ufGroupId = NULL,
-    $ctype = NULL
+    $params,
+    int $contactID
   ) {
 
     $data = $contactDetails = [];
 
     // get the contact details (hier)
-    if ($contactID) {
-      $details = CRM_Contact_BAO_Contact::getHierContactDetails($contactID, $fields);
+    $details = CRM_Contact_BAO_Contact::getHierContactDetails($contactID, []);
 
-      $contactDetails = $details[$contactID];
-      $data['contact_type'] = $contactDetails['contact_type'] ?? NULL;
-      $data['contact_sub_type'] = $contactDetails['contact_sub_type'] ?? NULL;
-    }
-    else {
-      //we should get contact type only if contact
-      if ($ufGroupId) {
-        $data['contact_type'] = CRM_Core_BAO_UFField::getProfileType($ufGroupId, TRUE, FALSE, TRUE);
-
-        //special case to handle profile with only contact fields
-        if ($data['contact_type'] == 'Contact') {
-          $data['contact_type'] = 'Individual';
-        }
-        elseif (CRM_Contact_BAO_ContactType::isaSubType($data['contact_type'])) {
-          $data['contact_type'] = CRM_Contact_BAO_ContactType::getBasicType($data['contact_type']);
-        }
-      }
-      elseif ($ctype) {
-        $data['contact_type'] = $ctype;
-      }
-      else {
-        $data['contact_type'] = 'Individual';
-      }
-    }
+    $contactDetails = $details[$contactID];
+    $data['contact_type'] = $contactDetails['contact_type'] ?? NULL;
+    $data['contact_sub_type'] = $contactDetails['contact_sub_type'] ?? NULL;
 
     //fix contact sub type CRM-5125
     if (array_key_exists('contact_sub_type', $params) &&
@@ -771,25 +743,12 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
       }
     }
 
-    if ($ctype == 'Organization') {
-      $data['organization_name'] = $contactDetails['organization_name'] ?? NULL;
-    }
-    elseif ($ctype == 'Household') {
-      $data['household_name'] = $contactDetails['household_name'] ?? NULL;
-    }
-
     $locationType = [];
     $count = 1;
 
-    if ($contactID) {
-      //add contact id
-      $data['contact_id'] = $contactID;
-      $primaryLocationType = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactID);
-    }
-    else {
-      $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
-      $defaultLocationId = $defaultLocation->id;
-    }
+    //add contact id
+    $data['contact_id'] = $contactID;
+    $primaryLocationType = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactID);
 
     $billingLocationTypeId = CRM_Core_BAO_LocationType::getBilling();
 
@@ -812,18 +771,13 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
       [$fieldName, $locTypeId, $typeId] = CRM_Utils_System::explode('-', $key, 3);
 
       if ($locTypeId == 'Primary') {
-        if ($contactID) {
-          if (in_array($fieldName, $blocks)) {
-            $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactID, FALSE, $fieldName);
-          }
-          else {
-            $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactID, FALSE, 'address');
-          }
-          $primaryLocationType = $locTypeId;
+        if (in_array($fieldName, $blocks)) {
+          $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactID, FALSE, $fieldName);
         }
         else {
-          $locTypeId = $defaultLocationId;
+          $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactID, FALSE, 'address');
         }
+        $primaryLocationType = $locTypeId;
       }
 
       if (is_numeric($locTypeId) &&
@@ -1045,23 +999,6 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
       $data['contact_type'] = 'Individual';
     }
 
-    //set the values for checkboxes (do_not_email, do_not_mail, do_not_trade, do_not_phone)
-    $privacy = CRM_Core_SelectValues::privacy();
-    foreach ($privacy as $key => $value) {
-      if (array_key_exists($key, $fields)) {
-        // do not reset values for existing contacts, if fields are added to a profile
-        if (array_key_exists($key, $params)) {
-          $data[$key] = $params[$key];
-          if (empty($params[$key])) {
-            $data[$key] = 0;
-          }
-        }
-        elseif (!$contactID) {
-          $data[$key] = 0;
-        }
-      }
-    }
-
     return $data;
   }
 
@@ -1156,7 +1093,7 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
    *
    * @param bool $checkPermissions
    *   Respect logged in user permissions.
-   * @param bool|NULL $reloadCacheIfEmpty
+   * @param bool|null $reloadCacheIfEmpty
    *  If not set explicitly this is calculated but it is preferred that it be set
    *  per comments on isSelected above.
    *
@@ -1934,7 +1871,7 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
       if (!isset($submitted['suffix_id']) && !empty($migrationInfo['main_details']['suffix_id'])) {
         $submitted['suffix_id'] = $migrationInfo['main_details']['suffix_id'];
       }
-      self::createContact($mainId, $submitted);
+      self::updateContact($mainId, $submitted);
     }
     $transaction->commit();
     CRM_Utils_Hook::post('merge', 'Contact', $mainId);
@@ -2688,7 +2625,7 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
   /**
    * Build up the location block for the contact in dedupe-screen display format.
    *
-   * @param integer $cid
+   * @param int $cid
    * @param array $blockInfo
    * @param string $blockName
    *