X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FDedupe%2FMergeHandler.php;h=cb0d64ebacdc9c8dbe4d5e615a590e23aa663be9;hb=78bf34eadc635e079b4b223df7ebd95d279ea8e0;hp=654e87c20930b6cd6aed0960a4e92fea3417244c;hpb=95c42d8be0834773e0b606a1c79471f4733060ab;p=civicrm-core.git diff --git a/CRM/Dedupe/MergeHandler.php b/CRM/Dedupe/MergeHandler.php index 654e87c209..cb0d64ebac 100644 --- a/CRM/Dedupe/MergeHandler.php +++ b/CRM/Dedupe/MergeHandler.php @@ -213,6 +213,79 @@ class CRM_Dedupe_MergeHandler { return $otherBlockDAO; } + /** + * Get blocks, if any, to update for the deleted contact. + * + * If the deleted contact no longer has a primary address but still has + * one or more blocks we want to ensure the remaining block is updated + * to have is_primary = 1 in case the contact is ever undeleted. + * + * @param string $entity + * + * @return array + * @throws \CRM_Core_Exception + */ + public function getBlocksToUpdateForDeletedContact($entity) { + $movedBlocks = $this->getLocationBlocksToMerge()[$entity]; + $deletedContactsBlocks = $this->getLocationBlocksForContactToRemove()[$entity]; + $unMovedBlocks = array_values(array_diff_key($deletedContactsBlocks, $movedBlocks)); + if (empty($unMovedBlocks) || empty($movedBlocks)) { + return []; + } + foreach (array_keys($movedBlocks) as $index) { + if ($deletedContactsBlocks[$index]['is_primary']) { + // We have moved the primary - change any other block to be primary. + $newPrimaryBlock = $this->getDAOForLocationEntity($entity); + $newPrimaryBlock->id = $unMovedBlocks[0]['id']; + $newPrimaryBlock->is_primary = 1; + return [$newPrimaryBlock->id => $newPrimaryBlock]; + } + } + return []; + } + + /** + * Get the details of the blocks to be transferred over for the given entity. + * + * @param string $entity + * + * @return array + */ + protected function getLocationBlocksToMoveForEntity($entity) { + $movedBlocks = $this->getLocationBlocksToMerge()[$entity]; + $blockDetails = $this->getLocationBlocksForContactToRemove()[$entity]; + return array_intersect_key($blockDetails, $movedBlocks); + } + + /** + * Does the contact to keep have location blocks for the given entity. + * + * @param string $entity + * + * @return bool + */ + public function contactToKeepHasLocationBlocksForEntity($entity) { + return !empty($this->getLocationBlocksForContactToKeep()[$entity]); + } + + /** + * Get the location blocks for the contact to be kept. + * + * @return array + */ + public function getLocationBlocksForContactToKeep() { + return $this->getMigrationInfo()['main_details']['location_blocks']; + } + + /** + * Get the location blocks for the contact to be deleted. + * + * @return array + */ + public function getLocationBlocksForContactToRemove() { + return $this->getMigrationInfo()['other_details']['location_blocks']; + } + /** * Get the DAO object appropriate to the location entity. *