src/Controller/Blo/CatalogController.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Blo;
  4. use App\Entity\Blo\Shop;
  5. use App\Repository\Blo\CategoryRepository;
  6. use App\Repository\Blo\ProductRepository;
  7. use App\Repository\Blo\ShopRepository;
  8. use App\Service\Blo\ProductAttributesHelper;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. #[Route('/boutique')]
  15. class CatalogController extends AbstractController
  16. {
  17.     public function __construct(
  18.         private readonly ProductRepository $productRepository,
  19.         private readonly CategoryRepository $categoryRepository,
  20.         private readonly ShopRepository $shopRepository,
  21.         private readonly PaginatorInterface $paginator,
  22.     ) {
  23.     }
  24.     #[Route(''name'blo_catalog'methods: ['GET'])]
  25.     public function index(Request $request): Response
  26.     {
  27.         $query $request->query->get('q''');
  28.         $categoryId $request->query->getInt('category');
  29.         $minPrice $request->query->get('min_price') ? (float) $request->query->get('min_price') : null;
  30.         $maxPrice $request->query->get('max_price') ? (float) $request->query->get('max_price') : null;
  31.         $promoOnly $request->query->getBoolean('promo');
  32.         $newestOnly $request->query->getBoolean('newest');
  33.         $qb $this->productRepository->createQueryBuilder('p')
  34.             ->innerJoin('p.shop''s')
  35.             ->where('p.status = :status')
  36.             ->andWhere('s.status = :shopStatus')
  37.             ->setParameter('status''APPROVED')
  38.             ->setParameter('shopStatus'Shop::STATUS_ACTIVE);
  39.         if ($promoOnly) {
  40.             $qb->andWhere('p.promoPrice IS NOT NULL')->andWhere('p.promoPrice > 0');
  41.         }
  42.         $qb->orderBy($newestOnly 'p.createdAt' 'p.updatedAt''DESC');
  43.         if ($query !== '') {
  44.             $qb->andWhere('p.title LIKE :query OR p.description LIKE :query')
  45.                 ->setParameter('query''%' $query '%');
  46.         }
  47.         if ($categoryId) {
  48.             $categoryIds $this->categoryRepository->getCategoryAndDescendantIds($categoryId);
  49.             $qb->andWhere('p.category IN (:catIds)')->setParameter('catIds'$categoryIds);
  50.         }
  51.         if ($minPrice !== null) {
  52.             $qb->andWhere('p.price >= :min')->setParameter('min', (string) $minPrice);
  53.         }
  54.         if ($maxPrice !== null) {
  55.             $qb->andWhere('p.price <= :max')->setParameter('max', (string) $maxPrice);
  56.         }
  57.         $products $this->paginator->paginate(
  58.             $qb,
  59.             $request->query->getInt('page'1),
  60.             20
  61.         );
  62.         $categories $this->categoryRepository->findRootCategories();
  63.         $categoriesTree $this->categoryRepository->findTreeForParentSelect(null);
  64.         return $this->render('blo/catalog/index.html.twig', [
  65.             'products' => $products,
  66.             'categories' => $categories,
  67.             'categoriesTree' => $categoriesTree,
  68.             'query' => $query,
  69.             'promoOnly' => $promoOnly,
  70.             'newestOnly' => $newestOnly,
  71.         ]);
  72.     }
  73.     #[Route('/product/{slug}'name'blo_product_show'methods: ['GET'])]
  74.     public function product(string $slug): Response
  75.     {
  76.         $product $this->productRepository->findBySlug($slug);
  77.         if (!$product) {
  78.             throw $this->createNotFoundException('Produit introuvable');
  79.         }
  80.         if ($product->getShop() && $product->getShop()->getStatus() !== Shop::STATUS_ACTIVE) {
  81.             throw $this->createNotFoundException('Produit introuvable');
  82.         }
  83.         $similarProducts $this->productRepository->findSimilar($product4);
  84.         return $this->render('blo/catalog/product.html.twig', [
  85.             'product' => $product,
  86.             'similarProducts' => $similarProducts,
  87.             'colorsMap' => ProductAttributesHelper::COLORS,
  88.         ]);
  89.     }
  90.     #[Route('/shop/{slug}'name'blo_shop_show'methods: ['GET'])]
  91.     public function shop(string $slugRequest $request): Response
  92.     {
  93.         $shop $this->shopRepository->findBySlug($slug);
  94.         if (!$shop) {
  95.             throw $this->createNotFoundException('Boutique introuvable');
  96.         }
  97.         $products $this->paginator->paginate(
  98.             $this->productRepository->createQueryBuilder('p')
  99.                 ->where('p.shop = :shop')
  100.                 ->andWhere('p.status = :status')
  101.                 ->setParameter('shop'$shop)
  102.                 ->setParameter('status''APPROVED')
  103.                 ->orderBy('p.createdAt''DESC'),
  104.             $request->query->getInt('page'1),
  105.             20
  106.         );
  107.         return $this->render('blo/catalog/shop.html.twig', [
  108.             'shop' => $shop,
  109.             'products' => $products,
  110.         ]);
  111.     }
  112. }