src/Controller/admin/CountryController.php line 146

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Controller\admin;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use App\Entity\Country;
  20. use App\Entity\Destination;
  21. /**
  22.  * Controller used to manage current user.
  23.  *
  24.  * @author Romain Monteil <monteil.romain@gmail.com>
  25.  */
  26. #[Route('/admin/country')]
  27. class CountryController extends AbstractController {
  28.     #[Route('/countries-list'methods: ['GET'], name'list_countries')]
  29.     public function listCountries(Request $requestManagerRegistry $doctrine) {
  30.         $em $doctrine->getManager();
  31.         $countries $em->getRepository(Country::class)->findAll();
  32.         return $this->render('admin/country/listCountries.html.twig', [
  33.                     'countries' => $countries,
  34.         ]);
  35.     }
  36.     #[Route('/add-country'methods: ['GET''POST'], name'add_country')]
  37.     public function addCountry(Request $requestManagerRegistry $doctrine) {
  38.         $em $doctrine->getManager();
  39.         if ($request->isMethod('post')) {
  40.             $country = new Country();
  41.             $country->setName($request->get('name'));
  42.             $country->setDescription($request->get('description'));
  43.             $country->setOrdre($request->get('ordre'));
  44.             $country->setPublished(1);
  45.             $em->persist($country);
  46.             $em->flush();
  47.             if ($request->files->get('image')) {
  48.                 $file $request->files->get('image');
  49.                 $country->setImage("uploads/country/cat-" $country->getId() . "/" $file->getClientOriginalName());
  50.                 $file->move("uploads/country/cat-" $country->getId() . "/"$file->getClientOriginalName());
  51.             }
  52.             $em->persist($country);
  53.             $em->flush();
  54.             $this->addFlash('success''Country created successfully');
  55.             return $this->redirectToRoute('list_countries');
  56.         }
  57.         return $this->render('admin/country/addCountry.html.twig', [
  58.         ]);
  59.     }
  60.     #[Route('/destinations-list'methods: ['GET'], name'list_destinations')]
  61.     public function listDestinations(Request $requestManagerRegistry $doctrine) {
  62.         $em $doctrine->getManager();
  63.         $destinations $em->getRepository(Destination::class)->findAll();
  64.         return $this->render('admin/country/listDestinations.html.twig', [
  65.                     'destinations' => $destinations,
  66.         ]);
  67.     }
  68.     #[Route('/add-destination'methods: ['GET''POST'], name'add_destination')]
  69.     public function addDestination(Request $requestManagerRegistry $doctrine) {
  70.         $em $doctrine->getManager();
  71.         $countries $em->getRepository(Country::class)->findAll();
  72.         if ($request->isMethod('post')) {
  73.             $destination = new Destination();
  74.             $destination->setName($request->get('name'));
  75.             $destination->setDescription($request->get('description'));
  76.             $destination->setOrdre($request->get('ordre'));
  77. //            $destination->setColor($request->get('color'));
  78.             $destination->setTypedestination($request->get('typedestination'));
  79.             $destination->setPublished(1);
  80.             $em->persist($destination);
  81.             $em->flush();
  82.             $country $em->getRepository(Country::class)->find($request->get('country'));
  83.             $country->addDestination($destination);
  84.             if ($request->files->get('image')) {
  85.                 $file $request->files->get('image');
  86.                 $destination->setImage("uploads/country/cat-" $destination->getId() . "/" $file->getClientOriginalName());
  87.                 $file->move("uploads/country/cat-" $destination->getId() . "/"$file->getClientOriginalName());
  88.             }
  89.             $em->persist($destination);
  90.             $em->flush();
  91.             $this->addFlash('success''Country created successfully');
  92.             return $this->redirectToRoute('list_destinations');
  93.         }
  94.         return $this->render('admin/country/addDestination.html.twig', [
  95.                     'countries' => $countries,
  96.         ]);
  97.     }
  98.     #[Route('/edit-country-{id}'methods: ['GET''POST'], name'edit_country')]
  99.     public function editCountry(Request $request$idManagerRegistry $doctrine) {
  100.         $em $doctrine->getManager();
  101.         $country $em->getRepository(Country::class)->find($id);
  102.         if ($request->isMethod('post')) {
  103.             $country->setName($request->get('name'));
  104.             $country->setDescription($request->get('description'));
  105.             $country->setOrdre($request->get('ordre'));
  106.             $country->setColor($request->get('color'));
  107.             $country->setPublished(1);
  108.             $em->persist($country);
  109.             $em->flush();
  110.             if ($request->files->get('image')) {
  111.                 $file $request->files->get('image');
  112.                 $country->setImage("uploads/country/cat-" $country->getId() . "/" $file->getClientOriginalName());
  113.                 $file->move("uploads/country/cat-" $country->getId() . "/"$file->getClientOriginalName());
  114.             }
  115.             $em->persist($country);
  116.             $em->flush();
  117.             $this->addFlash('success''Country created successfully');
  118.             return $this->redirectToRoute('list_countries');
  119.         }
  120.         return $this->render('admin/country/edit/editCountry.html.twig', [
  121.                     'country' => $country,
  122.         ]);
  123.     }
  124.     #[Route('/edit-destination-{id}'methods: ['GET''POST'], name'edit_destination')]
  125.     public function editDestination(Request $request$idManagerRegistry $doctrine) {
  126.         $em $doctrine->getManager();
  127.         $destination $em->getRepository(Destination::class)->find($id);
  128.         $countries $em->getRepository(Country::class)->findAll();
  129.         if ($request->isMethod('post')) {
  130.             $destination->setName($request->get('name'));
  131.             $destination->setDescription($request->get('description'));
  132.             $destination->setOrdre($request->get('ordre'));
  133. //            $destination->setColor($request->get('color'));
  134.             $destination->setTypedestination($request->get('typedestination'));
  135.             $destination->setPublished(1);
  136.             $em->persist($destination);
  137.             $em->flush();
  138.             $country $em->getRepository(Country::class)->find($request->get('country'));
  139.             $country->addDestination($destination);
  140.             if ($request->files->get('image')) {
  141.                 $file $request->files->get('image');
  142.                 $destination->setImage("uploads/country/cat-" $country->getId() . "/" $file->getClientOriginalName());
  143.                 $file->move("uploads/country/cat-" $country->getId() . "/"$file->getClientOriginalName());
  144.             }
  145.             $em->persist($country);
  146.             $em->flush();
  147.             $this->addFlash('success''Destination created successfully');
  148.             return $this->redirectToRoute('list_countries');
  149.         }
  150.         return $this->render('admin/country/edit/editDestination.html.twig', [
  151.                     'destination' => $destination,
  152.                     'countries' => $countries,
  153.         ]);
  154.     }
  155. }