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

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