選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NewsController.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use App\Repository\UserRepository;
  4. use Lc\ShopBundle\Context\NewsInterface;
  5. use Lc\ShopBundle\Context\UserInterface;
  6. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. class NewsController extends AdminController
  9. {
  10. public function sendAction()
  11. {
  12. $idNews = $this->request->get('id') ;
  13. $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findBy([
  14. 'isSubscribedNewsletter' => true
  15. ]) ;
  16. $countUsers = count($users) ;
  17. $messages = [];
  18. $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
  19. if($news) {
  20. foreach ($users as $user) {
  21. $message = new \Swift_Message('[Place du Local] '.$news->getTitle());
  22. $message->addTo($user->getEmail())
  23. ->addFrom('no-reply@opendistrib.net', 'Place du Local')
  24. ->setBody($this->renderView('mail/news.html.twig', [
  25. 'message' => $news->getDescription(),
  26. 'image' => $news->getImage(),
  27. ]), 'text/html');
  28. array_push($messages, $message);
  29. }
  30. $result = $this->mailjetTransport->bulkSend($messages);
  31. if($countUsers > 0) {
  32. $this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');
  33. }
  34. else {
  35. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  36. }
  37. }
  38. else {
  39. throw new NotFoundHttpException('Actualité introuvable') ;
  40. }
  41. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  42. }
  43. }