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ů.

170 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\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. $paramsTemplate['user'] =$user;
  71. if(!isset($packageMessagesArray[$i])) {
  72. $packageMessagesArray[$i] = [] ;
  73. }
  74. if($user->getEmail() && strlen($user->getEmail())) {
  75. $packageMessagesArray[$i][] = [
  76. 'To' => [
  77. [
  78. 'Email' => $user->getEmail(),
  79. 'Name' => $user->getFirstname().' '.$user->getLastname(),
  80. ]
  81. ],
  82. 'From' => [
  83. 'Email' => $this->getParameter('app.noreply_email'),
  84. 'Name' => $this->getParameter('app.site_name')
  85. ],
  86. 'Subject' => $currentMerchant->getMerchantConfig('email-subject-prefix').' '.$news->getTitle(),
  87. 'TextPart' => $this->renderView('mail/news-text.html.twig', $paramsTemplate),
  88. 'HTMLPart' => $this->renderView('mail/news-html.html.twig', $paramsTemplate),
  89. 'CustomCampaign' => $news->getTitle(),
  90. 'DeduplicateCampaign' => true
  91. ] ;
  92. if(count($packageMessagesArray[$i]) >= $packageMessageLimit) {
  93. $i ++ ;
  94. }
  95. }
  96. }
  97. if($countUsers > 0) {
  98. $mj = new \Mailjet\Client($this->mailjetTransport->getApiKey(), $this->mailjetTransport->getApiSecret(),true,['version' => 'v3.1']);
  99. foreach($packageMessagesArray as $messagesArray) {
  100. $body = [
  101. 'Messages' => $messagesArray
  102. ];
  103. $response = $mj->post(Resources::$Email, ['body' => $body]);
  104. }
  105. if(isset($response) && $response->success()) {
  106. $this->addFlash('success', 'Actualité envoyée à '.$countUsers.' utilisateurs.');
  107. $news->setIsSent(true) ;
  108. $this->em->persist($news);
  109. $this->em->flush() ;
  110. }
  111. else {
  112. $this->addFlash('error', "Une erreur est survenue lors de l'envoi de l'actualité.");
  113. }
  114. }
  115. else {
  116. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  117. }
  118. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  119. }
  120. public function getNewsletter()
  121. {
  122. $newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;
  123. if($newsletter) {
  124. return $newsletter ;
  125. }
  126. else {
  127. throw new \ErrorException('Aucune newsletter n\'est liée à ce Merchant.') ;
  128. }
  129. }
  130. public function getNews()
  131. {
  132. $idNews = $this->request->get('id') ;
  133. $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
  134. if($news) {
  135. return $news ;
  136. }
  137. else {
  138. throw new NotFoundHttpException('Actualité introuvable') ;
  139. }
  140. }
  141. }