custom/plugins/CioCustomerMailbox/src/Subscriber/ShowUnreadMessagesModalSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. namespace CioCustomerMailbox\Subscriber;
  3. use CioCustomerMailbox\Service\MailboxConfigService;
  4. use CioImports\Event\ImportStockMissingProductNotificationEvent;
  5. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerDeletedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  14. use Shopware\Core\Framework\Event\BusinessEvent;
  15. use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
  16. use Shopware\Core\Framework\Event\BusinessEventDefinition;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Shopware\Storefront\Controller\AuthController;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  22. use Symfony\Component\HttpKernel\Event\RequestEvent;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. use Shopware\Storefront\Event\StorefrontRenderEvent;
  25. class ShowUnreadMessagesModalSubscriber implements EventSubscriberInterface
  26. {
  27.     CONST WHITELIST_ROUTES = [
  28.         '/account/login',
  29.         '/widgets/checkout/info',
  30.         '/account/recover',
  31.         '/account/recover/password',
  32.         '/checkout/offcanvas',
  33.         '/cookie/offcanvas',
  34.         '/impressum',
  35.         '/account/mailbox'
  36.     ];
  37.     protected EntityRepository $messagesRepository;
  38.     protected MailboxConfigService $mailboxConfigService;
  39.     public function __construct(EntityRepository $messagesRepositoryMailboxConfigService $mailboxConfigService)
  40.     {
  41.         $this->messagesRepository $messagesRepository;
  42.         $this->mailboxConfigService $mailboxConfigService;
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             StorefrontRenderEvent::class => 'onStorefrontRenderEvent'
  48.         ];
  49.     }
  50.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  51.     {
  52.         if($event->getSalesChannelContext()->getSalesChannel()->isMaintenance()) {
  53.             return;
  54.         }
  55.         if ($event->getSalesChannelContext()->getCustomer() instanceof CustomerEntity) {
  56.             $criteria = new Criteria();
  57.             // only messages for the current customer
  58.             $criteria->addFilter(new EqualsFilter('customer_id'$event->getSalesChannelContext()->getCustomer()->getId()));
  59.             // only messages that are not read
  60.             $criteria->addFilter(new EqualsFilter('read_at'null));
  61.             // only messages that are published at the moment
  62.             $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  63.                 new EqualsFilter('message_content.visible_from'null),
  64.                 new RangeFilter('message_content.visible_from', [
  65.                     RangeFilter::LTE => (new \DateTime())->setTimezone(new \DateTimeZone('Europe/Berlin'))->format('Y-m-d H:i:s')
  66.                 ])
  67.             ]));
  68.             $count $this->messagesRepository->search($criteria$event->getSalesChannelContext()->getContext())->count();
  69.             if ($count 0) {
  70.                 $event->setParameter('unreadMessagesCount'$count);
  71.             }
  72.         }
  73.         if (in_array($event->getRequest()->getPathInfo(), self::WHITELIST_ROUTES)) {
  74.             return;
  75.         }
  76.         if ($event->getRequest()->getSession()->has('mailbox_remind_later')) {
  77.             $remindLater $event->getRequest()->getSession()->get('mailbox_remind_later');
  78.             if ($remindLater instanceof \DateTime && $remindLater > new \DateTime()) {
  79.                 return;
  80.             }
  81.         }
  82.         if ($event->getSalesChannelContext()->getCustomer() instanceof CustomerEntity) {
  83.             $criteria = new Criteria();
  84.             // only messages for the current customer
  85.             $criteria->addFilter(new EqualsFilter('customer_id'$event->getSalesChannelContext()->getCustomer()->getId()));
  86.             // only messages that are published at the moment and older than x days
  87.             $criteria->addFilter(
  88.                 new RangeFilter(
  89.                     'message_content.visible_from',
  90.                     [
  91.                         'lte' => (new \DateTime())->setTimezone(new \DateTimeZone('Europe/Berlin'))->modify('-' $this->mailboxConfigService->getShowModalAfterDays() . ' days')->format('Y-m-d H:i:s')
  92.                     ]
  93.                 )
  94.             );
  95.             // only messages that are not read
  96.             $criteria->addFilter(new EqualsFilter('read_at'null));
  97.             // only messages that are important
  98.             $criteria->addFilter(new EqualsFilter('message_content.important'true));
  99.             $count $this->messagesRepository->search($criteria$event->getSalesChannelContext()->getContext())->count();
  100.             if ($count 0) {
  101.                 $event->setParameter('showUnreadMessagesModal'true);
  102.             }
  103.         }
  104.     }
  105. }