<?php
namespace App\Controller\Front;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\Manager\CartManager;
use App\Entity\Cart;
use App\Entity\CartItem;
use App\Entity\Product;
class CartController extends FrontController
{
public function block(CartManager $cartMgr)
{
$cart = $cartMgr->getCart();
// foreach ($cart->getItems() as $item){
// dump($item->getProduct());
// }
// dump($cart);
return $this->render('front/cart/block.html.twig', [
'cart' => $cart
]);
}
/**
* @Route("/{_locale}/cart/details", name="cart_details", requirements={"_locale":"fr|en"}, options={"utf8": true})
*/
public function details(CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr, \App\Manager\CustomerManager $customerMgr)
{
$cart = $cartMgr->getCart();
$gifts = $em->getRepository('App:Product')->getGifts();
$priceMgr->updateCartPrice($cart);
$discounts = [];
foreach($cart->getItems() as $item){
$product = $item->getProduct();
$discounts[$product->getId()] = $priceMgr->getDiscountsByProduct($product);
}
return $this->render('front/cart/details.html.twig', [
'cart' => $cart,
'gifts' => $gifts,
'discounts' => $discounts
]);
}
/**
* @Route("/{_locale}/cart/empty", name="cart_empty")
*/
public function empty(CartManager $cartMgr)
{
$cartMgr->empty();
return $this->redirectToRoute('index');
}
/**
* @Route("/{_locale}/cart/discounts", name="cart_discounts")
*/
public function discounts(CartManager $cartMgr)
{
$cart = $cartMgr->getCart();
$content = $this->renderView('front/cart/discounts.html.twig', [
'cart' => $cart
]);
return new JsonResponse([
'img' => $content
]);
}
/**
* @Route("/{_locale}/cart/gift/add", name="cart_add_gift")
*/
public function addGift(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\Session $session, \Symfony\Contracts\Translation\TranslatorInterface $trans)
{
$output = ['popup'=>''];
$cart = $cartMgr->getCart();
$qty = 1;
$pid = $request->get('pid',null);
if(!$pid){
throw new NotFoundHttpException ();
}
$product = $em->getRepository(Product::class)->find($pid);
if(empty($product)){
throw new NotFoundHttpException ();
}
if(!$cart->hasGift() && $product->isGift()){
if(!$cartMgr->add($product,1)){
$session->getFlashBag()->add('error', $trans->trans('Une erreur s\'est produite.'));
}
}else{
$session->getFlashBag()->add('error', $trans->trans('Vous disposez déjà d\'un cadeau dans votre panier.'));
}
return $this->redirectToRoute('cart_details');
}
/**
* @Route("/{_locale}/aj/cart/add", name="cart_add")
*/
public function add(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr)
{
$output = ['popup'=>''];
$cart = $cartMgr->getCart();
$qty = $request->get('qty',[]);
$addResult = false;
$addedProducts = [];
$addedQty = [];
if(!is_array($qty))
throw new NotFoundHttpException ();
$error = "";
$success = false;
$cartItem = false;
$productsAdded = [];
foreach($qty as $id_product=>$product_qty){
if(empty($product_qty))
continue;
$product = $em->getRepository(Product::class)->find($id_product);
if(!empty($product)){
$cartItem = $cart->getItemByProduct($product);
$addResult = $cartMgr->add($product,$product_qty);
if($this->_checkCartResponse($addResult)){
$productsAdded[] = $product;
$success = true;
}else{
$error = "Une erreur s'est produite";
}
}
}
$priceMgr->updateCartPrice($cart);
return $this->renderJsonCart($cart, $product, $priceMgr);
}
/**
* @Route("/{_locale}/aj/cart/update", name="cart_update")
*/
public function update(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr)
{
$output = [];
$id = $request->get('id',false);
$qty = $request->get('qty',0);
if($id){
$product = $em->getRepository(Product::class)->find($id);
}
if(empty($product))
throw new NotFoundHttpException ();
$cart = $cartMgr->update($product,$qty);
$priceMgr->updateCartPrice($cart);
return $this->renderJsonCart($cart, $product, $priceMgr);
}
/**
* @Route("/{_locale}/aj/cart/address/{type}", name="cart_update_address", requirements={"type"="shipping|billing"})
*/
public function address(Request $request, $type, CartManager $cartMgr, \App\Manager\CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em)
{
$id = $request->get('id',false);
$type = $request->get('type',false);
$address = null;
$customer = $customerMgr->getCustomer();
if($id){
$address = $em->getRepository('App:Address')->find($id);
}
if($address->getCustomer()!=$customer){
throw new AccessDeniedException ();
}
$cart = $cartMgr->getCart();
$cart = $cartMgr->setAddress($address,$type);
$output['success'] = true;
$output['cart'] = $cart->toArray();
return $this->renderJsonResponse($output);
}
/**
* @Route("/{_locale}/aj/cart/carrier", name="cart_carrier")
*/
public function carrier(Request $request, CartManager $cartMgr, \App\Manager\CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
{
$id = $request->get('id',false);
$cart = $cartMgr->getCart();
$output['success'] = false;
if($request->isMethod('POST') && preg_match('/^[a-zA-Z\-]+$/', $id)){
$carrier = $em->getRepository(\App\Entity\Carrier::class)->findOneByCode($id);
if($carrier){
$output['success'] = true;
$cart = $cartMgr->setCarrier($carrier);
}else{
$output['message'] = $translator->trans('Transporteur inconnu...');
}
}
$output['cart'] = $cart->toArray();
return $this->renderJsonResponse($output);
}
/**
* @Route("/{_locale}/aj/cart/remove/{id}", name="cart_remove", requirements={"id"="\d+"})
*/
public function delete($id, CartManager $cartMgr, \App\Manager\CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr)
{
$output = [
'success' => false
];
$product = $em->getRepository(Product::class)->find($id);
if($product){
$cart = $cartMgr->remove($product);
$success = true;
$output['cart'] = $cart->toArray();
}
$cart = $cartMgr->getCart();
$priceMgr->updateCartPrice($cart);
return $this->renderJsonCart($cart, $product, $priceMgr);
}
/**
* @Route("/{_locale}/aj/cart/add-coupon", name="cart_add_coupon")
*/
public function addCoupon(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\DiscountManager $discountMgr, \Symfony\Contracts\Translation\TranslatorInterface $translator)
{
$output = [
'success' => false,
'error' => ''
];
if($request->isMethod('POST')){
$cart = $cartMgr->getCart();
if($cart->getCustomer()){
$code = $request->get('code');
if($code){
$coupon = $em->getRepository(\App\Entity\Coupon::class)->getByCode($code);
if($coupon){
$output['code'] = $coupon->getCode();
$test = $discountMgr->checkCoupon($coupon, $cartMgr->getCart());
if($test === true){
$output['success'] = $cartMgr->addCoupon($coupon);
$output['line'] = $this->renderView('front/cart/coupon-line.html.twig', [
'coupon'=>$coupon,
'cart'=>$cart
]);
$output['cart'] = $cart->toArray();
}else{
$output['error'] = $test;
}
}else{
$output['error'] = $translator->trans('Coupon non valide');
}
}else{
$output['error'] = $translator->trans('Coupon non valide');
}
}else{
$output['error'] = $translator->trans('Veuillez vous identifier.');
}
}
return new JsonResponse($output);
}
/**
* @Route("/{_locale}/aj/cart/remove-coupon", name="cart_remove_coupon")
*/
public function removeCoupon(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\DiscountManager $discountMgr, \Symfony\Contracts\Translation\TranslatorInterface $translator)
{
$output = [
'success' => false,
'error' => ''
];
if($request->isMethod('POST')){
$cart = $cartMgr->getCart();
if($cart->getCustomer()){
$id = $request->get('id');
if($id){
$coupon = $em->getRepository(\App\Entity\Coupon::class)->find($id);
$cart->removeCoupon($coupon);
$cartMgr->save($cart);
$output['success'] = true;
$output['cart'] = $cart->toArray();
}
}
}
return new JsonResponse($output);
}
protected function getCartBlock($cart, $route='')
{
return $this->renderView('front/cart/block.html.twig',['cart'=>$cart,'route'=>$route]);
}
protected function getCartLine(CartItem $cartItem)
{
$discounts = [
$cartItem->getProduct()->getId() => $this->priceMgr->getDiscountsByProduct($cartItem->getProduct())
];
return $this->renderView('front/cart/cart-line.html.twig',[
'item' => $cartItem,
'discounts' => $discounts
]);
}
protected function getDiscountInfos(CartItem $cartItem)
{
if($cartItem->getMarketingRule()) {
return $this->renderView('front/catalog/product/view/discount.html.twig',[
'discount' => $cartItem->getMarketingRule()
]);
}else if($cartItem->getDiscountType()) {
return $this->renderView('front/catalog/product/view/discount-type.html.twig',[
'discountType' => $cartItem->getDiscountType()
]);
}
return '';
}
protected function getDiscounts(Cart $cart)
{
$discounts = [];
foreach ($cart->getItems() as $item) {
if($item->getMarketingRule()) {
$discounts[$item->getProduct()->getId()] = $this->renderView('front/catalog/product/view/discount.html.twig',[
'discount' => $item->getMarketingRule()
]);
}else if($item->getDiscountType()) {
$discounts[$item->getProduct()->getId()] = $this->renderView('front/catalog/product/view/discount-type.html.twig',[
'discountType' => $item->getDiscountType()
]);
}
}
return $discounts;
}
protected function _checkCartResponse($response)
{
return is_object($response) && (get_class($response)=='App\Entity\Cart');
}
protected function getPriceGrids(Product $product, \App\Manager\PriceManager $priceMgr) {
$output = [];
$cart = $this->cartMgr->getCart();
$priceGroup = $this->cartMgr->getPriceContext();
if($product->hasParent()){
$parent = $product->getParent();
foreach($parent->getChildren() as $child){
$grid = $priceMgr->getPriceGrid($child);
$output[] = $this->renderView('front/catalog/product/view/price-grid.html.twig', [
'priceGrid' => $grid,
'product' => $child,
'cart' => $cart,
'priceGroup' => $priceGroup
]);
}
}else{
$grid = $priceMgr->getPriceGrid($product);
$output[] = $this->renderView('front/catalog/product/view/price-grid.html.twig', [
'priceGrid' => $grid,
'product' => $product,
'cart' => $cart,
'priceGroup' => $priceGroup
]);
}
return $output;
}
protected function renderJsonCart(Cart $cart, Product $product, \App\Manager\PriceManager $priceMgr) {
$output['success'] = true;
$output['cart'] = $cart->toArray();
$output['product'] = $product->toArray();
$output['blockCart'] = $this->getCartBlock($cart, null);
$item = $cart->getItemByProduct($product);
$output['cartLine'] = $item ? $this->getCartLine($item) : '';
$output['discountLine'] = $item ? $this->getDiscountInfos($item) : '';
$output['discounts'] = $this->getDiscounts($cart);
// $output['newItem'] = $this->renderView('front/cart/block-item.html.twig', [$item => $cart->getItemByProduct($product)]);
$output['currentQty'] = $cart->getItemQuantityByProduct($product);
$output['priceGrids'] = $this->getPriceGrids($product, $priceMgr);
return $this->renderJsonResponse($output);
}
}