<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller\admin;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
use App\Entity\Country;
use App\Entity\Destination;
/**
* Controller used to manage current user.
*
* @author Romain Monteil <monteil.romain@gmail.com>
*/
#[Route('/admin/country')]
class CountryController extends AbstractController {
#[Route('/countries-list', methods: ['GET'], name: 'list_countries')]
public function listCountries(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$countries = $em->getRepository(Country::class)->findAll();
return $this->render('admin/country/listCountries.html.twig', [
'countries' => $countries,
]);
}
#[Route('/add-country', methods: ['GET', 'POST'], name: 'add_country')]
public function addCountry(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
if ($request->isMethod('post')) {
$country = new Country();
$country->setName($request->get('name'));
$country->setDescription($request->get('description'));
$country->setOrdre($request->get('ordre'));
$country->setPublished(1);
$em->persist($country);
$em->flush();
if ($request->files->get('image')) {
$file = $request->files->get('image');
$country->setImage("uploads/country/cat-" . $country->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/country/cat-" . $country->getId() . "/", $file->getClientOriginalName());
}
$em->persist($country);
$em->flush();
$this->addFlash('success', 'Country created successfully');
return $this->redirectToRoute('list_countries');
}
return $this->render('admin/country/addCountry.html.twig', [
]);
}
#[Route('/destinations-list', methods: ['GET'], name: 'list_destinations')]
public function listDestinations(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$destinations = $em->getRepository(Destination::class)->findAll();
return $this->render('admin/country/listDestinations.html.twig', [
'destinations' => $destinations,
]);
}
#[Route('/add-destination', methods: ['GET', 'POST'], name: 'add_destination')]
public function addDestination(Request $request, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$countries = $em->getRepository(Country::class)->findAll();
if ($request->isMethod('post')) {
$destination = new Destination();
$destination->setName($request->get('name'));
$destination->setDescription($request->get('description'));
$destination->setOrdre($request->get('ordre'));
// $destination->setColor($request->get('color'));
$destination->setTypedestination($request->get('typedestination'));
$destination->setPublished(1);
$em->persist($destination);
$em->flush();
$country = $em->getRepository(Country::class)->find($request->get('country'));
$country->addDestination($destination);
if ($request->files->get('image')) {
$file = $request->files->get('image');
$destination->setImage("uploads/country/cat-" . $destination->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/country/cat-" . $destination->getId() . "/", $file->getClientOriginalName());
}
$em->persist($destination);
$em->flush();
$this->addFlash('success', 'Country created successfully');
return $this->redirectToRoute('list_destinations');
}
return $this->render('admin/country/addDestination.html.twig', [
'countries' => $countries,
]);
}
#[Route('/edit-country-{id}', methods: ['GET', 'POST'], name: 'edit_country')]
public function editCountry(Request $request, $id, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$country = $em->getRepository(Country::class)->find($id);
if ($request->isMethod('post')) {
$country->setName($request->get('name'));
$country->setDescription($request->get('description'));
$country->setOrdre($request->get('ordre'));
$country->setColor($request->get('color'));
$country->setPublished(1);
$em->persist($country);
$em->flush();
if ($request->files->get('image')) {
$file = $request->files->get('image');
$country->setImage("uploads/country/cat-" . $country->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/country/cat-" . $country->getId() . "/", $file->getClientOriginalName());
}
$em->persist($country);
$em->flush();
$this->addFlash('success', 'Country created successfully');
return $this->redirectToRoute('list_countries');
}
return $this->render('admin/country/edit/editCountry.html.twig', [
'country' => $country,
]);
}
#[Route('/edit-destination-{id}', methods: ['GET', 'POST'], name: 'edit_destination')]
public function editDestination(Request $request, $id, ManagerRegistry $doctrine) {
$em = $doctrine->getManager();
$destination = $em->getRepository(Destination::class)->find($id);
$countries = $em->getRepository(Country::class)->findAll();
if ($request->isMethod('post')) {
$destination->setName($request->get('name'));
$destination->setDescription($request->get('description'));
$destination->setOrdre($request->get('ordre'));
// $destination->setColor($request->get('color'));
$destination->setTypedestination($request->get('typedestination'));
$destination->setPublished(1);
$em->persist($destination);
$em->flush();
$country = $em->getRepository(Country::class)->find($request->get('country'));
$country->addDestination($destination);
if ($request->files->get('image')) {
$file = $request->files->get('image');
$destination->setImage("uploads/country/cat-" . $country->getId() . "/" . $file->getClientOriginalName());
$file->move("uploads/country/cat-" . $country->getId() . "/", $file->getClientOriginalName());
}
$em->persist($country);
$em->flush();
$this->addFlash('success', 'Destination created successfully');
return $this->redirectToRoute('list_countries');
}
return $this->render('admin/country/edit/editDestination.html.twig', [
'destination' => $destination,
'countries' => $countries,
]);
}
}