custom/plugins/ShopStudioBlog/src/Storefront/Controller/BlogPostController.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ShopStudio\Blog\Storefront\Controller;
  3. use ShopStudio\Blog\Content\BlogPost\Exception\BlogPostCommentsDisabledException;
  4. use ShopStudio\Blog\Content\BlogPost\SalesChannel\Comment\AbstractBlogPostCommentSaveRoute;
  5. use ShopStudio\Blog\Storefront\Page\BlogPost\BlogPostPageLoader;
  6. use ShopStudio\Blog\Storefront\Page\BlogPost\Comment\BlogPostCommentLoader;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  9. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Controller\StorefrontController;
  13. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. /**
  18.  * @since 1.0.0
  19.  *
  20.  * @RouteScope(scopes={"storefront"})
  21.  */
  22. class BlogPostController extends StorefrontController
  23. {
  24.     /**
  25.      * @since 1.0.0
  26.      */
  27.     private BlogPostPageLoader $blogPostPageLoader;
  28.     /**
  29.      * @since 1.0.0
  30.      */
  31.     private AbstractBlogPostCommentSaveRoute $blogPostCommentSaveRoute;
  32.     /**
  33.      * @since 1.0.0
  34.      */
  35.     private BlogPostCommentLoader $blogPostCommentLoader;
  36.     /**
  37.      * @since 1.0.0
  38.      */
  39.     private SystemConfigService $systemConfigService;
  40.     /**
  41.      * @since 1.0.0
  42.      */
  43.     public function __construct(
  44.         BlogPostPageLoader $blogPostPageLoader,
  45.         AbstractBlogPostCommentSaveRoute $blogPostCommentSaveRoute,
  46.         BlogPostCommentLoader $blogPostCommentLoader,
  47.         SystemConfigService $systemConfigService
  48.     ) {
  49.         $this->blogPostPageLoader $blogPostPageLoader;
  50.         $this->blogPostCommentSaveRoute $blogPostCommentSaveRoute;
  51.         $this->blogPostCommentLoader $blogPostCommentLoader;
  52.         $this->systemConfigService $systemConfigService;
  53.     }
  54.     /**
  55.      * @since 1.0.0
  56.      *
  57.      * @HttpCache()
  58.      *
  59.      * @Route("/blog/detail/{blogPostId}", name="shop-studio-blog.frontend.blog-post.detail.page", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  60.      */
  61.     public function index(SalesChannelContext $contextRequest $request): Response
  62.     {
  63.         $page $this->blogPostPageLoader->load($request$context);
  64.         $commentSuccess $request->get('success');
  65.         return $this->renderStorefront('@Storefront/storefront/page/shop-studio-blog-blog-post-detail/index.html.twig', [
  66.             'page' => $page,
  67.             'commentSuccess' => $commentSuccess,
  68.         ]);
  69.     }
  70.     /**
  71.      * @since 1.0.0
  72.      *
  73.      * @Route("/blog/detail/{blogPostId}/comments", name="shop-studio-blog.frontend.blog-post.detail.comments", methods={"GET","POST"}, defaults={"XmlHttpRequest"=true})
  74.      */
  75.     public function loadComments(Request $requestSalesChannelContext $context): Response
  76.     {
  77.         $this->checkCommentsDisabled($context);
  78.         $comments $this->blogPostCommentLoader->load($request$context);
  79.         $commentSuccess $request->get('success');
  80.         return $this->renderStorefront('@Storefront/storefront/page/shop-studio-blog-blog-post-detail/comments/index.html.twig', [
  81.             'comments' => $comments,
  82.             'commentSuccess' => $commentSuccess,
  83.         ]);
  84.     }
  85.     /**
  86.      * @since 1.0.0
  87.      *
  88.      * @Route("/blog/detail/{blogPostId}/comment", name="shop-studio-blog.frontend.blog-post.detail.comments.save", methods={"POST"}, defaults={"XmlHttpRequest"=true})
  89.      */
  90.     public function saveComments(string $blogPostIdRequestDataBag $dataSalesChannelContext $context): Response
  91.     {
  92.         $this->checkCommentsDisabled($context);
  93.         try {
  94.             $this->blogPostCommentSaveRoute->save($blogPostId$data$context);
  95.         } catch (ConstraintViolationException $formViolations) {
  96.             return $this->forwardToRoute(
  97.                 'shop-studio-blog.frontend.blog-post.detail.comments',
  98.                 [
  99.                     'blogPostId' => $blogPostId,
  100.                     'success' => -1,
  101.                     'formViolations' => $formViolations,
  102.                     'data' => $data,
  103.                 ],
  104.                 [
  105.                     'blogPostId' => $blogPostId,
  106.                 ]
  107.             );
  108.         }
  109.         $forwardParams = [
  110.             'blogPostId' => $blogPostId,
  111.             'success' => 1,
  112.             'data' => $data,
  113.         ];
  114.         if ($data->has('id')) {
  115.             $forwardParams['success'] = 2;
  116.         }
  117.         return $this->forwardToRoute(
  118.             'shop-studio-blog.frontend.blog-post.detail.comments',
  119.             $forwardParams,
  120.             [
  121.                 'blogPostId' => $blogPostId,
  122.             ]
  123.         );
  124.     }
  125.     /**
  126.      * @since 2.0.0
  127.      */
  128.     private function checkCommentsDisabled(SalesChannelContext $context): void
  129.     {
  130.         $commentsDisabled $this->systemConfigService->getBool('ShopStudioBlog.config.commentsDisabled'$context->getSalesChannel()->getId());
  131.         if ($commentsDisabled) {
  132.             throw new BlogPostCommentsDisabledException();
  133.         }
  134.     }
  135. }