src/Controller/Front/ProduitController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Produit;
  4. use App\Entity\User;
  5. use App\Repository\BasketRepository;
  6. use App\Repository\ProduitRepository;
  7. use App\Services\BasketService;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Security;
  13. /**
  14.  * @Route("/produit")
  15.  */
  16. class ProduitController extends AbstractController
  17. {
  18.     #[Route('/'name'app_front_produit_list')]
  19.     public function index(Request $requestProduitRepository $produitRepository): Response
  20.     {
  21.         // $search = ($request->query->has("search") && !empty($request->query->has("search"))) ? $request->query->get("search") : "";
  22.         $produits $produitRepository->findAll();
  23.         // foreach ($produits as $key => $produit) {
  24.         //     $logo = $produit->getLogo();
  25.         //     if (!is_null($logo) && !empty($logo)) $produit->setLogo("/uploads/images/".$logo);
  26.         // }
  27.         // $basketService = new BasketService($basketRepository, $produitRepository);
  28.         return $this->render('front/produit/index.html.twig', [
  29.             'produits' => $produits,
  30.             // 'nbPanier' => count($basketService->getAllBaskets($user)),
  31.             // 'dateArray' => $this->dateArray($translator),
  32.             // 'classes' => $currentPathService->classes(),
  33.         ]);
  34.     }
  35.     /**
  36.      * @Route("/show/{id}", name="app_front_produit_show", methods={"GET"})
  37.      */
  38.     public function show(Produit $produit): Response
  39.     {
  40.         $logo $produit->getLogo();
  41.         $data = [
  42.             'numero' => $produit->getNumero(),
  43.             'logo' => (!is_null($logo) && !empty($logo)) ? "/uploads/images/".$logo null,
  44.             'nom' => $produit->getNom(),
  45.             'prix' => $produit->getPrixDeVente(),
  46.             'rom' => $produit->getRom(),
  47.             'ram' => $produit->getRam(),
  48.             'chipset' => !is_null($produit->getChipset()) ? $produit->getChipset() : "",
  49.             'os' => !is_null($produit->getOs()) ? $produit->getOs() : "",
  50.             'battery' => !is_null($produit->getBatteryCapacity()) ? $produit->getBatteryCapacity() : "",
  51.             'cam' => [
  52.                 'apn' => !is_null($produit->getMainCameraResolution()) ? $produit->getMainCameraResolution() : "",
  53.                 'selfie' => !is_null($produit->getSelfieCameraResolution()) ? $produit->getSelfieCameraResolution() : "",
  54.                 'features' => !is_null($produit->getMainCameraFeatures()) ? $produit->getMainCameraFeatures() : "",
  55.             ],
  56.             'wifi' => $produit->getBuiltInWifi() == 'yes' ? [
  57.                 'version' => !is_null($produit->getWifiVersion()) ? $produit->getWifiVersion() : "",
  58.                 'features' => !is_null($produit->getWifiFeatures()) ? $produit->getWifiFeatures() : "",
  59.             ] : [],
  60.             'bluetooth' => $produit->getBuiltInBluetooth() == 'yes' ? [
  61.                 'version' => !is_null($produit->getBluetoothVersion()) ? $produit->getBluetoothVersion() : "",
  62.                 'features' => !is_null($produit->getBluetoothFeatures()) ? $produit->getBluetoothFeatures() : "",
  63.             ] : [],
  64.         ];
  65.         return $this->json($data);
  66.     }
  67.     /**
  68.      * @Route("/basket/{type}", name="app_front_produit_basket", methods={"POST"})
  69.      */
  70.     public function basketAction(string $typeRequest $requestSecurity $securityProduitRepository $produitRepositoryBasketRepository $basketRepository): Response
  71.     {
  72.         $user $security->getUser();
  73.         if( $user instanceof User ){
  74.             if ($request->request->has("action") && $request->request->get("action") == $type) {
  75.                 $produit $request->request->get("produit");
  76.                 $basketService = new BasketService($basketRepository$produitRepository);
  77.                 if ($request->request->get("action") == "add") {
  78.                     $basketService->toBasket($user$produitRepository->find($produit));
  79.                     $produitsGot $basketService->getAllBaskets($usertrue);
  80.                     $produits = [];
  81.                     // $total = 0;
  82.                     foreach ($produitsGot as $key => $value) {
  83.                         // $total += $value->getProduit()->getPrixDeVente();
  84.                         $logo $value->getProduit()->getLogo();
  85.                         $produits[] = [
  86.                             'id' => $value->getProduit()->getId(),
  87.                             'logo' => (!is_null($logo) && !empty($logo)) ? "/uploads/images/".$logo null,
  88.                             'nom' => $value->getProduit()->getNom(),
  89.                             'rom' => $value->getProduit()->getRom(),
  90.                             'ram' => $value->getProduit()->getRam(),
  91.                             'prix' => number_format($value->getProduit()->getPrixDeVente(), 0','' '),
  92.                         ];
  93.                     }
  94.                     $groupedArray = [];
  95.                     foreach ($produits as $item) {
  96.                         $id $item['id'];
  97.                         if (!isset($groupedArray[$id])) {
  98.                             $groupedArray[$id] = $item;
  99.                             $groupedArray[$id]['quantity'] = 1;
  100.                         } else {
  101.                             $groupedArray[$id]['quantity']++;
  102.                         }
  103.                     }
  104.                     // Convert associative array to indexed array
  105.                     $produits array_values($groupedArray);
  106.                     return $this->json([
  107.                         'success' => true,
  108.                         'produits' => $produits,
  109.                         'nbPanier' => count($basketService->getAllBaskets($user)),
  110.                     ]);
  111.                 } elseif (in_array($request->request->get("action"), ["increase""decrease"])) {
  112.                     if ($request->request->get("action") == "increase") {
  113.                         $basketService->toBasket($user$produitRepository->find($produit));
  114.                     } elseif ($request->request->get("action") == "decrease") {
  115.                         $basketService->removeLatestBasket($user$produitRepository->find($produit));
  116.                     }
  117.                     $produitsGot $basketService->getAllBaskets($user);
  118.                     $total 0;
  119.                     foreach ($produitsGot as $key => $value) {
  120.                         $total += $value->getProduit()->getPrixDeVente();
  121.                     }
  122.                     return $this->json([
  123.                         'success' => true,
  124.                         'basket' => [
  125.                             'nbPanier' => count($basketService->getAllBaskets($user)),
  126.                             'total' => number_format($total0','' ')
  127.                         ],
  128.                     ]);
  129.                 }
  130.             }
  131.         }
  132.         return $this->json([
  133.             'success' => false,
  134.             'message' => "Vous devez vous connecter pour ajouter un produit au panier.",
  135.         ]);
  136.         // return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  137.     }
  138. }