From 3d91f05191b3c907591109ed148d5c492fb388d7 Mon Sep 17 00:00:00 2001 From: Eric WAGNER <eric.wagner@etu.unistra.fr> Date: Thu, 30 Mar 2023 16:26:09 +0200 Subject: [PATCH] =?UTF-8?q?Syst=C3=A8me=20de=20panier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/CartController.php | 88 +++++++++++++++++++++++++++ src/Controller/CategoryController.php | 6 ++ src/Controller/ClothesController.php | 11 ++++ src/Controller/EcurieController.php | 16 +++++ src/Form/ClothesType.php | 15 ++++- templates/cart/index.html.twig | 53 ++++++++++++++++ templates/category/show.html.twig | 31 ++++++---- templates/clothes/index.html.twig | 2 +- 8 files changed, 206 insertions(+), 16 deletions(-) create mode 100644 src/Controller/CartController.php create mode 100644 templates/cart/index.html.twig diff --git a/src/Controller/CartController.php b/src/Controller/CartController.php new file mode 100644 index 0000000..7c40414 --- /dev/null +++ b/src/Controller/CartController.php @@ -0,0 +1,88 @@ +<?php + +namespace App\Controller; + +use App\Entity\Clothes; +use App\Form\ClothesType; +use App\Repository\ClothesRepository; + +use App\Entity\Category; +use App\Form\CategoryType; +use App\Repository\CategoryRepository; + +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Symfony\Component\Routing\Annotation\Route; + +#[Route('/cart')] +class CartController extends AbstractController +{ + #[Route('/', name: 'app_cart_index', methods: ['GET', 'POST'])] + public function index(SessionInterface $session, ClothesRepository $clothesRepository) + { + $panier = $session->get("panier", []); + + // On "fabrique" les données + $dataPanier = []; + $total = 0; + + foreach($panier as $id => $quantite) { + $clothes = $clothesRepository->find($id); + $dataPanier[] = [ + "clothes" => $clothes, + "quantite" => $quantite + ]; + $total += $clothes->getPrice() * $quantite; + } + + $categoryRepository = $this->getDoctrine() + ->getRepository(Category::class); + + + return $this->render('cart/index.html.twig', [ + 'dataPanier' => $dataPanier, + 'total' => $total, + 'categories' => $categoryRepository->findAll(), + ]); + } + + #[Route('/add/{id}', name: 'app_cart_add', methods: ['GET', 'POST'])] + public function add(Clothes $clothes, SessionInterface $session) + { + // On récupère le panier actuel + $panier = $session->get("panier", []); + $id = $clothes->getId(); + // dd($panier); + + if (!empty($panier[$id])) { + $panier[$id]++; + } else { + $panier[$id] = 1; + } + + // On sauvegarde dans la session + $session->set("panier", $panier); + return $this->redirectToRoute("app_cart_index"); + } + + #[Route('/remove/{id}', name: 'app_cart_remove', methods: ['GET', 'POST'])] + public function remove(Clothes $clothes, SessionInterface $session) + { + // On récupère le panier actuel + $panier = $session->get("panier", []); + $id = $clothes->getId(); + + if (!empty($panier[$id])) { + if ($panier[$id] > 1) { + $panier[$id]--; + } else { + unset($panier[$id]); + } + } + + // On sauvegarde dans la session + $session->set("panier", $panier); + + return $this->redirectToRoute("app_cart_index"); + } +} diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php index 4bcf50d..16d9e02 100644 --- a/src/Controller/CategoryController.php +++ b/src/Controller/CategoryController.php @@ -33,9 +33,12 @@ class CategoryController extends AbstractController return $this->redirectToRoute('app_category_index', [], Response::HTTP_SEE_OTHER); } + $categoryRepository = $this->getDoctrine() + ->getRepository(Category::class); return $this->renderForm('category/new.html.twig', [ 'category' => $category, + 'categories' => $categoryRepository, 'form' => $form, ]); } @@ -43,8 +46,11 @@ class CategoryController extends AbstractController #[Route('/{id}', name: 'app_category_show', methods: ['GET'])] public function show(Category $category): Response { + $categoryRepository = $this->getDoctrine() + ->getRepository(Category::class); return $this->render('category/show.html.twig', [ 'category' => $category, + 'categories' => $categoryRepository->findAll(), ]); } diff --git a/src/Controller/ClothesController.php b/src/Controller/ClothesController.php index a26bd16..303a1fd 100644 --- a/src/Controller/ClothesController.php +++ b/src/Controller/ClothesController.php @@ -5,6 +5,11 @@ namespace App\Controller; use App\Entity\Clothes; use App\Form\ClothesType; use App\Repository\ClothesRepository; + +use App\Entity\Category; +use App\Form\CategoryType; +use App\Repository\CategoryRepository; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -16,8 +21,11 @@ class ClothesController extends AbstractController #[Route('/', name: 'app_clothes_index', methods: ['GET'])] public function index(ClothesRepository $clothesRepository): Response { + $categoryRepository = $this->getDoctrine() + ->getRepository(Category::class); return $this->render('clothes/index.html.twig', [ 'clothes' => $clothesRepository->findAll(), + 'categories' => $categoryRepository->findAll(), ]); } @@ -33,9 +41,12 @@ class ClothesController extends AbstractController return $this->redirectToRoute('app_clothes_index', [], Response::HTTP_SEE_OTHER); } + $categoryRepository = $this->getDoctrine() + ->getRepository(Category::class); return $this->renderForm('clothes/new.html.twig', [ 'clothes' => $clothes, + 'categories' => $categoryRepository->findAll(), 'form' => $form, ]); } diff --git a/src/Controller/EcurieController.php b/src/Controller/EcurieController.php index f53c3a7..8f54f66 100644 --- a/src/Controller/EcurieController.php +++ b/src/Controller/EcurieController.php @@ -5,6 +5,11 @@ namespace App\Controller; use App\Entity\Ecurie; use App\Form\EcurieType; use App\Repository\EcurieRepository; + +use App\Entity\Category; +use App\Form\CategoryType; +use App\Repository\CategoryRepository; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -16,8 +21,12 @@ class EcurieController extends AbstractController #[Route('/', name: 'app_ecurie_index', methods: ['GET'])] public function index(EcurieRepository $ecurieRepository): Response { + $categoryRepository = $this->getDoctrine() + ->getRepository(Category::class); + return $this->render('ecurie/index.html.twig', [ 'ecuries' => $ecurieRepository->findAll(), + 'categories' => $categoryRepository->findAll(), ]); } @@ -28,6 +37,9 @@ class EcurieController extends AbstractController $form = $this->createForm(EcurieType::class, $ecurie); $form->handleRequest($request); + $categoryRepository = $this->getDoctrine() + ->getRepository(Category::class); + if ($form->isSubmitted() && $form->isValid()) { $ecurieRepository->save($ecurie, true); @@ -37,14 +49,18 @@ class EcurieController extends AbstractController return $this->renderForm('ecurie/new.html.twig', [ 'ecurie' => $ecurie, 'form' => $form, + 'categories' => $categoryRepository->findAll(), ]); } #[Route('/{id}', name: 'app_ecurie_show', methods: ['GET'])] public function show(Ecurie $ecurie): Response { + $categoryRepository = $this->getDoctrine() + ->getRepository(Category::class); return $this->render('ecurie/show.html.twig', [ 'ecurie' => $ecurie, + 'categories' => $categoryRepository->findAll(), ]); } diff --git a/src/Form/ClothesType.php b/src/Form/ClothesType.php index f858b7e..17677cd 100644 --- a/src/Form/ClothesType.php +++ b/src/Form/ClothesType.php @@ -3,6 +3,11 @@ namespace App\Form; use App\Entity\Clothes; +use App\Entity\Category; +use App\Entity\Ecurie; + +use Symfony\Bridge\Doctrine\Form\Type\EntityType; + use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -15,8 +20,14 @@ class ClothesType extends AbstractType ->add('name') ->add('picture') ->add('price') - ->add('category') - ->add('ecurie') + ->add('category', EntityType::class, [ // version 1 : SELECT + 'class' => Category::class, + 'choice_label' => 'name' + ]) + ->add('ecurie', EntityType::class, [ // version 1 : SELECT + 'class' => Ecurie::class, + 'choice_label' => 'name' + ]) ; } diff --git a/templates/cart/index.html.twig b/templates/cart/index.html.twig new file mode 100644 index 0000000..4d87650 --- /dev/null +++ b/templates/cart/index.html.twig @@ -0,0 +1,53 @@ +{% extends 'base.html.twig' %} + +{% block title %}Votre Panier{% endblock %} + +{% block body %} + <table> + <thead> + <tr> + <td> + Clothes + </td> + <td> + Price + </td> + <td> + Quantite + </td> + <td> + Total + </td> + <td> + Actions + </td> + </tr> + </thead> + <tbody> + {% for element in dataPanier %} + <tr> + <td>{{element.clothes.name}}</td> + <td>{{element.clothes.price}} €</td> + <td>{{element.quantite}}</td> + <td>{{element.quantite * element.clothes.price}} €</td> + <td> + <a href="{{ path('app_cart_add', {id: element.clothes.id}) }}"><i class="bi bi-cart-plus"></i></a> + <a href=""><i class="bi bi-cart-dash"></i></a> + <a href=""><i class="bi bi-cart-x"></i></a> + </td> + </tr> + {% else %} + <tr> + <td colspan="5" class="text-center">Votre Panier est vide</td> + </tr> + {% endfor %} + </tbody> + <tfoot> + <tr> + <td colspan="3">Total</td> + <td>{{ total }} €</td> + <td></td> + </tr> + </tfoot> + </table> +{% endblock %} \ No newline at end of file diff --git a/templates/category/show.html.twig b/templates/category/show.html.twig index d86f086..8a0c4a1 100644 --- a/templates/category/show.html.twig +++ b/templates/category/show.html.twig @@ -3,20 +3,25 @@ {% block title %}Category{% endblock %} {% block body %} - <h1>Category</h1> + <h1 class="text-3xl capitalize px-[5rem]">{{ category.name }}</h1> - <table class="table"> - <tbody> - <tr> - <th>Id</th> - <td>{{ category.id }}</td> - </tr> - <tr> - <th>Name</th> - <td>{{ category.name }}</td> - </tr> - </tbody> - </table> + <div class="flex justify-center"> + <ul class="max-w-[80%] flex flex-wrap"> + {% for clothes in category.clothes %} + <li class="max-w-[33%] border-b-2"> + <a href="{{ path('app_cart_add', {id: clothes.id}) }}"> + <article class="flex flex-col justify-center"> + <img src="{{ clothes.picture }}" alt="{{ clothes.name }}"> + <div class="p-5"> + <h3 class="text-[#CC0000] font-bold">{{ clothes.price }} €</h3> + <p>{{ clothes.name }}</p> + </div> + </article> + </a> + </li> + {% endfor %} + </ul> + </div> <a href="{{ path('app_category_index') }}">back to list</a> diff --git a/templates/clothes/index.html.twig b/templates/clothes/index.html.twig index c618a11..b557909 100644 --- a/templates/clothes/index.html.twig +++ b/templates/clothes/index.html.twig @@ -20,7 +20,7 @@ <tr> <td>{{ clothes.id }}</td> <td>{{ clothes.name }}</td> - <td>{{ clothes.picture }}</td> + <td><img src="{{ clothes.picture }}" alt=""></td> <td>{{ clothes.price }}</td> <td> <a href="{{ path('app_clothes_show', {'id': clothes.id}) }}">show</a> -- GitLab