From ab2d3eed43715f1533fd152b7e014ea54b98f949 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 13 Oct 2022 13:05:34 +0300 Subject: [PATCH] ref --- application/Espo/Core/ApplicationUser.php | 6 +++-- .../ExtGroup/EmailGroup/SendType.php | 27 +++++++++++++------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/application/Espo/Core/ApplicationUser.php b/application/Espo/Core/ApplicationUser.php index 954be84523..0d886e7dba 100644 --- a/application/Espo/Core/ApplicationUser.php +++ b/application/Espo/Core/ApplicationUser.php @@ -39,6 +39,8 @@ use RuntimeException; */ class ApplicationUser { + public const SYSTEM_USER_ID = 'system'; + private Container $container; private EntityManagerProxy $entityManagerProxy; @@ -53,14 +55,14 @@ class ApplicationUser */ public function setupSystemUser(): void { - $user = $this->entityManagerProxy->getEntity('User', 'system'); + $user = $this->entityManagerProxy->getEntityById(User::ENTITY_TYPE, self::SYSTEM_USER_ID); if (!$user) { throw new RuntimeException("System user is not found."); } $user->set('ipAddress', $_SERVER['REMOTE_ADDR'] ?? null); - $user->set('type', 'system'); + $user->set('type', User::TYPE_SYSTEM); $this->container->set('user', $user); } diff --git a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php index c0dbd7d89c..970cafc5a5 100644 --- a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php +++ b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php @@ -29,6 +29,9 @@ namespace Espo\Core\Formula\Functions\ExtGroup\EmailGroup; +use Espo\Core\ApplicationUser; +use Espo\Entities\Email; +use Exception; use Espo\Core\Formula\{ Functions\BaseFunction, ArgumentList, @@ -61,52 +64,60 @@ class SendType extends BaseFunction implements $em = $this->entityManager; - $email = $em->getEntity('Email', $id); + /** @var ?Email $email */ + $email = $em->getEntityById(Email::ENTITY_TYPE, $id); if (!$email) { $this->log("Email '{$id}' does not exist."); + return false; } - $status = $email->get('status'); + $status = $email->getStatus(); - if ($status && in_array($status, ['Sent'])) { + if ($status === Email::STATUS_SENT) { $this->log("Can't send email that has 'Sent' status."); + return false; } /** @var \Espo\Services\Email $service */ - $service = $this->serviceFactory->create('Email'); + $service = $this->serviceFactory->create(Email::ENTITY_TYPE); $service->loadAdditionalFields($email); $toSave = false; - if ($status !== 'Sending') { - $email->set('status', 'Sending'); + if ($status !== Email::STATUS_SENDING) { + $email->set('status', Email::STATUS_SENDING); + $toSave = true; } if (!$email->get('from')) { $from = $this->config->get('outboundEmailFromAddress'); + if ($from) { $email->set('from', $from); + $toSave = true; } } if ($toSave) { $em->saveEntity($email, [ - 'modifiedById' => 'system', + 'modifiedById' => ApplicationUser::SYSTEM_USER_ID, 'silent' => true, ]); } try { $service->sendEntity($email); - } catch (\Exception $e) { + } + catch (Exception $e) { $message = $e->getMessage(); $this->log("Error while sending. Message: {$message}." , 'error'); + return false; }