You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 satır
3.3KB

  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->getUserCurrent();
  15. $productFamily = $this->_getProductFamily($request);
  16. if($user && $productFamily) {
  17. if ($user->getFavoriteProductFamilies()->contains($productFamily)) {
  18. $user->removeFavoriteProductFamily($productFamily);
  19. $isFavorite = false;
  20. $message = 'Le produit a bien été supprimé de vos favoris';
  21. } else {
  22. $user->addFavoriteProductFamily($productFamily);
  23. $isFavorite = true;
  24. $message = 'Le produit a bien été ajouté à vos favoris';
  25. }
  26. $this->_saveUser($user);
  27. return new JsonResponse([
  28. 'return' => 'success',
  29. 'is_favorite' => $isFavorite,
  30. 'message' => $message
  31. ]);
  32. }
  33. else {
  34. return $this->_errorResponse();
  35. }
  36. }
  37. /**
  38. * @Route("/favorite/add", name="lc_frontend_favorite_add")
  39. */
  40. public function add(Request $request)
  41. {
  42. $user = $this->getUserCurrent();
  43. $productFamily = $this->_getProductFamily($request);
  44. if($user && $productFamily) {
  45. $user->addFavoriteProductFamily($productFamily);
  46. $this->_saveUser($user);
  47. return new JsonResponse([
  48. 'return' => 'success',
  49. 'message' => 'Le produit a bien été ajouté à vos favoris'
  50. ]);
  51. }
  52. else {
  53. return $this->_errorResponse();
  54. }
  55. }
  56. /**
  57. * @Route("/favorite/delete", name="lc_frontend_favorite_delete")
  58. */
  59. public function delete(Request $request)
  60. {
  61. $user = $this->getUserCurrent();
  62. $productFamily = $this->_getProductFamily($request);
  63. if($user && $productFamily) {
  64. $user->removeFavoriteProductFamily($productFamily);
  65. $this->_saveUser($user);
  66. return new JsonResponse([
  67. 'return' => 'success',
  68. 'message' => 'Le produit a bien été supprimé de vos favoris'
  69. ]);
  70. }
  71. else {
  72. return $this->_errorResponse();
  73. }
  74. }
  75. private function _saveUser($user)
  76. {
  77. $entityManager = $this->getEntityManager();
  78. $entityManager->update($user);
  79. $entityManager->flush();
  80. }
  81. private function _getProductFamily($request)
  82. {
  83. $idProductFamily = $request->request->get('idProductFamily');
  84. if($idProductFamily) {
  85. $productFamily = $this->getProductFamilyContainer()->getStore()->getOneById($idProductFamily);
  86. }
  87. if (isset($productFamily) && $productFamily) {
  88. return $productFamily;
  89. }
  90. }
  91. private function _errorResponse()
  92. {
  93. return new JsonResponse([
  94. 'return' => 'error',
  95. 'message' => 'Une erreur est survenue.'
  96. ]);
  97. }
  98. }