src/Controller/Front/CommentController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Comment;
  4. use App\Entity\Masjid;
  5. use App\Form\CommentType;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Mime\Email;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Component\Mime\Address;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("/comment")
  16.  */
  17. class CommentController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/{masjidid}/new", name="comment_new", methods={"GET","POST"})
  21.      */
  22.     public function new( $masjididRequest $requestMailerInterface $mailer): Response
  23.     {
  24.         $comment = new Comment();
  25.         $entityManager $this->getDoctrine()->getManager();
  26.         $masjid $this->getDoctrine()
  27.             ->getRepository(Masjid::class)
  28.             ->find($masjidid);
  29.         $author $this->get('security.token_storage')->getToken()->getUser();
  30.         $form $this->createForm(CommentType::class, $comment);
  31.         $form->handleRequest($request);
  32.         if ($form->isSubmitted() && $form->isValid()) {
  33.             $comment->setMasjid($masjid);
  34.             $comment->setAuthor($author);
  35.             $entityManager->persist($comment);
  36.             $entityManager->flush();
  37.             /*
  38.             $message ="Un nouveau commentaire";
  39.             $the_mail = "sharjah.masjids@gmail.com";
  40.             $email = (new TemplatedEmail())
  41.                 ->from('sharjah.masjids@gmail.com')
  42.                 ->to('sharjah.masjids@gmail.com')
  43.                 ->subject('New comment on sharjah-masjids.com')
  44.                 // path of the Twig template to render
  45.                 ->htmlTemplate('mail/comment_new.html.twig')
  46.                 // pass variables (name => value) to the template
  47.                 ->context([
  48.                     'mess' => $message,
  49.                     'masjid' => $masjid,
  50.                     'the_mail' => $the_mail,
  51.                 ])
  52.             ;
  53.             try {
  54.                 $mailer->send($email);
  55.             } catch (TransportExceptionInterface $e) {
  56.                 // some error prevented the email sending; display an
  57.                 // error message or try to resend the message
  58.             }
  59.             */
  60.       
  61.             return $this->redirectToRoute('masjid_show',['slug'=>$masjid->getSlug()]);
  62.         }
  63.         return $this->render('comment/new.html.twig', [
  64.             'comment' => $comment,
  65.             'form' => $form->createView(),
  66.             'masjidid' => $masjidid
  67.         ]);
  68.     }
  69. }