您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123 行
5.3KB

  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. class NewsController extends AdminController
  17. {
  18. protected $parameterBag ;
  19. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em,
  20. MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator,
  21. ParameterBagInterface $parameterBag)
  22. {
  23. $this->parameterBag = $parameterBag ;
  24. parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator);
  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. $newsletter = $this->getNewsletter() ;
  54. $news = $this->getNews() ;
  55. $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
  56. $countUsers = count($users) ;
  57. $messages = [];
  58. foreach ($users as $user) {
  59. $message = new \Swift_Message('[Place du Local] '.$news->getTitle());
  60. $message->addTo($user->getEmail())
  61. ->addFrom($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
  62. ->setBody($this->renderView('mail/news-html.html.twig', [
  63. 'news' => $news,
  64. 'newsletter' => $newsletter,
  65. 'user' => $user
  66. ]), 'text/html');
  67. array_push($messages, $message);
  68. }
  69. if($countUsers > 0) {
  70. $result = $this->mailjetTransport->bulkSend($messages);
  71. $this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');
  72. $news->setIsSent(true) ;
  73. $this->em->persist($news);
  74. $this->em->flush() ;
  75. }
  76. else {
  77. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  78. }
  79. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  80. }
  81. public function getNewsletter()
  82. {
  83. $newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;
  84. if($newsletter) {
  85. return $newsletter ;
  86. }
  87. else {
  88. throw new \ErrorException('Aucune newsletter n\'est liée à ce Merchant.') ;
  89. }
  90. }
  91. public function getNews()
  92. {
  93. $idNews = $this->request->get('id') ;
  94. $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
  95. if($news) {
  96. return $news ;
  97. }
  98. else {
  99. throw new NotFoundHttpException('Actualité introuvable') ;
  100. }
  101. }
  102. }