From 9287a0b77da87d64f59a2dad35aa38992883a07e Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 12 Nov 2019 15:24:34 +1300 Subject: [PATCH] [REF] refactor on nasty Dedupe function I found with other pieces of refactoring static functions that creating a class to support the refactor made it much easier as that way I could leverage the fact thatt classes have properties and get away from the crazy param passing that characterizes a nest of static functions. This adds a class for that purpose and moves a small chunk of code handling into the class. The goal is to move the handling is done purely for the form back onto the form.... --- CRM/Dedupe/MergeHandler.php | 113 ++++++++++++++++++++++++++++++++++++ CRM/Dedupe/Merger.php | 12 +--- 2 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 CRM/Dedupe/MergeHandler.php diff --git a/CRM/Dedupe/MergeHandler.php b/CRM/Dedupe/MergeHandler.php new file mode 100644 index 0000000000..b28c006300 --- /dev/null +++ b/CRM/Dedupe/MergeHandler.php @@ -0,0 +1,113 @@ +toKeepID; + } + + /** + * @param mixed $toKeepID + */ + public function setToKeepID($toKeepID) { + $this->toKeepID = $toKeepID; + } + + /** + * @return mixed + */ + public function getToRemoveID() { + return $this->toRemoveID; + } + + /** + * @param mixed $toRemoveID + */ + public function setToRemoveID($toRemoveID) { + $this->toRemoveID = $toRemoveID; + } + + /** + * CRM_Dedupe_MergeHandler constructor. + * + * @param int $toKeepID + * ID of contact to be kept. + * @param int $toRemoveID + * ID of contact to be removed. + */ + public function __construct(int $toKeepID, int $toRemoveID) { + $this->setToKeepID($toKeepID); + $this->setToRemoveID($toRemoveID); + } + + /** + * Get an array of tables that relate to the contact entity and will need consideration in a merge. + * + * The list of potential tables is filtered by tables which have data for the relevant contacts. + */ + public function getTablesRelatedToTheMergePair() { + $relTables = CRM_Dedupe_Merger::relTables(); + $activeRelTables = CRM_Dedupe_Merger::getActiveRelTables($this->toRemoveID); + $activeMainRelTables = CRM_Dedupe_Merger::getActiveRelTables($this->toKeepID); + foreach ($relTables as $name => $null) { + if (!in_array($name, $activeRelTables, TRUE) && + !(($name === 'rel_table_users') && in_array($name, $activeMainRelTables, TRUE)) + ) { + unset($relTables[$name]); + } + } + return $relTables; + } + +} diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 9b65f22017..20ba01c383 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -1315,17 +1315,9 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m } } - $relTables = CRM_Dedupe_Merger::relTables(); - $activeRelTables = CRM_Dedupe_Merger::getActiveRelTables($otherId); - $activeMainRelTables = CRM_Dedupe_Merger::getActiveRelTables($mainId); + $mergeHandler = new CRM_Dedupe_MergeHandler((int) $mainId, (int) $otherId); + $relTables = $mergeHandler->getTablesRelatedToTheMergePair(); foreach ($relTables as $name => $null) { - if (!in_array($name, $activeRelTables) && - !(($name == 'rel_table_users') && in_array($name, $activeMainRelTables)) - ) { - unset($relTables[$name]); - continue; - } - $relTableElements[] = ['checkbox', "move_$name"]; $migrationInfo["move_$name"] = 1; -- 2.25.1