@@ -5,15 +5,18 @@ namespace Lc\ShopBundle\Controller\Frontend ; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | |||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |||
use Symfony\Component\Security\Core\Security; | |||
class BaseController extends AbstractController | |||
{ | |||
protected $em ; | |||
protected $merchantUtils ; | |||
protected $security ; | |||
public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils) | |||
public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils) | |||
{ | |||
$this->em = $em ; | |||
$this->security = $security ; | |||
$this->merchantUtils = $merchantUtils ; | |||
} | |||
@@ -11,6 +11,7 @@ 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 CartController extends BaseController | |||
{ | |||
@@ -21,9 +22,9 @@ class CartController extends BaseController | |||
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->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ; | |||
} |
@@ -0,0 +1,104 @@ | |||
<?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') ; | |||
} | |||
} | |||
} |
@@ -3,6 +3,7 @@ | |||
namespace Lc\ShopBundle\Model; | |||
use App\Entity\Newsletter; | |||
use App\Entity\ProductFamily; | |||
use Doctrine\Common\Collections\ArrayCollection; | |||
use Doctrine\Common\Collections\Collection; | |||
use Doctrine\ORM\Mapping as ORM; | |||
@@ -70,6 +71,11 @@ abstract class User extends UserModelFOS | |||
*/ | |||
protected $newsletters; | |||
/** | |||
* @ORM\ManyToMany(targetEntity="Lc\ShopBundle\Context\ProductFamilyInterface") | |||
*/ | |||
protected $favoriteProductFamilies; | |||
public function __construct() | |||
{ | |||
parent::__construct(); | |||
@@ -78,6 +84,7 @@ abstract class User extends UserModelFOS | |||
$this->orders = new ArrayCollection(); | |||
$this->carts = new ArrayCollection(); | |||
$this->newsletters = new ArrayCollection(); | |||
$this->favoriteProductFamilies = new ArrayCollection(); | |||
} | |||
public function setEmail($email) | |||
@@ -307,4 +314,30 @@ abstract class User extends UserModelFOS | |||
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; | |||
} | |||
} |
@@ -25,4 +25,16 @@ lc_frontend_cart_delete_product: | |||
lc_frontend_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 |