<?php
namespace CioCustomerMailbox\Subscriber;
use CioCustomerMailbox\Service\MailboxConfigService;
use CioImports\Event\ImportStockMissingProductNotificationEvent;
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\Event\CustomerDeletedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
use Shopware\Core\Framework\Event\BusinessEvent;
use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
use Shopware\Core\Framework\Event\BusinessEventDefinition;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Controller\AuthController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Shopware\Storefront\Event\StorefrontRenderEvent;
class ShowUnreadMessagesModalSubscriber implements EventSubscriberInterface
{
CONST WHITELIST_ROUTES = [
'/account/login',
'/widgets/checkout/info',
'/account/recover',
'/account/recover/password',
'/checkout/offcanvas',
'/cookie/offcanvas',
'/impressum',
'/account/mailbox'
];
protected EntityRepository $messagesRepository;
protected MailboxConfigService $mailboxConfigService;
public function __construct(EntityRepository $messagesRepository, MailboxConfigService $mailboxConfigService)
{
$this->messagesRepository = $messagesRepository;
$this->mailboxConfigService = $mailboxConfigService;
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onStorefrontRenderEvent'
];
}
public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
{
if($event->getSalesChannelContext()->getSalesChannel()->isMaintenance()) {
return;
}
if ($event->getSalesChannelContext()->getCustomer() instanceof CustomerEntity) {
$criteria = new Criteria();
// only messages for the current customer
$criteria->addFilter(new EqualsFilter('customer_id', $event->getSalesChannelContext()->getCustomer()->getId()));
// only messages that are not read
$criteria->addFilter(new EqualsFilter('read_at', null));
// only messages that are published at the moment
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('message_content.visible_from', null),
new RangeFilter('message_content.visible_from', [
RangeFilter::LTE => (new \DateTime())->setTimezone(new \DateTimeZone('Europe/Berlin'))->format('Y-m-d H:i:s')
])
]));
$count = $this->messagesRepository->search($criteria, $event->getSalesChannelContext()->getContext())->count();
if ($count > 0) {
$event->setParameter('unreadMessagesCount', $count);
}
}
if (in_array($event->getRequest()->getPathInfo(), self::WHITELIST_ROUTES)) {
return;
}
if ($event->getRequest()->getSession()->has('mailbox_remind_later')) {
$remindLater = $event->getRequest()->getSession()->get('mailbox_remind_later');
if ($remindLater instanceof \DateTime && $remindLater > new \DateTime()) {
return;
}
}
if ($event->getSalesChannelContext()->getCustomer() instanceof CustomerEntity) {
$criteria = new Criteria();
// only messages for the current customer
$criteria->addFilter(new EqualsFilter('customer_id', $event->getSalesChannelContext()->getCustomer()->getId()));
// only messages that are published at the moment and older than x days
$criteria->addFilter(
new RangeFilter(
'message_content.visible_from',
[
'lte' => (new \DateTime())->setTimezone(new \DateTimeZone('Europe/Berlin'))->modify('-' . $this->mailboxConfigService->getShowModalAfterDays() . ' days')->format('Y-m-d H:i:s')
]
)
);
// only messages that are not read
$criteria->addFilter(new EqualsFilter('read_at', null));
// only messages that are important
$criteria->addFilter(new EqualsFilter('message_content.important', true));
$count = $this->messagesRepository->search($criteria, $event->getSalesChannelContext()->getContext())->count();
if ($count > 0) {
$event->setParameter('showUnreadMessagesModal', true);
}
}
}
}