Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

166 lines
7.5KB

  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\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use \Mailjet\Resources ;
  17. class NewsController extends AdminController
  18. {
  19. protected $parameterBag ;
  20. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em,
  21. MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator,
  22. ParameterBagInterface $parameterBag)
  23. {
  24. $this->parameterBag = $parameterBag ;
  25. parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator);
  26. }
  27. public function sendTestAction()
  28. {
  29. $newsletter = $this->getNewsletter() ;
  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. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  51. }
  52. public function sendAction()
  53. {
  54. ini_set('memory_limit', '-1');
  55. set_time_limit(0);
  56. $currentMerchant = $this->merchantUtils->getMerchantCurrent() ;
  57. $newsletter = $this->getNewsletter() ;
  58. $news = $this->getNews() ;
  59. $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
  60. $countUsers = count($users) ;
  61. $packageMessagesArray = [];
  62. $packageMessageLimit = 50 ;
  63. $paramsTemplate = [
  64. 'news' => $news,
  65. 'newsletter' => $newsletter,
  66. 'user' => $this->security->getUser()
  67. ] ;
  68. $i = 0 ;
  69. foreach ($users as $user) {
  70. if(!isset($packageMessagesArray[$i])) {
  71. $packageMessagesArray[$i] = [] ;
  72. }
  73. if($user->getEmail() && strlen($user->getEmail())) {
  74. $packageMessagesArray[$i][] = [
  75. 'To' => [
  76. [
  77. 'Email' => $user->getEmail(),
  78. 'Name' => $user->getFirstname().' '.$user->getLastname(),
  79. ]
  80. ],
  81. 'From' => [
  82. 'Email' => $this->getParameter('app.noreply_email'),
  83. 'Name' => $this->getParameter('app.site_name')
  84. ],
  85. 'Subject' => $currentMerchant->getMerchantConfig('email-subject-prefix').' '.$news->getTitle(),
  86. 'TextPart' => $this->renderView('mail/news-text.html.twig', $paramsTemplate),
  87. 'HTMLPart' => $this->renderView('mail/news-html.html.twig', $paramsTemplate),
  88. 'CustomCampaign' => $news->getTitle(),
  89. 'DeduplicateCampaign' => true
  90. ] ;
  91. if(count($packageMessagesArray[$i]) >= $packageMessageLimit) {
  92. $i ++ ;
  93. }
  94. }
  95. }
  96. if($countUsers > 0) {
  97. $mj = new \Mailjet\Client($this->mailjetTransport->getApiKey(), $this->mailjetTransport->getApiSecret(),true,['version' => 'v3.1']);
  98. foreach($packageMessagesArray as $messagesArray) {
  99. $body = [
  100. 'Messages' => $messagesArray
  101. ];
  102. $response = $mj->post(Resources::$Email, ['body' => $body]);
  103. }
  104. if(isset($response) && $response->success()) {
  105. $this->addFlash('success', 'Actualité envoyée à '.$countUsers.' utilisateurs.');
  106. $news->setIsSent(true) ;
  107. $this->em->persist($news);
  108. $this->em->flush() ;
  109. }
  110. else {
  111. $this->addFlash('error', "Une erreur est survenue lors de l'envoi de l'actualité.");
  112. }
  113. }
  114. else {
  115. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  116. }
  117. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  118. }
  119. public function getNewsletter()
  120. {
  121. $newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;
  122. if($newsletter) {
  123. return $newsletter ;
  124. }
  125. else {
  126. throw new \ErrorException('Aucune newsletter n\'est liée à ce Merchant.') ;
  127. }
  128. }
  129. public function getNews()
  130. {
  131. $idNews = $this->request->get('id') ;
  132. $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
  133. if($news) {
  134. return $news ;
  135. }
  136. else {
  137. throw new NotFoundHttpException('Actualité introuvable') ;
  138. }
  139. }
  140. }