← Back
Editing: CommentaireController.php
<?php namespace App\Controller; use App\Entity\Commentaire; use App\Form\CommentaireType; use App\Repository\CommentaireRepository; use Doctrine\ORM\EntityManagerInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route(path: '/commentaire')] class CommentaireController extends AbstractController { /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/', name: 'commentaire_index', methods: ['GET'])] public function index(CommentaireRepository $commentaireRepository): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $comments = $commentaireRepository->findAll(); } else { $comments = $commentaireRepository->findByAgency($this->getUser()->getAgency()); } return $this->render('commentaire/index.html.twig', [ 'commentaires' => $comments, ]); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/new', name: 'commentaire_new', methods: ['GET', 'POST'])] public function new(Request $request, EntityManagerInterface $em): Response { $this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN') ? $options['role'] = 'admin' : $options['role'] = 'non-admin'; $commentaire = new Commentaire(); $form = $this->createForm(CommentaireType::class, $commentaire, $options); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { if ('non-admin' === $options['role'] && null !== $this->getUser()->getAgency()) { $commentaire->setAgency($this->getUser()->getAgency()); } $em->persist($commentaire); $em->flush(); return $this->redirectToRoute('commentaire_index'); } return $this->render('commentaire/new.html.twig', [ 'commentaire' => $commentaire, 'form' => $form->createView(), ]); } #[Route(path: '/{id}', name: 'commentaire_show', methods: ['GET'])] public function show(Commentaire $commentaire): Response { return $this->render('commentaire/show.html.twig', [ 'commentaire' => $commentaire, ]); } #[Route(path: '/{id}/edit', name: 'commentaire_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Commentaire $commentaire, EntityManagerInterface $em): Response { $form = $this->createForm(CommentaireType::class, $commentaire); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { if (null === $commentaire->getAgency() && null === $commentaire->getItem()) { $commentaire->setAgency($this->getUser()->getAgency()); } $em->flush(); return $this->redirectToRoute('commentaire_index', [ 'id' => $commentaire->getId(), ]); } return $this->render('commentaire/edit.html.twig', [ 'commentaire' => $commentaire, 'form' => $form->createView(), ]); } #[Route(path: '/{id}', name: 'commentaire_delete', methods: ['DELETE'])] public function delete(Request $request, Commentaire $commentaire, EntityManagerInterface $em): Response { if ($this->isCsrfTokenValid('delete'.$commentaire->getId(), $request->request->get('_token'))) { $em->remove($commentaire); $em->flush(); } return $this->redirectToRoute('commentaire_index'); } }
Save File
Cancel