<?php declare(strict_types=1);
namespace ShopStudio\Blog\Storefront\Controller;
use ShopStudio\Blog\Content\BlogPost\Exception\BlogPostCommentsDisabledException;
use ShopStudio\Blog\Content\BlogPost\SalesChannel\Comment\AbstractBlogPostCommentSaveRoute;
use ShopStudio\Blog\Storefront\Page\BlogPost\BlogPostPageLoader;
use ShopStudio\Blog\Storefront\Page\BlogPost\Comment\BlogPostCommentLoader;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @since 1.0.0
*
* @RouteScope(scopes={"storefront"})
*/
class BlogPostController extends StorefrontController
{
/**
* @since 1.0.0
*/
private BlogPostPageLoader $blogPostPageLoader;
/**
* @since 1.0.0
*/
private AbstractBlogPostCommentSaveRoute $blogPostCommentSaveRoute;
/**
* @since 1.0.0
*/
private BlogPostCommentLoader $blogPostCommentLoader;
/**
* @since 1.0.0
*/
private SystemConfigService $systemConfigService;
/**
* @since 1.0.0
*/
public function __construct(
BlogPostPageLoader $blogPostPageLoader,
AbstractBlogPostCommentSaveRoute $blogPostCommentSaveRoute,
BlogPostCommentLoader $blogPostCommentLoader,
SystemConfigService $systemConfigService
) {
$this->blogPostPageLoader = $blogPostPageLoader;
$this->blogPostCommentSaveRoute = $blogPostCommentSaveRoute;
$this->blogPostCommentLoader = $blogPostCommentLoader;
$this->systemConfigService = $systemConfigService;
}
/**
* @since 1.0.0
*
* @HttpCache()
*
* @Route("/blog/detail/{blogPostId}", name="shop-studio-blog.frontend.blog-post.detail.page", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
*/
public function index(SalesChannelContext $context, Request $request): Response
{
$page = $this->blogPostPageLoader->load($request, $context);
$commentSuccess = $request->get('success');
return $this->renderStorefront('@Storefront/storefront/page/shop-studio-blog-blog-post-detail/index.html.twig', [
'page' => $page,
'commentSuccess' => $commentSuccess,
]);
}
/**
* @since 1.0.0
*
* @Route("/blog/detail/{blogPostId}/comments", name="shop-studio-blog.frontend.blog-post.detail.comments", methods={"GET","POST"}, defaults={"XmlHttpRequest"=true})
*/
public function loadComments(Request $request, SalesChannelContext $context): Response
{
$this->checkCommentsDisabled($context);
$comments = $this->blogPostCommentLoader->load($request, $context);
$commentSuccess = $request->get('success');
return $this->renderStorefront('@Storefront/storefront/page/shop-studio-blog-blog-post-detail/comments/index.html.twig', [
'comments' => $comments,
'commentSuccess' => $commentSuccess,
]);
}
/**
* @since 1.0.0
*
* @Route("/blog/detail/{blogPostId}/comment", name="shop-studio-blog.frontend.blog-post.detail.comments.save", methods={"POST"}, defaults={"XmlHttpRequest"=true})
*/
public function saveComments(string $blogPostId, RequestDataBag $data, SalesChannelContext $context): Response
{
$this->checkCommentsDisabled($context);
try {
$this->blogPostCommentSaveRoute->save($blogPostId, $data, $context);
} catch (ConstraintViolationException $formViolations) {
return $this->forwardToRoute(
'shop-studio-blog.frontend.blog-post.detail.comments',
[
'blogPostId' => $blogPostId,
'success' => -1,
'formViolations' => $formViolations,
'data' => $data,
],
[
'blogPostId' => $blogPostId,
]
);
}
$forwardParams = [
'blogPostId' => $blogPostId,
'success' => 1,
'data' => $data,
];
if ($data->has('id')) {
$forwardParams['success'] = 2;
}
return $this->forwardToRoute(
'shop-studio-blog.frontend.blog-post.detail.comments',
$forwardParams,
[
'blogPostId' => $blogPostId,
]
);
}
/**
* @since 2.0.0
*/
private function checkCommentsDisabled(SalesChannelContext $context): void
{
$commentsDisabled = $this->systemConfigService->getBool('ShopStudioBlog.config.commentsDisabled', $context->getSalesChannel()->getId());
if ($commentsDisabled) {
throw new BlogPostCommentsDisabledException();
}
}
}