* 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;
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).
$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.
// 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);
}
$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]);
}
}
}