Browse Source

[Backend] Évolution envoi actus : gestion email test

master
Guillaume 4 years ago
parent
commit
28e092552d
2 changed files with 90 additions and 39 deletions
  1. +7
    -1
      ShopBundle/Controller/Backend/AdminController.php
  2. +83
    -38
      ShopBundle/Controller/Backend/NewsController.php

+ 7
- 1
ShopBundle/Controller/Backend/AdminController.php View File

use Lc\ShopBundle\Services\UtilsManager; use Lc\ShopBundle\Services\UtilsManager;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport; use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextareaType;
protected $merchantUtils; protected $merchantUtils;
protected $mailjetTransport; protected $mailjetTransport;
protected $orderUtils; protected $orderUtils;
protected $mailUtils ;
protected $translator; protected $translator;
protected $parameterBag ;
protected $filtersForm = null; protected $filtersForm = null;


public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em,
MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator)
MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator,
ParameterBagInterface $parameterBag)
{ {
$this->security = $security; $this->security = $security;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->utils = $utilsManager->getUtils(); $this->utils = $utilsManager->getUtils();
$this->merchantUtils = $utilsManager->getMerchantUtils(); $this->merchantUtils = $utilsManager->getMerchantUtils();
$this->orderUtils = $utilsManager->getOrderUtils();; $this->orderUtils = $utilsManager->getOrderUtils();;
$this->mailUtils = $utilsManager->getMailUtils() ;
$this->translator = $translator; $this->translator = $translator;
$this->parameterBag = $parameterBag ;
} }


public function createCustomForm($class, $action, $parameters, $data = true) public function createCustomForm($class, $action, $parameters, $data = true)

+ 83
- 38
ShopBundle/Controller/Backend/NewsController.php View File

use FOS\UserBundle\Model\UserManagerInterface; use FOS\UserBundle\Model\UserManagerInterface;
use Lc\ShopBundle\Context\NewsInterface; use Lc\ShopBundle\Context\NewsInterface;
use Lc\ShopBundle\Context\UserInterface; use Lc\ShopBundle\Context\UserInterface;
use Lc\ShopBundle\Services\MailUtils;
use Lc\ShopBundle\Services\UtilsManager; use Lc\ShopBundle\Services\UtilsManager;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport; use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;


class NewsController extends AdminController class NewsController extends AdminController
{ {
public function sendTestAction()
{
$newsletter = $this->getNewsletter() ;
$news = $this->getNews() ;

$merchantCurrent = $this->merchantUtils->getMerchantCurrent() ;
$merchantConfigEmailContact = $merchantCurrent->getMerchantConfig('email-contact');

if($merchantConfigEmailContact && strlen($merchantConfigEmailContact)) {
$this->mailUtils->send([
MailUtils::SUBJECT => $news->getTitle(),
MailUtils::TO_EMAIL => $merchantConfigEmailContact,
MailUtils::TO_NAME => $this->parameterBag->get('app.site_name'),
MailUtils::CONTENT_TEMPLATE => 'mail/news',
MailUtils::CONTENT_DATA => [
'news' => $news,
'newsletter' => $newsletter,
'user' => $this->security->getUser()
],
]);

$this->addFlash('success', 'Actualité de test envoyée à '.$merchantConfigEmailContact);
}
else {
throw new \ErrorException("L'email de contact n'est pas défini pour ce Merchant.") ;
}

return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
}

public function sendAction() public function sendAction()
{ {
$idNews = $this->request->get('id') ;
$newsletter = $this->getNewsletter() ;


$newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;
$news = $this->getNews() ;
$users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
$countUsers = count($users) ;
$messages = [];


if($newsletter) {
$users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
$countUsers = count($users) ;
$messages = [];
$news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
if($news) {
foreach ($users as $user) {
$message = new \Swift_Message('[Place du Local] '.$news->getTitle());
$message->addTo($user->getEmail())
->addFrom($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
->setBody($this->renderView('mail/news.html.twig', [
'message' => $news->getDescription(),
'image' => $news->getImage(),
'newsletter' => $newsletter,
'user' => $user
]), 'text/html');
array_push($messages, $message);
}

if($countUsers > 0) {
$result = $this->mailjetTransport->bulkSend($messages);

$this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');

$news->setIsSent(true) ;
$this->em->persist($news);
$this->em->flush() ;
}
else {
$this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
}
}
else {
throw new NotFoundHttpException('Actualité introuvable') ;
}
foreach ($users as $user) {
$message = new \Swift_Message('[Place du Local] '.$news->getTitle());
$message->addTo($user->getEmail())
->addFrom($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
->setBody($this->renderView('mail/news-html.html.twig', [
'news' => $news,
'newsletter' => $newsletter,
'user' => $user
]), 'text/html');
array_push($messages, $message);
}

if($countUsers > 0) {
$result = $this->mailjetTransport->bulkSend($messages);

$this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');

$news->setIsSent(true) ;
$this->em->persist($news);
$this->em->flush() ;
} }
else { else {
throw new \ErrorException('Aucune newsletter n\'est lié à ce Merchant.') ;
$this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
} }


return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ; return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
} }


public function getNewsletter()
{
$newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;
if($newsletter) {
return $newsletter ;
}
else {
throw new \ErrorException('Aucune newsletter n\'est liée à ce Merchant.') ;
}
}

public function getNews()
{
$idNews = $this->request->get('id') ;
$news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;

if($news) {
return $news ;
}
else {
throw new NotFoundHttpException('Actualité introuvable') ;
}
}

} }

Loading…
Cancel
Save