|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
-
- namespace Lc\ShopBundle\Controller\Frontend ;
-
- use Doctrine\ORM\EntityManagerInterface;
- use FOS\UserBundle\Util\UserManipulator;
- use Lc\ShopBundle\Context\GlobalParamInterface;
- use Lc\ShopBundle\Form\Frontend\NewsletterType;
- use Lc\ShopBundle\Repository\NewsletterRepository;
- use Lc\ShopBundle\Repository\UserRepository;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\Form\FormError;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
-
- class NewsletterController extends AbstractController
- {
- protected $em ;
- protected $userRepository ;
- protected $globalParam ;
- protected $newsletterRepository ;
-
- public function __construct(EntityManagerInterface $em, UserRepository $userRepository, NewsletterRepository $newsletterRepository, GlobalParamInterface $globalParam)
- {
- $this->em = $em ;
- $this->userRepository = $userRepository ;
- $this->newsletterRepository = $newsletterRepository ;
- $this->globalParam = $globalParam ;
- }
-
- public function subscribe(Request $request, UserManipulator $userManipulator)
- {
- $form = $this->createForm(NewsletterType::class);
-
- $form->handleRequest($request);
-
- if ($form->isSubmitted() && $form->isValid()) {
-
- $data = $form->getData() ;
- $email = $data['email'] ;
- $userExist = $this->userRepository->findOneBy(['email' => $email]) ;
- $newsletter = $this->globalParam->getCurrentMerchant()->getNewsletter() ;
-
- if($newsletter) {
- $userSubscribe = false ;
- if($userExist) {
- if($this->getUser() && $userExist && $this->getUser() == $userExist) {
- if($userExist->getNewsletters()->contains($newsletter)) {
- $this->addFlash('error','Vous êtes déjà inscrit à cette newsletter.');
- }
- else {
- $userExist->addNewsletter($newsletter) ;
- $userSubscribe = true ;
- $this->em->persist($userExist);
- }
- }
- else {
- $form->get('email')->addError(new FormError('Cette adresse email est déjà utilisée')) ;
- }
- }
- else {
- $user = $userManipulator->create($email, '9e438d8a7b036ea9b8d4375377d47e1a', $email, false, false) ;
- $user->addNewsletter($newsletter) ;
- $user->addRole('ROLE_NEWSLETTER') ;
- $this->em->persist($user);
- $userSubscribe = true ;
- }
-
- if($userSubscribe) {
- $this->addFlash('success','Vous avez bien été ajouté à la newsletter.');
- $this->em->flush() ;
- return $this->redirectToRoute('frontend_newsletter_subscribe') ;
- }
- }
- else {
- throw new \ErrorException('Aucune newsletter définie.') ;
- }
- }
-
- return $this->render('frontend/site/newsletter.html.twig', [
- 'form' => $form->createView()
- ]);
- }
-
- public function unsubscribe($idUser, $idNewsletter)
- {
- $user = $this->userRepository->find($idUser) ;
- $newsletter = $this->newsletterRepository->find($idNewsletter) ;
-
- if(!$user) {
- throw new NotFoundHttpException('Utilisateur introuvable') ;
- }
-
- if(!$newsletter) {
- throw new NotFoundHttpException('Newsletter introuvable') ;
- }
-
- if($user->getNewsletters()->contains($newsletter)) {
- $user->removeNewsletter($newsletter) ;
- $this->em->persist($user);
- $this->em->flush() ;
-
- return $this->render('frontend/site/newsletter_confirm_unsubscribe.html.twig');
- }
-
- return $this->redirectToRoute('frontend_index') ;
- }
- }
|