<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\ProductRepository;
use App\Repository\ProductCategoryRepository;
use App\Service\FileHandler;
use Exception;
use App\Entity\Product;
use App\Form\ProductFilterType;
use Knp\Component\Pager\PaginatorInterface;
#[Route('/boutique')]
class ShopController extends AbstractController
{
private $entityManager;
private $productRepository;
private $productCategoryRepository;
private $fileHandler;
public function __construct(EntityManagerInterface $entityManager,
ProductRepository $productRepository,
ProductCategoryRepository $productCategoryRepository,
FileHandler $fileHandler)
{
$this->entityManager = $entityManager;
$this->productRepository = $productRepository;
$this->productCategoryRepository = $productCategoryRepository;
$this->fileHandler = $fileHandler;
}
#[Route('/', name: 'app_shop_index')]
public function index(Request $request, PaginatorInterface $paginator): Response
{
$error = null;
$limit = 12;
$page = $request->query->getInt('page', 1);
$product = new Product();
$form = $this->createForm(ProductFilterType::class, $product, [
'method' => 'GET',
'admin' => false
])
;
$form->handleRequest($request);
$options = [];
$options['prixMin'] = $form->get('prixMin')->getData();
$options['prixMax'] = $form->get('prixMax')->getData();
$options['orderBy'] = $form->get('orderBy')->getData();
$options['langue'] = null;
$options['admin'] = false;
$options['favorite'] = $form->get('favorite')->getData();
$options['order'] = $form->get('order')->getData();
$options['user'] = $this->getUser();
$query = $this->productRepository->getSearchQuery($product, $options);
$productList = $paginator->paginate(
$query,
$page,
$limit
);
$productCategories = $this->productCategoryRepository->findAll();
return $this->render('shop/shop_index.html.twig', [
'controller_name' => 'ShopController',
'form' => $form->createView(),
'products' => $productList,
'productCategories' => $productCategories,
"product_filter" => [
"prixMin"=>$form->get('prixMin')->getData(),
"prixMax"=>$form->get('prixMax')->getData(),
"favorite"=>$form->get('favorite')->getData(),
"productCategory"=>$form->get('productCategory')->getData(),
]
]);
}
}