Skip to content

Commit

Permalink
[REF] refactor on nasty Dedupe function
Browse files Browse the repository at this point in the history
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....
  • Loading branch information
eileenmcnaughton committed Nov 12, 2019
1 parent 33c3ad9 commit 9287a0b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 10 deletions.
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

0 comments on commit 9287a0b

Please sign in to comment.