Merge pull request #23226 from MegaphoneJon/individual-employers
[civicrm-core.git] / CRM / Dedupe / MergeHandler.php
index 7d8ad7ca2f6cfa40a99b20d1a33ce8f6a7ae2762..b6a58b886a70e0fb1ec5883e832819fb0ede7e5a 100644 (file)
@@ -377,24 +377,22 @@ class CRM_Dedupe_MergeHandler {
    * The use of the new hook is tested, including the fact it is called before contributions are merged, as this
    * is likely to be significant data in merge hooks.
    *
+   * @throws \API_Exception
    * @throws \CRM_Core_Exception
    */
   public function mergeLocations(): void {
-    $mergeHandler = $this;
-    $locBlocks = $mergeHandler->getLocationBlocksToMerge();
+    $locBlocks = $this->getLocationBlocksToMerge();
     $blocksDAO = [];
-    $migrationInfo = $mergeHandler->getMigrationInfo();
+    $migrationInfo = $this->getMigrationInfo();
 
     // @todo Handle OpenID (not currently in API).
     if (!empty($locBlocks)) {
-      $locationBlocks = CRM_Dedupe_Merger::getLocationBlockInfo();
 
-      $primaryBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($mergeHandler->getToKeepID(), ['is_primary' => 1]);
-      $billingBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($mergeHandler->getToKeepID(), ['is_billing' => 1]);
+      $primaryBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($this->getToKeepID(), ['is_primary' => 1]);
+      $billingBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($this->getToKeepID(), ['is_billing' => 1]);
 
       foreach ($locBlocks as $name => $block) {
         $blocksDAO[$name] = ['delete' => [], 'update' => []];
-        $daoName = 'CRM_Core_DAO_' . $locationBlocks[$name]['label'];
         $changePrimary = FALSE;
         $primaryDAOId = (array_key_exists($name, $primaryBlockIds)) ? array_pop($primaryBlockIds[$name]) : NULL;
         $billingDAOId = (array_key_exists($name, $billingBlockIds)) ? array_pop($billingBlockIds[$name]) : NULL;
@@ -405,7 +403,7 @@ class CRM_Dedupe_MergeHandler {
           if (!$otherBlockId) {
             continue;
           }
-          $otherBlockDAO = $mergeHandler->copyDataToNewBlockDAO($otherBlockId, $name, $blkCount);
+          $otherBlockDAO = $this->copyDataToNewBlockDAO($otherBlockId, $name, $blkCount);
 
           // If we're deliberately setting this as primary then add the flag
           // and remove it from the current primary location (if there is one).
@@ -413,12 +411,6 @@ class CRM_Dedupe_MergeHandler {
           $set_primary = $migrationInfo['location_blocks'][$name][$blkCount]['set_other_primary'] ?? NULL;
           if (!$changePrimary && $set_primary == "1") {
             $otherBlockDAO->is_primary = 1;
-            if ($primaryDAOId) {
-              $removePrimaryDAO = $mergeHandler->getDAOForLocationEntity($name);
-              $removePrimaryDAO->id = $primaryDAOId;
-              $removePrimaryDAO->is_primary = 0;
-              $blocksDAO[$name]['update'][$primaryDAOId] = $removePrimaryDAO;
-            }
             $changePrimary = TRUE;
           }
           // Otherwise, if main contact already has primary, set it to 0.
@@ -433,7 +425,7 @@ class CRM_Dedupe_MergeHandler {
 
           // overwrite - need to delete block which belongs to main-contact.
           if (!empty($mainBlockId) && $values['is_replace']) {
-            $deleteDAO = $mergeHandler->getDAOForLocationEntity($name);
+            $deleteDAO = $this->getDAOForLocationEntity($name);
             $deleteDAO->id = $mainBlockId;
             $deleteDAO->find(TRUE);
 
@@ -449,20 +441,28 @@ class CRM_Dedupe_MergeHandler {
           }
           $blocksDAO[$name]['update'][$otherBlockDAO->id] = $otherBlockDAO;
         }
-        $blocksDAO[$name]['update'] += $mergeHandler->getBlocksToUpdateForDeletedContact($name);
+        $blocksDAO[$name]['update'] += $this->getBlocksToUpdateForDeletedContact($name);
       }
     }
 
-    CRM_Utils_Hook::alterLocationMergeData($blocksDAO, $mergeHandler->getToKeepID(), $mergeHandler->getToRemoveID(), $migrationInfo);
+    CRM_Utils_Hook::alterLocationMergeData($blocksDAO, $this->getToKeepID(), $this->getToRemoveID(), $migrationInfo);
     foreach ($blocksDAO as $blockDAOs) {
       if (!empty($blockDAOs['update'])) {
         foreach ($blockDAOs['update'] as $blockDAO) {
-          $blockDAO->save();
+          $entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($blockDAO));
+          $values = ['checkPermissions' => FALSE];
+          foreach ($blockDAO->fields() as $field) {
+            if (isset($blockDAO->{$field['name']})) {
+              $values['values'][$field['name']] = $blockDAO->{$field['name']};
+            }
+          }
+          civicrm_api4($entity, 'update', $values);
         }
       }
       if (!empty($blockDAOs['delete'])) {
         foreach ($blockDAOs['delete'] as $blockDAO) {
-          $blockDAO->delete();
+          $entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($blockDAO));
+          civicrm_api4($entity, 'delete', ['where' => [['id', '=', $blockDAO->id]], 'checkPermissions' => FALSE]);
         }
       }
     }