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'];
  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 $notifyEmail
  104.      * @param \Pimcore\Model\Workflow $workflow
  105.      * @param AbstractElement $subject
  106.      * @param string $mailType
  107.      * @param string $mailPath
  108.      */
  109.     private function handleNotifyPostWorkflowEmail(Transition $transition, \Symfony\Component\Workflow\Workflow $workflowAbstractElement $subjectstring $mailTypestring $mailPath, array $notifyUsers, array $notifyRoles)
  110.     {
  111.         //notify users
  112.         $subjectType = (Service::getType($subject) == 'object' $subject->getClassName() : Service::getType($subject));
  113.         $this->mailService->sendWorkflowEmailNotification(
  114.             $notifyUsers,
  115.             $notifyRoles,
  116.             $workflow,
  117.             $subjectType,
  118.             $subject,
  119.             $transition->getLabel(),
  120.             $mailType,
  121.             $mailPath
  122.         );
  123.     }
  124.     private function handleNotifyPostWorkflowPimcoreNotification(Transition $transition, \Symfony\Component\Workflow\Workflow $workflowAbstractElement $subject, array $notifyUsers, array $notifyRoles)
  125.     {
  126.         $subjectType = (Service::getType($subject) == 'object' $subject->getClassName() : Service::getType($subject));
  127.         $this->pimcoreNotificationService->sendPimcoreNotification(
  128.             $notifyUsers,
  129.             $notifyRoles,
  130.             $workflow,
  131.             $subjectType,
  132.             $subject,
  133.             $transition->getLabel()
  134.         );
  135.     }
  136.     /**
  137.      * check's if the event subscriber should be executed
  138.      *
  139.      * @param Event $event
  140.      *
  141.      * @return bool
  142.      */
  143.     private function checkEvent(Event $event): bool
  144.     {
  145.         return $this->isEnabled()
  146.             && $event->getTransition() instanceof Transition
  147.             && $event->getSubject() instanceof AbstractElement;
  148.     }
  149.     /**
  150.      * @return bool
  151.      */
  152.     public function isEnabled(): bool
  153.     {
  154.         return $this->enabled;
  155.     }
  156.     /**
  157.      * @param bool $enabled
  158.      */
  159.     public function setEnabled(bool $enabled): void
  160.     {
  161.         $this->enabled $enabled;
  162.     }
  163.     public static function getSubscribedEvents()
  164.     {
  165.         return [
  166.             'workflow.completed' => ['onWorkflowCompleted'0]
  167.         ];
  168.     }
  169. }