src/Controller/CountryController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Country;
  4. use App\Form\CountryType;
  5. use App\Repository\CountryRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. #[Route(path'/country')]
  17. // #[IsGranted('ROLE_ADMIN')]
  18. #[Security("is_granted('ROLE_ADMIN') or 
  19.             is_granted('ROLE_DIRECTEUR')")]
  20. class CountryController extends AbstractController {
  21.     private EntityManagerInterface $em;
  22.     private CountryRepository $countryRepository;
  23.     private TranslatorInterface $translator;
  24.     public function __construct(
  25.         EntityManagerInterface $em,
  26.         CountryRepository $countryRepository,
  27.         TranslatorInterface $translator
  28.     ) {
  29.         $this->em $em;
  30.         $this->countryRepository $countryRepository;
  31.         $this->translator $translator;
  32.     }
  33.     #[Route(path'/'name'country_index'methods: ['GET'])]
  34.     public function indexAction(): Response {
  35.         return $this->render('country/index.html.twig', [
  36.             'countries' => $this->countryRepository->findAll()
  37.         ]);
  38.     }
  39.     #[IsGranted('ROLE_ADMIN')]
  40.     #[Route(path'/new'name'country_new'methods: ['GET''POST'])]
  41.     public function newAction(Request $request): RedirectResponse|Response {
  42.         $country = new Country();
  43.         // Adaptation for specific provinces and countries adaptation
  44.         if ($_ENV['STRUCT_PROVINCE_COUNTRY_CITY'] == 'true') {
  45.             // $country->setName("country for province");
  46.             $country->setIsoCode("");
  47.             $country->setPhoneDigit(0);
  48.             $country->setPhoneCode(0);
  49.             $country->setValid(true);
  50.         }
  51.         $form $this->createForm(CountryType::class, $country);
  52.         $form->handleRequest($request);
  53.         if ($form->isSubmitted() && $form->isValid()) {
  54.             $this->em->persist($country);
  55.             $this->em->flush();
  56.             return $this->redirectToRoute('country_show', ['id' => $country->getId()]);
  57.         }
  58.         return $this->render('country/new.html.twig', [
  59.             'country' => $country,
  60.             'form' => $form->createView(),
  61.         ]);
  62.     }
  63.     #[IsGranted('ROLE_ADMIN')]
  64.     #[Route(path'/{id}'name'country_show'methods: ['GET'])]
  65.     public function showAction(Country $country): Response {
  66.         return $this->render('country/show.html.twig', array(
  67.             'country' => $country,
  68.         ));
  69.     }
  70.     #[IsGranted('ROLE_ADMIN')]
  71.     #[Route(path'/{id}/edit'name'country_edit'methods: ['GET''POST'])]
  72.     public function editAction(Request $requestCountry $country): RedirectResponse|Response {
  73.         $editForm $this->createForm(CountryType::class, $country);
  74.         $editForm->handleRequest($request);
  75.         if ($editForm->isSubmitted() && $editForm->isValid()) {
  76.             $this->em->flush();
  77.             return $this->redirectToRoute('country_show', ['id' => $country->getId()]);
  78.         }
  79.         return $this->render('country/edit.html.twig', [
  80.             'country' => $country,
  81.             'edit_form' => $editForm->createView(),
  82.         ]);
  83.     }
  84.     #[IsGranted('ROLE_ADMIN')]
  85.     #[Route(path'/delete/{id}'name'country_delete'methods: ['GET'])]
  86.     public function deleteAction(Request $request, ?Country $country): RedirectResponse {
  87.         if (array_key_exists('HTTP_REFERER'$request->server->all())) {
  88.             if ($country) {
  89.                 $this->em->remove($country);
  90.                 $this->em->flush();
  91.                 $this->addFlash('success'$this->translator->trans('flashbag.the_deletion_is_done_successfully'));
  92.             } else {
  93.                 $this->addFlash('warning'$this->translator->trans('flashbag.unable_to_delete_the_country'));
  94.                 return $this->redirect($request->server->all()['HTTP_REFERER']);
  95.             }
  96.         }
  97.         return $this->redirectToRoute('country_index');
  98.     }
  99. }