<?php
namespace App\Services;
use App\Entity\SiteNewsletter;
use App\Entity\SiteShop;
use App\Entity\Customer\Customer;
use App\Form\Type\SiteNewsletterType;
use App\Services\SendInBlueService;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class NewsletterService {
protected $container;
protected $formFactory;
protected $router;
public function __construct(ContainerInterface $container,
FormFactoryInterface $formFactory,
RouterInterface $router)
{
$this->container = $container;
$this->formFactory = $formFactory;
$this->router = $router;
}
public function getForm(
Request $request,
EntityManagerInterface $entityManager
)
{
$customer = $this->container->get('sylius.context.customer')->getCustomer();
$form = $this->generateForm($request->query->get('email'), $entityManager);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$preferences = $form['preferences']->getData();
$email = $form['email']->getData();
$favoriteShop = $form['favoriteShop']->getData();
if($form->getClickedButton()->getName() == 'update' || $form->getClickedButton()->getName() == 'subscribe') {
if(empty($customer)) {
$customer = $entityManager->getRepository(Customer::class)->findOneBy([
'email' => $email
]);
}
if(!empty($customer)) {
$customer->setSubscribedToNewsletter(true);
if(!empty($favoriteShop)) {
$customer->setFavoriteShop($favoriteShop);
}
$entityManager->persist($customer);
$entityManager->flush();
SendInBlueService::updateContact($email, true, $preferences, $favoriteShop, $customer);
}
else {
SendInBlueService::updateContact($email, true, $preferences, $favoriteShop);
}
}
else if($form->getClickedButton()->getName() == 'unsubscribe') {
if(empty($customer)) {
$customer = $entityManager->getRepository(Customer::class)->findOneBy([
'email' => $email
]);
}
if(!empty($customer)) {
$customer->setSubscribedToNewsletter(false);
if(!empty($favoriteShop)) {
$customer->setFavoriteShop($favoriteShop);
}
$entityManager->persist($customer);
$entityManager->flush();
SendInBlueService::updateContact($email, false);
}
else {
SendInBlueService::updateContact($email, false, []);
}
}
$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');
// return $this->redirect($request->getUri());
// return new RedirectResponse($this->router->generate('app_newsletter'));
// return new RedirectResponse($request->getUri());
}
return $form->createView();
// $formEmail = $this->renderView('newsletter/formemail.html.twig', ['form' => $form]);
// $formAll = $this->renderView('newsletter/formall.html.twig', ['form' => $form]);
// $formView = $this->renderView('newsletter/form.html.twig', [
// 'formEmail' => $formEmail,
// "formAll" => $formAll,
// 'form' => $form
// ]);
// return $this->render('newsletter.html.twig', [
// "formView" => $formView
// ]);
}
public function generateForm($email='', $entityManager, $readonly=true) {
$customer = $this->container->get('sylius.context.customer')->getCustomer();
$brevoPreferences = SendInBlueService::getPreferencesChoices();
if(!empty($customer)) {
$siteContact = new SiteNewsletter();
$siteContact->setEmail($customer->getEmail());
$brevoContact = SendInBlueService::getContactInfos($customer->getEmail());
if(!empty($brevoContact) && $brevoContact->getEmailBlacklisted()) {
$form = $this->formFactory->create(SiteNewsletterType::class, $siteContact,
[ 'disable_email' => true, 'disable_button_subscribe' => '', 'disable_button_update' => 'hidden-field' ]);
}
else {
$form = $this->formFactory->create(SiteNewsletterType::class, $siteContact,
[ 'disable_email' => true, 'disable_button_subscribe' => 'hidden-field', 'disable_button_update' => '' ]);
}
// $repoSiteNewsletterPreference = $entityManager->getRepository(SiteNewsletterPreference::class);
// $customerSiteNewsletterPreferences = $repoSiteNewsletterPreference->findBy(['customer' => $customer]);
//On met à jour l'iscription newsletter si on est pas raccord
if($customer->isSubscribedToNewsletter() && $brevoContact->getEmailBlacklisted()) {
SendInBlueService::updateContact($customer->getEmail(), $customer->isSubscribedToNewsletter());
}
$formPreferences = [];
foreach($brevoPreferences as $brevoPreference) {
if(in_array($brevoPreference, $brevoContact->getListIds())) {
$formPreferences[] = $brevoPreference;
}
}
// dump($brevoContact->getEmailBlacklisted());
if(!$brevoContact->getEmailBlacklisted()) {
$form->get('preferences')->setData($formPreferences);
}
// $form->get('preferences')->setData($formPreferences);
$form->get('favoriteShop')->setData($customer->getFavoriteShop());
}
else if($email) {
$siteContact = new SiteNewsletter();
$siteContact->setEmail($email);
$brevoContact = SendInBlueService::getContactInfos($email);
if(!empty($brevoContact) && $brevoContact->getEmailBlacklisted()) {
$form = $this->formFactory->create(SiteNewsletterType::class, $siteContact,
[ 'disable_email' => $readonly, 'disable_button_subscribe' => '', 'disable_button_update' => 'hidden-field' ]);
}
else {
$form = $this->formFactory->create(SiteNewsletterType::class, $siteContact,
[ 'disable_email' => $readonly, 'disable_button_subscribe' => 'hidden-field', 'disable_button_update' => '' ]);
}
// $form = $this->formFactory->create(SiteNewsletterType::class, $siteContact,
// [ 'disable_email' => $readonly, 'disable_button_subscribe' => 'hidden-field', 'disable_button_update' => '' ]);
// dump($brevoContact);
if(!empty($brevoContact)) {
$formPreferences = [];
foreach($brevoPreferences as $brevoPreference) {
if(in_array($brevoPreference, $brevoContact->getListIds())) {
$formPreferences[] = $brevoPreference;
}
}
if(!$brevoContact->getEmailBlacklisted()) {
$form->get('preferences')->setData($formPreferences);
}
$attributes = $brevoContact->getAttributes();
// dump($attributes);
if(array_key_exists('BOUTIQUE_PREFEREE', $attributes)) {
$siteShop = $entityManager->getRepository(SiteShop::class)->findOneBy([
'slug' => $attributes['BOUTIQUE_PREFEREE']
]);
$form->get('favoriteShop')->setData($siteShop);
}
}
}
else {
$form = $this->formFactory->create(SiteNewsletterType::class, null, [ 'disable_email' => false, 'disable_button_subscribe' => '', 'disable_button_update' => 'hidden-field' ]);
}
return $form;
}
}