Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] refactor on nasty Dedupe function #15830

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions CRM/Dedupe/MergeHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2020 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2020
*
* This class exists primarily for the purposes of supporting code clean up in the Merger class.
*
* It is expected to be fast-moving and calling it outside the refactoring work is not advised.
*/
class CRM_Dedupe_MergeHandler {

/**
* ID of contact to be kept.
*
* @var int
*/
protected $toKeepID;

/**
* ID of contact to be merged and deleted.
*
* @var int
*/
protected $toRemoveID;

/**
* @return mixed
*/
public function getToKeepID() {
return $this->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;
}

}
12 changes: 2 additions & 10 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1315,17 +1315,9 @@ public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissio
}
}

$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;

Expand Down