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

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