custom/plugins/HatslogicSwProductsAndLayoutsScheduler/src/Subscriber/CmsPageScheduler.php line 196

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * (c) 2Hats Logic Solutions <info@2hatslogic.com>
  4.  */
  5. namespace Hatslogic\Sw\ProductsAndLayoutsScheduler\Subscriber;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Doctrine\DBAL\Connection;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Shopware\Core\PlatformRequest;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Shopware\Storefront\Event\StorefrontRenderEvent;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  19. use Shopware\Core\Framework\Context;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  21. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  22. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  23. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  24. use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
  25. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  26. use Shopware\Core\Content\Category\CategoryEntity;
  27. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  28. use Hatslogic\Sw\ProductsAndLayoutsScheduler\HatslogicSwProductsAndLayoutsScheduler;
  29. use Symfony\Component\HttpFoundation\Session\Session;
  30. class CmsPageScheduler implements EventSubscriberInterface
  31. {
  32.    /**
  33.      * @var ContainerInterface
  34.      */
  35.     protected $container;
  36.     /**
  37.      * @var Connection
  38.      */
  39.     private $connection;
  40.     /**
  41.      * @var RequestStack
  42.      */
  43.     private $requestStack;
  44.     /**
  45.      * @var RouterInterface
  46.      */
  47.     private $router;
  48.     /**
  49.      * @var SystemConfigService
  50.      */
  51.     private $systemConfigService;
  52.     /**
  53.      * @var EntityRepositoryInterface
  54.      */
  55.     private $cmsPageRepository;
  56.     /**
  57.      * @var EntityDefinition
  58.      */
  59.     private $categoryDefinition;
  60.     /**
  61.      * @var SalesChannelRepositoryInterface
  62.      */
  63.     private $categoryRepository;
  64.     /**
  65.      * @var SalesChannelCmsPageLoaderInterface
  66.      */
  67.     private $cmsPageLoader;
  68.     /**
  69.      * @var Session
  70.      */
  71.     private $session;
  72.     public function __construct(
  73.         ContainerInterface $container
  74.         Connection $connection,
  75.         RequestStack $requestStack,
  76.         RouterInterface $router,
  77.         SystemConfigService $systemConfigService,
  78.         EntityDefinition $categoryDefinition,
  79.         SalesChannelRepositoryInterface $categoryRepository,
  80.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  81.         EntityRepositoryInterface $cmsPageRepository,
  82.         Session $session
  83.     ) {
  84.         $this->container $container;
  85.         $this->connection $connection;
  86.         $this->requestStack $requestStack;
  87.         $this->router $router;
  88.         $this->systemConfigService $systemConfigService;
  89.         $this->cmsPageRepository $cmsPageRepository;
  90.         $this->categoryDefinition $categoryDefinition;
  91.         $this->categoryRepository $categoryRepository;
  92.         $this->cmsPageLoader $cmsPageLoader;
  93.         $this->session $session;
  94.     }
  95.     public static function getSubscribedEvents(): array
  96.     {
  97.         return [
  98.             StorefrontRenderEvent::class => 'onStorefrontRender',
  99.             KernelEvents::REQUEST => 'onCmsPageLoaded',
  100.             NavigationPageLoadedEvent::class => 'onNavigationPageLoaded'
  101.         ];
  102.     }
  103.     private function loadCategory(string $categoryIdSalesChannelContext $context): CategoryEntity
  104.     {
  105.         $criteria = new Criteria([$categoryId]);
  106.         $criteria->addAssociation('media');
  107.         $category $this->categoryRepository->search($criteria$context)->get($categoryId);
  108.         return $category;
  109.     }
  110.     public function onNavigationPageLoaded(NavigationPageLoadedEvent $event)
  111.     {
  112.         $master $this->requestStack->getMasterRequest();
  113.         $salesChannelId $master->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  114.         $route $master->attributes->get('_route');
  115.         if(($route == 'frontend.navigation.page' || $route == 'frontend.home.page') && $this->isLayoutSchedulerActive($salesChannelId))
  116.         {
  117.             $cmsPageDetails $event->getPage()->getCmsPage();
  118.             $customFields $cmsPageDetails->getCustomFields();
  119.             if(!empty($customFields))
  120.             {
  121.                 $cmsPageVisibilityStatus $this->getCmsPageVisibilityStatus($customFields);
  122.                 if(!$cmsPageVisibilityStatus) {
  123.                     $salesChannelContext $event->getSalesChannelContext();
  124.                     $request $event->getRequest();
  125.                     $defaultCategoryCmsPage $this->getDefaultCategoryCmsPage();
  126.                     $pageId $defaultCategoryCmsPage->getId();
  127.                     $navigationId $request->get('navigationId'$salesChannelContext->getSalesChannel()->getNavigationCategoryId());
  128.                     $category $this->loadCategory($navigationId$salesChannelContext);
  129.                     if ($pageId && !empty($category)) {
  130.                         $resolverContext = new EntityResolverContext($salesChannelContext$request$this->categoryDefinition$category);
  131.                         $pages $this->cmsPageLoader->load($request, new Criteria([$pageId]), $salesChannelContext$category->getTranslation('slotConfig'), $resolverContext);
  132.                         $event->getPage()->setCmsPage($pages->get($pageId));
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.         return;
  138.     }
  139.     private function getDefaultCategoryCmsPage()
  140.     {
  141.         $criteria = new Criteria();
  142.         $criteria->addFilter(new EqualsFilter('type''product_list'));
  143.         $criteria->addFilter(new EqualsFilter('locked'1));
  144.         $result $this->cmsPageRepository->search($criteriaContext::createDefaultContext());
  145.         $cmsPageDetails $result->first();
  146.         return ($cmsPageDetails) ? $cmsPageDetailsnull;
  147.     }
  148.     public function getCmsPageById($id)
  149.     {
  150.         $criteria = (new Criteria())
  151.             ->addFilter(new EqualsFilter(
  152.             'cms_page.id'$id));
  153.         $result $this->cmsPageRepository->search($criteriaContext::createDefaultContext());
  154.         $cmsPageDetails $result->first();
  155.         return ($cmsPageDetails) ? $cmsPageDetailsnull;
  156.     }
  157.     public function onCmsPageLoaded(RequestEvent $event)
  158.     {
  159.         $request $event->getRequest();
  160.         $attributes $request->attributes;
  161.         $route $attributes->get('_route');
  162.         if($route == 'frontend.cms.page')
  163.         {
  164.             $cmsPageId $attributes->get('id');
  165.             $salesChannelId $attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  166.             $xmlHttpRequest $attributes->get('XmlHttpRequest');
  167.             if($xmlHttpRequest !=&& $this->isLayoutSchedulerActive($salesChannelId))
  168.             {
  169.                 $cmsPageDetails $this->getCmsPageById($cmsPageId);
  170.                 if(empty($cmsPageDetails)) {
  171.                     return;
  172.                 }
  173.                 $customFields $cmsPageDetails->getCustomFields();
  174.                 if(!empty($customFields))
  175.                 {
  176.                     $cmsPageVisibilityStatus $this->getCmsPageVisibilityStatus($customFields);
  177.                     if(!$cmsPageVisibilityStatus)
  178.                     {
  179.                        $response = new RedirectResponse($this->router->generate('frontend.home.page'), 301);
  180.                         $event->setResponse($response);
  181.                         return;
  182.                     }
  183.                 }
  184.             }
  185.         }
  186.         return;
  187.     }
  188.     public function getCmsPageVisibilityStatus($customFields)
  189.     {
  190.         $pageVisible true;
  191.         if(!empty($customFields))
  192.         {
  193.             $currentDateTimeTimeStamp strtotime(date(HatslogicSwProductsAndLayoutsScheduler::SCHEDULER_DATE_TIME_FORMAT));
  194.             $publishedFrom = isset($customFields['layouts_scheduler_published_from']) ? $customFields['layouts_scheduler_published_from'] : '';
  195.             $publishedTo = isset($customFields['layouts_scheduler_published_to']) ? $customFields['layouts_scheduler_published_to'] : '';
  196.             $publishedFromTimeStamp = !empty($publishedFrom) ? strtotime($publishedFrom) : '';
  197.             $publishedToTimeStamp = !empty($publishedTo) ? strtotime($publishedTo) : '';
  198.             if(!empty($publishedFrom) || !empty($publishedTo)) {
  199.                 $pageVisible false;
  200.             }
  201.             if(!empty($publishedFrom) && !empty($publishedTo))
  202.             {
  203.                 if(($publishedFromTimeStamp <= $currentDateTimeTimeStamp) && ($publishedToTimeStamp >= $currentDateTimeTimeStamp))
  204.                 {
  205.                     $pageVisible true;
  206.                 }
  207.             } else if(!empty($publishedFrom)) {
  208.                 if($publishedFromTimeStamp <= $currentDateTimeTimeStamp)
  209.                 {
  210.                     $pageVisible true;
  211.                 }
  212.             } else if(!empty($publishedTo)) {
  213.                 if($publishedToTimeStamp >= $currentDateTimeTimeStamp)
  214.                 {
  215.                     $pageVisible true;
  216.                 }
  217.             }
  218.         } 
  219.         return $pageVisible;
  220.     }
  221.     public function onStorefrontRender(StorefrontRenderEvent $event)
  222.     {
  223.         $route $event->getRequest()->get('_route');
  224.         $xmlHttpRequest $event->getRequest()->get('XmlHttpRequest');
  225.         $cmsPageVisibilityStatus true;
  226.         $salesChannelId $event->getRequest()->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  227.         if($route == 'frontend.cms.page' && $xmlHttpRequest == && $this->isLayoutSchedulerActive($salesChannelId))
  228.         {
  229.             $parameters $event->getParameters();
  230.             $customFields $parameters['cmsPage']->get('customFields');
  231.             if(!empty($customFields))
  232.             {
  233.                 $cmsPageVisibilityStatus $this->getCmsPageVisibilityStatus($customFields);
  234.             }
  235.         }
  236.         $event->setParameter('cmsPageVisibilityStatus'$cmsPageVisibilityStatus);
  237.     }
  238.     public function isLayoutSchedulerActive($salesChannelId)
  239.     {
  240.         $islayoutSchedulerActive $this->systemConfigService->get('HatslogicSwProductsAndLayoutsScheduler.config.enableLayoutsScheduler'$salesChannelId);
  241.         return $islayoutSchedulerActive;
  242.     } 
  243. }