diff --git a/src/Controller/CartController.php b/src/Controller/CartController.php new file mode 100644 index 0000000000000000000000000000000000000000..7c4041473f02001e0b3da2e1292fc79fb858b5a3 --- /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 4bcf50de1a4dfcab750a70edb3422552bd3f1f4d..16d9e02df50ef691df2c3922b9418e14389b7d6b 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 a26bd166f65a817db572a543dfd3d68757a73e7b..303a1fd2bf57958aa076e82c9b989a055dfd755c 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 f53c3a7ab4841fd4c522fc18180f8ce52380297d..8f54f665b2f1121f05977cb0921cbf8823087df4 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 f858b7e4b42dd0515dc6de367f8d648f2a62834d..17677cdd0e666de2679bfd2acbd39e940469397b 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 0000000000000000000000000000000000000000..4d87650102e7d0f9b1a77e086353ddd6fc8da268 --- /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 d86f086b27375e26bdbf20d676ebaf810a26e970..8a0c4a13d7f655c3aadcebd4e98a34274c223608 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 c618a11a3d4ad46bd63ca9c3b5aa9d4b3ea47e21..b5579097ca616abf698b4f625a39aea373204112 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>