<?php declare(strict_types=1);
namespace Nd\Stl\Storefront\Controller;
use Nd\Stl\Storefront\Page\Look\LookPageLoader;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotCollection;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\DataResolver\CmsSlotsDataResolver;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
use Shopware\Core\Content\Product\SalesChannel\Detail\ProductConfiguratorLoader;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\CountAggregation;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class LookController extends StorefrontController
{
private EntityRepositoryInterface $lookRepository;
private LookPageLoader $lookPageLoader;
private GenericPageLoaderInterface $genericLoader;
private CmsSlotsDataResolver $cmsSlotsDataResolver;
public function __construct(GenericPageLoaderInterface $genericLoader, LookPageLoader $lookPageLoader, EntityRepositoryInterface $lookRepository,
CmsSlotsDataResolver $cmsSlotsDataResolver)
{
$this->lookPageLoader = $lookPageLoader;
$this->lookRepository = $lookRepository;
$this->genericLoader = $genericLoader;
$this->cmsSlotsDataResolver = $cmsSlotsDataResolver;
}
/**
* @Route("/look/{lookId}", name="frontend.look.detail", methods={"GET"})
*/
public function lookDetail(Request $request, SalesChannelContext $context): Response
{
$lookId = $request->attributes->get('lookId');
if (!$lookId) {
return $this->renderStorefront('@Storefront/storefront/page/look/detail/not-found.html.twig', []);
}
$page = $this->lookPageLoader->load($request, $context);
$criteria = new Criteria([$lookId]);
try {
$look = $this->lookRepository->search($criteria, $context->getContext())->first();
} catch (InvalidUuidException $exception) {
return $this->renderStorefront('@Storefront/storefront/page/manufacturer/detail/not-found.html.twig', ['page' => $page]);
}
return $this->renderStorefront('@Storefront/storefront/page/content/index.html.twig', ['page' => $page]);
}
/**
* @Route("/nd/look", name="frontend.look.overview", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
public function lookOverview(Request $request, SalesChannelContext $context): Response
{
$request->query->set('isFromAjax', true);
$resolverContext = new ResolverContext($context, $request);
$slot = new CmsSlotEntity();
$slot->setUniqueIdentifier('id');
$slot->setType('shop-the-look-overview');
$slot->setSlot("slot");
$slots = new CmsSlotCollection([$slot]);
$slots = $this->cmsSlotsDataResolver->resolve($slots, $resolverContext);
$slot = $slots->first();
return $this->renderStorefront('@Storefront/storefront/element/cms-element-shop-the-look-overview.html.twig', [
'element' => $slot
]);
// $criteria = new Criteria();
// $criteria->addAssociation("cover");
// $gender = $request->get('gender');
// if ($gender) {
// $criteria->addFilter(new EqualsFilter("genderFilter", $gender));
// }
// $looksCount = 8;
// $page = $request->get('page');
// if (!$page || !is_numeric($page)) {
// $page = 1;
// }
// $page = (int)$page - 1;
//
// $currentpage = $request->get('page');
// if (!$currentpage || !is_numeric($currentpage)) {
// $currentpage = 1;
// }
//
// $criteria->setOffset($looksCount * $page);
// $criteria->setLimit($looksCount);
// $criteria->addSorting(new FieldSorting('createdAt', 'desc'));
//
// $criteria->addAggregation(
// new CountAggregation('count-looks', 'id')
// );
// $looks = $this->lookRepository->search($criteria, $context->getContext());
// $totalLooks = $looks->getAggregations()->get('count-looks')->getCount();
// $pages = (int)ceil($totalLooks / 8);
// $page = $this->genericLoader->load($request, $context);
//
//
// if (!$looks || $looks->count() == 0) {
// return $this->renderStorefront('@Storefront/storefront/page/look/empty.html.twig', [
// 'looks' => null,
// 'page' => $page,
// 'totalLooks' => 0,
// 'pages' => $pages,
// 'currentpage' => $currentpage
// ]);
// }
//
// return $this->renderStorefront('@Storefront/storefront/element/cms-element-shop-the-look-overview.html.twig', [
// 'looks' => $looks,
// 'page' => $page,
// 'totalLooks' => $totalLooks,
// 'pages' => $pages,
// 'currentpage' => $currentpage
// ]);
}
}