Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

FavoriteController.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\Product;
  3. use Lc\CaracoleBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. class FavoriteController extends AbstractController
  8. {
  9. /**
  10. * @Route("/favorite/toggle", name="lc_frontend_favorite_toggle")
  11. */
  12. public function toggle(Request $request)
  13. {
  14. $user = $this->_getUser();
  15. $productFamily = $this->_getProductFamily($request);
  16. if ($user->getFavoriteProductFamilies()->contains($productFamily)) {
  17. $user->removeFavoriteProductFamily($productFamily);
  18. $isFavorite = false;
  19. $message = 'Le produit a bien été supprimé de vos favoris';
  20. } else {
  21. $user->addFavoriteProductFamily($productFamily);
  22. $isFavorite = true;
  23. $message = 'Le produit a bien été ajouté à vos favoris';
  24. }
  25. $this->_saveUser($user);
  26. return new JsonResponse([
  27. 'return' => 'success',
  28. 'is_favorite' => $isFavorite,
  29. 'message' => $message
  30. ]);
  31. }
  32. /**
  33. * @Route("/favorite/add", name="lc_frontend_favorite_add")
  34. */
  35. public function add(Request $request)
  36. {
  37. $user = $this->_getUser();
  38. $productFamily = $this->_getProductFamily($request);
  39. $user->addFavoriteProductFamily($productFamily);
  40. $this->_saveUser($user);
  41. return new JsonResponse([
  42. 'return' => 'success',
  43. 'message' => 'Le produit a bien été ajouté à vos favoris'
  44. ]);
  45. }
  46. /**
  47. * @Route("/favorite/delete", name="lc_frontend_favorite_delete")
  48. */
  49. public function delete(Request $request)
  50. {
  51. $user = $this->_getUser();
  52. $productFamily = $this->_getProductFamily($request);
  53. $user->removeFavoriteProductFamily($productFamily);
  54. $this->_saveUser($user);
  55. return new JsonResponse([
  56. 'return' => 'success',
  57. 'message' => 'Le produit a bien été supprimé de vos favoris'
  58. ]);
  59. }
  60. private function _getUser()
  61. {
  62. $user = $this->getUserCurrent();
  63. if (!$user) {
  64. throw new \ErrorException('Vous devez être connecté pour gérer vos favoris');
  65. }
  66. return $user;
  67. }
  68. private function _saveUser($user)
  69. {
  70. $entityManager = $this->getEntityManager();
  71. $entityManager->persist($user);
  72. $entityManager->flush();
  73. }
  74. private function _getProductFamily($request)
  75. {
  76. $idProductFamily = $request->request->get('idProductFamily');
  77. if($idProductFamily) {
  78. $productFamily = $this->getProductFamilyContainer()->getStore()->getOneById($idProductFamily);
  79. }
  80. if (isset($productFamily) && $productFamily) {
  81. return $productFamily;
  82. } else {
  83. throw new \ErrorException('Ce produit est introuvable');
  84. }
  85. }
  86. }