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

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Workflow\EventSubscriber;
  16. use Pimcore\Model\DataObject\Concrete;
  17. use Pimcore\Model\Element\ElementInterface;
  18. use Pimcore\Model\Element\Service;
  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.     protected NotificationEmailService $mailService;
  36.     protected Workflow\Notification\PimcoreNotificationService $pimcoreNotificationService;
  37.     protected TranslatorInterface $translator;
  38.     protected bool $enabled true;
  39.     protected Workflow\ExpressionService $expressionService;
  40.     protected Workflow\Manager $workflowManager;
  41.     public function __construct(NotificationEmailService $mailServiceWorkflow\Notification\PimcoreNotificationService $pimcoreNotificationServiceTranslatorInterface $translatorWorkflow\ExpressionService $expressionServiceWorkflow\Manager $workflowManager)
  42.     {
  43.         $this->mailService $mailService;
  44.         $this->pimcoreNotificationService $pimcoreNotificationService;
  45.         $this->translator $translator;
  46.         $this->expressionService $expressionService;
  47.         $this->workflowManager $workflowManager;
  48.     }
  49.     public function onWorkflowCompleted(Event $event): void
  50.     {
  51.         if (!$this->checkEvent($event)) {
  52.             return;
  53.         }
  54.         /** @var ElementInterface $subject */
  55.         $subject $event->getSubject();
  56.         /** @var Transition $transition */
  57.         $transition $event->getTransition();
  58.         $workflow $this->workflowManager->getWorkflowByName($event->getWorkflowName());
  59.         $notificationSettings $transition->getNotificationSettings();
  60.         foreach ($notificationSettings as $notificationSetting) {
  61.             $condition $notificationSetting['condition'] ?? null;
  62.             if (empty($condition) || $this->expressionService->evaluateExpression($workflow$subject$condition)) {
  63.                 $notifyUsers $notificationSetting['notifyUsers'] ?? [];
  64.                 $notifyRoles $notificationSetting['notifyRoles'] ?? [];
  65.                 if (in_array(self::NOTIFICATION_CHANNEL_MAIL$notificationSetting['channelType'])) {
  66.                     $this->handleNotifyPostWorkflowEmail($transition$workflow$subject$notificationSetting['mailType'], $notificationSetting['mailPath'], $notifyUsers$notifyRoles);
  67.                 }
  68.                 if (in_array(self::NOTIFICATION_CHANNEL_PIMCORE_NOTIFICATION$notificationSetting['channelType'])) {
  69.                     $this->handleNotifyPostWorkflowPimcoreNotification($transition$workflow$subject$notifyUsers$notifyRoles);
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     private function handleNotifyPostWorkflowEmail(Transition $transition\Symfony\Component\Workflow\Workflow $workflowElementInterface $subjectstring $mailTypestring $mailPath, array $notifyUsers, array $notifyRoles): void
  75.     {
  76.         //notify users
  77.         $subjectType = ($subject instanceof Concrete $subject->getClassName() : Service::getElementType($subject));
  78.         $this->mailService->sendWorkflowEmailNotification(
  79.             $notifyUsers,
  80.             $notifyRoles,
  81.             $workflow,
  82.             $subjectType,
  83.             $subject,
  84.             $transition->getLabel(),
  85.             $mailType,
  86.             $mailPath
  87.         );
  88.     }
  89.     private function handleNotifyPostWorkflowPimcoreNotification(Transition $transition\Symfony\Component\Workflow\Workflow $workflowElementInterface $subject, array $notifyUsers, array $notifyRoles): void
  90.     {
  91.         $subjectType = ($subject instanceof Concrete $subject->getClassName() : Service::getElementType($subject));
  92.         $this->pimcoreNotificationService->sendPimcoreNotification(
  93.             $notifyUsers,
  94.             $notifyRoles,
  95.             $workflow,
  96.             $subjectType,
  97.             $subject,
  98.             $transition->getLabel()
  99.         );
  100.     }
  101.     /**
  102.      * check's if the event subscriber should be executed
  103.      */
  104.     private function checkEvent(Event $event): bool
  105.     {
  106.         return $this->isEnabled()
  107.             && $event->getTransition() instanceof Transition
  108.             && $event->getSubject() instanceof ElementInterface;
  109.     }
  110.     public function isEnabled(): bool
  111.     {
  112.         return $this->enabled;
  113.     }
  114.     public function setEnabled(bool $enabled): void
  115.     {
  116.         $this->enabled $enabled;
  117.     }
  118.     public static function getSubscribedEvents(): array
  119.     {
  120.         return [
  121.             'workflow.completed' => ['onWorkflowCompleted'0],
  122.         ];
  123.     }
  124. }