src/Controller/FavorisController.php line 62
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\SecurityBundle\Security;
use App\Repository\WishItemRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\ProductRepository;
use App\Repository\ProductCollectionRepository;
use App\Repository\CategoryRepository;
use App\Repository\ProductVariantRepository;
use App\Repository\OptionValueRepository;
use App\Repository\CustomerRepository;
use App\Repository\WishlistRepository;
use App\Entity\WishItem;
use App\Entity\NewsletterSubscriber;
use App\Form\NewsletterSubscriberType;
class FavorisController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
private ProductRepository $productRepository,
private ProductCollectionRepository $collectionRepository,
private CategoryRepository $categoryRepository,
private ProductVariantRepository $variantRepository,
private OptionValueRepository $optionRepository,
private Security $security,
private CustomerRepository $customerRepository,
private WishlistRepository $wishlistRepository,
private WishItemRepository $wishItemRepository,
)
{
}
#[Route('/{_locale}/{gender}/favoris', name: 'app_favoris_index',requirements:['gender'=> "mens|women"],defaults:["gender" => "mens"])]
#[Route('/{_locale}/favoris',name: 'app_favoris_index_index')]
public function favoris(Request $request)
{
$session = $request->getSession();
$gender = $request->get('gender');
$genderId = $gender == "mens" ? 1 : 2;
//all categories
// $categories = $this->categoryRepository->findAll();
//main categories (level 1)
$mainCategories = $this->categoryRepository->findBy(["level" => 1]);
//Men sub categories (level 2)
$subCategories = $gender ? $this->categoryRepository->findBy(["parentCategory" => $genderId,"level" => 2]) : null;
$favoris = $session->get('favoris',[]);
$user = $this->security->getUser();
if($user){
if($customer = $this->customerRepository->findOneBy(['email' => $user->getEmail()])){
$wishlist = $this->wishlistRepository->findOneBy(["customer" => $customer]);
$favItems = $wishlist->getItems();
}else{
$favItems = null;
}
}else{
return $this->redirectToRoute('customer_login');
}
//TODO: this reccurent template elements should be generated using a template service class
// $menuCategories = $this->categoryRepository->findAll();
$newsLetterSubscriber = new NewsletterSubscriber();
$form = $this->createForm(NewsletterSubscriberType::class,$newsLetterSubscriber);
// newsltetter
$form->handleRequest($request);
// dd($form);
if($form->isSubmitted() && $form->isValid()){
$newsLetterSubscriber = $form->getData();
$this->entityManager->persist($newsLetterSubscriber);
$this->entityManager->flush();
}
if($gender){
$templatePath = $genderId == 1 ? 'mens/favoris/favoris.twig' : 'women/favoris/favoris.twig';
}else{
$templatePath = 'front_end/favoris/favoris.twig';
}
//END TEMPLATE ELEMENTS
return $this->render($templatePath, [
'controller_name' => 'FrontEndController',
'favItems' => $favItems,
// 'categories' => $menuCategories,
'form' => $form,
'pageName' => 'Favoris',
'mainCategories' => $mainCategories,
'subCategories' => $subCategories,
// 'categories' => $categories,
'activePage' => getenv('GENDER')
]);
}
#[Route('{_locale}/{gender}/addToFavoris', name: 'app_add_to_favoris',requirements:["gender" => "mens|women"],defaults:["gender" => "mens"])]
public function addToFavoris(Request $request,Security $security): Response
{
// GET current user
$user = $security->getUser();
if($user and in_array("ROLE_CUSTOMER",$user->getRoles())){
if ($customer = $this->customerRepository->findOneBy(['email' => $user->getEmail()])) {
if($productId = $request->get('id')) {
$product = $this->productRepository->find($productId);
$pid = $product->getId();
$apiId = $product->getApiId();
$name = $product->getName();
$price = $product->firstVariantPrice();
$image = $product->getMainImage();
$imagePath = $image;
}
$wishlist = $this->wishlistRepository->findOneBy(['customer' => $customer]);
$session = $request->getSession();
if (!in_array($pid, $wishlist->itemsPids())) {
$wishitem = new WishItem();
$wishitem->setPid($pid);
$wishitem->setApiId($apiId);
$wishitem->setName($name);
$wishitem->setImage($imagePath);
$wishitem->setSellingPrice(intval($price));
$wishlist->addItem($wishitem);
$this->entityManager->persist($wishlist);
$status = 1;
} else {
if ( $wishitem = $this->wishItemRepository->findOneBy(["wishlist" => $wishlist,"pid" => $pid]) ) {
$wishlist->removeItem($wishitem);
$status = 0;
}
}
$this->entityManager->flush();
$session->set("wishlist", $wishlist->itemsPids());
return $this->json(['status' => $status]);
}
}else{
$redirect_url = "/".$request->getLocale()."/".$request->get('gender')."/private_customer";
return $this->json(["msg" => "", "status" => -1,"redirect_url" => $redirect_url]);
}
}
#[Route('{_locale}/{gender}/moveToCart', name: 'app_moveToCart',requirements:["gender" => "mens|women"],defaults:["gender" => "mens"])]
public function moveToCart(Request $request)
{
# 1 - adding item to cart
if($productId = $request->get('id')){
$product = $this->variantRepository->find($productId);
$productModelId = $product->getProduct()->getId();
};
$productApiId = $product->getApiId();
$productName = $product->getName();
$productQuantity = $request->get('quantity');
$productImage = $product->getMainImage();
$productPrice =$product->getPrice();
$session = $request->getSession();
$cart = $session->get('cart',[]);
$total_cart = $session->get('total_cart');
# $imagePath = "/uploads/tbint/".$productApiId."/".$productImage;
$imagePath = $productImage;
$cart[$productId] = [
'name' => $productName,
'quantity' => $productQuantity,
'image' => $imagePath,
'price' => $productPrice,
'id' => $productId,
'total_price' => $productPrice * $productQuantity
];
$total_cart = array_sum(array_column($cart,'total_price'));
$session->set('cart',$cart);
$session->set('total_price',$total_cart);
# 2 - removing item from whishlist
$user = $this->security->getUser();
$customer = $this->customerRepository->findOneBy(["email" => $user->getEmail()]);
if($user && $customer){
$wishlist = $this->wishlistRepository->findOneBy(["customer" => $customer]);
if($wishItem = $this->wishItemRepository->findOneBy(["wishlist" => $wishlist,"pid" => $productModelId])) {
$wishlist->removeItem($wishItem);
$this->entityManager->persist($wishlist);
$this->entityManager->flush();
};
}
return $this->json(["product" => $cart[$productId], 'total_price' => $total_cart,"status" => 1]);
}
#[Route('{_locale}/{gender}/getFavoriteItemDetails', name: 'app_favorite_details',requirements:['gender' => 'mens|women'],defaults: ["gender" => "mens"])]
public function getFavoriteItemDetails(Request $request)
{
$variants_json = [];
$pid = $request->get('pid');
$product = $this->productRepository->find($pid);
$variants = $product->getVariants();
foreach ($variants as $variant) {
$available = $variant->getQuantity() > 0 ? True : False;
$variantImage = $variant->getMainImage();
$item = ['name'=> $variant->getName(),'vid'=> $variant->getId(),"image" => $variantImage,'available'=> $available];
array_push($variants_json,$item);
}
$productImage = $product->getMainImage();
$data = [
'variants' => json_encode($variants_json),
"productName" => $product->getName(),
"productImage" => $productImage,
"description" => $product->getDescription(),
"price" => $product->firstVariantPrice(),
];
return $this->json($data);
}
#[Route('{_locale}/removeFromFavoris',name: 'app_removeFromFav')]
public function removeFromFavoris(Request $request)
{
$productId = $request->get('pid');
$this->removeItem($productId);
return $this->json(["msg" => "wishitem deleted", "status" => 1]);
}
private function removeItem($pid){
if (empty($pid)) {
throw new \Exception("Product id is not set !!", 1);
}
$user = $this->security->getUser();
$customer = $this->customerRepository->findOneBy(["email" => $user->getEmail()]);
if($user && $customer){
$wishlist = $this->wishlistRepository->findOneBy(["customer" => $customer]);
if($wishItem = $this->wishItemRepository->findOneBy(["wishlist" => $wishlist,"pid" => $pid])) {
$wishlist->removeItem($wishItem);
$this->entityManager->persist($wishlist);
$this->entityManager->flush();
};
}
}
}