|
- <?php
-
- namespace Lc\ShopBundle\Controller\Admin;
-
- use App\Repository\UserRepository;
- use Lc\ShopBundle\Context\NewsInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
-
- class NewsController extends AdminController
- {
-
- public function sendAction()
- {
- $idNews = $this->request->get('id') ;
- $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findBy([
- 'isSubscribedNewsletter' => true
- ]) ;
- $countUsers = count($users) ;
- $messages = [];
- $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
- if($news) {
- foreach ($users as $user) {
- $message = new \Swift_Message('[Place du Local] '.$news->getTitle());
- $message->addTo($user->getEmail())
- ->addFrom($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
- ->setBody($this->renderView('mail/news.html.twig', [
- 'message' => $news->getDescription(),
- 'image' => $news->getImage(),
- ]), 'text/html');
- array_push($messages, $message);
- }
-
- $result = $this->mailjetTransport->bulkSend($messages);
-
- if($countUsers > 0) {
- $this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');
- }
- else {
- $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
- }
- }
- else {
- throw new NotFoundHttpException('Actualité introuvable') ;
- }
-
- return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
- }
-
- }
|