Skip to content

Commit

Permalink
Determine name of DAO when needed rather than passing a variable around
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Sep 22, 2020
1 parent a427fad commit 99d57de
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
40 changes: 36 additions & 4 deletions CRM/Dedupe/MergeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,19 @@ public function getLocationBlocksToMerge(): array {
* This is intended as a refactoring step - not the long term function. Do not
* call from any function other than the one it is taken from (Merger::mergeLocations).
*
* @param string $daoName
* @param int $otherBlockId
* @param string $name
* @param int $blkCount
*
* @return mixed
* @return CRM_Core_DAO_Address|CRM_Core_DAO_Email|CRM_Core_DAO_IM|CRM_Core_DAO_Phone|CRM_Core_DAO_Website
*
* @throws \CRM_Core_Exception
*/
public function copyDataToNewBlockDAO(string $daoName, $otherBlockId, $name, $blkCount) {
public function copyDataToNewBlockDAO($otherBlockId, $name, $blkCount) {
$locationBlocks = CRM_Dedupe_Merger::getLocationBlockInfo();
$migrationInfo = $this->getMigrationInfo();
// For the block which belongs to other-contact, link the location block to main-contact
$otherBlockDAO = new $daoName();
$otherBlockDAO = $this->getDAOForLocationEntity($name);
$otherBlockDAO->contact_id = $this->getToKeepID();

// Get the ID of this block on the 'other' contact, otherwise skip
Expand All @@ -225,4 +226,35 @@ public function copyDataToNewBlockDAO(string $daoName, $otherBlockId, $name, $bl
return $otherBlockDAO;
}

/**
* Get the DAO object appropriate to the location entity.
*
* @param string $entity
*
* @return CRM_Core_DAO_Address|CRM_Core_DAO_Email|CRM_Core_DAO_IM|CRM_Core_DAO_Phone|CRM_Core_DAO_Website
* @throws \CRM_Core_Exception
*/
public function getDAOForLocationEntity($entity) {
switch ($entity) {
case 'email':
return new CRM_Core_DAO_Email();

case 'address':
return new CRM_Core_DAO_Address();

case 'phone':
return new CRM_Core_DAO_Phone();

case 'website':
return new CRM_Core_DAO_Website();

case 'im':
return new CRM_Core_DAO_IM();

default:
// Mostly here, along with the switch over a more concise format, to help IDEs understand the possibilities.
throw new CRM_Core_Exception('Unsupported entity');
}
}

}
6 changes: 3 additions & 3 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ public static function mergeLocations($mergeHandler) {
if (!$otherBlockId) {
continue;
}
$otherBlockDAO = $mergeHandler->copyDataToNewBlockDAO($daoName, $otherBlockId, $name, $blkCount);
$otherBlockDAO = $mergeHandler->copyDataToNewBlockDAO($otherBlockId, $name, $blkCount);

// If we're deliberately setting this as primary then add the flag
// and remove it from the current primary location (if there is one).
Expand All @@ -1829,7 +1829,7 @@ public static function mergeLocations($mergeHandler) {
if (!$changePrimary && $set_primary == "1") {
$otherBlockDAO->is_primary = 1;
if ($primaryDAOId) {
$removePrimaryDAO = new $daoName();
$removePrimaryDAO = $mergeHandler->getDAOForLocationEntity($name);
$removePrimaryDAO->id = $primaryDAOId;
$removePrimaryDAO->is_primary = 0;
$blocksDAO[$name]['update'][$primaryDAOId] = $removePrimaryDAO;
Expand All @@ -1848,7 +1848,7 @@ public static function mergeLocations($mergeHandler) {

// overwrite - need to delete block which belongs to main-contact.
if (!empty($mainBlockId) && $values['is_replace']) {
$deleteDAO = new $daoName();
$deleteDAO = $mergeHandler->getDAOForLocationEntity($name);
$deleteDAO->id = $mainBlockId;
$deleteDAO->find(TRUE);

Expand Down

0 comments on commit 99d57de

Please sign in to comment.