use Doctrine\ORM\EntityManagerInterface; | use Doctrine\ORM\EntityManagerInterface; | ||||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | use Lc\ShopBundle\Context\MerchantUtilsInterface; | ||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||||
use Symfony\Component\Security\Core\Security; | |||||
class BaseController extends AbstractController | class BaseController extends AbstractController | ||||
{ | { | ||||
protected $em ; | protected $em ; | ||||
protected $merchantUtils ; | protected $merchantUtils ; | ||||
protected $security ; | |||||
public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils) | |||||
public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils) | |||||
{ | { | ||||
$this->em = $em ; | $this->em = $em ; | ||||
$this->security = $security ; | |||||
$this->merchantUtils = $merchantUtils ; | $this->merchantUtils = $merchantUtils ; | ||||
} | } | ||||
use Lc\ShopBundle\Context\ProductFamilyInterface; | use Lc\ShopBundle\Context\ProductFamilyInterface; | ||||
use Symfony\Component\HttpFoundation\JsonResponse; | use Symfony\Component\HttpFoundation\JsonResponse; | ||||
use Symfony\Component\HttpFoundation\Request; | use Symfony\Component\HttpFoundation\Request; | ||||
use Symfony\Component\Security\Core\Security; | |||||
class CartController extends BaseController | class CartController extends BaseController | ||||
{ | { | ||||
protected $orderProducts = [] ; | protected $orderProducts = [] ; | ||||
public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils) | |||||
public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils, OrderUtilsInterface $orderUtils) | |||||
{ | { | ||||
parent::__construct($em, $merchantUtils); | |||||
parent::__construct($em, $security, $merchantUtils); | |||||
$this->orderUtils = $orderUtils ; | $this->orderUtils = $orderUtils ; | ||||
$this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ; | $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ; | ||||
} | } |
<?php | |||||
namespace Lc\ShopBundle\Controller\Frontend ; | |||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | |||||
use Lc\ShopBundle\Context\OrderUtilsInterface; | |||||
use Lc\ShopBundle\Context\ProductFamilyInterface; | |||||
use Symfony\Component\HttpFoundation\JsonResponse; | |||||
use Symfony\Component\HttpFoundation\Request; | |||||
use Symfony\Component\Security\Core\Security; | |||||
class FavoriteController extends BaseController | |||||
{ | |||||
protected $productFamilyRepository ; | |||||
public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils) | |||||
{ | |||||
parent::__construct($em, $security, $merchantUtils); | |||||
$this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ; | |||||
} | |||||
public function toggle(Request $request) | |||||
{ | |||||
$user = $this->_getUser() ; | |||||
$productFamily = $this->_getProductFamily($request) ; | |||||
if($user->getFavoriteProductFamilies()->contains($productFamily)) { | |||||
$user->removeFavoriteProductFamily($productFamily) ; | |||||
$isFavorite = false ; | |||||
$message = 'Le produit a bien été supprimé de vos favoris' ; | |||||
} | |||||
else { | |||||
$user->addFavoriteProductFamily($productFamily) ; | |||||
$isFavorite = true ; | |||||
$message = 'Le produit a bien été ajouté à vos favoris' ; | |||||
} | |||||
$this->_saveUser($user) ; | |||||
return new JsonResponse([ | |||||
'return' => 'success', | |||||
'is_favorite' => $isFavorite, | |||||
'message' => $message | |||||
]) ; | |||||
} | |||||
public function add(Request $request) | |||||
{ | |||||
$user = $this->_getUser() ; | |||||
$productFamily = $this->_getProductFamily($request) ; | |||||
$user->addFavoriteProductFamily($productFamily) ; | |||||
$this->_saveUser($user) ; | |||||
return new JsonResponse([ | |||||
'return' => 'success', | |||||
'message' => 'Le produit a bien été ajouté à vos favoris' | |||||
]) ; | |||||
} | |||||
public function delete(Request $request) | |||||
{ | |||||
$user = $this->_getUser() ; | |||||
$productFamily = $this->_getProductFamily($request) ; | |||||
$user->removeFavoriteProductFamily($productFamily) ; | |||||
$this->_saveUser($user) ; | |||||
return new JsonResponse([ | |||||
'return' => 'success', | |||||
'message' => 'Le produit a bien été supprimé de vos favoris' | |||||
]) ; | |||||
} | |||||
private function _getUser() | |||||
{ | |||||
$user = $this->security->getUser() ; | |||||
if(!$user) { | |||||
throw new \ErrorException('Vous devez être connecté pour gérer vos favoris') ; | |||||
} | |||||
return $user ; | |||||
} | |||||
private function _saveUser($user) | |||||
{ | |||||
$this->em->persist($user); | |||||
$this->em->flush() ; | |||||
} | |||||
private function _getProductFamily($request) | |||||
{ | |||||
$idProductFamily = $request->request->get('idProductFamily') ; | |||||
$productFamily = $this->productFamilyRepository->find($idProductFamily) ; | |||||
if($productFamily) { | |||||
return $productFamily ; | |||||
} | |||||
else { | |||||
throw new \ErrorException('Ce produit est introuvable') ; | |||||
} | |||||
} | |||||
} |
namespace Lc\ShopBundle\Model; | namespace Lc\ShopBundle\Model; | ||||
use App\Entity\Newsletter; | use App\Entity\Newsletter; | ||||
use App\Entity\ProductFamily; | |||||
use Doctrine\Common\Collections\ArrayCollection; | use Doctrine\Common\Collections\ArrayCollection; | ||||
use Doctrine\Common\Collections\Collection; | use Doctrine\Common\Collections\Collection; | ||||
use Doctrine\ORM\Mapping as ORM; | use Doctrine\ORM\Mapping as ORM; | ||||
*/ | */ | ||||
protected $newsletters; | protected $newsletters; | ||||
/** | |||||
* @ORM\ManyToMany(targetEntity="Lc\ShopBundle\Context\ProductFamilyInterface") | |||||
*/ | |||||
protected $favoriteProductFamilies; | |||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
parent::__construct(); | parent::__construct(); | ||||
$this->orders = new ArrayCollection(); | $this->orders = new ArrayCollection(); | ||||
$this->carts = new ArrayCollection(); | $this->carts = new ArrayCollection(); | ||||
$this->newsletters = new ArrayCollection(); | $this->newsletters = new ArrayCollection(); | ||||
$this->favoriteProductFamilies = new ArrayCollection(); | |||||
} | } | ||||
public function setEmail($email) | public function setEmail($email) | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
* @return Collection|ProductFamily[] | |||||
*/ | |||||
public function getFavoriteProductFamilies(): Collection | |||||
{ | |||||
return $this->favoriteProductFamilies; | |||||
} | |||||
public function addFavoriteProductFamily(ProductFamily $favoriteProductFamily): self | |||||
{ | |||||
if (!$this->favoriteProductFamilies->contains($favoriteProductFamily)) { | |||||
$this->favoriteProductFamilies[] = $favoriteProductFamily; | |||||
} | |||||
return $this; | |||||
} | |||||
public function removeFavoriteProductFamily(ProductFamily $favoriteProductFamily): self | |||||
{ | |||||
if ($this->favoriteProductFamilies->contains($favoriteProductFamily)) { | |||||
$this->favoriteProductFamilies->removeElement($favoriteProductFamily); | |||||
} | |||||
return $this; | |||||
} | |||||
} | } |
lc_frontend_cart_summary: | lc_frontend_cart_summary: | ||||
path: /lc/cart/summary | path: /lc/cart/summary | ||||
controller: Lc\ShopBundle\Controller\Frontend\CartController::summary | |||||
controller: Lc\ShopBundle\Controller\Frontend\CartController::summary | |||||
lc_frontend_favorite_toggle: | |||||
path: /lc/favorite/toggle | |||||
controller: Lc\ShopBundle\Controller\Frontend\FavoriteController::toggle | |||||
lc_frontend_favorite_add: | |||||
path: /lc/favorite/add | |||||
controller: Lc\ShopBundle\Controller\Frontend\FavoriteController::add | |||||
lc_frontend_favorite_delete: | |||||
path: /lc/favorite/delete | |||||
controller: Lc\ShopBundle\Controller\Frontend\FavoriteController::delete |