Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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