You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewsController.php 4.7KB

пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class NewsController extends AdminController
  16. {
  17. public function sendTestAction()
  18. {
  19. $newsletter = $this->getNewsletter() ;
  20. $news = $this->getNews() ;
  21. $merchantCurrent = $this->merchantUtils->getMerchantCurrent() ;
  22. $merchantConfigEmailContact = $merchantCurrent->getMerchantConfig('email-contact');
  23. if($merchantConfigEmailContact && strlen($merchantConfigEmailContact)) {
  24. $this->mailUtils->send([
  25. MailUtils::SUBJECT => $news->getTitle(),
  26. MailUtils::TO_EMAIL => $merchantConfigEmailContact,
  27. MailUtils::TO_NAME => $this->parameterBag->get('app.site_name'),
  28. MailUtils::CONTENT_TEMPLATE => 'mail/news',
  29. MailUtils::CONTENT_DATA => [
  30. 'news' => $news,
  31. 'newsletter' => $newsletter,
  32. 'user' => $this->security->getUser()
  33. ],
  34. ]);
  35. $this->addFlash('success', 'Actualité de test envoyée à '.$merchantConfigEmailContact);
  36. }
  37. else {
  38. throw new \ErrorException("L'email de contact n'est pas défini pour ce Merchant.") ;
  39. }
  40. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  41. }
  42. public function sendAction()
  43. {
  44. $newsletter = $this->getNewsletter() ;
  45. $news = $this->getNews() ;
  46. $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
  47. $countUsers = count($users) ;
  48. $messages = [];
  49. foreach ($users as $user) {
  50. $message = new \Swift_Message('[Place du Local] '.$news->getTitle());
  51. $message->addTo($user->getEmail())
  52. ->addFrom($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
  53. ->setBody($this->renderView('mail/news-html.html.twig', [
  54. 'news' => $news,
  55. 'newsletter' => $newsletter,
  56. 'user' => $user
  57. ]), 'text/html');
  58. array_push($messages, $message);
  59. }
  60. if($countUsers > 0) {
  61. $result = $this->mailjetTransport->bulkSend($messages);
  62. $this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');
  63. $news->setIsSent(true) ;
  64. $this->em->persist($news);
  65. $this->em->flush() ;
  66. }
  67. else {
  68. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  69. }
  70. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  71. }
  72. public function getNewsletter()
  73. {
  74. $newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;
  75. if($newsletter) {
  76. return $newsletter ;
  77. }
  78. else {
  79. throw new \ErrorException('Aucune newsletter n\'est liée à ce Merchant.') ;
  80. }
  81. }
  82. public function getNews()
  83. {
  84. $idNews = $this->request->get('id') ;
  85. $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
  86. if($news) {
  87. return $news ;
  88. }
  89. else {
  90. throw new NotFoundHttpException('Actualité introuvable') ;
  91. }
  92. }
  93. }