Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

51 lines
2.2KB

  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. }