Initial commit
This commit is contained in:
68
application/Espo/Tools/EmailTemplate/Api/PostPrepare.php
Normal file
68
application/Espo/Tools/EmailTemplate/Api/PostPrepare.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate\Api;
|
||||
|
||||
use Espo\Core\Api\Action;
|
||||
use Espo\Core\Api\Request;
|
||||
use Espo\Core\Api\Response;
|
||||
use Espo\Core\Api\ResponseComposer;
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Tools\EmailTemplate\Data;
|
||||
use Espo\Tools\EmailTemplate\Service;
|
||||
|
||||
/**
|
||||
* Prepares an email data with an email template applied.
|
||||
*/
|
||||
class PostPrepare implements Action
|
||||
{
|
||||
public function __construct(private Service $service) {}
|
||||
|
||||
public function process(Request $request): Response
|
||||
{
|
||||
$id = $request->getRouteParam('id');
|
||||
|
||||
if ($id === null) {
|
||||
throw new BadRequest();
|
||||
}
|
||||
|
||||
$body = $request->getParsedBody();
|
||||
|
||||
$data = Data::create()
|
||||
->withRelatedType($body->relatedType ?? null)
|
||||
->withRelatedId($body->relatedId ?? null)
|
||||
->withParentType($body->parentType ?? null)
|
||||
->withParentId($body->parentId ?? null)
|
||||
->withEmailAddress($body->emailAddress ?? null);
|
||||
|
||||
$result = $this->service->process($id, $data);
|
||||
|
||||
return ResponseComposer::json($result->getValueMap());
|
||||
}
|
||||
}
|
||||
169
application/Espo/Tools/EmailTemplate/Data.php
Normal file
169
application/Espo/Tools/EmailTemplate/Data.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Entities\User;
|
||||
|
||||
class Data
|
||||
{
|
||||
/** @var array<string, Entity> */
|
||||
private $entityHash = [];
|
||||
private ?string $emailAddress = null;
|
||||
private ?Entity $parent = null;
|
||||
private ?string $parentId = null;
|
||||
private ?string $parentType = null;
|
||||
private ?string $relatedId = null;
|
||||
private ?string $relatedType = null;
|
||||
private ?User $user = null;
|
||||
|
||||
/**
|
||||
* @return array<string,Entity> $entityHash
|
||||
*/
|
||||
public function getEntityHash(): array
|
||||
{
|
||||
return $this->entityHash;
|
||||
}
|
||||
|
||||
public function getEmailAddress(): ?string
|
||||
{
|
||||
return $this->emailAddress;
|
||||
}
|
||||
|
||||
public function getParent(): ?Entity
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
public function getParentId(): ?string
|
||||
{
|
||||
return $this->parentId;
|
||||
}
|
||||
|
||||
public function getParentType(): ?string
|
||||
{
|
||||
return $this->parentType;
|
||||
}
|
||||
|
||||
public function getRelatedId(): ?string
|
||||
{
|
||||
return $this->relatedId;
|
||||
}
|
||||
|
||||
public function getRelatedType(): ?string
|
||||
{
|
||||
return $this->relatedType;
|
||||
}
|
||||
|
||||
public function getUser(): ?User
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* An entity hash.
|
||||
*
|
||||
* @param array<string,Entity> $entityHash
|
||||
*/
|
||||
public function withEntityHash(array $entityHash): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->entityHash = $entityHash;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* An email address.
|
||||
*/
|
||||
public function withEmailAddress(?string $emailAddress): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->emailAddress = $emailAddress;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withParent(?Entity $parent): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->parent = $parent;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withParentId(?string $parentId): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->parentId = $parentId;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withParentType(?string $parentType): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->parentType = $parentType;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withRelatedId(?string $relatedId): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->relatedId = $relatedId;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function withRelatedType(?string $relatedType): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->relatedType = $relatedType;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* A user to apply ACL for.
|
||||
*/
|
||||
public function withUser(?User $user): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->user = $user;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
131
application/Espo/Tools/EmailTemplate/EntityMapProvider.php
Normal file
131
application/Espo/Tools/EmailTemplate/EntityMapProvider.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
use Espo\Core\Acl\GlobalRestriction;
|
||||
use Espo\Core\AclManager;
|
||||
use Espo\Core\Record\ServiceContainer;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Entities\User;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
/**
|
||||
* @since 9.2.0
|
||||
* @internal
|
||||
*/
|
||||
class EntityMapProvider
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private AclManager $aclManager,
|
||||
private ServiceContainer $serviceContainer,
|
||||
private Metadata $metadata,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array<string, Entity>
|
||||
*/
|
||||
public function get(Entity $entity, User $user, bool $applyAcl): array
|
||||
{
|
||||
/** @var array<string, string> $map */
|
||||
$map = $this->metadata->get("app.emailTemplate.entityLinkMapping.{$entity->getEntityType()}") ?? [];
|
||||
|
||||
$output = [];
|
||||
|
||||
foreach ($map as $entityType => $link) {
|
||||
$related = $this->getRelated(
|
||||
entity: $entity,
|
||||
link: $link,
|
||||
user: $user,
|
||||
applyAcl: $applyAcl,
|
||||
);
|
||||
|
||||
if ($related) {
|
||||
$output[$entityType] = $related;
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
private function getRelated(
|
||||
Entity $entity,
|
||||
string $link,
|
||||
User $user,
|
||||
bool $applyAcl,
|
||||
): ?Entity {
|
||||
|
||||
$entityDefs = $this->entityManager->getDefs()->getEntity($entity->getEntityType());
|
||||
|
||||
$forbiddenLinkList = $this->aclManager->getScopeRestrictedLinkList(
|
||||
$entity->getEntityType(),
|
||||
[
|
||||
GlobalRestriction::TYPE_FORBIDDEN,
|
||||
GlobalRestriction::TYPE_INTERNAL,
|
||||
GlobalRestriction::TYPE_ONLY_ADMIN,
|
||||
]
|
||||
);
|
||||
|
||||
if ($applyAcl) {
|
||||
if (
|
||||
$entityDefs->hasField($link) &&
|
||||
!$this->aclManager->checkField($user, $entity->getEntityType(), $link)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (in_array($link, $forbiddenLinkList)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$related = $this->entityManager
|
||||
->getRelation($entity, $link)
|
||||
->findOne();
|
||||
|
||||
if (!$related) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
$applyAcl &&
|
||||
!$this->aclManager->checkEntityRead($user, $related)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->serviceContainer
|
||||
->get($related->getEntityType())
|
||||
->loadAdditionalFields($related);
|
||||
|
||||
return $related;
|
||||
}
|
||||
}
|
||||
178
application/Espo/Tools/EmailTemplate/Formatter.php
Normal file
178
application/Espo/Tools/EmailTemplate/Formatter.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
use Espo\Core\ORM\Type\FieldType;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\DateTime as DateTimeUtil;
|
||||
use Espo\Core\Utils\NumberUtil;
|
||||
use Espo\Core\Utils\Language;
|
||||
|
||||
use Espo\ORM\Type\AttributeType;
|
||||
use Stringable;
|
||||
|
||||
class Formatter
|
||||
{
|
||||
public function __construct(
|
||||
private Metadata $metadata,
|
||||
private Config $config,
|
||||
private DateTimeUtil $dateTime,
|
||||
private NumberUtil $number,
|
||||
private Language $language
|
||||
) {}
|
||||
|
||||
public function formatAttributeValue(Entity $entity, string $attribute, bool $isPlainText = false): ?string
|
||||
{
|
||||
$value = $entity->get($attribute);
|
||||
|
||||
$fieldType = $this->metadata
|
||||
->get(['entityDefs', $entity->getEntityType(), 'fields', $attribute, 'type']);
|
||||
|
||||
$attributeType = $entity->getAttributeType($attribute);
|
||||
|
||||
if ($fieldType === FieldType::ENUM) {
|
||||
if ($value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$label = $this->language->translateOption($value, $attribute, $entity->getEntityType());
|
||||
|
||||
$translationPath = $this->metadata->get(
|
||||
['entityDefs', $entity->getEntityType(), 'fields', $attribute, 'translation']
|
||||
);
|
||||
|
||||
if ($translationPath) {
|
||||
$label = $this->language->get($translationPath . '.' . $value, $label);
|
||||
}
|
||||
|
||||
return $label;
|
||||
}
|
||||
|
||||
if (
|
||||
$fieldType === FieldType::ARRAY ||
|
||||
$fieldType === FieldType::MULTI_ENUM ||
|
||||
$fieldType === FieldType::CHECKLIST
|
||||
) {
|
||||
$valueList = [];
|
||||
|
||||
if (!is_array($value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
foreach ($value as $v) {
|
||||
$valueList[] = $this->language->translateOption($v, $attribute, $entity->getEntityType());
|
||||
}
|
||||
|
||||
return implode(', ', $valueList);
|
||||
}
|
||||
|
||||
if ($attributeType === AttributeType::DATE) {
|
||||
if (!$value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->dateTime->convertSystemDate($value);
|
||||
}
|
||||
|
||||
if ($attributeType === AttributeType::DATETIME) {
|
||||
if (!$value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->dateTime->convertSystemDateTime($value);
|
||||
}
|
||||
|
||||
if ($attributeType === AttributeType::TEXT) {
|
||||
if (!is_string($value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($fieldType === FieldType::WYSIWYG) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ($isPlainText) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return nl2br($value);
|
||||
}
|
||||
|
||||
if ($attributeType === AttributeType::FLOAT) {
|
||||
if (!is_float($value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$decimalPlaces = 2;
|
||||
|
||||
if ($fieldType === FieldType::CURRENCY) {
|
||||
$decimalPlaces = $this->config->get('currencyDecimalPlaces');
|
||||
}
|
||||
|
||||
return $this->number->format($value, $decimalPlaces);
|
||||
}
|
||||
|
||||
if ($attributeType === AttributeType::INT) {
|
||||
if (!is_int($value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (
|
||||
$fieldType === FieldType::AUTOINCREMENT ||
|
||||
$fieldType === FieldType::INT &&
|
||||
$this->metadata
|
||||
->get(['entityDefs', $entity->getEntityType(), 'fields', $attribute, 'disableFormatting'])
|
||||
) {
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
return $this->number->format($value);
|
||||
}
|
||||
|
||||
if (
|
||||
!is_string($value) && is_scalar($value) ||
|
||||
$value instanceof Stringable
|
||||
) {
|
||||
return strval($value);
|
||||
}
|
||||
|
||||
if ($value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!is_string($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
182
application/Espo/Tools/EmailTemplate/InsertField/Service.php
Normal file
182
application/Espo/Tools/EmailTemplate/InsertField/Service.php
Normal file
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero 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\Name\Field;
|
||||
use Espo\Core\ORM\Type\FieldType;
|
||||
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->loadAdditionalFields($e);
|
||||
$recordService->prepareEntityForOutput($e);
|
||||
|
||||
$ignoreTypeList = [
|
||||
FieldType::IMAGE,
|
||||
FieldType::FILE,
|
||||
FieldType::WYSIWYG,
|
||||
FieldType::LINK_MULTIPLE,
|
||||
FieldType::ATTACHMENT_MULTIPLE,
|
||||
FieldType::BOOL,
|
||||
'map',
|
||||
];
|
||||
|
||||
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(Field::NAME),
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getEmailAddressRepository(): EmailAddressRepository
|
||||
{
|
||||
/** @var EmailAddressRepository */
|
||||
return $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE);
|
||||
}
|
||||
}
|
||||
76
application/Espo/Tools/EmailTemplate/Params.php
Normal file
76
application/Espo/Tools/EmailTemplate/Params.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
/**
|
||||
* Immutable.
|
||||
*/
|
||||
class Params
|
||||
{
|
||||
private bool $applyAcl = false;
|
||||
private bool $copyAttachments = false;
|
||||
|
||||
public function applyAcl(): bool
|
||||
{
|
||||
return $this->applyAcl;
|
||||
}
|
||||
|
||||
public function copyAttachments(): bool
|
||||
{
|
||||
return $this->copyAttachments;
|
||||
}
|
||||
|
||||
/**
|
||||
* To apply ACL.
|
||||
*/
|
||||
public function withApplyAcl(bool $applyAcl = true): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->applyAcl = $applyAcl;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* To copy template attachments records. Not needed if an email not supposed to be stored.
|
||||
*/
|
||||
public function withCopyAttachments(bool $copyAttachments = true): self
|
||||
{
|
||||
$obj = clone $this;
|
||||
$obj->copyAttachments = $copyAttachments;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public static function create(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
}
|
||||
35
application/Espo/Tools/EmailTemplate/Placeholder.php
Normal file
35
application/Espo/Tools/EmailTemplate/Placeholder.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
interface Placeholder
|
||||
{
|
||||
public function get(Data $data): string;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate\Placeholders;
|
||||
|
||||
use DateTime;
|
||||
use DateTimezone;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Tools\EmailTemplate\Data;
|
||||
use Espo\Tools\EmailTemplate\Placeholder;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class CurrentYear implements Placeholder
|
||||
{
|
||||
public function __construct(
|
||||
private Config\ApplicationConfig $applicationConfig,
|
||||
) {}
|
||||
|
||||
public function get(Data $data): string
|
||||
{
|
||||
try {
|
||||
$now = new DateTime('now', new DateTimezone($this->applicationConfig->getTimeZone()));
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException($e->getMessage());
|
||||
}
|
||||
|
||||
return $now->format('Y');
|
||||
}
|
||||
}
|
||||
49
application/Espo/Tools/EmailTemplate/Placeholders/Now.php
Normal file
49
application/Espo/Tools/EmailTemplate/Placeholders/Now.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate\Placeholders;
|
||||
|
||||
use Espo\Core\Utils\DateTime;
|
||||
use Espo\Tools\EmailTemplate\Data;
|
||||
use Espo\Tools\EmailTemplate\Placeholder;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class Now implements Placeholder
|
||||
{
|
||||
public function __construct(
|
||||
private DateTime $dateTime
|
||||
) {}
|
||||
|
||||
public function get(Data $data): string
|
||||
{
|
||||
return $this->dateTime->getNowString();
|
||||
}
|
||||
}
|
||||
49
application/Espo/Tools/EmailTemplate/Placeholders/Today.php
Normal file
49
application/Espo/Tools/EmailTemplate/Placeholders/Today.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate\Placeholders;
|
||||
|
||||
use Espo\Core\Utils\DateTime;
|
||||
use Espo\Tools\EmailTemplate\Data;
|
||||
use Espo\Tools\EmailTemplate\Placeholder;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class Today implements Placeholder
|
||||
{
|
||||
public function __construct(
|
||||
private DateTime $dateTime
|
||||
) {}
|
||||
|
||||
public function get(Data $data): string
|
||||
{
|
||||
return $this->dateTime->getTodayString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
use Espo\Core\InjectableFactory;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
|
||||
class PlaceholdersProvider
|
||||
{
|
||||
public function __construct(
|
||||
private Metadata $metadata,
|
||||
private InjectableFactory $injectableFactory
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{string, Placeholder}[]
|
||||
*/
|
||||
public function get(): array
|
||||
{
|
||||
$defs = $this->metadata->get("app.emailTemplate.placeholders") ?? [];
|
||||
|
||||
/** @var string[] $list */
|
||||
$list = array_keys($defs);
|
||||
|
||||
usort($list, function ($a, $b) use ($defs) {
|
||||
$o1 = $defs[$a]['order'] ?? 0;
|
||||
$o2 = $defs[$b]['order'] ?? 0;
|
||||
|
||||
return $o1 - $o2;
|
||||
});
|
||||
|
||||
return array_map(function ($name) use ($defs) {
|
||||
/** @var class-string<Placeholder> $className */
|
||||
$className = $defs[$name]['className'];
|
||||
|
||||
$placeholder = $this->injectableFactory->create($className);
|
||||
|
||||
return [$name, $placeholder];
|
||||
}, $list);
|
||||
}
|
||||
}
|
||||
449
application/Espo/Tools/EmailTemplate/Processor.php
Normal file
449
application/Espo/Tools/EmailTemplate/Processor.php
Normal file
@@ -0,0 +1,449 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
use Espo\Core\Templates\Entities\Person as PersonTemplate;
|
||||
use Espo\Modules\Crm\Entities\Account;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\Modules\Crm\Entities\Lead;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\Core\AclManager;
|
||||
use Espo\Core\Record\ServiceContainer;
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\FileStorage\Manager as FileStorageManager;
|
||||
use Espo\Core\Entities\Person;
|
||||
use Espo\Core\Htmlizer\HtmlizerFactory as HtmlizerFactory;
|
||||
use Espo\Core\Htmlizer\Htmlizer;
|
||||
use Espo\Core\Acl\GlobalRestriction;
|
||||
use Espo\Entities\EmailTemplate;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Entities\Attachment;
|
||||
use Espo\Entities\EmailAddress;
|
||||
use Espo\Repositories\EmailAddress as EmailAddressRepository;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Processor
|
||||
{
|
||||
private const KEY_PARENT = 'Parent';
|
||||
|
||||
public function __construct(
|
||||
private Formatter $formatter,
|
||||
private EntityManager $entityManager,
|
||||
private AclManager $aclManager,
|
||||
private ServiceContainer $recordServiceContainer,
|
||||
private Config $config,
|
||||
private FileStorageManager $fileStorageManager,
|
||||
private User $user,
|
||||
private HtmlizerFactory $htmlizerFactory,
|
||||
private PlaceholdersProvider $placeholdersProvider,
|
||||
private EntityMapProvider $entityMapProvider,
|
||||
) {}
|
||||
|
||||
public function process(EmailTemplate $template, Params $params, Data $data): Result
|
||||
{
|
||||
$user = $data->getUser() ?? $this->user;
|
||||
|
||||
[$entityHash, $data] = $this->prepare($data, $user, $params);
|
||||
|
||||
$subject = $template->getSubject() ?? '';
|
||||
$body = $template->getBody() ?? '';
|
||||
|
||||
$parent = $entityHash[self::KEY_PARENT] ?? null;
|
||||
|
||||
if ($parent && !$this->config->get('emailTemplateHtmlizerDisabled')) {
|
||||
$handlebarsInSubject = str_contains($subject, '{{') && str_contains($subject, '}}');
|
||||
$handlebarsInBody = str_contains($body, '{{') && str_contains($body, '}}');
|
||||
|
||||
if ($handlebarsInSubject || $handlebarsInBody) {
|
||||
$htmlizer = $this->createHtmlizer($params, $user);
|
||||
|
||||
if ($handlebarsInSubject) {
|
||||
$subject = $htmlizer->render($parent, $subject);
|
||||
}
|
||||
|
||||
if ($handlebarsInBody) {
|
||||
$body = $htmlizer->render($parent, $body, null, null, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($entityHash as $type => $entity) {
|
||||
$subject = $this->processText(
|
||||
type: $type,
|
||||
entity: $entity,
|
||||
text: $subject,
|
||||
user: $user,
|
||||
skipAcl: !$params->applyAcl(),
|
||||
isHtml: $template->isHtml(),
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($entityHash as $type => $entity) {
|
||||
$body = $this->processText(
|
||||
type: $type,
|
||||
entity: $entity,
|
||||
text: $body,
|
||||
user: $user,
|
||||
skipAcl: !$params->applyAcl(),
|
||||
isHtml: $template->isHtml(),
|
||||
);
|
||||
}
|
||||
|
||||
$subject = $this->processPlaceholders($subject, $data);
|
||||
$body = $this->processPlaceholders($body, $data);
|
||||
|
||||
$attachmentList = $params->copyAttachments() ?
|
||||
$this->copyAttachments($template) : [];
|
||||
|
||||
return new Result(
|
||||
subject: $subject,
|
||||
body: $body,
|
||||
isHtml: $template->isHtml(),
|
||||
attachmentList: $attachmentList,
|
||||
);
|
||||
}
|
||||
|
||||
private function processPlaceholders(string $text, Data $data): string
|
||||
{
|
||||
foreach ($this->placeholdersProvider->get() as [$key, $placeholder]) {
|
||||
$value = $placeholder->get($data);
|
||||
|
||||
$text = str_replace('{' . $key . '}', $value, $text);
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
private function processText(
|
||||
string $type,
|
||||
Entity $entity,
|
||||
string $text,
|
||||
User $user,
|
||||
bool $skipLinks = false,
|
||||
?string $prefixLink = null,
|
||||
bool $skipAcl = false,
|
||||
bool $isHtml = true
|
||||
): string {
|
||||
|
||||
$attributeList = $entity->getAttributeList();
|
||||
|
||||
$forbiddenAttributeList = [];
|
||||
|
||||
if (!$skipAcl) {
|
||||
$forbiddenAttributeList = array_merge(
|
||||
$this->aclManager->getScopeForbiddenAttributeList($user, $entity->getEntityType()),
|
||||
$this->aclManager->getScopeRestrictedAttributeList(
|
||||
$entity->getEntityType(),
|
||||
[
|
||||
GlobalRestriction::TYPE_FORBIDDEN,
|
||||
GlobalRestriction::TYPE_INTERNAL,
|
||||
GlobalRestriction::TYPE_ONLY_ADMIN,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($attributeList as $attribute) {
|
||||
if (in_array($attribute, $forbiddenAttributeList)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_object($entity->get($attribute))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$entity->getAttributeType($attribute)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $this->formatter->formatAttributeValue($entity, $attribute, !$isHtml);
|
||||
|
||||
if (is_null($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$variableName = $attribute;
|
||||
|
||||
if (!is_null($prefixLink)) {
|
||||
$variableName = "$prefixLink.$attribute";
|
||||
}
|
||||
|
||||
$text = str_replace("{{$type}.$variableName}", $value, $text);
|
||||
}
|
||||
|
||||
if (!$skipLinks && $entity->hasId()) {
|
||||
$text = $this->processLinks(
|
||||
type: $type,
|
||||
entity: $entity,
|
||||
text: $text,
|
||||
user: $user,
|
||||
skipAcl: $skipAcl,
|
||||
isHtml: $isHtml,
|
||||
);
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
private function processLinks(
|
||||
string $type,
|
||||
Entity $entity,
|
||||
string $text,
|
||||
User $user,
|
||||
bool $skipAcl,
|
||||
bool $isHtml,
|
||||
): string {
|
||||
|
||||
$entityDefs = $this->entityManager->getDefs()->getEntity($entity->getEntityType());
|
||||
|
||||
$forbiddenLinkList = $skipAcl ?
|
||||
$this->aclManager->getScopeRestrictedLinkList(
|
||||
$entity->getEntityType(),
|
||||
[
|
||||
GlobalRestriction::TYPE_FORBIDDEN,
|
||||
GlobalRestriction::TYPE_INTERNAL,
|
||||
GlobalRestriction::TYPE_ONLY_ADMIN,
|
||||
]
|
||||
) :
|
||||
[];
|
||||
|
||||
foreach ($entity->getRelationList() as $relation) {
|
||||
if (in_array($relation, $forbiddenLinkList)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
!in_array($entity->getRelationType($relation), [
|
||||
Entity::BELONGS_TO,
|
||||
Entity::BELONGS_TO_PARENT,
|
||||
Entity::HAS_ONE,
|
||||
])
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
!$skipAcl &&
|
||||
$entityDefs->hasField($relation) &&
|
||||
!$this->aclManager->checkField($user, $entity->getEntityType(), $relation)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$relatedEntity = $this->entityManager
|
||||
->getRelation($entity, $relation)
|
||||
->findOne();
|
||||
|
||||
if (!$relatedEntity) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$skipAcl) {
|
||||
try {
|
||||
$hasAccess = $this->aclManager->checkEntityRead($user, $relatedEntity);
|
||||
} catch (Exception) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$hasAccess) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$text = $this->processText(
|
||||
type: $type,
|
||||
entity: $relatedEntity,
|
||||
text: $text,
|
||||
user: $user,
|
||||
skipLinks: true,
|
||||
prefixLink: $relation,
|
||||
skipAcl: $skipAcl,
|
||||
isHtml: $isHtml,
|
||||
);
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Attachment[]
|
||||
*/
|
||||
private function copyAttachments(EmailTemplate $template): array
|
||||
{
|
||||
$copiedAttachments = [];
|
||||
|
||||
/** @var iterable<Attachment> $attachments */
|
||||
$attachments = $this->entityManager
|
||||
->getRelation($template, 'attachments')
|
||||
->find();
|
||||
|
||||
foreach ($attachments as $attachment) {
|
||||
$clone = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getNew();
|
||||
|
||||
$data = $attachment->getValueMap();
|
||||
|
||||
unset($data->parentType);
|
||||
unset($data->parentId);
|
||||
unset($data->id);
|
||||
|
||||
$clone->set($data);
|
||||
$clone->setSourceId($attachment->getSourceId());
|
||||
$clone->setStorage($attachment->getStorage());
|
||||
|
||||
if (!$this->fileStorageManager->exists($attachment)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->entityManager->saveEntity($clone);
|
||||
|
||||
$copiedAttachments[] = $clone;
|
||||
}
|
||||
|
||||
return $copiedAttachments;
|
||||
}
|
||||
|
||||
private function createHtmlizer(Params $params, User $user): Htmlizer
|
||||
{
|
||||
if (!$params->applyAcl()) {
|
||||
return $this->htmlizerFactory->createNoAcl();
|
||||
}
|
||||
|
||||
return $this->htmlizerFactory->createForUser($user);
|
||||
}
|
||||
|
||||
private function getEmailAddressRepository(): EmailAddressRepository
|
||||
{
|
||||
/** @var EmailAddressRepository */
|
||||
return $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{array<string, Entity>, Data}
|
||||
*/
|
||||
private function prepare(Data $data, User $user, Params $params): array
|
||||
{
|
||||
$entityHash = $data->getEntityHash();
|
||||
|
||||
if (!isset($entityHash[User::ENTITY_TYPE])) {
|
||||
$entityHash[User::ENTITY_TYPE] = $user;
|
||||
}
|
||||
|
||||
$foundByAddressEntity = null;
|
||||
|
||||
if ($data->getEmailAddress()) {
|
||||
$foundByAddressEntity = $this->getEmailAddressRepository()->getEntityByAddress(
|
||||
$data->getEmailAddress(),
|
||||
null,
|
||||
[
|
||||
Contact::ENTITY_TYPE,
|
||||
Lead::ENTITY_TYPE,
|
||||
Account::ENTITY_TYPE,
|
||||
User::ENTITY_TYPE,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if ($foundByAddressEntity) {
|
||||
if ($foundByAddressEntity instanceof Person) {
|
||||
$entityHash[PersonTemplate::TEMPLATE_TYPE] = $foundByAddressEntity;
|
||||
}
|
||||
|
||||
if (!isset($entityHash[$foundByAddressEntity->getEntityType()])) {
|
||||
$entityHash[$foundByAddressEntity->getEntityType()] = $foundByAddressEntity;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
!$data->getParent() &&
|
||||
$data->getParentId() &&
|
||||
$data->getParentType()
|
||||
) {
|
||||
$parent = $this->entityManager->getEntityById($data->getParentType(), $data->getParentId());
|
||||
|
||||
if ($parent) {
|
||||
$service = $this->recordServiceContainer->get($data->getParentType());
|
||||
|
||||
$service->loadAdditionalFields($parent);
|
||||
|
||||
if (
|
||||
$params->applyAcl() &&
|
||||
!$this->aclManager->checkEntityRead($this->user, $parent)
|
||||
) {
|
||||
$parent = null;
|
||||
}
|
||||
|
||||
$data = $data->withParent($parent);
|
||||
}
|
||||
}
|
||||
|
||||
if ($data->getParent()) {
|
||||
$parent = $data->getParent();
|
||||
|
||||
$entityHash[$parent->getEntityType()] = $parent;
|
||||
$entityHash[self::KEY_PARENT] = $parent;
|
||||
|
||||
if (
|
||||
!isset($entityHash[PersonTemplate::TEMPLATE_TYPE]) &&
|
||||
$parent instanceof Person
|
||||
) {
|
||||
$entityHash[PersonTemplate::TEMPLATE_TYPE] = $parent;
|
||||
}
|
||||
}
|
||||
|
||||
if ($data->getParent()) {
|
||||
$entityHash = array_merge(
|
||||
$entityHash,
|
||||
$this->entityMapProvider->get($data->getParent(), $user, $params->applyAcl())
|
||||
);
|
||||
|
||||
$entityHash[$data->getParent()->getEntityType()] = $data->getParent();
|
||||
}
|
||||
|
||||
if ($data->getRelatedId() && $data->getRelatedType()) {
|
||||
$related = $this->entityManager->getEntityById($data->getRelatedType(), $data->getRelatedId());
|
||||
|
||||
if (
|
||||
$related &&
|
||||
$params->applyAcl() &&
|
||||
!$this->aclManager->checkEntityRead($this->user, $related)
|
||||
) {
|
||||
$related = null;
|
||||
}
|
||||
|
||||
if ($related) {
|
||||
$entityHash[$related->getEntityType()] = $related;
|
||||
}
|
||||
}
|
||||
|
||||
return [$entityHash, $data];
|
||||
}
|
||||
}
|
||||
116
application/Espo/Tools/EmailTemplate/Result.php
Normal file
116
application/Espo/Tools/EmailTemplate/Result.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Entities\Attachment;
|
||||
|
||||
use stdClass;
|
||||
|
||||
class Result
|
||||
{
|
||||
private $subject;
|
||||
private $body;
|
||||
private $isHtml = false;
|
||||
private $attachmentList = [];
|
||||
|
||||
/**
|
||||
* @param Attachment[] $attachmentList
|
||||
*/
|
||||
public function __construct(
|
||||
string $subject,
|
||||
string $body,
|
||||
bool $isHtml,
|
||||
array $attachmentList
|
||||
) {
|
||||
$this->subject = $subject;
|
||||
$this->body = $body;
|
||||
$this->isHtml = $isHtml;
|
||||
$this->attachmentList = $attachmentList;
|
||||
}
|
||||
|
||||
public function getSubject(): string
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function isHtml(): bool
|
||||
{
|
||||
return $this->isHtml;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Attachment[]
|
||||
*/
|
||||
public function getAttachmentList(): array
|
||||
{
|
||||
return $this->attachmentList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAttachmentIdList(): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($this->attachmentList as $attachment) {
|
||||
$list[] = $attachment->getId();
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getValueMap(): stdClass
|
||||
{
|
||||
$attachmentsIds = [];
|
||||
$attachmentsNames = (object) [];
|
||||
|
||||
foreach ($this->attachmentList as $attachment) {
|
||||
$id = $attachment->getId();
|
||||
|
||||
$attachmentsIds[] = $id;
|
||||
$attachmentsNames->$id = $attachment->get(Field::NAME);
|
||||
}
|
||||
|
||||
return (object) [
|
||||
'subject' => $this->subject,
|
||||
'body' => $this->body,
|
||||
'isHtml' => $this->isHtml,
|
||||
'attachmentsIds' => $attachmentsIds,
|
||||
'attachmentsNames' => $attachmentsNames,
|
||||
];
|
||||
}
|
||||
}
|
||||
80
application/Espo/Tools/EmailTemplate/Service.php
Normal file
80
application/Espo/Tools/EmailTemplate/Service.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://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 Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\EmailTemplate;
|
||||
|
||||
use Espo\Core\Acl;
|
||||
use Espo\Core\Exceptions\ForbiddenSilent;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\Entities\EmailTemplate;
|
||||
use Espo\Entities\User;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
class Service
|
||||
{
|
||||
public function __construct(
|
||||
private Processor $processor,
|
||||
private User $user,
|
||||
private Acl $acl,
|
||||
private EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Prepare an email data with an applied template.
|
||||
*
|
||||
* @throws NotFound
|
||||
* @throws ForbiddenSilent
|
||||
*/
|
||||
public function process(string $emailTemplateId, Data $data, ?Params $params = null): Result
|
||||
{
|
||||
/** @var ?EmailTemplate $emailTemplate */
|
||||
$emailTemplate = $this->entityManager->getEntityById(EmailTemplate::ENTITY_TYPE, $emailTemplateId);
|
||||
|
||||
if (!$emailTemplate) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
$params ??= Params::create()
|
||||
->withApplyAcl(true)
|
||||
->withCopyAttachments(true);
|
||||
|
||||
if (
|
||||
$params->applyAcl() &&
|
||||
!$this->acl->checkEntityRead($emailTemplate)
|
||||
) {
|
||||
throw new ForbiddenSilent();
|
||||
}
|
||||
|
||||
if (!$data->getUser()) {
|
||||
$data = $data->withUser($this->user);
|
||||
}
|
||||
|
||||
return $this->processor->process($emailTemplate, $params, $data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user