ref
This commit is contained in:
@@ -29,15 +29,23 @@
|
||||
|
||||
namespace Espo\Modules\Crm\Controllers;
|
||||
|
||||
use Espo\Core\Acl\Table;
|
||||
use Espo\Core\Controllers\Record;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\Api\Request;
|
||||
use Espo\Modules\Crm\Services\Campaign as Service;
|
||||
|
||||
use Espo\Modules\Crm\Entities\Campaign as CampaignEntity;
|
||||
use Espo\Modules\Crm\Tools\Campaign\MailMergeService;
|
||||
use stdClass;
|
||||
|
||||
class Campaign extends \Espo\Core\Controllers\Record
|
||||
class Campaign extends Record
|
||||
{
|
||||
/**
|
||||
* @throws BadRequest
|
||||
* @throws Forbidden
|
||||
* @throws Error
|
||||
*/
|
||||
public function postActionGenerateMailMergePdf(Request $request): stdClass
|
||||
{
|
||||
$data = $request->getParsedBody();
|
||||
@@ -50,18 +58,16 @@ class Campaign extends \Espo\Core\Controllers\Record
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if (!$this->getAcl()->checkScope('Campaign', 'read')) {
|
||||
if (!$this->acl->checkScope(CampaignEntity::ENTITY_TYPE, Table::ACTION_READ)) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
$attachmentId = $this->injectableFactory
|
||||
->create(MailMergeService::class)
|
||||
->generate($data->campaignId, $data->link);
|
||||
|
||||
return (object) [
|
||||
'id' => $this->getCampaignService()->generateMailMergePdf($data->campaignId, $data->link, true)
|
||||
'id' => $attachmentId,
|
||||
];
|
||||
}
|
||||
|
||||
private function getCampaignService(): Service
|
||||
{
|
||||
/** @var Service */
|
||||
return $this->getRecordService();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,11 +32,25 @@ namespace Espo\Modules\Crm\Entities;
|
||||
use Espo\Core\Field\Date;
|
||||
use Espo\Core\Field\Link;
|
||||
use Espo\Core\Field\LinkMultiple;
|
||||
use Espo\Core\ORM\Entity;
|
||||
|
||||
class Campaign extends \Espo\Core\ORM\Entity
|
||||
class Campaign extends Entity
|
||||
{
|
||||
public const ENTITY_TYPE = 'Campaign';
|
||||
|
||||
public const TYPE_EMAIL = 'Email';
|
||||
public const TYPE_MAIL = 'Mail';
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->get('name');
|
||||
}
|
||||
|
||||
public function getType(): ?string
|
||||
{
|
||||
return $this->get('type');
|
||||
}
|
||||
|
||||
public function getStartDate(): ?Date
|
||||
{
|
||||
/** @var ?Date */
|
||||
|
||||
@@ -29,20 +29,8 @@
|
||||
|
||||
namespace Espo\Modules\Crm\Services;
|
||||
|
||||
use Espo\Entities\Template;
|
||||
use Espo\Modules\Crm\Tools\Campaign\MailMergeGenerator;
|
||||
|
||||
use Espo\Modules\Crm\Entities\Campaign as CampaignEntity;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
|
||||
use Espo\ORM\EntityCollection;
|
||||
use Espo\Services\Record;
|
||||
|
||||
use Espo\Core\Di;
|
||||
|
||||
/**
|
||||
@@ -54,25 +42,6 @@ class Campaign extends Record implements
|
||||
{
|
||||
use Di\DefaultLanguageSetter;
|
||||
|
||||
/**
|
||||
* @var array<string,string[]>
|
||||
*/
|
||||
protected $entityTypeAddressFieldListMap = [
|
||||
'Account' => ['billingAddress', 'shippingAddress'],
|
||||
'Contact' => ['address'],
|
||||
'Lead' => ['address'],
|
||||
'User' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $targetLinkList = [
|
||||
'accounts',
|
||||
'contacts',
|
||||
'leads',
|
||||
'users',
|
||||
];
|
||||
|
||||
public function logLeadCreated(
|
||||
string $campaignId,
|
||||
@@ -376,157 +345,5 @@ class Campaign extends Record implements
|
||||
$this->entityManager->saveEntity($logRecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequest
|
||||
* @throws Error
|
||||
* @throws Forbidden
|
||||
*/
|
||||
public function generateMailMergePdf(string $campaignId, string $link, bool $checkAcl = false): string
|
||||
{
|
||||
/** @var CampaignEntity $campaign */
|
||||
$campaign = $this->entityManager->getEntity('Campaign', $campaignId);
|
||||
|
||||
if ($checkAcl && !$this->acl->check($campaign, 'read')) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
/** @var string $targetEntityType */
|
||||
$targetEntityType = $campaign->getRelationParam($link, 'entity');
|
||||
|
||||
if ($checkAcl) {
|
||||
if (!$this->acl->check($targetEntityType, 'read')) {
|
||||
throw new Forbidden("Could not mail merge campaign because access to target entity type is forbidden.");
|
||||
}
|
||||
}
|
||||
|
||||
if (!in_array($link, $this->targetLinkList)) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if ($campaign->get('type') !== 'Mail') {
|
||||
throw new Error("Could not mail merge campaign not of Mail type.");
|
||||
}
|
||||
|
||||
if (
|
||||
!$campaign->get($link . 'TemplateId')
|
||||
) {
|
||||
throw new Error("Could not mail merge campaign w/o specified template.");
|
||||
}
|
||||
|
||||
/** @var ?Template $template */
|
||||
$template = $this->entityManager->getEntity(Template::ENTITY_TYPE, $campaign->get($link . 'TemplateId'));
|
||||
|
||||
if (!$template) {
|
||||
throw new Error("Template not found.");
|
||||
}
|
||||
|
||||
if ($template->get('entityType') !== $targetEntityType) {
|
||||
throw new Error("Template is not of proper entity type.");
|
||||
}
|
||||
|
||||
$campaign->loadLinkMultipleField('targetLists');
|
||||
$campaign->loadLinkMultipleField('excludingTargetLists');
|
||||
|
||||
if (count($campaign->getLinkMultipleIdList('targetLists') ?? []) === 0) {
|
||||
throw new Error("Could not mail merge campaign w/o any specified target list.");
|
||||
}
|
||||
|
||||
$metTargetHash = [];
|
||||
$targetEntityList = [];
|
||||
|
||||
/** @var iterable<\Espo\Modules\Crm\Entities\TargetList> $excludingTargetListList */
|
||||
$excludingTargetListList = $this->entityManager
|
||||
->getRDBRepository('Campaign')
|
||||
->getRelation($campaign, 'excludingTargetLists')
|
||||
->find();
|
||||
|
||||
foreach ($excludingTargetListList as $excludingTargetList) {
|
||||
$recordList = $this->entityManager
|
||||
->getRDBRepository('TargetList')
|
||||
->getRelation($excludingTargetList, $link)
|
||||
->find();
|
||||
|
||||
foreach ($recordList as $excludingTarget) {
|
||||
$hashId = $excludingTarget->getEntityType() . '-' . $excludingTarget->getId();
|
||||
$metTargetHash[$hashId] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$addressFieldList = $this->entityTypeAddressFieldListMap[$targetEntityType];
|
||||
|
||||
/** @var iterable<\Espo\Modules\Crm\Entities\TargetList> $targetListCollection */
|
||||
$targetListCollection = $this->entityManager
|
||||
->getRDBRepository('Campaign')
|
||||
->getRelation($campaign, 'targetLists')
|
||||
->find();
|
||||
|
||||
foreach ($targetListCollection as $targetList) {
|
||||
if (!$campaign->get($link . 'TemplateId')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$entityList = $this->entityManager
|
||||
->getRDBRepository('TargetList')
|
||||
->getRelation($targetList, $link)
|
||||
->where([
|
||||
'@relation.optedOut' => false,
|
||||
])
|
||||
->find();
|
||||
|
||||
foreach ($entityList as $e) {
|
||||
$hashId = $e->getEntityType() . '-'. $e->getId();
|
||||
|
||||
if (!empty($metTargetHash[$hashId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$metTargetHash[$hashId] = true;
|
||||
|
||||
if ($campaign->get('mailMergeOnlyWithAddress')) {
|
||||
if (empty($addressFieldList)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hasAddress = false;
|
||||
|
||||
foreach ($addressFieldList as $addressField) {
|
||||
if (
|
||||
$e->get($addressField . 'Street') ||
|
||||
$e->get($addressField . 'PostalCode')
|
||||
) {
|
||||
$hasAddress = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$hasAddress) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$targetEntityList[] = $e;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($targetEntityList)) {
|
||||
throw new Error("No targets available for mail merge.");
|
||||
}
|
||||
|
||||
$filename = $campaign->get('name') . ' - ' .
|
||||
$this->defaultLanguage->translateLabel($targetEntityType, 'scopeNamesPlural');
|
||||
|
||||
/** @var EntityCollection<Entity> $collection */
|
||||
$collection = $this->entityManager
|
||||
->getCollectionFactory()
|
||||
->create($targetEntityType, $targetEntityList);
|
||||
|
||||
return $this->injectableFactory
|
||||
->create(MailMergeGenerator::class)
|
||||
->generate(
|
||||
$collection,
|
||||
$template,
|
||||
$campaign->getId(),
|
||||
$filename
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,9 @@ class MailMergeGenerator
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a mail-merge PDF.
|
||||
*
|
||||
* @return string An attachment ID.
|
||||
* @param EntityCollection<Entity> $collection
|
||||
* @throws Error
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2022 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\Campaign;
|
||||
|
||||
use Espo\Core\Acl;
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Utils\Language;
|
||||
use Espo\Entities\Template;
|
||||
use Espo\Modules\Crm\Entities\Campaign as CampaignEntity;
|
||||
use Espo\Modules\Crm\Entities\TargetList;
|
||||
use Espo\ORM\Collection;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityCollection;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
class MailMergeService
|
||||
{
|
||||
/** @var array<string, string[]> */
|
||||
protected $entityTypeAddressFieldListMap = [
|
||||
'Account' => ['billingAddress', 'shippingAddress'],
|
||||
'Contact' => ['address'],
|
||||
'Lead' => ['address'],
|
||||
'User' => [],
|
||||
];
|
||||
|
||||
/** @var string[] */
|
||||
protected $targetLinkList = [
|
||||
'accounts',
|
||||
'contacts',
|
||||
'leads',
|
||||
'users',
|
||||
];
|
||||
|
||||
private EntityManager $entityManager;
|
||||
private Acl $acl;
|
||||
private Language $defaultLanguage;
|
||||
private MailMergeGenerator $generator;
|
||||
|
||||
public function __construct(
|
||||
EntityManager $entityManager,
|
||||
Acl $acl,
|
||||
Language $defaultLanguage,
|
||||
MailMergeGenerator $generator
|
||||
) {
|
||||
$this->entityManager = $entityManager;
|
||||
$this->acl = $acl;
|
||||
$this->defaultLanguage = $defaultLanguage;
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string An attachment ID.
|
||||
* @throws BadRequest
|
||||
* @throws Error
|
||||
* @throws Forbidden
|
||||
*/
|
||||
public function generate(string $campaignId, string $link, bool $checkAcl = true): string
|
||||
{
|
||||
/** @var CampaignEntity $campaign */
|
||||
$campaign = $this->entityManager->getEntityById(CampaignEntity::ENTITY_TYPE, $campaignId);
|
||||
|
||||
if ($checkAcl && !$this->acl->checkEntityRead($campaign)) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
/** @var string $targetEntityType */
|
||||
$targetEntityType = $campaign->getRelationParam($link, 'entity');
|
||||
|
||||
if ($checkAcl && !$this->acl->check($targetEntityType, Acl\Table::ACTION_READ)) {
|
||||
throw new Forbidden("Could not mail merge campaign because access to target entity type is forbidden.");
|
||||
}
|
||||
|
||||
if (!in_array($link, $this->targetLinkList)) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
if ($campaign->getType() !== CampaignEntity::TYPE_MAIL) {
|
||||
throw new Error("Could not mail merge campaign not of Mail type.");
|
||||
}
|
||||
|
||||
$templateId = $campaign->get($link . 'TemplateId');
|
||||
|
||||
if (!$templateId) {
|
||||
throw new Error("Could not mail merge campaign w/o specified template.");
|
||||
}
|
||||
|
||||
/** @var ?Template $template */
|
||||
$template = $this->entityManager->getEntityById(Template::ENTITY_TYPE, $templateId);
|
||||
|
||||
if (!$template) {
|
||||
throw new Error("Template not found.");
|
||||
}
|
||||
|
||||
if ($template->getTargetEntityType() !== $targetEntityType) {
|
||||
throw new Error("Template is not of proper entity type.");
|
||||
}
|
||||
|
||||
$campaign->loadLinkMultipleField('targetLists');
|
||||
$campaign->loadLinkMultipleField('excludingTargetLists');
|
||||
|
||||
if (count($campaign->getLinkMultipleIdList('targetLists') ?? []) === 0) {
|
||||
throw new Error("Could not mail merge campaign w/o any specified target list.");
|
||||
}
|
||||
|
||||
$metTargetHash = [];
|
||||
$targetEntityList = [];
|
||||
|
||||
/** @var Collection<TargetList> $excludingTargetListList */
|
||||
$excludingTargetListList = $this->entityManager
|
||||
->getRDBRepository(CampaignEntity::ENTITY_TYPE)
|
||||
->getRelation($campaign, 'excludingTargetLists')
|
||||
->find();
|
||||
|
||||
foreach ($excludingTargetListList as $excludingTargetList) {
|
||||
$recordList = $this->entityManager
|
||||
->getRDBRepository(TargetList::ENTITY_TYPE)
|
||||
->getRelation($excludingTargetList, $link)
|
||||
->find();
|
||||
|
||||
foreach ($recordList as $excludingTarget) {
|
||||
$hashId = $excludingTarget->getEntityType() . '-' . $excludingTarget->getId();
|
||||
$metTargetHash[$hashId] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$addressFieldList = $this->entityTypeAddressFieldListMap[$targetEntityType];
|
||||
|
||||
/** @var Collection<TargetList> $targetListCollection */
|
||||
$targetListCollection = $this->entityManager
|
||||
->getRDBRepository(CampaignEntity::ENTITY_TYPE)
|
||||
->getRelation($campaign, 'targetLists')
|
||||
->find();
|
||||
|
||||
foreach ($targetListCollection as $targetList) {
|
||||
if (!$campaign->get($link . 'TemplateId')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$entityList = $this->entityManager
|
||||
->getRDBRepository(TargetList::ENTITY_TYPE)
|
||||
->getRelation($targetList, $link)
|
||||
->where([
|
||||
'@relation.optedOut' => false,
|
||||
])
|
||||
->find();
|
||||
|
||||
foreach ($entityList as $e) {
|
||||
$hashId = $e->getEntityType() . '-'. $e->getId();
|
||||
|
||||
if (!empty($metTargetHash[$hashId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$metTargetHash[$hashId] = true;
|
||||
|
||||
if ($campaign->get('mailMergeOnlyWithAddress')) {
|
||||
if (empty($addressFieldList)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hasAddress = false;
|
||||
|
||||
foreach ($addressFieldList as $addressField) {
|
||||
if (
|
||||
$e->get($addressField . 'Street') ||
|
||||
$e->get($addressField . 'PostalCode')
|
||||
) {
|
||||
$hasAddress = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$hasAddress) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$targetEntityList[] = $e;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($targetEntityList)) {
|
||||
throw new Error("No targets available for mail merge.");
|
||||
}
|
||||
|
||||
$filename = $campaign->getName() . ' - ' .
|
||||
$this->defaultLanguage->translateLabel($targetEntityType, 'scopeNamesPlural');
|
||||
|
||||
/** @var EntityCollection<Entity> $collection */
|
||||
$collection = $this->entityManager
|
||||
->getCollectionFactory()
|
||||
->create($targetEntityType, $targetEntityList);
|
||||
|
||||
return $this->generator->generate(
|
||||
$collection,
|
||||
$template,
|
||||
$campaign->getId(),
|
||||
$filename
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user