Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

108 lines
5.0KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Frontend ;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use FOS\UserBundle\Util\UserManipulator;
  5. use Lc\ShopBundle\Context\GlobalParamInterface;
  6. use Lc\ShopBundle\Form\Frontend\NewsletterType;
  7. use Lc\ShopBundle\Repository\NewsletterRepository;
  8. use Lc\ShopBundle\Repository\UserRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Form\FormError;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. class NewsletterController extends AbstractController
  14. {
  15. protected $em ;
  16. protected $userRepository ;
  17. protected $globalParam ;
  18. protected $newsletterRepository ;
  19. public function __construct(EntityManagerInterface $em, UserRepository $userRepository, NewsletterRepository $newsletterRepository, GlobalParamInterface $globalParam)
  20. {
  21. $this->em = $em ;
  22. $this->userRepository = $userRepository ;
  23. $this->newsletterRepository = $newsletterRepository ;
  24. $this->globalParam = $globalParam ;
  25. }
  26. public function subscribe(Request $request, UserManipulator $userManipulator)
  27. {
  28. $form = $this->createForm(NewsletterType::class);
  29. $form->handleRequest($request);
  30. if ($form->isSubmitted() && $form->isValid()) {
  31. $data = $form->getData() ;
  32. $email = $data['email'] ;
  33. $userExist = $this->userRepository->findOneBy(['email' => $email]) ;
  34. $newsletter = $this->globalParam->getCurrentMerchant()->getNewsletter() ;
  35. if($newsletter) {
  36. $userSubscribe = false ;
  37. if($userExist) {
  38. if($this->getUser() && $userExist && $this->getUser() == $userExist) {
  39. if($userExist->getNewsletters()->contains($newsletter)) {
  40. $this->addFlash('error','Vous êtes déjà inscrit à cette newsletter.');
  41. }
  42. else {
  43. $userExist->addNewsletter($newsletter) ;
  44. $userSubscribe = true ;
  45. $this->em->persist($userExist);
  46. }
  47. }
  48. else {
  49. $form->get('email')->addError(new FormError('Cette adresse email est déjà utilisée')) ;
  50. }
  51. }
  52. else {
  53. $user = $userManipulator->create($email, '9e438d8a7b036ea9b8d4375377d47e1a', $email, false, false) ;
  54. $user->addNewsletter($newsletter) ;
  55. $user->addRole('ROLE_NEWSLETTER') ;
  56. $this->em->persist($user);
  57. $userSubscribe = true ;
  58. }
  59. if($userSubscribe) {
  60. $this->addFlash('success','Vous avez bien été ajouté à la newsletter.');
  61. $this->em->flush() ;
  62. return $this->redirectToRoute('frontend_newsletter_subscribe') ;
  63. }
  64. }
  65. else {
  66. throw new \ErrorException('Aucune newsletter définie.') ;
  67. }
  68. }
  69. return $this->render('frontend/site/newsletter.html.twig', [
  70. 'form' => $form->createView()
  71. ]);
  72. }
  73. public function unsubscribe($idUser, $idNewsletter)
  74. {
  75. $user = $this->userRepository->find($idUser) ;
  76. $newsletter = $this->newsletterRepository->find($idNewsletter) ;
  77. if(!$user) {
  78. throw new NotFoundHttpException('Utilisateur introuvable') ;
  79. }
  80. if(!$newsletter) {
  81. throw new NotFoundHttpException('Newsletter introuvable') ;
  82. }
  83. if($user->getNewsletters()->contains($newsletter)) {
  84. $user->removeNewsletter($newsletter) ;
  85. $this->em->persist($user);
  86. $this->em->flush() ;
  87. return $this->render('frontend/site/newsletter_confirm_unsubscribe.html.twig');
  88. }
  89. return $this->redirectToRoute('frontend_index') ;
  90. }
  91. }