<?php
namespace Boldr\Shop\ShopBundle\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\{ Request, Response };
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\EntityRepository;
use Boldr\Shop\ShopBundle\Presenter\Category\CategoryPresenter;
use Boldr\Shop\ShopBundle\Presenter\Product\ProductPresenter;
use Boldr\Shop\ShopBundle\Entity\{ Product, ProductImage, Category, CategoryTranslation };
use Boldr\Shop\ShopBundle\ShopContext;
use Psr\EventDispatcher\EventDispatcherInterface;
use Boldr\Shop\ShopBundle\Event\ShopPagePreloadEvent;
use Boldr\Shop\ShopBundle\Stock\StockManagerInterface;
class ShopController extends AbstractController
{
public static function getSubscribedServices()
{
return array_merge(parent::getSubscribedServices(), [
CategoryPresenter::class,
ProductPresenter::class,
ShopContext::class,
StockManagerInterface::class
]);
}
/**
* @Route({
* "en": "/order",
* "nl": "/bestellen"
* }, name="shop_shop")
*/
public function shop(Request $request, EventDispatcherInterface $eventDispatcher): Response
{
// Parameters
$resultsPerPage = null;
$page = 1;
$locale = $request->getLocale();
$customer = $this->get(ShopContext::class)->getCustomer();
$currency = $this->get(ShopContext::class)->getCurrency();
$preloadEvent = new ShopPagePreloadEvent();
$eventDispatcher->dispatch($preloadEvent);
/** @var EntityRepository */
$categoryRepository = $this->getDoctrine()->getRepository(Category::class);
/** @var EntityRepository */
$categoryTranslationRepository = $this->getDoctrine()->getRepository(CategoryTranslation::class);
$categoryTranslationRepository->findAll();
$categoryRepository->findAll();
$categories = $categoryRepository->findBy(['parent' => null, 'visible' => true], ['sortOrder' => 'ASC']);
$categoryPresenter = $this->get(CategoryPresenter::class);
$categoryViews = array_map(
fn($category) => $categoryPresenter->createCategoryView($category, $locale),
$categories
);
/** @var EntityRepository */
$productRepository = $this->getDoctrine()->getRepository(Product::class);
$productsQb = $productRepository->createQueryBuilder('p')
->select('p, v, t, c, vt, pa, pi')
->leftJoin('p.images', 'pi')
->leftJoin('p.categories', 'c')
->leftJoin('p.translations', 't')
->leftJoin('p.variants', 'v')
->leftJoin('v.translations', 'vt')
->leftJoin('v.attributes', 'pa')
->andWhere('p.enabled = true')
->andWhere('c.visible = true')
->andWhere('p.deleted = false')
->setMaxResults($resultsPerPage);
if ($resultsPerPage !== null)
{
$productsQb->setFirstResult(($page - 1) * $resultsPerPage);
}
$this->get(StockManagerInterface::class)->limitQueryBuilderToAvailable($productsQb, 'p', null, $customer);
$products = $productsQb
->getQuery()
->getResult();
$productPresenter = $this->get(ProductPresenter::class);
$catalogProductViews = array_map(
fn($product) => $productPresenter->createCatalogProductView($product, $customer, $currency, $locale),
$products
);
if ($request->query->has('json') || in_array('application/json', $request->getAcceptableContentTypes()))
{
return $this->json([
'catalogProducts' => $catalogProductViews,
'categories' => $categoryViews
]);
}
return $this->render('@BoldrShop/shop.html.twig', [
'catalogProducts' => $catalogProductViews,
'categories' => $categoryViews
]);
}
}