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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Frontend ;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  5. use Lc\ShopBundle\Context\OrderUtilsInterface;
  6. use Lc\ShopBundle\Context\ProductFamilyInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Security\Core\Security;
  10. class FavoriteController extends BaseController
  11. {
  12. protected $productFamilyRepository ;
  13. public function __construct(EntityManagerInterface $em, Security $security, MerchantUtilsInterface $merchantUtils)
  14. {
  15. parent::__construct($em, $security, $merchantUtils);
  16. $this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
  17. }
  18. public function toggle(Request $request)
  19. {
  20. $user = $this->_getUser() ;
  21. $productFamily = $this->_getProductFamily($request) ;
  22. if($user->getFavoriteProductFamilies()->contains($productFamily)) {
  23. $user->removeFavoriteProductFamily($productFamily) ;
  24. $isFavorite = false ;
  25. $message = 'Le produit a bien été supprimé de vos favoris' ;
  26. }
  27. else {
  28. $user->addFavoriteProductFamily($productFamily) ;
  29. $isFavorite = true ;
  30. $message = 'Le produit a bien été ajouté à vos favoris' ;
  31. }
  32. $this->_saveUser($user) ;
  33. return new JsonResponse([
  34. 'return' => 'success',
  35. 'is_favorite' => $isFavorite,
  36. 'message' => $message
  37. ]) ;
  38. }
  39. public function add(Request $request)
  40. {
  41. $user = $this->_getUser() ;
  42. $productFamily = $this->_getProductFamily($request) ;
  43. $user->addFavoriteProductFamily($productFamily) ;
  44. $this->_saveUser($user) ;
  45. return new JsonResponse([
  46. 'return' => 'success',
  47. 'message' => 'Le produit a bien été ajouté à vos favoris'
  48. ]) ;
  49. }
  50. public function delete(Request $request)
  51. {
  52. $user = $this->_getUser() ;
  53. $productFamily = $this->_getProductFamily($request) ;
  54. $user->removeFavoriteProductFamily($productFamily) ;
  55. $this->_saveUser($user) ;
  56. return new JsonResponse([
  57. 'return' => 'success',
  58. 'message' => 'Le produit a bien été supprimé de vos favoris'
  59. ]) ;
  60. }
  61. private function _getUser()
  62. {
  63. $user = $this->security->getUser() ;
  64. if(!$user) {
  65. throw new \ErrorException('Vous devez être connecté pour gérer vos favoris') ;
  66. }
  67. return $user ;
  68. }
  69. private function _saveUser($user)
  70. {
  71. $this->em->persist($user);
  72. $this->em->flush() ;
  73. }
  74. private function _getProductFamily($request)
  75. {
  76. $idProductFamily = $request->request->get('idProductFamily') ;
  77. $productFamily = $this->productFamilyRepository->find($idProductFamily) ;
  78. if($productFamily) {
  79. return $productFamily ;
  80. }
  81. else {
  82. throw new \ErrorException('Ce produit est introuvable') ;
  83. }
  84. }
  85. }