custom/plugins/SasBlogModule/src/Controller/BlogController.php line 91

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sas\BlogModule\Controller;
  3. use Sas\BlogModule\Content\Blog\BlogEntriesEntity;
  4. use Sas\BlogModule\Page\Search\BlogSearchPageLoader;
  5. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  6. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  11. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  12. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Storefront\Controller\StorefrontController;
  16. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  17. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  18. use Shopware\Storefront\Page\Navigation\NavigationPage;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. /**
  23.  * @RouteScope(scopes={"storefront"})
  24.  */
  25. class BlogController extends StorefrontController
  26. {
  27.     private GenericPageLoaderInterface $genericPageLoader;
  28.     private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
  29.     private SystemConfigService $systemConfigService;
  30.     private EntityRepositoryInterface $blogRepository;
  31.     private BlogSearchPageLoader $blogSearchPageLoader;
  32.     public function __construct(
  33.         SystemConfigService $systemConfigService,
  34.         GenericPageLoaderInterface $genericPageLoader,
  35.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  36.         EntityRepositoryInterface $blogRepository,
  37.         BlogSearchPageLoader $blogSearchPageLoader
  38.     ) {
  39.         $this->systemConfigService $systemConfigService;
  40.         $this->genericPageLoader $genericPageLoader;
  41.         $this->cmsPageLoader $cmsPageLoader;
  42.         $this->blogRepository $blogRepository;
  43.         $this->blogSearchPageLoader $blogSearchPageLoader;
  44.     }
  45.     /**
  46.      * @HttpCache()
  47.      * @Route("/sas_blog/search", name="sas.frontend.blog.search", methods={"GET"})
  48.      */
  49.     public function search(Request $requestSalesChannelContext $context): Response
  50.     {
  51.         try {
  52.             $page $this->blogSearchPageLoader->load($request$context);
  53.         } catch (MissingRequestParameterException $missingRequestParameterException) {
  54.             return $this->forwardToRoute('frontend.home.page');
  55.         }
  56.         return $this->renderStorefront('@Storefront/storefront/page/blog-search/index.html.twig', ['page' => $page]);
  57.     }
  58.     /**
  59.      * @HttpCache()
  60.      * @Route("/widgets/blog-search", name="widgets.blog.search.pagelet", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  61.      *
  62.      * @throws MissingRequestParameterException
  63.      */
  64.     public function ajax(Request $requestSalesChannelContext $context): Response
  65.     {
  66.         $request->request->set('no-aggregations'true);
  67.         $page $this->blogSearchPageLoader->load($request$context);
  68.         $response $this->renderStorefront('@Storefront/storefront/page/blog-search/search-pagelet.html.twig', ['page' => $page]);
  69.         $response->headers->set('x-robots-tag''noindex');
  70.         return $response;
  71.     }
  72.     /**
  73.      * @HttpCache()
  74.      * @Route("/sas_blog/{articleId}", name="sas.frontend.blog.detail", methods={"GET"})
  75.      */
  76.     public function detailAction(string $articleIdRequest $requestSalesChannelContext $context): Response
  77.     {
  78.         $page $this->genericPageLoader->load($request$context);
  79.         $page NavigationPage::createFrom($page);
  80.         $criteria = new Criteria([$articleId]);
  81.         $criteria->addAssociations(['author.salutation''blogCategories']);
  82.         $results $this->blogRepository->search($criteria$context->getContext())->getEntities();
  83.         /** @var BlogEntriesEntity $entry */
  84.         $entry $results->first();
  85.         if (!$entry) {
  86.             throw new PageNotFoundException($articleId);
  87.         }
  88.         $pages $this->cmsPageLoader->load(
  89.             $request,
  90.             new Criteria([$this->systemConfigService->get('SasBlogModule.config.cmsBlogDetailPage')]),
  91.             $context
  92.         );
  93.         $page->setCmsPage($pages->first());
  94.         $metaInformation $page->getMetaInformation();
  95.         $metaInformation->setAuthor($entry->getAuthor()->getTranslated()['name']);
  96.         $page->setMetaInformation($metaInformation);
  97.         $page->setNavigationId($page->getHeader()->getNavigation()->getActive()->getId());
  98.         return $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', [
  99.             'page' => $page,
  100.             'entry' => $entry,
  101.         ]);
  102.     }
  103.     /**
  104.      * @HttpCache()
  105.      * @Route("/blog/rss", name="frontend.sas.blog.rss", methods={"GET"})
  106.      */
  107.     public function rss(Request $requestSalesChannelContext $context): Response
  108.     {
  109.         $criteria = new Criteria();
  110.         $dateTime = new \DateTime();
  111.         $criteria->addAssociations(['author.salutation']);
  112.         $criteria->addFilter(
  113.             new EqualsFilter('active'true),
  114.             new RangeFilter('publishedAt', [RangeFilter::LTE => $dateTime->format(\DATE_ATOM)])
  115.         );
  116.         $results $this->blogRepository->search($criteria$context->getContext())->getEntities();
  117.         $page $this->genericPageLoader->load($request$context);
  118.         $page NavigationPage::createFrom($page);
  119.         $response $this->renderStorefront('@SasBlogModule/storefront/page/rss.html.twig', [
  120.             'results' => $results,
  121.             'page' => $page,
  122.         ]);
  123.         $response->headers->set('Content-Type''application/xml; charset=utf-8');
  124.         return $response;
  125.     }
  126. }