<?php declare(strict_types=1);
namespace Acris\Faq\Storefront\Controller;
use Acris\Faq\Components\Faq\Exception\FaqIdNotFoundException;
use Acris\Faq\Components\Faq\Exception\FaqNotFoundException;
use Acris\Faq\Core\Content\Cms\DataResolver\Element\DummyClasses\SalesChannelCmsPageDummyLoader;
use Acris\Faq\Custom\FaqDefinition;
use Acris\Faq\Storefront\Page\FaqDetail\FaqDetailPageLoader;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class FaqController extends StorefrontController
{
private FaqDetailPageLoader $faqDetailPageLoader;
private SalesChannelCmsPageDummyLoader $salesChannelCmsPageDummyLoader;
private FaqDefinition $faqDefinition;
public function __construct(
FaqDetailPageLoader $faqDetailPageLoader,
SalesChannelCmsPageDummyLoader $salesChannelCmsPageDummyLoader,
FaqDefinition $faqDefinition
)
{
$this->faqDetailPageLoader = $faqDetailPageLoader;
$this->salesChannelCmsPageDummyLoader = $salesChannelCmsPageDummyLoader;
$this->faqDefinition = $faqDefinition;
}
/**
* @Route("/faq/detail/{faqId}", name="frontend.faq.detail", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest": true})
*/
public function detail(string $faqId, Request $request, SalesChannelContext $salesChannelContext): Response
{
if (!$faqId) {
throw new FaqIdNotFoundException();
}
$page = $this->faqDetailPageLoader->load($faqId, $request, $salesChannelContext);
if($page->getFaq()->getCmsPageId()){
$cmsPages = $this->salesChannelCmsPageDummyLoader->load(
$request,
new Criteria([$page->getFaq()->getCmsPageId()]),
$salesChannelContext,
null,
new EntityResolverContext($salesChannelContext, $request, $this->faqDefinition, $page->getFaq()));
if($cmsPages){
$page->getFaq()->setCmsPage($cmsPages->first());
}
}
if(!empty($page)){
return $this->renderStorefront('@Storefront/storefront/page/faq-detail/acris-faq-detail.html.twig', ['page' => $page]);
}
throw new FaqNotFoundException();
}
}