src/Controller/ActivityController.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Activity;
  4. use App\Form\ActivityType;
  5. use App\Repository\ActivityRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. #[Route(path'/activity')]
  16. #[IsGranted('ROLE_ADMIN')]
  17. class ActivityController extends AbstractController {
  18.     private EntityManagerInterface $manager;
  19.     private ActivityRepository $activityRepository;
  20.     private TranslatorInterface $translator;
  21.     public function __construct(
  22.         EntityManagerInterface $manager,
  23.         ActivityRepository $activityRepository,
  24.         TranslatorInterface $translator
  25.     ) {
  26.         $this->manager $manager;
  27.         $this->activityRepository $activityRepository;
  28.         $this->translator $translator;
  29.     }
  30.     #[Route(path'/'name'activity_index'methods: ['GET'])]
  31.     public function indexAction(): Response {
  32.         $activities $this->activityRepository->findAll();
  33.         return $this->render('activity/index.html.twig', ['activities' => $activities]);
  34.     }
  35.     #[Route(path'/new'name'activity_new'methods: ['GET''POST'])]
  36.     public function newAction(Request $request): RedirectResponse|Response {
  37.         $activity = new Activity();
  38.         $form $this->createForm(ActivityType::class, $activity);
  39.         $form->handleRequest($request);
  40.         if ($form->isSubmitted() && $form->isValid()) {
  41.             $this->manager->persist($activity);
  42.             $this->manager->flush();
  43.             return $this->redirectToRoute('activity_show', ['id' => $activity->getId()]);
  44.         }
  45.         return $this->render('activity/new.html.twig', [
  46.             'activity' => $activity,
  47.             'form' => $form->createView()
  48.         ]);
  49.     }
  50.     #[Route(path'/{id}'name'activity_show'methods: ['GET'])]
  51.     public function showAction(Activity $activity): Response {
  52.         return $this->render('activity/show.html.twig', [
  53.             'activity' => $activity,
  54.         ]);
  55.     }
  56.     #[Route(path'/{id}/edit'name'activity_edit'methods: ['GET''POST'])]
  57.     public function editAction(Request $requestActivity $activity): RedirectResponse|Response {
  58.         $editForm $this->createForm(ActivityType::class, $activity);
  59.         $editForm->handleRequest($request);
  60.         if ($editForm->isSubmitted() && $editForm->isValid()) {
  61.             $this->manager->flush();
  62.             return $this->redirectToRoute('activity_edit', ['id' => $activity->getId()]);
  63.         }
  64.         return $this->render('activity/edit.html.twig', [
  65.             'activity' => $activity,
  66.             'edit_form' => $editForm->createView()
  67.         ]);
  68.     }
  69.     #[Route(path'/delete/{id}'name'activity_delete'methods: ['GET'])]
  70.     public function deleteAction(Request $request, ?Activity $activity): RedirectResponse {
  71.         if (array_key_exists('HTTP_REFERER'$request->server->all())) {
  72.             if ($activity) {
  73.                 $this->manager->remove($activity);
  74.                 $this->manager->flush();
  75.                 $this->addFlash('success'$this->translator->trans('flashbag.the_deletion_is_done_successfully'));
  76.             } else {
  77.                 $this->addFlash('warning'$this->translator->trans('flashbag.unable_to_delete_the_job'));
  78.                 return $this->redirect($request->server->all()['HTTP_REFERER']);
  79.             }
  80.         }
  81.         return $this->redirectToRoute('activity_index');
  82.     }
  83. }