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.6KB

pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. $news = $this->getNews() ;
  30. $merchantCurrent = $this->merchantUtils->getMerchantCurrent() ;
  31. $merchantConfigEmailContact = $merchantCurrent->getMerchantConfig('email-contact');
  32. if($merchantConfigEmailContact && strlen($merchantConfigEmailContact)) {
  33. $this->mailUtils->send([
  34. MailUtils::SUBJECT => $news->getTitle(),
  35. MailUtils::TO_EMAIL => $merchantConfigEmailContact,
  36. MailUtils::TO_NAME => $this->parameterBag->get('app.site_name'),
  37. MailUtils::CONTENT_TEMPLATE => 'mail/news',
  38. MailUtils::CONTENT_DATA => [
  39. 'news' => $news,
  40. 'newsletter' => $newsletter,
  41. 'user' => $this->security->getUser()
  42. ],
  43. ]);
  44. $this->addFlash('success', 'Actualité de test envoyée à '.$merchantConfigEmailContact);
  45. }
  46. else {
  47. throw new \ErrorException("L'email de contact n'est pas défini pour ce Merchant.") ;
  48. }
  49. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  50. }
  51. public function sendAction()
  52. {
  53. ini_set('memory_limit', '-1');
  54. set_time_limit(0);
  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. $paramsTemplate['user'] =$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. }