vendor/pimcore/pimcore/lib/Workflow/EventSubscriber/NotificationEmailSubscriber.php line 80

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Workflow\EventSubscriber;
  15. use Pimcore\Model\Element\AbstractElement;
  16. use Pimcore\Model\Element\Service;
  17. use Pimcore\Model\Element\ValidationException;
  18. use Pimcore\Workflow;
  19. use Pimcore\Workflow\NotificationEmail\NotificationEmailService;
  20. use Pimcore\Workflow\Transition;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\Translation\TranslatorInterface;
  23. use Symfony\Component\Workflow\Event\Event;
  24. class NotificationEmailSubscriber implements EventSubscriberInterface
  25. {
  26.     const MAIL_TYPE_TEMPLATE 'template';
  27.     const MAIL_TYPE_DOCUMENT 'pimcore_document';
  28.     const DEFAULT_MAIL_TEMPLATE_PATH '@PimcoreCore/Workflow/NotificationEmail/notificationEmail.html.twig';
  29.     /**
  30.      * @var TranslatorInterface
  31.      */
  32.     private $mailService;
  33.     /**
  34.      * @var TranslatorInterface
  35.      */
  36.     private $translator;
  37.     /**
  38.      * @var bool
  39.      */
  40.     private $enabled true;
  41.     /**
  42.      * @var Workflow\ExpressionService
  43.      */
  44.     private $expressionService;
  45.     /**
  46.      * @var Workflow\Manager
  47.      */
  48.     private $workflowManager;
  49.     /**
  50.      * NotificationEmailSubscriber constructor.
  51.      *
  52.      * @param NotificationEmailService $mailService
  53.      * @param TranslatorInterface $translator
  54.      * @param Workflow\ExpressionService $expressionService
  55.      * @param Workflow\Manager $manager
  56.      */
  57.     public function __construct(NotificationEmailService $mailServiceTranslatorInterface $translatorWorkflow\ExpressionService $expressionServiceWorkflow\Manager $manager)
  58.     {
  59.         $this->mailService $mailService;
  60.         $this->translator $translator;
  61.         $this->expressionService $expressionService;
  62.         $this->workflowManager $manager;
  63.     }
  64.     /**
  65.      * @param Event $event
  66.      *
  67.      * @throws ValidationException
  68.      */
  69.     public function onWorkflowCompleted(Event $event)
  70.     {
  71.         if (!$this->checkEvent($event)) {
  72.             return;
  73.         }
  74.         /**
  75.          * @var AbstractElement $subject
  76.          * @var Transition $transition
  77.          */
  78.         $subject $event->getSubject();
  79.         $transition $event->getTransition();
  80.         $workflow $this->workflowManager->getWorkflowByName($event->getWorkflowName());
  81.         $notificationSettings $transition->getNotificationSettings();
  82.         foreach ($notificationSettings as $notificationSetting) {
  83.             $condition $notificationSetting['condition'];
  84.             if (empty($condition) || $this->expressionService->evaluateExpression($workflow$subject$condition)) {
  85.                 $notifyUsers $notificationSetting['notifyUsers'] ?? [];
  86.                 $notifyRoles $notificationSetting['notifyRoles'] ?? [];
  87.                 $this->handleNotifyPostWorkflow($transition$workflow$subject$notificationSetting['mailType'], $notificationSetting['mailPath'], $notifyUsers$notifyRoles);
  88.             }
  89.         }
  90.     }
  91.     /**
  92.      * @param Transition $notifyEmail
  93.      * @param \Pimcore\Model\Workflow $workflow
  94.      * @param AbstractElement $subject
  95.      * @param string $mailType
  96.      * @param string $mailPath
  97.      */
  98.     private function handleNotifyPostWorkflow(Transition $transition, \Symfony\Component\Workflow\Workflow $workflowAbstractElement $subjectstring $mailTypestring $mailPath, array $notifyUsers, array $notifyRoles)
  99.     {
  100.         //notify users
  101.         $subjectType = (Service::getType($subject) == 'object' $subject->getClassName() : Service::getType($subject));
  102.         $this->mailService->sendWorkflowEmailNotification(
  103.             $notifyUsers,
  104.             $notifyRoles,
  105.             $workflow,
  106.             $subjectType,
  107.             $subject,
  108.             $transition->getLabel(),
  109.             $mailType,
  110.             $mailPath
  111.         );
  112.     }
  113.     /**
  114.      * check's if the event subscriber should be executed
  115.      *
  116.      * @param Event $event
  117.      *
  118.      * @return bool
  119.      */
  120.     private function checkEvent(Event $event): bool
  121.     {
  122.         return $this->isEnabled()
  123.             && $event->getTransition() instanceof Transition
  124.             && $event->getSubject() instanceof AbstractElement;
  125.     }
  126.     /**
  127.      * @return bool
  128.      */
  129.     public function isEnabled(): bool
  130.     {
  131.         return $this->enabled;
  132.     }
  133.     /**
  134.      * @param bool $enabled
  135.      */
  136.     public function setEnabled(bool $enabled): void
  137.     {
  138.         $this->enabled $enabled;
  139.     }
  140.     public static function getSubscribedEvents()
  141.     {
  142.         return [
  143.             'workflow.completed' => ['onWorkflowCompleted'0]
  144.         ];
  145.     }
  146. }