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.

51 satır
2.3KB

  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($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
  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. }