diff --git a/Config/config.php b/Config/config.php index 0894eedc6..cfd0aecb3 100644 --- a/Config/config.php +++ b/Config/config.php @@ -757,6 +757,7 @@ 'custom_item.route.provider', 'mautic.custom.model.item', 'custom_object.config.provider', + 'custom_item.xref.contact.repository', ], ], 'custom_item.post_save.subscriber' => [ diff --git a/EventListener/ContactSubscriber.php b/EventListener/ContactSubscriber.php index 16e27da98..3a0bf8a3b 100644 --- a/EventListener/ContactSubscriber.php +++ b/EventListener/ContactSubscriber.php @@ -7,12 +7,14 @@ use Doctrine\ORM\EntityManager; use Mautic\LeadBundle\Entity\LeadEventLog; use Mautic\LeadBundle\Entity\LeadEventLogRepository; +use Mautic\LeadBundle\Event\LeadMergeEvent; use Mautic\LeadBundle\Event\LeadTimelineEvent; use Mautic\LeadBundle\LeadEvents; use MauticPlugin\CustomObjectsBundle\Exception\NotFoundException; use MauticPlugin\CustomObjectsBundle\Model\CustomItemModel; use MauticPlugin\CustomObjectsBundle\Provider\ConfigProvider; use MauticPlugin\CustomObjectsBundle\Provider\CustomItemRouteProvider; +use MauticPlugin\CustomObjectsBundle\Repository\CustomItemXrefContactRepository; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Translation\TranslatorInterface; @@ -43,18 +45,25 @@ class ContactSubscriber implements EventSubscriberInterface */ private $configProvider; + /** + * @var CustomItemXrefContactRepository + */ + private $customItemXrefContactRepository; + public function __construct( EntityManager $entityManager, TranslatorInterface $translator, CustomItemRouteProvider $routeProvider, CustomItemModel $customItemModel, - ConfigProvider $configProvider + ConfigProvider $configProvider, + CustomItemXrefContactRepository $customItemXrefContactRepository, ) { $this->entityManager = $entityManager; $this->translator = $translator; $this->routeProvider = $routeProvider; $this->customItemModel = $customItemModel; $this->configProvider = $configProvider; + $this->customItemXrefContactRepository = $customItemXrefContactRepository; } /** @@ -64,6 +73,7 @@ public static function getSubscribedEvents(): array { return [ LeadEvents::TIMELINE_ON_GENERATE => 'onTimelineGenerate', + LeadEvents::LEAD_POST_MERGE => 'onContactMerge', ]; } @@ -99,6 +109,15 @@ public function onTimelineGenerate(LeadTimelineEvent $event): void } } + public function onContactMerge(LeadMergeEvent $event): void + { + if (!$this->configProvider->pluginIsEnabled()) { + return; + } + + $this->customItemXrefContactRepository->mergeLead($event->getVictor(), $event->getLoser()); + } + /** * @return mixed[] */ diff --git a/Repository/CustomItemXrefContactRepository.php b/Repository/CustomItemXrefContactRepository.php index 8297ed6c8..86be4732d 100644 --- a/Repository/CustomItemXrefContactRepository.php +++ b/Repository/CustomItemXrefContactRepository.php @@ -63,4 +63,32 @@ public function getContactIdsLinkedToCustomItem(int $customItemId, int $limit, i ->getQuery() ->getResult(); } + + public function mergeLead(Lead $victor, Lead $loser): void + { + // Move all custom item references to the victor lead, but only if the victor doesn't already have + // a reference to the custom item + $existingAlias = CustomItemXrefContact::TABLE_ALIAS.'_Check'; + + $this->createQueryBuilder(CustomItemXrefContact::TABLE_ALIAS) + ->update() + ->set(CustomItemXrefContact::TABLE_ALIAS.'.contact', ':victor') + ->where(CustomItemXrefContact::TABLE_ALIAS.'.contact = :loser') + ->andWhere( + $this->createQueryBuilder(CustomItemXrefContact::TABLE_ALIAS) + ->expr() + ->notIn( + CustomItemXrefContact::TABLE_ALIAS.'.customItem', + $this->createQueryBuilder($existingAlias) + ->select('IDENTITY('.$existingAlias.'.customItem)') + ->where($existingAlias.'.contact = :victor') + ->getDQL() + ) + ) + ->setParameter('victor', $victor->getId()) + ->setParameter('loser', $loser->getId()) + ->getQuery() + ->execute(); + + } }