src/Controller/ShopController.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Repository\ProductRepository;
  9. use App\Repository\ProductCategoryRepository;
  10. use App\Service\FileHandler;
  11. use Exception;
  12. use App\Entity\Product;
  13. use App\Form\ProductFilterType;
  14. use Knp\Component\Pager\PaginatorInterface;
  15. #[Route('/boutique')]
  16. class ShopController extends AbstractController
  17. {
  18. private $entityManager;
  19. private $productRepository;
  20. private $productCategoryRepository;
  21. private $fileHandler;
  22. public function __construct(EntityManagerInterface $entityManager,
  23. ProductRepository $productRepository,
  24. ProductCategoryRepository $productCategoryRepository,
  25. FileHandler $fileHandler)
  26. {
  27. $this->entityManager = $entityManager;
  28. $this->productRepository = $productRepository;
  29. $this->productCategoryRepository = $productCategoryRepository;
  30. $this->fileHandler = $fileHandler;
  31. }
  32. #[Route('/', name: 'app_shop_index')]
  33. public function index(Request $request, PaginatorInterface $paginator): Response
  34. {
  35. $error = null;
  36. $limit = 12;
  37. $page = $request->query->getInt('page', 1);
  38. $product = new Product();
  39. $form = $this->createForm(ProductFilterType::class, $product, [
  40. 'method' => 'GET',
  41. 'admin' => false
  42. ])
  43. ;
  44. $form->handleRequest($request);
  45. $options = [];
  46. $options['prixMin'] = $form->get('prixMin')->getData();
  47. $options['prixMax'] = $form->get('prixMax')->getData();
  48. $options['orderBy'] = $form->get('orderBy')->getData();
  49. $options['langue'] = null;
  50. $options['admin'] = false;
  51. $options['favorite'] = $form->get('favorite')->getData();
  52. $options['order'] = $form->get('order')->getData();
  53. $options['user'] = $this->getUser();
  54. $query = $this->productRepository->getSearchQuery($product, $options);
  55. $productList = $paginator->paginate(
  56. $query,
  57. $page,
  58. $limit
  59. );
  60. $productCategories = $this->productCategoryRepository->findAll();
  61. return $this->render('shop/shop_index.html.twig', [
  62. 'controller_name' => 'ShopController',
  63. 'form' => $form->createView(),
  64. 'products' => $productList,
  65. 'productCategories' => $productCategories,
  66. "product_filter" => [
  67. "prixMin"=>$form->get('prixMin')->getData(),
  68. "prixMax"=>$form->get('prixMax')->getData(),
  69. "favorite"=>$form->get('favorite')->getData(),
  70. "productCategory"=>$form->get('productCategory')->getData(),
  71. ]
  72. ]);
  73. }
  74. }