67 行
3.3KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use FOS\UserBundle\Model\UserManagerInterface;
  6. use Lc\ShopBundle\Context\NewsInterface;
  7. use Lc\ShopBundle\Context\UserInterface;
  8. use Lc\ShopBundle\Services\UtilsManager;
  9. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class NewsController extends AdminController
  14. {
  15. public function sendAction()
  16. {
  17. $idNews = $this->request->get('id') ;
  18. $newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;
  19. if($newsletter) {
  20. $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
  21. $countUsers = count($users) ;
  22. $messages = [];
  23. $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
  24. if($news) {
  25. foreach ($users as $user) {
  26. $message = new \Swift_Message('[Place du Local] '.$news->getTitle());
  27. $message->addTo($user->getEmail())
  28. ->addFrom($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
  29. ->setBody($this->renderView('mail/news.html.twig', [
  30. 'message' => $news->getDescription(),
  31. 'image' => $news->getImage(),
  32. 'newsletter' => $newsletter,
  33. 'user' => $user
  34. ]), 'text/html');
  35. array_push($messages, $message);
  36. }
  37. if($countUsers > 0) {
  38. $result = $this->mailjetTransport->bulkSend($messages);
  39. $this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');
  40. $news->setIsSent(true) ;
  41. $this->em->persist($news);
  42. $this->em->flush() ;
  43. }
  44. else {
  45. $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
  46. }
  47. }
  48. else {
  49. throw new NotFoundHttpException('Actualité introuvable') ;
  50. }
  51. }
  52. else {
  53. throw new \ErrorException('Aucune newsletter n\'est lié à ce Merchant.') ;
  54. }
  55. return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
  56. }
  57. }