vendor/pimcore/pimcore/lib/Workflow/EventSubscriber/NotificationSubscriber.php line 88

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\Notification\NotificationEmailService;
  20. use Pimcore\Workflow\Transition;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\Workflow\Event\Event;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. class NotificationSubscriber implements EventSubscriberInterface
  25. {
  26.     const MAIL_TYPE_TEMPLATE 'template';
  27.     const MAIL_TYPE_DOCUMENT 'pimcore_document';
  28.     const NOTIFICATION_CHANNEL_MAIL 'mail';
  29.     const NOTIFICATION_CHANNEL_PIMCORE_NOTIFICATION 'pimcore_notification';
  30.     const DEFAULT_MAIL_TEMPLATE_PATH '@PimcoreCore/Workflow/NotificationEmail/notificationEmail.html.twig';
  31.     /**
  32.      * @var NotificationEmailService
  33.      */
  34.     protected $mailService;
  35.     /**
  36.      * @var Workflow\Notification\PimcoreNotificationService
  37.      */
  38.     protected $pimcoreNotificationService;
  39.     /**
  40.      * @var TranslatorInterface
  41.      */
  42.     protected $translator;
  43.     /**
  44.      * @var bool
  45.      */
  46.     protected $enabled true;
  47.     /**
  48.      * @var Workflow\ExpressionService
  49.      */
  50.     protected $expressionService;
  51.     /**
  52.      * @var Workflow\Manager
  53.      */
  54.     protected $workflowManager;
  55.     /**
  56.      * @param NotificationEmailService $mailService
  57.      * @param Workflow\Notification\PimcoreNotificationService $pimcoreNotificationService
  58.      * @param TranslatorInterface $translator
  59.      * @param Workflow\ExpressionService $expressionService
  60.      * @param Workflow\Manager $workflowManager
  61.      */
  62.     public function __construct(NotificationEmailService $mailServiceWorkflow\Notification\PimcoreNotificationService $pimcoreNotificationServiceTranslatorInterface $translatorWorkflow\ExpressionService $expressionServiceWorkflow\Manager $workflowManager)
  63.     {
  64.         $this->mailService $mailService;
  65.         $this->pimcoreNotificationService $pimcoreNotificationService;
  66.         $this->translator $translator;
  67.         $this->expressionService $expressionService;
  68.         $this->workflowManager $workflowManager;
  69.     }
  70.     /**
  71.      * @param Event $event
  72.      *
  73.      * @throws ValidationException
  74.      */
  75.     public function onWorkflowCompleted(Event $event)
  76.     {
  77.         if (!$this->checkEvent($event)) {
  78.             return;
  79.         }
  80.         /**
  81.          * @var AbstractElement $subject
  82.          * @var Transition $transition
  83.          */
  84.         $subject $event->getSubject();
  85.         $transition $event->getTransition();
  86.         $workflow $this->workflowManager->getWorkflowByName($event->getWorkflowName());
  87.         $notificationSettings $transition->getNotificationSettings();
  88.         foreach ($notificationSettings as $notificationSetting) {
  89.             $condition $notificationSetting['condition'] ?? null;
  90.             if (empty($condition) || $this->expressionService->evaluateExpression($workflow$subject$condition)) {
  91.                 $notifyUsers $notificationSetting['notifyUsers'] ?? [];
  92.                 $notifyRoles $notificationSetting['notifyRoles'] ?? [];
  93.                 if (in_array(self::NOTIFICATION_CHANNEL_MAIL$notificationSetting['channelType'])) {
  94.                     $this->handleNotifyPostWorkflowEmail($transition$workflow$subject$notificationSetting['mailType'], $notificationSetting['mailPath'], $notifyUsers$notifyRoles);
  95.                 }
  96.                 if (in_array(self::NOTIFICATION_CHANNEL_PIMCORE_NOTIFICATION$notificationSetting['channelType'])) {
  97.                     $this->handleNotifyPostWorkflowPimcoreNotification($transition$workflow$subject$notifyUsers$notifyRoles);
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     /**
  103.      * @param Transition $transition
  104.      * @param \Symfony\Component\Workflow\Workflow $workflow
  105.      * @param AbstractElement $subject
  106.      * @param string $mailType
  107.      * @param string $mailPath
  108.      * @param array $notifyUsers
  109.      * @param array $notifyRoles
  110.      */
  111.     private function handleNotifyPostWorkflowEmail(Transition $transition, \Symfony\Component\Workflow\Workflow $workflowAbstractElement $subjectstring $mailTypestring $mailPath, array $notifyUsers, array $notifyRoles)
  112.     {
  113.         //notify users
  114.         $subjectType = (Service::getType($subject) == 'object' $subject->getClassName() : Service::getType($subject));
  115.         $this->mailService->sendWorkflowEmailNotification(
  116.             $notifyUsers,
  117.             $notifyRoles,
  118.             $workflow,
  119.             $subjectType,
  120.             $subject,
  121.             $transition->getLabel(),
  122.             $mailType,
  123.             $mailPath
  124.         );
  125.     }
  126.     /**
  127.      * @param Transition $transition
  128.      * @param \Symfony\Component\Workflow\Workflow $workflow
  129.      * @param AbstractElement $subject
  130.      * @param array $notifyUsers
  131.      * @param array $notifyRoles
  132.      */
  133.     private function handleNotifyPostWorkflowPimcoreNotification(Transition $transition, \Symfony\Component\Workflow\Workflow $workflowAbstractElement $subject, array $notifyUsers, array $notifyRoles)
  134.     {
  135.         $subjectType = (Service::getType($subject) == 'object' $subject->getClassName() : Service::getType($subject));
  136.         $this->pimcoreNotificationService->sendPimcoreNotification(
  137.             $notifyUsers,
  138.             $notifyRoles,
  139.             $workflow,
  140.             $subjectType,
  141.             $subject,
  142.             $transition->getLabel()
  143.         );
  144.     }
  145.     /**
  146.      * check's if the event subscriber should be executed
  147.      *
  148.      * @param Event $event
  149.      *
  150.      * @return bool
  151.      */
  152.     private function checkEvent(Event $event): bool
  153.     {
  154.         return $this->isEnabled()
  155.             && $event->getTransition() instanceof Transition
  156.             && $event->getSubject() instanceof AbstractElement;
  157.     }
  158.     /**
  159.      * @return bool
  160.      */
  161.     public function isEnabled(): bool
  162.     {
  163.         return $this->enabled;
  164.     }
  165.     /**
  166.      * @param bool $enabled
  167.      */
  168.     public function setEnabled(bool $enabled): void
  169.     {
  170.         $this->enabled $enabled;
  171.     }
  172.     public static function getSubscribedEvents()
  173.     {
  174.         return [
  175.             'workflow.completed' => ['onWorkflowCompleted'0]
  176.         ];
  177.     }
  178. }