vendor/boldr/shop-bundle/src/Controller/ShopController.php line 93

Open in your IDE?
  1. <?php
  2. namespace Boldr\Shop\ShopBundle\Controller;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Component\HttpFoundation\{ Request, Response };
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Doctrine\ORM\EntityRepository;
  7. use Boldr\Shop\ShopBundle\Presenter\Category\CategoryPresenter;
  8. use Boldr\Shop\ShopBundle\Presenter\Product\ProductPresenter;
  9. use Boldr\Shop\ShopBundle\Entity\{ Product, ProductImage, Category, CategoryTranslation };
  10. use Boldr\Shop\ShopBundle\ShopContext;
  11. use Psr\EventDispatcher\EventDispatcherInterface;
  12. use Boldr\Shop\ShopBundle\Event\ShopPagePreloadEvent;
  13. use Boldr\Shop\ShopBundle\Stock\StockManagerInterface;
  14. class ShopController extends AbstractController
  15. {
  16. public static function getSubscribedServices()
  17. {
  18. return array_merge(parent::getSubscribedServices(), [
  19. CategoryPresenter::class,
  20. ProductPresenter::class,
  21. ShopContext::class,
  22. StockManagerInterface::class
  23. ]);
  24. }
  25. /**
  26. * @Route({
  27. * "en": "/order",
  28. * "nl": "/bestellen"
  29. * }, name="shop_shop")
  30. */
  31. public function shop(Request $request, EventDispatcherInterface $eventDispatcher): Response
  32. {
  33. // Parameters
  34. $resultsPerPage = null;
  35. $page = 1;
  36. $locale = $request->getLocale();
  37. $customer = $this->get(ShopContext::class)->getCustomer();
  38. $currency = $this->get(ShopContext::class)->getCurrency();
  39. $preloadEvent = new ShopPagePreloadEvent();
  40. $eventDispatcher->dispatch($preloadEvent);
  41. /** @var EntityRepository */
  42. $categoryRepository = $this->getDoctrine()->getRepository(Category::class);
  43. /** @var EntityRepository */
  44. $categoryTranslationRepository = $this->getDoctrine()->getRepository(CategoryTranslation::class);
  45. $categoryTranslationRepository->findAll();
  46. $categoryRepository->findAll();
  47. $categories = $categoryRepository->findBy(['parent' => null, 'visible' => true], ['sortOrder' => 'ASC']);
  48. $categoryPresenter = $this->get(CategoryPresenter::class);
  49. $categoryViews = array_map(
  50. fn($category) => $categoryPresenter->createCategoryView($category, $locale),
  51. $categories
  52. );
  53. /** @var EntityRepository */
  54. $productRepository = $this->getDoctrine()->getRepository(Product::class);
  55. $productsQb = $productRepository->createQueryBuilder('p')
  56. ->select('p, v, t, c, vt, pa, pi')
  57. ->leftJoin('p.images', 'pi')
  58. ->leftJoin('p.categories', 'c')
  59. ->leftJoin('p.translations', 't')
  60. ->leftJoin('p.variants', 'v')
  61. ->leftJoin('v.translations', 'vt')
  62. ->leftJoin('v.attributes', 'pa')
  63. ->andWhere('p.enabled = true')
  64. ->andWhere('c.visible = true')
  65. ->andWhere('p.deleted = false')
  66. ->setMaxResults($resultsPerPage);
  67. if ($resultsPerPage !== null)
  68. {
  69. $productsQb->setFirstResult(($page - 1) * $resultsPerPage);
  70. }
  71. $this->get(StockManagerInterface::class)->limitQueryBuilderToAvailable($productsQb, 'p', null, $customer);
  72. $products = $productsQb
  73. ->getQuery()
  74. ->getResult();
  75. $productPresenter = $this->get(ProductPresenter::class);
  76. $catalogProductViews = array_map(
  77. fn($product) => $productPresenter->createCatalogProductView($product, $customer, $currency, $locale),
  78. $products
  79. );
  80. if ($request->query->has('json') || in_array('application/json', $request->getAcceptableContentTypes()))
  81. {
  82. return $this->json([
  83. 'catalogProducts' => $catalogProductViews,
  84. 'categories' => $categoryViews
  85. ]);
  86. }
  87. return $this->render('@BoldrShop/shop.html.twig', [
  88. 'catalogProducts' => $catalogProductViews,
  89. 'categories' => $categoryViews
  90. ]);
  91. }
  92. }