<?php
namespace Maxia\MaxiaVariantsTable6\Storefront\Controller;
use Maxia\MaxiaVariantsTable6\Core\Content\Product\SalesChannel\Detail\AbstractVariantsTableRoute;
use Maxia\MaxiaVariantsTable6\Service\ConfigService;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
use Shopware\Storefront\Framework\Routing\StorefrontResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class VariantsTableController extends StorefrontController
{
/** @var ConfigService */
private $configService;
/** @var AbstractVariantsTableRoute */
private $variantsTableRoute;
public function __construct(ConfigService $configService, AbstractVariantsTableRoute $variantsTableRoute)
{
$this->configService = $configService;
$this->variantsTableRoute = $variantsTableRoute;
}
/**
* @param Request $request
*
* @HttpCache()
* @RouteScope(scopes={"storefront"})
* @Route(
* "/maxia-variants-table",
* name="frontend.plugins.maxia-variants-table.ajax",
* options={"seo"="false"},
* defaults={"csrf_protected"=false, "XmlHttpRequest"=true},
* methods={"GET"}
* )
*/
public function indexAction(Request $request)
{
/** @var SalesChannelContext $context */
$context = $request->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
$config = $this->configService->getPluginConfig($context);
if (!$config->isPluginEnabled()) {
throw new NotFoundHttpException();
}
$context->addExtension('maxiaVariantsTable', $config);
$variantsTableResponse = $this->variantsTableRoute->load($request->get('productId'), $request, $context);
if (!$variantsTableResponse || !$variantsTableResponse->getPagelet()) {
throw new NotFoundHttpException();
}
$response = new StorefrontResponse(json_encode($variantsTableResponse->getPagelet()->getBootstrapTableConfig()['data']));
$response->setContext($context);
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('x-robots-tag', 'noindex');
return $response;
}
}