Skip to content

Commit

Permalink
Migrate the second hook
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Feb 6, 2025
1 parent 0c47760 commit b4e1979
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
16 changes: 0 additions & 16 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3498,22 +3498,6 @@ public static function findDuplicates(array $dedupeParams, array $contextParams
];
$dedupeParams += $dedupeParams['match_params'];
CRM_Utils_Hook::findDuplicates($dedupeParams, $dedupeResults, $contextParams);
if (!$dedupeResults['handled']) {
$rgBao = new CRM_Dedupe_BAO_DedupeRuleGroup();
if (!$rgBao->fillTable($dedupeParams['rule_group_id'], [], $dedupeParams['match_params'])) {
return [];
}

$dao = CRM_Core_DAO::executeQuery($rgBao->thresholdQuery($checkPermission));
$dupes = [];
while ($dao->fetch()) {
if (isset($dao->id) && $dao->id) {
$dupes[] = $dao->id;
}
}
CRM_Core_DAO::executeQuery($rgBao->tableDropQuery());
$dedupeResults['ids'] = array_diff($dupes, $dedupeParams['excluded_contact_ids']);
}
return $dedupeResults['ids'] ?? [];
}

Expand Down
37 changes: 37 additions & 0 deletions CRM/Dedupe/BAO/DedupeRuleGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CRM_Dedupe_BAO_DedupeRuleGroup extends CRM_Dedupe_DAO_DedupeRuleGroup impl
public static function getSubscribedEvents(): array {
return [
'hook_civicrm_findExistingDuplicates' => ['hook_civicrm_findExistingDuplicates', -200],
'hook_civicrm_findDuplicates' => ['hook_civicrm_findDuplicates', -200],
];
}

Expand Down Expand Up @@ -163,6 +164,42 @@ public static function hook_civicrm_findExistingDuplicates(GenericHookEvent $eve
\CRM_Core_DAO::executeQuery($ruleGroup->tableDropQuery());
}

public static function hook_civicrm_findDuplicates(GenericHookEvent $event): void {
if (!empty($event->dedupeResults['handled'])) {
// @todo - in time we can deprecate this & expect them to use stopPropagation().
return;
}
$rgBao = new CRM_Dedupe_BAO_DedupeRuleGroup();
if (!$rgBao->fillTable($event->dedupeParams['rule_group_id'], [], $event->dedupeParams['match_params'], FALSE)) {
$event->dedupeResults['ids'] = [];
return;
}
$aclFrom = '';
$aclWhere = '';
$dedupeTable = $rgBao->temporaryTables['dedupe'];
$contactType = $rgBao->contact_type;
$threshold = $rgBao->threshold;
if ($event->dedupeParams['check_permission']) {
[$aclFrom, $aclWhere] = CRM_Contact_BAO_Contact_Permission::cacheClause('civicrm_contact');
$aclWhere = $aclWhere ? "AND {$aclWhere}" : '';
}
$query = "SELECT dedupe.id1 as id
FROM $dedupeTable dedupe JOIN civicrm_contact
ON dedupe.id1 = civicrm_contact.id {$aclFrom}
WHERE contact_type = '{$contactType}' AND is_deleted = 0 $aclWhere
AND weight >= {$threshold}";

$dao = CRM_Core_DAO::executeQuery($query);
$dupes = [];
while ($dao->fetch()) {
if (isset($dao->id) && $dao->id) {
$dupes[] = $dao->id;
}
}
CRM_Core_DAO::executeQuery($rgBao->tableDropQuery());
$event->dedupeResults['ids'] = array_diff($dupes, $event->dedupeParams['excluded_contact_ids']);
}

/**
* Fill the dedupe finder table.
*
Expand Down
25 changes: 25 additions & 0 deletions ext/legacydedupefinder/Civi/LegacyFinder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Finder extends AutoSubscriber {
public static function getSubscribedEvents(): array {
return [
'hook_civicrm_findExistingDuplicates' => ['findExistingDuplicates', -150],
'hook_civicrm_findDuplicates' => ['findDuplicates', -150],
];
}

Expand Down Expand Up @@ -39,4 +40,28 @@ public static function findExistingDuplicates(GenericHookEvent $event): void {
\CRM_Core_DAO::executeQuery($ruleGroup->tableDropQuery());
}

public static function findDuplicates(GenericHookEvent $event): void {
$event->stopPropagation();

if (!empty($event->dedupeResults['handled'])) {
// @todo - in time we can deprecate this & expect them to use stopPropagation().
return;
}
$rgBao = new \CRM_Dedupe_BAO_DedupeRuleGroup();
if (!$rgBao->fillTable($event->dedupeParams['rule_group_id'], [], $event->dedupeParams['match_params'], TRUE)) {
$event->dedupeResults['ids'] = [];
return;
}

$dao = \CRM_Core_DAO::executeQuery($rgBao->thresholdQuery($event->dedupeParams['check_permission']));
$dupes = [];
while ($dao->fetch()) {
if (isset($dao->id) && $dao->id) {
$dupes[] = $dao->id;
}
}
\CRM_Core_DAO::executeQuery($rgBao->tableDropQuery());
$event->dedupeResults['ids'] = array_diff($dupes, $event->dedupeParams['excluded_contact_ids']);
}

}
15 changes: 15 additions & 0 deletions ext/legacydedupefinder/tests/phpunit/Civi/LegacyFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,19 @@ public function testFinderMergeInGroup(): void {
$this->assertArrayNotHasKey($this->ids['Contact']['mary_2'], $contacts);
}

public function testFindDuplicate(): void {
$this->createTestEntity('Contact', [
'email_primary.email' => '[email protected]',
'first_name' => 'Bob',
]);
$matches = Contact::getDuplicates(FALSE)
->setDedupeRule('Individual.Unsupervised')
->setValues([
'email_primary.email' => '[email protected]',
'first_name' => 'Bob',
])
->execute();
$this->assertCount(1, $matches);
}

}

0 comments on commit b4e1979

Please sign in to comment.