Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

180 lines
7.6KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use FOS\UserBundle\Model\UserManagerInterface;
  6. use Lc\ShopBundle\Context\NewsInterface;
  7. use Lc\ShopBundle\Context\UserInterface;
  8. use Lc\ShopBundle\Services\MailUtils;
  9. use Lc\ShopBundle\Services\UtilsManager;
  10. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use \Mailjet\Resources ;
  18. class NewsController extends AdminController
  19. {
  20. protected $parameterBag ;
  21. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator, SessionInterface $session, ParameterBagInterface $parameterBag)
  22. {
  23. parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator, $session);
  24. $this->parameterBag = $parameterBag ;
  25. }
  26. public function sendTestAction()
  27. {
  28. $newsletter = $this->getNewsletter() ;
  29. if($newsletter) {
  30. $news = $this->getNews() ;
  31. $merchantCurrent = $this->merchantUtils->getMerchantCurrent() ;
  32. $merchantConfigEmailContact = $merchantCurrent->getMerchantConfig('email-contact');
  33. if($merchantConfigEmailContact && strlen($merchantConfigEmailContact)) {
  34. $this->mailUtils->send([
  35. MailUtils::SUBJECT => $news->getTitle(),
  36. MailUtils::TO_EMAIL => $merchantConfigEmailContact,
  37. MailUtils::TO_NAME => $this->parameterBag->get('app.site_name'),
  38. MailUtils::CONTENT_TEMPLATE => 'mail/news',
  39. MailUtils::CONTENT_DATA => [
  40. 'news' => $news,
  41. 'newsletter' => $newsletter,
  42. 'user' => $this->security->getUser()
  43. ],
  44. ]);
  45. $this->addFlash('success', 'Actualité de test envoyée à '.$merchantConfigEmailContact);
  46. }
  47. else {
  48. throw new \ErrorException("L'email de contact n'est pas défini pour ce Merchant.") ;
  49. }
  50. }
  51. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  52. }
  53. public function sendAction()
  54. {
  55. ini_set('memory_limit', '-1');
  56. set_time_limit(0);
  57. $currentMerchant = $this->merchantUtils->getMerchantCurrent() ;
  58. $newsletter = $this->getNewsletter() ;
  59. if($newsletter) {
  60. $news = $this->getNews() ;
  61. $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
  62. $countUsers = count($users) ;
  63. $packageMessagesArray = [];
  64. $packageMessageLimit = 50 ;
  65. $paramsTemplate = [
  66. 'news' => $news,
  67. 'newsletter' => $newsletter,
  68. 'user' => $this->security->getUser()
  69. ] ;
  70. $i = 0 ;
  71. foreach ($users as $user) {
  72. $paramsTemplate['user'] =$user;
  73. if(!isset($packageMessagesArray[$i])) {
  74. $packageMessagesArray[$i] = [] ;
  75. }
  76. if($user->getEmail() && strlen($user->getEmail())) {
  77. $packageMessagesArray[$i][] = [
  78. 'To' => [
  79. [
  80. 'Email' => $user->getEmail(),
  81. 'Name' => $user->getFirstname().' '.$user->getLastname(),
  82. ]
  83. ],
  84. 'From' => [
  85. 'Email' => $this->getParameter('app.noreply_email'),
  86. 'Name' => $this->getParameter('app.site_name')
  87. ],
  88. 'Subject' => $currentMerchant->getMerchantConfig('email-subject-prefix').' '.$news->getTitle(),
  89. 'TextPart' => $this->renderView('mail/news-text.html.twig', $paramsTemplate),
  90. 'HTMLPart' => $this->renderView('mail/news-html.html.twig', $paramsTemplate),
  91. 'CustomCampaign' => $news->getTitle(),
  92. 'DeduplicateCampaign' => true
  93. ] ;
  94. if(count($packageMessagesArray[$i]) >= $packageMessageLimit) {
  95. $i ++ ;
  96. }
  97. }
  98. }
  99. if($countUsers > 0) {
  100. $mj = new \Mailjet\Client($this->mailjetTransport->getApiKey(), $this->mailjetTransport->getApiSecret(),true,['version' => 'v3.1']);
  101. foreach($packageMessagesArray as $messagesArray) {
  102. $body = [
  103. 'Messages' => $messagesArray
  104. ];
  105. $response = $mj->post(Resources::$Email, ['body' => $body]);
  106. }
  107. if(isset($response) && $response->success()) {
  108. $this->addFlash('success', 'Actualité envoyée à '.$countUsers.' utilisateurs.');
  109. $news->setIsSent(true) ;
  110. $this->em->persist($news);
  111. $this->em->flush() ;
  112. }
  113. else {
  114. $this->addFlash('error', "Une erreur est survenue lors de l'envoi de l'actualité.");
  115. }
  116. }
  117. else {
  118. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  119. }
  120. }
  121. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  122. }
  123. public function getNewsletter()
  124. {
  125. $news = $this->getNews() ;
  126. if($news) {
  127. $newsletter = $news->getNewsletter() ;
  128. }
  129. if(isset($newsletter) && $newsletter) {
  130. return $newsletter ;
  131. }
  132. else {
  133. $this->utils->addFlash('error', 'Aucune newsletter n\'est liée à l\'actualité.');
  134. }
  135. }
  136. public function getNews()
  137. {
  138. $idNews = $this->request->get('id') ;
  139. $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
  140. if($news) {
  141. return $news ;
  142. }
  143. else {
  144. throw new NotFoundHttpException('Actualité introuvable') ;
  145. }
  146. }
  147. }