|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public function toggle(Request $request) |
|
|
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'; |
|
|
|
|
|
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
|
|
if($user) { |
|
|
|
|
|
$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 |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
return $this->_errorUserNotConnected(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
|
|
'return' => 'success', |
|
|
|
|
|
'is_favorite' => $isFavorite, |
|
|
|
|
|
'message' => $message |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public function add(Request $request) |
|
|
public function add(Request $request) |
|
|
{ |
|
|
{ |
|
|
$user = $this->_getUser(); |
|
|
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
|
|
if($user) { |
|
|
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
|
$user->addFavoriteProductFamily($productFamily); |
|
|
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
|
$user->addFavoriteProductFamily($productFamily); |
|
|
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
|
|
'return' => 'success', |
|
|
|
|
|
'message' => 'Le produit a bien été ajouté à vos favoris' |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
|
|
'return' => 'success', |
|
|
|
|
|
'message' => 'Le produit a bien été ajouté à vos favoris' |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
return $this->_errorUserNotConnected(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public function delete(Request $request) |
|
|
public function delete(Request $request) |
|
|
{ |
|
|
{ |
|
|
$user = $this->_getUser(); |
|
|
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
|
|
|
|
$user->removeFavoriteProductFamily($productFamily); |
|
|
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
|
|
if($user) { |
|
|
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
|
|
'return' => 'success', |
|
|
|
|
|
'message' => 'Le produit a bien été supprimé de vos favoris' |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
$user->removeFavoriteProductFamily($productFamily); |
|
|
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
|
private function _getUser() |
|
|
|
|
|
{ |
|
|
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
|
|
if (!$user) { |
|
|
|
|
|
throw new \ErrorException('Vous devez être connecté pour gérer vos favoris'); |
|
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
|
|
'return' => 'success', |
|
|
|
|
|
'message' => 'Le produit a bien été supprimé de vos favoris' |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
return $this->_errorUserNotConnected(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return $user; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private function _saveUser($user) |
|
|
private function _saveUser($user) |
|
|
{ |
|
|
{ |
|
|
$entityManager = $this->getEntityManager(); |
|
|
$entityManager = $this->getEntityManager(); |
|
|
$entityManager->persist($user); |
|
|
|
|
|
|
|
|
$entityManager->update($user); |
|
|
$entityManager->flush(); |
|
|
$entityManager->flush(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($productFamily) && $productFamily) { |
|
|
if (isset($productFamily) && $productFamily) { |
|
|
return $productFamily; |
|
|
return $productFamily; |
|
|
} else { |
|
|
|
|
|
throw new \ErrorException('Ce produit est introuvable'); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private function _errorUserNotConnected() |
|
|
|
|
|
{ |
|
|
|
|
|
return new JsonResponse([ |
|
|
|
|
|
'return' => 'error', |
|
|
|
|
|
'message' => 'Vous devez être connecté pour gérer vos favoris.' |
|
|
|
|
|
]); |
|
|
|
|
|
} |
|
|
} |
|
|
} |