This commit is contained in:
Yuri Kuznetsov
2022-10-18 18:16:10 +03:00
parent 5485966c1c
commit d3e65f9ba1
4 changed files with 192 additions and 135 deletions
+8 -12
View File
@@ -41,11 +41,11 @@ use Espo\Core\Api\Request;
use Espo\Core\Mail\SmtpParams;
use Espo\Entities\Email as EmailEntity;
use Espo\Services\Email as Service;
use Espo\Services\EmailTemplate as EmailTemplateService;
use Espo\Tools\Email\SendService;
use Espo\Tools\Email\Service as ToolService;
use Espo\Tools\Email\TestSendData;
use Espo\Tools\EmailTemplate\InsertField\Service as InsertFieldService;
use stdClass;
class Email extends Record
@@ -331,11 +331,13 @@ class Email extends Record
throw new Forbidden();
}
return $this->getEmailTemplateService()->getInsertFieldData([
'parentId' => $request->getQueryParam('parentId'),
'parentType' => $request->getQueryParam('parentType'),
'to' => $request->getQueryParam('to'),
]);
return $this->injectableFactory
->create(InsertFieldService::class)
->getData(
$request->getQueryParam('parentType'),
$request->getQueryParam('parentId'),
$request->getQueryParam('to')
);
}
private function getEmailToolService(): ToolService
@@ -353,10 +355,4 @@ class Email extends Record
/** @var Service */
return $this->getRecordService();
}
private function getEmailTemplateService(): EmailTemplateService
{
/** @var EmailTemplateService */
return $this->getServiceFactory()->create('EmailTemplate');
}
}
-114
View File
@@ -30,22 +30,13 @@
namespace Espo\Services;
use Espo\Core\Exceptions\Forbidden;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\Tools\EmailTemplate\Processor;
use Espo\Tools\EmailTemplate\Params;
use Espo\Tools\EmailTemplate\Data;
use Espo\Tools\EmailTemplate\Formatter;
use Espo\Entities\EmailTemplate as EmailTemplateEntity;
use Espo\Entities\EmailAddress;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Di;
use stdClass;
/**
* @deprecated For bc. Use `Espo\Tools\EmailTemplate\Service`.
*
@@ -123,113 +114,8 @@ class EmailTemplate extends Record implements
return $this->parseTemplate($emailTemplate, $params, $copyAttachments);
}
/**
* @param array<string,mixed> $params
*/
public function getInsertFieldData(array $params): stdClass
{
$to = $params['to'] ?? null;
$parentId = $params['parentId'] ?? null;
$parentType = $params['parentType'] ?? null;
$result = (object) [];
$dataList = [];
if ($parentId && $parentType) {
$e = $this->entityManager->getEntity($parentType, $parentId);
if ($e && $this->acl->check($e)) {
$dataList[] = [
'type' => 'parent',
'entity' => $e,
];
}
}
if ($to) {
$e = $this->getEmailAddressRepository()->getEntityByAddress($to, null, ['Contact', 'Lead', 'Account']);
if ($e && $e->getEntityType() !== 'User' && $this->acl->check($e)) {
$dataList[] = [
'type' => 'to',
'entity' => $e,
];
}
}
$fm = $this->fieldUtil;
$formatter = $this->createFormatter();
foreach ($dataList as $item) {
$type = $item['type'];
$e = $item['entity'];
$entityType = $e->getEntityType();
$recordService = $this->recordServiceContainer->get($entityType);
$recordService->prepareEntityForOutput($e);
$ignoreTypeList = ['image', 'file', 'map', 'wysiwyg', 'linkMultiple', 'attachmentMultiple', 'bool'];
foreach ($fm->getEntityTypeFieldList($entityType) as $field) {
$fieldType = $fm->getEntityTypeFieldParam($entityType, $field, 'type');
$fieldAttributeList = $fm->getAttributeList($entityType, $field);
if (
$fm->getEntityTypeFieldParam($entityType, $field, 'disabled') ||
$fm->getEntityTypeFieldParam($entityType, $field, 'directAccessDisabled') ||
$fm->getEntityTypeFieldParam($entityType, $field, 'templatePlaceholderDisabled') ||
in_array($fieldType, $ignoreTypeList)
) {
foreach ($fieldAttributeList as $a) {
$e->clear($a);
}
}
}
$attributeList = $fm->getEntityTypeAttributeList($entityType);
$values = (object) [];
foreach ($attributeList as $a) {
if (!$e->has($a)) {
continue;
}
$value = $formatter->formatAttributeValue($e, $a);
if ($value !== null && $value !== '') {
$values->$a = $value;
}
}
$result->$type = (object) [
'entityType' => $e->getEntityType(),
'id' => $e->getId(),
'values' => $values,
'name' => $e->get('name'),
];
}
return $result;
}
private function createProcessor(): Processor
{
return $this->injectableFactory->create(Processor::class);
}
private function createFormatter(): Formatter
{
return $this->injectableFactory->create(Formatter::class);
}
private function getEmailAddressRepository(): EmailAddressRepository
{
/** @var EmailAddressRepository */
return $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE);
}
}
@@ -39,15 +39,11 @@ use Espo\Core\Utils\Language;
class Formatter
{
private $metadata;
private $config;
private $dateTime;
private $number;
private $language;
private Metadata $metadata;
private Config $config;
private DateTimeUtil $dateTime;
private NumberUtil $number;
private Language $language;
public function __construct(
Metadata $metadata,
@@ -0,0 +1,179 @@
<?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\Tools\EmailTemplate\InsertField;
use Espo\Core\Acl;
use Espo\Core\Acl\Table;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Record\ServiceContainer;
use Espo\Core\Utils\FieldUtil;
use Espo\Entities\Email;
use Espo\Entities\EmailAddress;
use Espo\Entities\User;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Contact;
use Espo\Modules\Crm\Entities\Lead;
use Espo\ORM\EntityManager;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\Tools\EmailTemplate\Formatter;
use stdClass;
class Service
{
private EntityManager $entityManager;
private Acl $acl;
private Formatter $formatter;
private FieldUtil $fieldUtil;
private ServiceContainer $recordServiceContainer;
public function __construct(
EntityManager $entityManager,
Acl $acl,
Formatter $formatter,
FieldUtil $fieldUtil,
ServiceContainer $recordServiceContainer
) {
$this->entityManager = $entityManager;
$this->acl = $acl;
$this->formatter = $formatter;
$this->fieldUtil = $fieldUtil;
$this->recordServiceContainer = $recordServiceContainer;
}
/**
* @throws Forbidden
*/
public function getData(?string $parentType, ?string $parentId, ?string $to): stdClass
{
if (!$this->acl->checkScope(Email::ENTITY_TYPE, Table::ACTION_CREATE)) {
throw new Forbidden();
}
$result = (object) [];
$dataList = [];
if ($parentId && $parentType) {
$e = $this->entityManager->getEntityById($parentType, $parentId);
if ($e && $this->acl->check($e)) {
$dataList[] = [
'type' => 'parent',
'entity' => $e,
];
}
}
if ($to) {
$e = $this->getEmailAddressRepository()
->getEntityByAddress($to, null,
[Contact::ENTITY_TYPE, Lead::ENTITY_TYPE, Account::ENTITY_TYPE]);
if ($e && $e->getEntityType() !== User::ENTITY_TYPE && $this->acl->check($e)) {
$dataList[] = [
'type' => 'to',
'entity' => $e,
];
}
}
$fm = $this->fieldUtil;
$formatter = $this->formatter;
foreach ($dataList as $item) {
$type = $item['type'];
$e = $item['entity'];
$entityType = $e->getEntityType();
$recordService = $this->recordServiceContainer->get($entityType);
$recordService->prepareEntityForOutput($e);
$ignoreTypeList = [
'image',
'file',
'map',
'wysiwyg',
'linkMultiple',
'attachmentMultiple',
'bool',
];
foreach ($fm->getEntityTypeFieldList($entityType) as $field) {
$fieldType = $fm->getEntityTypeFieldParam($entityType, $field, 'type');
$fieldAttributeList = $fm->getAttributeList($entityType, $field);
if (
$fm->getEntityTypeFieldParam($entityType, $field, 'disabled') ||
$fm->getEntityTypeFieldParam($entityType, $field, 'directAccessDisabled') ||
$fm->getEntityTypeFieldParam($entityType, $field, 'templatePlaceholderDisabled') ||
in_array($fieldType, $ignoreTypeList)
) {
foreach ($fieldAttributeList as $a) {
$e->clear($a);
}
}
}
$attributeList = $fm->getEntityTypeAttributeList($entityType);
$values = (object) [];
foreach ($attributeList as $a) {
if (!$e->has($a)) {
continue;
}
$value = $formatter->formatAttributeValue($e, $a);
if ($value !== null && $value !== '') {
$values->$a = $value;
}
}
$result->$type = (object) [
'entityType' => $e->getEntityType(),
'id' => $e->getId(),
'values' => $values,
'name' => $e->get('name'),
];
}
return $result;
}
private function getEmailAddressRepository(): EmailAddressRepository
{
/** @var EmailAddressRepository */
return $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE);
}
}