src/Services/NewsletterService.php line 108

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\SiteNewsletter;
  4. use App\Entity\SiteShop;
  5. use App\Entity\Customer\Customer;
  6. use App\Form\Type\SiteNewsletterType;
  7. use App\Services\SendInBlueService;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. class NewsletterService {
  15.     protected $container;
  16.     protected $formFactory;
  17.     protected $router;
  18.     public function __construct(ContainerInterface $container
  19.         FormFactoryInterface $formFactory,
  20.         RouterInterface $router)
  21.     {
  22.         $this->container $container;
  23.         $this->formFactory $formFactory;
  24.         $this->router $router;
  25.     }
  26.     public function getForm(
  27.         Request $request
  28.         EntityManagerInterface $entityManager
  29.     )
  30.     {
  31.         $customer $this->container->get('sylius.context.customer')->getCustomer();
  32.         $form $this->generateForm($request->query->get('email'), $entityManager);
  33.         
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()){
  36.             $preferences $form['preferences']->getData();
  37.             $email $form['email']->getData();
  38.             $favoriteShop $form['favoriteShop']->getData();
  39.             if($form->getClickedButton()->getName() == 'update' || $form->getClickedButton()->getName() == 'subscribe') {
  40.                 if(empty($customer)) {
  41.                     $customer $entityManager->getRepository(Customer::class)->findOneBy([
  42.                         'email' => $email
  43.                     ]);
  44.                 }
  45.                 if(!empty($customer)) {
  46.                     $customer->setSubscribedToNewsletter(true);
  47.                     if(!empty($favoriteShop)) {
  48.                         $customer->setFavoriteShop($favoriteShop);
  49.                     }
  50.                     $entityManager->persist($customer);
  51.                     $entityManager->flush();
  52.                     SendInBlueService::updateContact($emailtrue$preferences$favoriteShop$customer);
  53.                 }
  54.                 else {
  55.                     SendInBlueService::updateContact($emailtrue$preferences$favoriteShop);
  56.                 }
  57.             }
  58.             else if($form->getClickedButton()->getName() == 'unsubscribe') {
  59.                 if(empty($customer)) {
  60.                     $customer $entityManager->getRepository(Customer::class)->findOneBy([
  61.                         'email' => $email
  62.                     ]);
  63.                 }
  64.                 if(!empty($customer)) {
  65.                     $customer->setSubscribedToNewsletter(false);
  66.                     if(!empty($favoriteShop)) {
  67.                         $customer->setFavoriteShop($favoriteShop);
  68.                     }
  69.                     $entityManager->persist($customer);
  70.                     $entityManager->flush();
  71.                     SendInBlueService::updateContact($emailfalse);
  72.                 }
  73.                 else {
  74.                     SendInBlueService::updateContact($emailfalse, []);
  75.                 }
  76.             }
  77.             $this->container->get('session')->getFlashBag()->add('success''Nous avons bien enregistré vos préférences de communication. Si vous avez la moindre question, merci de nous contacter au 02 97 59 14 23 (du lundi au vendredi) ou par mail : support@ppmc.fr');
  78.             // return $this->redirect($request->getUri());
  79.             // return new RedirectResponse($this->router->generate('app_newsletter'));
  80.             // return new RedirectResponse($request->getUri());
  81.             
  82.         }
  83.         
  84.         return $form->createView();
  85.         // $formEmail = $this->renderView('newsletter/formemail.html.twig', ['form' => $form]);
  86.         // $formAll = $this->renderView('newsletter/formall.html.twig', ['form' => $form]);
  87.         
  88.         // $formView = $this->renderView('newsletter/form.html.twig', [
  89.         //     'formEmail' => $formEmail,
  90.         //     "formAll"     => $formAll,
  91.         //     'form' => $form
  92.         // ]);
  93.         // return $this->render('newsletter.html.twig', [
  94.         //     "formView" => $formView
  95.         // ]);
  96.     }
  97.     public function generateForm($email=''$entityManager$readonly=true) {
  98.         $customer $this->container->get('sylius.context.customer')->getCustomer();
  99.         $brevoPreferences SendInBlueService::getPreferencesChoices();
  100.         if(!empty($customer)) {
  101.             $siteContact = new SiteNewsletter();
  102.             $siteContact->setEmail($customer->getEmail());
  103.             $brevoContact SendInBlueService::getContactInfos($customer->getEmail());
  104.             if(!empty($brevoContact) && $brevoContact->getEmailBlacklisted()) {
  105.                 $form $this->formFactory->create(SiteNewsletterType::class, $siteContact,
  106.                 [ 'disable_email' => true'disable_button_subscribe' => '''disable_button_update' => 'hidden-field' ]);
  107.             }
  108.             else {
  109.                 $form $this->formFactory->create(SiteNewsletterType::class, $siteContact,
  110.                 [ 'disable_email' => true'disable_button_subscribe' => 'hidden-field''disable_button_update' => '' ]);
  111.             }
  112.             // $repoSiteNewsletterPreference = $entityManager->getRepository(SiteNewsletterPreference::class);
  113.             // $customerSiteNewsletterPreferences = $repoSiteNewsletterPreference->findBy(['customer' => $customer]);
  114.             
  115.             //On met à jour l'iscription newsletter si on est pas raccord
  116.             if($customer->isSubscribedToNewsletter() && $brevoContact->getEmailBlacklisted()) {
  117.                 SendInBlueService::updateContact($customer->getEmail(), $customer->isSubscribedToNewsletter());
  118.             }
  119.             $formPreferences = [];
  120.             foreach($brevoPreferences as $brevoPreference) {
  121.                 if(in_array($brevoPreference$brevoContact->getListIds())) {
  122.                     $formPreferences[] = $brevoPreference;
  123.                 }
  124.             }
  125.             // dump($brevoContact->getEmailBlacklisted());
  126.             if(!$brevoContact->getEmailBlacklisted()) {
  127.                 $form->get('preferences')->setData($formPreferences);
  128.             }
  129.             // $form->get('preferences')->setData($formPreferences);
  130.             $form->get('favoriteShop')->setData($customer->getFavoriteShop());    
  131.         }
  132.         else if($email) {
  133.             $siteContact = new SiteNewsletter();
  134.             $siteContact->setEmail($email);
  135.             $brevoContact SendInBlueService::getContactInfos($email);
  136.             if(!empty($brevoContact) && $brevoContact->getEmailBlacklisted()) {
  137.                 $form $this->formFactory->create(SiteNewsletterType::class, $siteContact,
  138.                 [ 'disable_email' => $readonly'disable_button_subscribe' => '''disable_button_update' => 'hidden-field' ]);
  139.             }
  140.             else {
  141.                 $form $this->formFactory->create(SiteNewsletterType::class, $siteContact,
  142.                 [ 'disable_email' => $readonly'disable_button_subscribe' => 'hidden-field''disable_button_update' => '' ]);
  143.             }
  144.             // $form = $this->formFactory->create(SiteNewsletterType::class, $siteContact,
  145.             // [ 'disable_email' => $readonly, 'disable_button_subscribe' => 'hidden-field', 'disable_button_update' => '' ]);
  146.             
  147.             
  148.             // dump($brevoContact);
  149.             if(!empty($brevoContact)) {
  150.                 $formPreferences = [];
  151.                 foreach($brevoPreferences as $brevoPreference) {
  152.                     if(in_array($brevoPreference$brevoContact->getListIds())) {
  153.                         $formPreferences[] = $brevoPreference;
  154.                     }
  155.                 }
  156.                 if(!$brevoContact->getEmailBlacklisted()) {
  157.                     $form->get('preferences')->setData($formPreferences);
  158.                 }
  159.                 
  160.                 $attributes $brevoContact->getAttributes();
  161.                 // dump($attributes);
  162.                 if(array_key_exists('BOUTIQUE_PREFEREE'$attributes)) {
  163.                     $siteShop $entityManager->getRepository(SiteShop::class)->findOneBy([
  164.                         'slug' => $attributes['BOUTIQUE_PREFEREE']
  165.                     ]);
  166.                     $form->get('favoriteShop')->setData($siteShop);
  167.                 }
  168.             }
  169.         }
  170.         else {
  171.             $form $this->formFactory->create(SiteNewsletterType::class, null, [ 'disable_email' => false'disable_button_subscribe' => '''disable_button_update' => 'hidden-field' ]);
  172.         }
  173.         return $form;
  174.     }
  175. }