This commit is contained in:
Yuri Kuznetsov
2022-01-20 13:30:37 +02:00
parent b3c56acd15
commit ab3fc73a18
4 changed files with 144 additions and 79 deletions
@@ -31,6 +31,8 @@ namespace Espo\Modules\Crm\EntryPoints;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\Modules\Crm\Tools\MassEmail\Util as MassEmailUtil;
use Espo\Core\{
Exceptions\NotFound,
Exceptions\BadRequest,
@@ -80,13 +82,16 @@ class SubscribeAgain implements EntryPoint
*/
protected $hasher;
private MassEmailUtil $util;
public function __construct(
EntityManager $entityManager,
ClientManager $clientManager,
HookManager $hookManager,
Config $config,
Metadata $metadata,
Hasher $hasher
Hasher $hasher,
MassEmailUtil $util
) {
$this->entityManager = $entityManager;
$this->clientManager = $clientManager;
@@ -94,6 +99,7 @@ class SubscribeAgain implements EntryPoint
$this->config = $config;
$this->metadata = $metadata;
$this->hasher = $hasher;
$this->util = $util;
}
public function run(Request $request, Response $response): void
@@ -160,46 +166,33 @@ class SubscribeAgain implements EntryPoint
}
}
$link = null;
$link = $this->util->getLinkByEntityType($target->getEntityType());
$m = [
'Account' => 'accounts',
'Contact' => 'contacts',
'Lead' => 'leads',
'User' => 'users',
];
$targetListList = $this->entityManager
->getRDBRepository('MassEmail')
->getRelation($massEmail, 'targetLists')
->find();
if (!empty($m[$target->getEntityType()])) {
$link = $m[$target->getEntityType()];
}
foreach ($targetListList as $targetList) {
$optedInResult = $this->entityManager
->getRDBRepository('TargetList')
->updateRelation($targetList, $link, $target->getId(), ['optedOut' => false]);
if ($link) {
$targetListList = $this->entityManager
->getRDBRepository('MassEmail')
->getRelation($massEmail, 'targetLists')
->find();
if ($optedInResult) {
$hookData = [
'link' => $link,
'targetId' => $targetId,
'targetType' => $targetType,
];
foreach ($targetListList as $targetList) {
$optedInResult = $this->entityManager
->getRDBRepository('TargetList')
->updateRelation($targetList, $link, $target->getId(), ['optedOut' => false]);
if ($optedInResult) {
$hookData = [
'link' => $link,
'targetId' => $targetId,
'targetType' => $targetType,
];
$this->hookManager
->process('TargetList', 'afterCancelOptOut', $targetList, [], $hookData);
}
$this->hookManager
->process('TargetList', 'afterCancelOptOut', $targetList, [], $hookData);
}
$this->hookManager->process($target->getEntityType(), 'afterCancelOptOut', $target, [], []);
$this->display(['queueItemId' => $queueItemId]);
}
$this->hookManager->process($target->getEntityType(), 'afterCancelOptOut', $target, [], []);
$this->display(['queueItemId' => $queueItemId]);
}
}
}
@@ -32,6 +32,8 @@ namespace Espo\Modules\Crm\EntryPoints;
use Espo\Modules\Crm\Services\Campaign as Service;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\Modules\Crm\Tools\MassEmail\Util as MassEmailUtil;
use Espo\Core\{
Exceptions\NotFound,
Exceptions\BadRequest,
@@ -86,6 +88,8 @@ class Unsubscribe implements EntryPoint
*/
protected $service;
private MassEmailUtil $util;
public function __construct(
EntityManager $entityManager,
ClientManager $clientManager,
@@ -93,7 +97,8 @@ class Unsubscribe implements EntryPoint
Config $config,
Metadata $metadata,
Hasher $hasher,
Service $service
Service $service,
MassEmailUtil $util
) {
$this->entityManager = $entityManager;
$this->clientManager = $clientManager;
@@ -102,6 +107,7 @@ class Unsubscribe implements EntryPoint
$this->metadata = $metadata;
$this->hasher = $hasher;
$this->service = $service;
$this->util = $util;
}
public function run(Request $request, Response $response): void
@@ -168,45 +174,32 @@ class Unsubscribe implements EntryPoint
}
}
$link = null;
$link = $this->util->getLinkByEntityType($target->getEntityType());
$m = [
'Account' => 'accounts',
'Contact' => 'contacts',
'Lead' => 'leads',
'User' => 'users',
];
$targetListList = $this->entityManager
->getRDBRepository('MassEmail')
->getRelation($massEmail, 'targetLists')
->find();
if (!empty($m[$target->getEntityType()])) {
$link = $m[$target->getEntityType()];
}
foreach ($targetListList as $targetList) {
$optedOutResult = $this->entityManager
->getRDBRepository('TargetList')
->updateRelation($targetList, $link, $target->getId(), ['optedOut' => true]);
if ($link) {
$targetListList = $this->entityManager
->getRDBRepository('MassEmail')
->getRelation($massEmail, 'targetLists')
->find();
if ($optedOutResult) {
$hookData = [
'link' => $link,
'targetId' => $targetId,
'targetType' => $targetType,
];
foreach ($targetListList as $targetList) {
$optedOutResult = $this->entityManager
->getRDBRepository('TargetList')
->updateRelation($targetList, $link, $target->getId(), ['optedOut' => true]);
if ($optedOutResult) {
$hookData = [
'link' => $link,
'targetId' => $targetId,
'targetType' => $targetType,
];
$this->hookManager->process('TargetList', 'afterOptOut', $targetList, [], $hookData);
}
$this->hookManager->process('TargetList', 'afterOptOut', $targetList, [], $hookData);
}
$this->hookManager->process($target->getEntityType(), 'afterOptOut', $target, [], []);
$this->display(['queueItemId' => $queueItemId]);
}
$this->hookManager->process($target->getEntityType(), 'afterOptOut', $target, [], []);
$this->display(['queueItemId' => $queueItemId]);
}
}
}
@@ -37,22 +37,28 @@ use Espo\Modules\Crm\Entities\MassEmail;
use Espo\Modules\Crm\Entities\EmailQueueItem;
use Espo\Core\Exceptions\Error;
use Espo\Core\ORM\EntityManager;
use Espo\ORM\EntityManager;
use Espo\Core\Utils\Metadata;
class Queue
{
protected $targetsLinkList = [
'accounts',
'contacts',
'leads',
'users',
];
/**
* @var string[]
*/
protected array $targetLinkList;
protected EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
private Metadata $metadata;
public function __construct(EntityManager $entityManager, Metadata $metadata)
{
$this->entityManager = $entityManager;
$this->metadata = $metadata;
$this->targetLinkList = $this->metadata->get(['scopes', 'TargetList', 'targetLinkList']) ?? [];
}
protected function cleanupQueueItems(MassEmail $massEmail): void
@@ -96,7 +102,7 @@ class Queue
->find();
foreach ($excludingTargetListList as $excludingTargetList) {
foreach ($this->targetsLinkList as $link) {
foreach ($this->targetLinkList as $link) {
$excludingList = $em->getRDBRepository(TargetList::ENTITY_TYPE)
->findRelated(
$excludingTargetList,
@@ -127,7 +133,7 @@ class Queue
->find();
foreach ($targetListCollection as $targetList) {
foreach ($this->targetsLinkList as $link) {
foreach ($this->targetLinkList as $link) {
$recordList = $em->getRDBRepository('TargetList')
->getRelation($targetList, $link)
->select(['id', 'emailAddress'])
@@ -0,0 +1,73 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Modules\Crm\Tools\MassEmail;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Defs;
use Espo\Core\Exceptions\Error;
use Espo\Modules\Crm\Entities\TargetList;
class Util
{
private Defs $ormDefs;
private Metadata $metadata;
/**
* @var string[]
*/
private array $targetLinkList;
public function __construct(Defs $ormDefs, Metadata $metadata)
{
$this->ormDefs = $ormDefs;
$this->metadata = $metadata;
$this->targetLinkList = $this->metadata->get(['scopes', 'TargetList', 'targetLinkList']) ?? [];
}
public function getLinkByEntityType(string $entityType): string
{
foreach ($this->targetLinkList as $link) {
$itemEntityType = $this->ormDefs
->getEntity(TargetList::ENTITY_TYPE)
->getRelation($link)
->getForeignEntityType();
if ($itemEntityType === $entityType) {
return $link;
}
}
throw new Error();
}
}