Skip to content
Snippets Groups Projects
Commit 3d91f051 authored by WAGNER ERIC's avatar WAGNER ERIC
Browse files

Système de panier

parent 6ae15fa0
No related merge requests found
<?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");
}
}
......@@ -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(),
]);
}
......
......@@ -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,
]);
}
......
......@@ -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(),
]);
}
......
......@@ -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'
])
;
}
......
{% 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
......@@ -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>
......
......@@ -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>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment