src/Controller/CartController.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. //use App\Form\CartConfirmationType;
  4. use App\Repository\ActivityRepository;
  5. use App\Repository\PurchaseItemRepository;
  6. use App\Service\Formatdate;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use App\Service\CartService;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  15. class CartController extends AbstractController
  16. {
  17.     /**
  18.      * @var CartService
  19.      */
  20.     protected $cartService;
  21.     /**
  22.      * @var ActivityRepository
  23.      */
  24.     protected $activityRepository;
  25.     public function __construct(Cartservice $cartService, ActivityRepository $activityRepository, PurchaseItemRepository $purchaseItemRepository)
  26.     {
  27.         $this->cartService = $cartService;
  28.         $this->activityRepository = $activityRepository;
  29.     }
  30.     /**
  31.      * @Route("/cart/add/{id<[0-9]+>}", name="cart_add", requirements={"id":"\d+"})
  32.      */
  33.     public function add($id, Request $request, PurchaseItemRepository $purchaseItemRepository)
  34.     {
  35.         // Récupérer la quantité demandée dans le panier
  36.         $cartItems = $this->cartService->getDetailedCartItems();
  37.         $activity = $this->activityRepository->find($id);
  38.         //dd($cartItems);
  39.         if (!$activity) {
  40.             throw $this->createNotFoundException("l activité demandée n'existe pas");
  41.         }
  42.         $this->cartService->add($id);
  43.         // Valider le panier
  44.         $this->cartService->validateOrder();
  45. //  $this->addFlash('success', "L activité a bien été ajoutée au panier");
  46.         if ($request->query->get('returnToCart')) {
  47.             return $this->redirectToRoute('cart_index');
  48.         }
  49.         return $this->redirectToRoute('cart_index');
  50.     }
  51.     /**
  52.      * @Route("/cart", name="cart_index")
  53.      */
  54.     public function index(CartService $cartService, Security $security, Formatdate $formatdate)
  55.     {
  56.         // Fetch the user object
  57.        //  $user = $security->getUser();
  58.         //dd($user);
  59.         // $form = $this->createForm(CartConfirmationType::class);
  60.         $detailedCart = $this->cartService->getDetailedCartItems();
  61.         $total = $this->cartService->getTotal();
  62.         $stock = $this->cartService->getStock();
  63.         
  64.         $isValidOrder = $this->cartService->validateOrder(); // Appel de la méthode validateOrder()
  65.         return $this->render('cart/index.html.twig', [
  66.             'items' => $detailedCart,
  67.             'total' => $total,
  68.             'stock' => $stock,
  69.             'isValidOrder' => $isValidOrder,
  70.             //  'confirmationForm' => $form->createView()
  71.         ]);
  72.     }
  73.     /**
  74.      *
  75.      * @Route("/cart/delete/{id<[0-9]+>}", name="cart_delete", requirements={"id":"\d+"})
  76.      */
  77.     public function delete($id)
  78.     {
  79.         $activity = $this->activityRepository->find($id);
  80.         if (!$activity) {
  81.             throw $this->createNotFoundException("l activitée $id demandée n'existe pas");
  82.         }
  83.         $this->cartService->remove($id);
  84.         //   dd($this->cartService);
  85.         return $this->redirectToRoute('cart_index');
  86.     }
  87.     /**
  88.      *
  89.      * @Route("/cart/decrement/{id<[0-9]+>}", name="cart_decrement",
  90.      *     requirements={"id": "\d+"})
  91.      */
  92.     public function decrement($id)
  93.     {
  94.         $activity = $this->activityRepository->find($id);
  95.         if (!$activity) {
  96.             throw $this->createNotFoundException("l activité $id demandée n'existe pas
  97.             et ne peut etre décrémenté");
  98.         }
  99.         $this->cartService->decrement($id);
  100.         return $this->redirectToRoute('cart_index');
  101.     }
  102.     /**
  103.      * @Route("/cart/deleteAll", name="cart_delete_all")
  104.      */
  105.     public function deleteAll()
  106.     {
  107.         $this->cartService->removeAll();
  108.         return $this->redirectToRoute('cart_index');
  109.     }
  110. }