59 lines
2.9KB

  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. $newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;
  14. if($newsletter) {
  15. $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
  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. 'newsletter' => $newsletter,
  28. 'user' => $user
  29. ]), 'text/html');
  30. array_push($messages, $message);
  31. }
  32. $result = $this->mailjetTransport->bulkSend($messages);
  33. if($countUsers > 0) {
  34. $this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');
  35. }
  36. else {
  37. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  38. }
  39. }
  40. else {
  41. throw new NotFoundHttpException('Actualité introuvable') ;
  42. }
  43. }
  44. else {
  45. throw new \ErrorException('Aucune newsletter n\'est lié à ce Merchant.') ;
  46. }
  47. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  48. }
  49. }