src/Controller/Forum/ForumPostController.php line 452

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Forum;
  3. use App\Entity\ForumCategory;
  4. use App\Entity\ForumComment;
  5. use App\Entity\ForumCommentReply;
  6. use App\Entity\ForumNotificationSubscriber;
  7. use App\Entity\ForumPost;
  8. use App\Entity\ForumReport;
  9. use App\Entity\ForumTopic;
  10. use App\Entity\ForumUser;
  11. use App\Entity\User;
  12. use App\Form\ForumCommentReplyType;
  13. use App\Form\ForumCommentType;
  14. use App\Form\ForumPostType;
  15. use App\Form\ForumReportType;
  16. use App\Repository\ForumPostRepository;
  17. use App\Service\Forum\ForumSubscribeNotificationService;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\String\Slugger\SluggerInterface;
  27. /**
  28.  * @Route("/forum")
  29.  *
  30.  */
  31. class ForumPostController extends AbstractController
  32. {
  33.     /**
  34.      *
  35.      * POST UPDATE SLUG AFTER HAVE ID
  36.      *
  37.      * @param ForumPost $post
  38.      * @param SluggerInterface $slugger
  39.      * @return void
  40.      */
  41.     private function postUpdateSlug(
  42.         ForumPost        $post,
  43.         SluggerInterface $slugger
  44.     )
  45.     {
  46.         $slug strtolower($slugger->slug($post->getTitle()));
  47.         $slug $post->getId() . "-" $slug;
  48.         $post->setSlug($slug);
  49.     }
  50.     /**
  51.      * RECUPERER LES ID DES TOPIC AVEC CATEGORIE
  52.      *
  53.      * @param EntityManagerInterface $entityManager
  54.      * @return array
  55.      */
  56.     private function getTopicsIDs(
  57.         EntityManagerInterface $entityManager
  58.     ): array
  59.     {
  60.         $categories $entityManager->getRepository(ForumCategory::class)->findAll();
  61.         $topicIDS = [];
  62.         foreach ($categories as $category) {
  63.             $categoryName $category->getName();
  64.             if (!isset($topicIDS[$categoryName])) {
  65.                 $topicIDS[$categoryName] = [];
  66.             }
  67.             foreach ($category->getTopics() as $topic) {
  68.                 $topicIDS[$categoryName][$topic->getName()] = $topic->getId();
  69.             }
  70.         }
  71.         return $topicIDS;
  72.     }
  73.     /**
  74.      * @Route("/{countryMin}/topic/recent", name="app_forum_recent")
  75.      *
  76.      */
  77.     public function recentPost(
  78.         ForumPostRepository $forumPostRepository,
  79.         Request             $request,
  80.         PaginatorInterface  $paginator,
  81.         string              $countryMin
  82.     ): Response
  83.     {
  84.         /** @var User $currentUser */
  85.         $currentUser $this->getUser();
  86.         /** @var ?ForumUser $currentForumUser */
  87.         $currentForumUser null;
  88.         if ($currentUser) {
  89.             $currentForumUser $currentUser->getForumUser();
  90.         }
  91.         $recentPost $forumPostRepository->recentPost();
  92.         $knpPage $request->get('page'1);
  93.         $knpPerPage 30;
  94.         /** @var ForumPost[] $posts */
  95.         $posts $paginator->paginate(
  96.             $recentPost,
  97.             $request->query->getInt('page'$knpPage),
  98.             $knpPerPage
  99.         );
  100.         return $this->render('forum/forum_post/recent.html.twig', [
  101.             'posts' => $posts,
  102.             'currentForumUser' => $currentForumUser,
  103.         ]);
  104.     }
  105.     /**
  106.      * @Route("/{countryMin}/new/{slug}", name="app_forum_forum_post_new")
  107.      * @IsGranted("ROLE_FREELANCE")
  108.      *
  109.      */
  110.     public function new(
  111.         Request                $request,
  112.         ForumPostRepository    $forumPostRepository,
  113.         EntityManagerInterface $entityManager,
  114.         SluggerInterface       $slugger,
  115.         string                 $countryMin,
  116.         ?string                $slug ''
  117.     ): Response
  118.     {
  119.         /** @var User $currentUser */
  120.         $currentUser $this->getUser();
  121.         $forumUser $currentUser->getForumUser();
  122.         $forumPost = new ForumPost();
  123.         $formOptions['topic_selected_id'] = null;
  124.         if ($slug) {
  125.             $topicSelected $entityManager->getRepository(ForumTopic::class)->findOneBy([
  126.                 'slug' => $slug
  127.             ]);
  128.             if ($topicSelected)
  129.                 $formOptions['topic_selected_id'] = $topicSelected->getId();
  130.         }
  131.         $formOptions['topicsIDs'] = $this->getTopicsIDs($entityManager);
  132.         $form $this->createForm(ForumPostType::class, $forumPost$formOptions);
  133.         $form->handleRequest($request);
  134.         if ($form->isSubmitted() && $form->isValid()) {
  135.             $forumPost->setForumUser($forumUser);
  136.             $topic $entityManager->getRepository(ForumTopic::class)->findOneBy([
  137.                 'id' => $form->get('topic')->getData()
  138.             ]);
  139.             if (!$topic) {
  140.                 throw $this->createAccessDeniedException();
  141.             }
  142.             $forumPost->setTopic($topic);
  143.             $forumPostRepository->add($forumPosttrue);
  144.             // UPDATE SLUG
  145.             $this->postUpdateSlug($forumPost$slugger);
  146.             $forumPostRepository->add($forumPosttrue);
  147.             return $this->redirectToRoute('app_forum_forum_post_show', [
  148.                 'country' => $forumPost->getTopic()->getCountryMin(),
  149.                 'topicslug' => $forumPost->getTopic()->getSlug(),
  150.                 'slug' => $forumPost->getSlug()
  151.             ], Response::HTTP_SEE_OTHER);
  152.         }
  153.         return $this->render('forum/forum_post/new.html.twig', [
  154.             'forum_post' => $forumPost,
  155.             'form' => $form->createView(),
  156.         ]);
  157.     }
  158.     /**
  159.      * @Route("/{country}/{topicslug}/{slug}", name="app_forum_forum_post_show")
  160.      *
  161.      */
  162.     public function show(
  163.         EntityManagerInterface            $entityManager,
  164.         Request                           $request,
  165.         ForumSubscribeNotificationService $subscribeNotificationService,
  166.         ForumPost                         $post // GET BY SLUG
  167.     ): Response
  168.     {
  169.         /** @var User $currentUser */
  170.         $currentUser $this->getUser();
  171.         /** @var ?ForumUser $currentForumUser */
  172.         $currentForumUser null;
  173.         //  -------------- VIEWED POST --------------
  174.         if ($currentUser) {
  175.             $currentForumUser $currentUser->getForumUser();
  176.             // ADD VIEWED POST
  177.             if (
  178.                 $currentForumUser
  179.                 && $currentUser !== $post->getForumUser()
  180.                 && $post->isActived()
  181.             ) {
  182.                 $post->addUserView($currentForumUser);
  183.                 $entityManager->persist($post);
  184.                 $entityManager->flush();
  185.             }
  186.         }
  187.         //  --------------  // VIEWED POST //  --------------
  188.         // REDIRECT IF NOT ACTIVE
  189.         if (
  190.             !$post->isActived()
  191.             && ($post->getForumUser() !== $currentForumUser && !$this->isGranted('ROLE_ADMIN'))
  192.         ) {
  193.             throw $this->createNotFoundException();
  194.         }
  195.         //  -------------- COMMENT SCRIPT --------------
  196.         $comment = new ForumComment();
  197.         $formComment $this->createForm(ForumCommentType::class, $comment);
  198.         $formComment->handleRequest($request);
  199.         if ($formComment->isSubmitted() && $formComment->isValid()) {
  200.             if ($post->isClosed()) {
  201.                 throw $this->createAccessDeniedException();
  202.             }
  203.             if ($currentForumUser) {
  204.                 $comment->setForumUser($currentForumUser);
  205.                 $comment->setPost($post);
  206.                 $entityManager->persist($comment);
  207.                 // ADD SUBSCRIBE
  208.                 $notificationSubscriber $entityManager->getRepository(ForumNotificationSubscriber::class)->findOneBy([
  209.                     'post' => $post,
  210.                     'forumUser' => $currentForumUser
  211.                 ]);
  212.                 if (!$notificationSubscriber) {
  213.                     $notificationSubscriber = new ForumNotificationSubscriber();
  214.                     $notificationSubscriber->setPost($post);
  215.                     $notificationSubscriber->setTypeComment();
  216.                     $notificationSubscriber->setForumUser($currentForumUser);
  217.                     $entityManager->persist($notificationSubscriber);
  218.                 }
  219.                 $entityManager->flush();
  220.                 // GENERATE NOTIFICATION
  221.                 $subscribeNotificationService->generateNotification($currentForumUser$post);
  222.                 return $this->redirectToRoute('app_forum_forum_post_show', [
  223.                     'country' => $post->getTopic()->getCountryMin(),
  224.                     'topicslug' => $post->getTopic()->getSlug(),
  225.                     'slug' => $post->getSlug()
  226.                 ]);
  227.             }
  228.         }
  229.         //  --------------  // COMMENT SCRIPT //  --------------
  230.         //  -------------- COMMENT SCRIPT --------------
  231.         /** @var ForumCommentReplyType[] $formReplys */
  232.         $formReplys = [];
  233.         foreach ($post->getComments() as $comment) {
  234.             $forumReply = new ForumCommentReply();
  235.             $formReplys['comment_' $comment->getId()] = $this->createForm(
  236.                 ForumCommentReplyType::class,
  237.                 $forumReply
  238.             )->createView();
  239.         }
  240.         //  --------------  // COMMENT SCRIPT //  --------------
  241.         return $this->render('forum/forum_post/show.html.twig', [
  242.             'post' => $post,
  243.             'currentForumUser' => $currentForumUser,
  244.             'formComment' => $formComment->createView(),
  245.             'formReplys' => $formReplys,
  246.             'reportForm' => $this->createForm(ForumReportType::class, new ForumReport())->createView(),
  247.         ]);
  248.     }
  249.     /**
  250.      * @Route("/comment/reply{id}", name="app_forum_forum_comment_reply")
  251.      * @IsGranted("ROLE_FREELANCE")
  252.      *
  253.      */
  254.     public function replyComment(
  255.         EntityManagerInterface            $entityManager,
  256.         Request                           $request,
  257.         ForumSubscribeNotificationService $subscribeNotificationService,
  258.         ForumComment                      $comment
  259.     ): Response
  260.     {
  261.         if ($comment->getPost()->isClosed()) {
  262.             throw $this->createAccessDeniedException();
  263.         }
  264.         /** @var User $currentUser */
  265.         $currentUser $this->getUser();
  266.         /** @var ?ForumUser $currentForumUser */
  267.         $currentForumUser null;
  268.         if ($currentUser) {
  269.             $currentForumUser $currentUser->getForumUser();
  270.         }
  271.         $reply = new ForumCommentReply();
  272.         $formComment $this->createForm(ForumCommentReplyType::class, $reply);
  273.         $formComment->handleRequest($request);
  274.         if ($formComment->isSubmitted() && $formComment->isValid()) {
  275.             if ($currentForumUser) {
  276.                 $reply->setForumUser($currentForumUser);
  277.                 $reply->setComment($comment);
  278.                 $entityManager->persist($reply);
  279.                 // ADD SUBSCRIBE
  280.                 $notificationSubscriber $entityManager->getRepository(ForumNotificationSubscriber::class)->findOneBy([
  281.                     'comment' => $comment,
  282.                     'forumUser' => $currentForumUser
  283.                 ]);
  284.                 if (!$notificationSubscriber) {
  285.                     $notificationSubscriber = new ForumNotificationSubscriber();
  286.                     $notificationSubscriber->setComment($comment);
  287.                     $notificationSubscriber->setPost($comment->getPost());
  288.                     $notificationSubscriber->setTypeReply();
  289.                     $notificationSubscriber->setForumUser($currentForumUser);
  290.                     $entityManager->persist($notificationSubscriber);
  291.                 }
  292.                 $entityManager->flush();
  293.                 // GENERATE NOTIFICATION
  294.                 $subscribeNotificationService->generateNotification($currentForumUser$comment->getPost(), $comment);
  295.             }
  296.         }
  297.         $post $comment->getPost();
  298.         return $this->redirectToRoute('app_forum_forum_post_show', [
  299.             'country' => $post->getTopic()->getCountryMin(),
  300.             'topicslug' => $post->getTopic()->getSlug(),
  301.             'slug' => $post->getSlug()
  302.         ]);
  303.     }
  304.     /**
  305.      * @Route("/{country}/post/edit/{id}", name="app_forum_forum_post_edit")
  306.      * @IsGranted("ROLE_FREELANCE")
  307.      *
  308.      */
  309.     public function edit(
  310.         EntityManagerInterface $entityManager,
  311.         Request                $request,
  312.         ForumPostRepository    $forumPostRepository,
  313.         SluggerInterface       $slugger,
  314.         ForumPost              $forumPost
  315.     ): Response
  316.     {
  317.         /** @var User $currentUser */
  318.         $currentUser $this->getUser();
  319.         /** @var ?ForumUser $currentForumUser */
  320.         $currentForumUser null;
  321.         if ($currentUser) {
  322.             $currentForumUser $currentUser->getForumUser();
  323.         }
  324.         if ($forumPost->getForumUser() !== $currentForumUser) {
  325.             throw $this->createAccessDeniedException();
  326.         }
  327.         if ($forumPost->isClosed()) {
  328.             throw $this->createAccessDeniedException();
  329.         }
  330.         $formOptions['topic_selected_id'] = $forumPost->getTopic()->getId();
  331.         $formOptions['topicsIDs'] = $this->getTopicsIDs($entityManager);
  332.         $form $this->createForm(ForumPostType::class, $forumPost$formOptions);
  333.         $form->handleRequest($request);
  334.         if ($form->isSubmitted() && $form->isValid()) {
  335.             $topic $entityManager->getRepository(ForumTopic::class)->findOneBy([
  336.                 'id' => $form->get('topic')->getData()
  337.             ]);
  338.             if (!$topic) {
  339.                 throw $this->createAccessDeniedException();
  340.             }
  341.             $forumPost->setTopic($topic);
  342.             $forumPostRepository->add($forumPosttrue);
  343.             // UPDATE SLUG
  344.             $this->postUpdateSlug($forumPost$slugger);
  345.             $forumPostRepository->add($forumPosttrue);
  346.             return $this->redirectToRoute('app_forum_forum_post_show', [
  347.                 'country' => $forumPost->getTopic()->getCountryMin(),
  348.                 'topicslug' => $forumPost->getTopic()->getSlug(),
  349.                 'slug' => $forumPost->getSlug()
  350.             ], Response::HTTP_SEE_OTHER);
  351.         }
  352.         return $this->render('forum/forum_post/edit.html.twig', [
  353.             'forum_post' => $forumPost,
  354.             'form' => $form->createView(),
  355.         ]);
  356.     }
  357.     /**
  358.      * @Route("/post/favorite", name="app_forum_forum_post_favorite_index")
  359.      * @IsGranted("ROLE_FREELANCE")
  360.      *
  361.      */
  362.     public function favorite(
  363.         PaginatorInterface $paginator,
  364.         Request            $request
  365.     ): Response
  366.     {
  367.         /** @var User $currentUser */
  368.         $currentUser $this->getUser();
  369.         /** @var ?ForumUser $currentForumUser */
  370.         $currentForumUser null;
  371.         if ($currentUser) {
  372.             $currentForumUser $currentUser->getForumUser();
  373.         }
  374.         $topicPosts $currentForumUser->getFavoritePost();
  375.         $knpPage $request->get('page'1);
  376.         $knpPerPage 30;
  377.         /** @var ForumPost[] $posts */
  378.         $posts $paginator->paginate(
  379.             $topicPosts,
  380.             $request->query->getInt('page'$knpPage),
  381.             $knpPerPage
  382.         );
  383.         return $this->render('forum/forum_post/favorite.html.twig', [
  384.             'posts' => $posts,
  385.             'currentForumUser' => $currentForumUser,
  386.         ]);
  387.     }
  388.     /**
  389.      * @Route("/post/favorite/add/{id}", name="app_forum_forum_post_favorite_add")
  390.      * @IsGranted("ROLE_FREELANCE")
  391.      *
  392.      */
  393.     public function favoriteAdd(
  394.         EntityManagerInterface $entityManager,
  395.         ForumPost              $post
  396.     ): Response
  397.     {
  398.         /** @var User $currentUser */
  399.         $currentUser $this->getUser();
  400.         /** @var ?ForumUser $currentForumUser */
  401.         $currentForumUser null;
  402.         if ($currentUser) {
  403.             $currentForumUser $currentUser->getForumUser();
  404.         }
  405.         if ($currentForumUser) {
  406.             if ($post->isFavorite($currentForumUser)) {
  407.                 $currentForumUser->removeFavoritePost($post);
  408.                 // DELETE SUBSCRIBE
  409.                 $notificationSubscriber $entityManager->getRepository(ForumNotificationSubscriber::class)->findOneBy([
  410.                     'post' => $post,
  411.                     'forumUser' => $currentForumUser
  412.                 ]);
  413.                 if ($notificationSubscriber) {
  414.                     $entityManager->remove($notificationSubscriber);
  415.                 }
  416.             } else {
  417.                 $currentForumUser->addFavoritePost($post);
  418.                 // ADD SUBSCRIBE
  419.                 $notificationSubscriber $entityManager->getRepository(ForumNotificationSubscriber::class)->findOneBy([
  420.                     'post' => $post,
  421.                     'forumUser' => $currentForumUser
  422.                 ]);
  423.                 if (!$notificationSubscriber) {
  424.                     $notificationSubscriber = new ForumNotificationSubscriber();
  425.                     $notificationSubscriber->setPost($post);
  426.                     $notificationSubscriber->setTypeFavory();
  427.                     $notificationSubscriber->setForumUser($currentForumUser);
  428.                     $entityManager->persist($notificationSubscriber);
  429.                 }
  430.             }
  431.             $entityManager->persist($currentForumUser);
  432.             $entityManager->flush();
  433.         } else {
  434.             throw $this->createAccessDeniedException();
  435.         }
  436.         $json = [
  437.             'status' => 200
  438.         ];
  439.         return new JsonResponse(json_encode($json), 200, [], true);
  440.     }
  441.     /**
  442.      * @Route("/f/re/report/submit", name="app_forum_post_report")
  443.      * @IsGranted("ROLE_FREELANCE")
  444.      *
  445.      */
  446.     public function reportPost(
  447.         Request                $request,
  448.         EntityManagerInterface $entityManager
  449.     ): Response
  450.     {
  451.         /** @var User $currentUser */
  452.         $currentUser $this->getUser();
  453.         /** @var ?ForumUser $currentForumUser */
  454.         $currentForumUser null;
  455.         if ($currentUser) {
  456.             $currentForumUser $currentUser->getForumUser();
  457.         }
  458.         /** @var ForumPost $post */
  459.         $post null;
  460.         $report = new ForumReport();
  461.         $formReport $this->createForm(ForumReportType::class, $report);
  462.         $formReport->handleRequest($request);
  463.         if ($formReport->isSubmitted() && $formReport->isValid()) {
  464.             if ($currentForumUser) {
  465.                 $typeEntity $formReport->get('report_entity')->getData();
  466.                 $rubriqueID $formReport->get('rubrique_id')->getData();
  467.                 $report->setForumUser($currentForumUser);
  468.                 if ($typeEntity == 'comment') {
  469.                     $comment $entityManager->getRepository(ForumComment::class)->findOneBy([
  470.                         'id' => $rubriqueID
  471.                     ]);
  472.                     $report->setComment($comment);
  473.                     $post $comment->getPost();
  474.                 } elseif ($typeEntity == 'reply') {
  475.                     $commentReply $entityManager->getRepository(ForumCommentReply::class)->find($rubriqueID);
  476.                     $report->setCommentReply($commentReply);
  477.                     $comment $commentReply->getComment();
  478.                     $post $comment->getPost();
  479.                 } elseif ($typeEntity == 'post') {
  480.                     $post $entityManager->getRepository(ForumPost::class)->findOneBy([
  481.                         'id' => $rubriqueID
  482.                     ]);
  483.                 }
  484.                 if ($post) {
  485.                     $report->setPost($post);
  486.                     $entityManager->persist($report);
  487.                     $entityManager->flush();
  488.                     $this->addFlash('toastr_success''Your report has been sent successfully');
  489.                     return $this->redirectToRoute('app_forum_forum_post_show', [
  490.                         'country' => $post->getTopic()->getCountryMin(),
  491.                         'topicslug' => $post->getTopic()->getSlug(),
  492.                         'slug' => $post->getSlug()
  493.                     ]);
  494.                 }
  495.             }
  496.         }
  497.         return $this->redirectToRoute('homepage');
  498.     }
  499.     /**
  500.      * @Route("/post/subscribe", name="app_forum_forum_post_subscribe")
  501.      * @IsGranted("ROLE_FREELANCE")
  502.      *
  503.      */
  504.     public function subscribeList(
  505.         EntityManagerInterface $entityManager,
  506.         PaginatorInterface     $paginator,
  507.         Request                $request
  508.     ): Response
  509.     {
  510.         /** @var User $currentUser */
  511.         $currentUser $this->getUser();
  512.         /** @var ?ForumUser $currentForumUser */
  513.         $currentForumUser null;
  514.         if ($currentUser) {
  515.             $currentForumUser $currentUser->getForumUser();
  516.         }
  517.         if (!$currentForumUser) {
  518.             throw $this->createAccessDeniedException();
  519.         }
  520.         /** @var ForumPost[] $subscribePost */
  521.         $subscribePost = [];
  522.         $subscribeNotification $entityManager->getRepository(ForumNotificationSubscriber::class)->findBy(
  523.             ['forumUser' => $currentForumUser],
  524.             ['id' => 'DESC'],
  525.         );
  526.         foreach ($subscribeNotification as $sbNotif) {
  527.             if (
  528.                 !in_array($sbNotif->getPost(), $subscribePost)
  529.                 && !$currentForumUser->getPosts()->contains($sbNotif->getPost())
  530.             ) {
  531.                 $subscribePost[] = $sbNotif->getPost();
  532.             }
  533.         }
  534.         return $this->render('forum/forum_post/subscribe_list.html.twig', [
  535.             'subscribePost' => $subscribePost,
  536.             'currentForumUser' => $currentForumUser,
  537.         ]);
  538.     }
  539.     /**
  540.      * @Route("/{countryMin}/search/", name="app_forum_forum_post_search")
  541.      * @IsGranted("ROLE_FREELANCE")
  542.      *
  543.      */
  544.     public function searchPost(
  545.         PaginatorInterface  $paginator,
  546.         Request             $request,
  547.         ForumPostRepository $forumPostRepository
  548.     ): Response
  549.     {
  550.         /** @var User $currentUser */
  551.         $currentUser $this->getUser();
  552.         $query $request->query->get("search");
  553.         /** @var ?ForumUser $currentForumUser */
  554.         $currentForumUser null;
  555.         if ($currentUser) {
  556.             $currentForumUser $currentUser->getForumUser();
  557.         }
  558.         $topicPosts $forumPostRepository->findBySearch($query);
  559.         $knpPage $request->get('page'1);
  560.         $knpPerPage 30;
  561.         /** @var ForumPost[] $posts */
  562.         $posts $paginator->paginate(
  563.             $topicPosts,
  564.             $request->query->getInt('page'$knpPage),
  565.             $knpPerPage
  566.         );
  567.         return $this->render('forum/forum_post/search.html.twig', [
  568.             'posts' => $posts,
  569.             'currentForumUser' => $currentForumUser,
  570.             'search' => $query
  571.         ]);
  572.     }
  573.     /**
  574.      * @Route("/closed/45654654{id}546543", name="app_forum_forum_post_closed")
  575.      *
  576.      */
  577.     public function close(
  578.         EntityManagerInterface $entityManager,
  579.         Request                $request,
  580.         ForumPost              $forumPost
  581.     ): Response
  582.     {
  583.         /** @var User $currentUser */
  584.         $currentUser $this->getUser();
  585.         /** @var ?ForumUser $currentForumUser */
  586.         $currentForumUser null;
  587.         $haveAccess false;
  588.         if ($currentUser) {
  589.             $currentForumUser $currentUser->getForumUser();
  590.         }
  591.         if ($forumPost->getForumUser() === $currentForumUser) {
  592.             $haveAccess true;
  593.         }
  594.         if($currentUser->isRole('ROLE_ADMIN')){
  595.             $haveAccess true;
  596.         }
  597.         if (! $haveAccess) {
  598.             throw $this->createAccessDeniedException();
  599.         }
  600.         $forumPost->setClosed(true);
  601.         $forumPost->setClosedAt(new \DateTime());
  602.         $entityManager->persist($forumPost);
  603.         $entityManager->flush();
  604.         return $this->redirectToRoute('app_forum_forum_post_show', [
  605.             'country' => $forumPost->getTopic()->getCountryMin(),
  606.             'topicslug' => $forumPost->getTopic()->getSlug(),
  607.             'slug' => $forumPost->getSlug()
  608.         ], Response::HTTP_SEE_OTHER);
  609.     }
  610.     public function delete(Request $requestForumPost $forumPostForumPostRepository $forumPostRepository): Response
  611.     {
  612.         if ($this->isCsrfTokenValid('delete' $forumPost->getId(), $request->request->get('_token'))) {
  613.             $forumPostRepository->remove($forumPosttrue);
  614.         }
  615.         return $this->redirectToRoute('app_forum_forum_post_index', [], Response::HTTP_SEE_OTHER);
  616.     }
  617. }