Skip to content

Commit

Permalink
Alter dedupe code to call api rather than bao->save()
Browse files Browse the repository at this point in the history
This achieves 2 things
1) hooks are no longer  bypassed
2) it leverages core handling for is_primary rather than this
somewhat unreliable attempt
  • Loading branch information
eileenmcnaughton committed Apr 12, 2021
1 parent de02266 commit de7b2b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions CRM/Dedupe/MergeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ protected function getSelectedType($entity, $blockIndex) {
* 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 {
Expand Down Expand Up @@ -410,12 +411,6 @@ public function mergeLocations(): void {
$set_primary = $migrationInfo['location_blocks'][$name][$blkCount]['set_other_primary'] ?? NULL;
if (!$changePrimary && $set_primary == "1") {
$otherBlockDAO->is_primary = 1;
if ($primaryDAOId) {
$removePrimaryDAO = $this->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.
Expand Down Expand Up @@ -454,12 +449,20 @@ public function mergeLocations(): void {
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]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4040,7 +4040,7 @@ public function testMerge() {
*
* @throws \CRM_Core_Exception
*/
public function testMergeWithBlankLocationData($isReverse) {
public function testMergeWithBlankLocationData($isReverse): void {
$this->createLoggedInUser();
$this->ids['contact'][0] = $this->callAPISuccess('contact', 'create', $this->_params)['id'];
$this->ids['contact'][1] = $this->callAPISuccess('contact', 'create', $this->_params)['id'];
Expand Down

0 comments on commit de7b2b2

Please sign in to comment.