custom/plugins/Shopware-6-Blog-Plugin-1.5.8/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(['blogAuthor.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.         if ($metaInformation $page->getMetaInformation()) {
  95.             $metaTitle $entry->getMetaTitle() ?? $entry->getTitle();
  96.             $metaDescription $entry->getMetaDescription() ?? $entry->getTeaser();
  97.             $metaAuthor $entry->getBlogAuthor()->getTranslated()['name'];
  98.             $metaInformation->setMetaTitle($metaTitle ?? '');
  99.             $metaInformation->setMetaDescription($metaDescription ?? '');
  100.             $metaInformation->setAuthor($metaAuthor ?? '');
  101.             $page->setMetaInformation($metaInformation);
  102.         }
  103.         $page->setNavigationId($page->getHeader()->getNavigation()->getActive()->getId());
  104.         return $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', [
  105.             'page' => $page,
  106.             'entry' => $entry,
  107.         ]);
  108.     }
  109.     /**
  110.      * @HttpCache()
  111.      * @Route("/blog/rss", name="frontend.sas.blog.rss", methods={"GET"})
  112.      */
  113.     public function rss(Request $requestSalesChannelContext $context): Response
  114.     {
  115.         $criteria = new Criteria();
  116.         $dateTime = new \DateTime();
  117.         $criteria->addAssociations(['blogAuthor.salutation']);
  118.         $criteria->addFilter(
  119.             new EqualsFilter('active'true),
  120.             new RangeFilter('publishedAt', [RangeFilter::LTE => $dateTime->format(\DATE_ATOM)])
  121.         );
  122.         $results $this->blogRepository->search($criteria$context->getContext())->getEntities();
  123.         $page $this->genericPageLoader->load($request$context);
  124.         $page NavigationPage::createFrom($page);
  125.         $response $this->renderStorefront('@SasBlogModule/storefront/page/rss.html.twig', [
  126.             'results' => $results,
  127.             'page' => $page,
  128.         ]);
  129.         $response->headers->set('Content-Type''application/xml; charset=utf-8');
  130.         return $response;
  131.     }
  132. }