Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NewsController.php 7.5KB

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