63 line
3.0KB

  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. $news->setIsSent(true) ;
  36. $this->em->persist($news);
  37. $this->em->flush() ;
  38. }
  39. else {
  40. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  41. }
  42. }
  43. else {
  44. throw new NotFoundHttpException('Actualité introuvable') ;
  45. }
  46. }
  47. else {
  48. throw new \ErrorException('Aucune newsletter n\'est lié à ce Merchant.') ;
  49. }
  50. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  51. }
  52. }