src/Controller/FavorisController.php line 62

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Bundle\SecurityBundle\Security;
  8. use App\Repository\WishItemRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Repository\ProductRepository;
  11. use App\Repository\ProductCollectionRepository;
  12. use App\Repository\CategoryRepository;
  13. use App\Repository\ProductVariantRepository;
  14. use App\Repository\OptionValueRepository;
  15. use App\Repository\CustomerRepository;
  16. use App\Repository\WishlistRepository;
  17. use App\Entity\WishItem;
  18. use App\Entity\NewsletterSubscriber;
  19. use App\Form\NewsletterSubscriberType;
  20. class FavorisController extends AbstractController
  21. {
  22.     public function __construct(
  23.         private EntityManagerInterface $entityManager,
  24.         private ProductRepository $productRepository,
  25.         private ProductCollectionRepository $collectionRepository,
  26.         private CategoryRepository $categoryRepository,
  27.         private ProductVariantRepository $variantRepository,
  28.         private OptionValueRepository $optionRepository,
  29.         private Security $security,
  30.         private CustomerRepository $customerRepository,
  31.         private WishlistRepository $wishlistRepository,
  32.         private WishItemRepository $wishItemRepository,
  33.     )
  34.     {
  35.         
  36.     }
  37.     #[Route('/{_locale}/{gender}/favoris'name'app_favoris_index',requirements:['gender'=> "mens|women"],defaults:["gender" => "mens"])]
  38.     #[Route('/{_locale}/favoris',name'app_favoris_index_index')]
  39.     public function favoris(Request $request)
  40.     {
  41.         $session $request->getSession();
  42.         $gender $request->get('gender');
  43.         $genderId $gender == "mens" 2;
  44.         //all categories
  45.         // $categories = $this->categoryRepository->findAll();
  46.         //main categories (level 1)
  47.         $mainCategories $this->categoryRepository->findBy(["level" => 1]);
  48.         //Men sub categories (level 2)
  49.         $subCategories $gender $this->categoryRepository->findBy(["parentCategory" => $genderId,"level" => 2]) : null;
  50.         
  51.         $favoris $session->get('favoris',[]);
  52.         $user $this->security->getUser();
  53.         if($user){
  54.             if($customer $this->customerRepository->findOneBy(['email' => $user->getEmail()])){
  55.                 $wishlist $this->wishlistRepository->findOneBy(["customer" => $customer]);
  56.                 $favItems $wishlist->getItems();
  57.             }else{
  58.                 $favItems null;
  59.             }
  60.         }else{
  61.             return $this->redirectToRoute('customer_login');
  62.         }
  63.         //TODO: this reccurent template elements should be generated using a template service class
  64.         // $menuCategories = $this->categoryRepository->findAll();
  65.         $newsLetterSubscriber = new NewsletterSubscriber();
  66.         $form $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
  67.         // newsltetter
  68.         $form->handleRequest($request);
  69.         // dd($form);
  70.         if($form->isSubmitted() && $form->isValid()){
  71.             $newsLetterSubscriber $form->getData();
  72.             $this->entityManager->persist($newsLetterSubscriber);
  73.             $this->entityManager->flush();                              
  74.         }
  75.         if($gender){
  76.             $templatePath $genderId == 'mens/favoris/favoris.twig' 'women/favoris/favoris.twig';
  77.         }else{
  78.             $templatePath 'front_end/favoris/favoris.twig';
  79.         }
  80.         //END TEMPLATE ELEMENTS
  81.         return $this->render($templatePath, [
  82.             'controller_name' => 'FrontEndController',
  83.             'favItems' => $favItems,
  84.             // 'categories' => $menuCategories,
  85.             'form' => $form,
  86.             'pageName' => 'Favoris',
  87.             'mainCategories' => $mainCategories,
  88.             'subCategories' => $subCategories,
  89.             // 'categories' => $categories,
  90.             'activePage' => getenv('GENDER')
  91.         ]);
  92.     }
  93.     #[Route('{_locale}/{gender}/addToFavoris'name'app_add_to_favoris',requirements:["gender" => "mens|women"],defaults:["gender" => "mens"])]
  94.     public function addToFavoris(Request $request,Security $security): Response
  95.     
  96.         // GET current user
  97.        
  98.         $user $security->getUser();
  99.         
  100.         if($user and in_array("ROLE_CUSTOMER",$user->getRoles())){
  101.             if ($customer $this->customerRepository->findOneBy(['email' => $user->getEmail()])) {
  102.                 if($productId $request->get('id')) {
  103.                     $product $this->productRepository->find($productId);
  104.                     $pid $product->getId();
  105.                     $apiId $product->getApiId();
  106.                     $name $product->getName();
  107.                     $price $product->firstVariantPrice();
  108.                     $image $product->getMainImage();
  109.                     $imagePath $image;
  110.                 }
  111.                 $wishlist $this->wishlistRepository->findOneBy(['customer' => $customer]);
  112.                 $session $request->getSession();
  113.                 if (!in_array($pid$wishlist->itemsPids())) {
  114.                     $wishitem = new WishItem();
  115.                     $wishitem->setPid($pid);
  116.                     $wishitem->setApiId($apiId);
  117.                     $wishitem->setName($name);
  118.                     $wishitem->setImage($imagePath);
  119.                     $wishitem->setSellingPrice(intval($price));
  120.                 
  121.                     $wishlist->addItem($wishitem);
  122.                     $this->entityManager->persist($wishlist);
  123.                     $status 1;
  124.                 } else {
  125.                     if ( $wishitem $this->wishItemRepository->findOneBy(["wishlist" => $wishlist,"pid" => $pid]) ) {
  126.                         $wishlist->removeItem($wishitem);
  127.                         $status 0;
  128.                     }
  129.                 }
  130.                 
  131.                 $this->entityManager->flush();
  132.                 $session->set("wishlist"$wishlist->itemsPids());
  133.                 
  134.                 return $this->json(['status' => $status]);
  135.             }
  136.         }else{
  137.                 $redirect_url "/".$request->getLocale()."/".$request->get('gender')."/private_customer";
  138.                 return $this->json(["msg" => """status" => -1,"redirect_url" => $redirect_url]);
  139.         }
  140.     }
  141.     #[Route('{_locale}/{gender}/moveToCart'name'app_moveToCart',requirements:["gender" => "mens|women"],defaults:["gender" => "mens"])]
  142.     public function moveToCart(Request $request)
  143.     {
  144.         # 1 - adding item to cart
  145.         if($productId $request->get('id')){
  146.             $product $this->variantRepository->find($productId);
  147.             $productModelId $product->getProduct()->getId(); 
  148.         };
  149.         $productApiId $product->getApiId();
  150.         $productName =  $product->getName();
  151.         $productQuantity $request->get('quantity');
  152.         $productImage $product->getMainImage();
  153.         $productPrice =$product->getPrice();
  154.         
  155.         $session $request->getSession();
  156.         $cart $session->get('cart',[]);
  157.         $total_cart $session->get('total_cart');
  158.         # $imagePath = "/uploads/tbint/".$productApiId."/".$productImage;
  159.         $imagePath $productImage;
  160.         $cart[$productId] = [
  161.             'name' => $productName,
  162.             'quantity' => $productQuantity,
  163.             'image' =>  $imagePath,
  164.             'price' => $productPrice,
  165.             'id' => $productId,
  166.             'total_price' => $productPrice $productQuantity
  167.         ];
  168.         $total_cart array_sum(array_column($cart,'total_price'));
  169.         $session->set('cart',$cart);
  170.         $session->set('total_price',$total_cart);
  171.         # 2 - removing item from whishlist
  172.         $user $this->security->getUser();
  173.         $customer $this->customerRepository->findOneBy(["email" => $user->getEmail()]);
  174.         if($user && $customer){
  175.             $wishlist $this->wishlistRepository->findOneBy(["customer" => $customer]);
  176.             if($wishItem $this->wishItemRepository->findOneBy(["wishlist" => $wishlist,"pid" => $productModelId])) {
  177.                 $wishlist->removeItem($wishItem);
  178.                     $this->entityManager->persist($wishlist);
  179.                     $this->entityManager->flush();
  180.             };
  181.         }
  182.         return $this->json(["product" => $cart[$productId], 'total_price' => $total_cart,"status" => 1]);
  183.     }
  184.     #[Route('{_locale}/{gender}/getFavoriteItemDetails'name'app_favorite_details',requirements:['gender' => 'mens|women'],defaults: ["gender" => "mens"])]
  185.     public function getFavoriteItemDetails(Request $request)
  186.     {
  187.         $variants_json = [];
  188.         $pid $request->get('pid');
  189.         $product $this->productRepository->find($pid);
  190.         $variants $product->getVariants();
  191.         foreach ($variants as $variant) {
  192.             $available $variant->getQuantity() > True False
  193.             $variantImage $variant->getMainImage();
  194.             $item = ['name'=> $variant->getName(),'vid'=> $variant->getId(),"image" => $variantImage,'available'=> $available];
  195.             array_push($variants_json,$item);
  196.         }
  197.         $productImage $product->getMainImage();
  198.         $data = [
  199.                     'variants' => json_encode($variants_json),
  200.                     "productName" => $product->getName(),
  201.                     "productImage" => $productImage,
  202.                     "description" => $product->getDescription(),
  203.                     "price" => $product->firstVariantPrice(),
  204.                 ];
  205.         
  206.         return $this->json($data);
  207.     }
  208.     #[Route('{_locale}/removeFromFavoris',name'app_removeFromFav')]
  209.     public function removeFromFavoris(Request $request)
  210.     {
  211.         $productId $request->get('pid');
  212.         $this->removeItem($productId);
  213.         return $this->json(["msg" => "wishitem deleted""status" => 1]);
  214.     }
  215.     private function removeItem($pid){
  216.         
  217.         if (empty($pid)) {
  218.             throw new \Exception("Product id is not set !!"1);
  219.         }
  220.         $user $this->security->getUser();
  221.         $customer $this->customerRepository->findOneBy(["email" => $user->getEmail()]);
  222.         if($user && $customer){
  223.             $wishlist $this->wishlistRepository->findOneBy(["customer" => $customer]);
  224.             if($wishItem $this->wishItemRepository->findOneBy(["wishlist" => $wishlist,"pid" => $pid])) {
  225.                 $wishlist->removeItem($wishItem);
  226.                     $this->entityManager->persist($wishlist);
  227.                     $this->entityManager->flush();
  228.             };
  229.         }
  230.     }
  231. }