Merge branch 'Johan'
This commit is contained in:
@@ -2,7 +2,15 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Participant;
|
||||
use App\Entity\Ville;
|
||||
use App\Repository\ParticipantRepository;
|
||||
use App\Repository\VilleRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
@@ -15,11 +23,120 @@ class AdminController extends AbstractController
|
||||
'controller_name' => 'AdminController',
|
||||
]);
|
||||
}
|
||||
|
||||
//Gestion des utilisateurs
|
||||
#[Route('/admin/user', name: 'app_adminUser')]
|
||||
public function adminUser(): Response
|
||||
public function adminUser(ParticipantRepository $participantRepository): Response
|
||||
{
|
||||
return $this->render('admin/user.html.twig', [
|
||||
'participants' => $participantRepository->findAll(),
|
||||
'controller_name' => 'AdminController',
|
||||
]);
|
||||
}
|
||||
#[Route('/admin/user/import', name: 'participant_import', methods: ['POST'])]
|
||||
public function import(Request $request, EntityManagerInterface $em): Response
|
||||
{
|
||||
$file = $request->files->get('csv_file');
|
||||
if ($file) {
|
||||
$csvData = array_map('str_getcsv', file($file->getPathname()));
|
||||
foreach ($csvData as $index => $row) {
|
||||
if ($index === 0) continue;
|
||||
$participant = new Participant();
|
||||
$participant->setNom($row[0]);
|
||||
$participant->setPrenom($row[1]);
|
||||
$participant->setTelephone($row[2]);
|
||||
$participant->setEmail($row[3]);
|
||||
$participant->setAdministrateur((bool)$row[4]);
|
||||
$participant->setActif((bool)$row[5]);
|
||||
$participant->setRoles(explode('|', $row[6]));
|
||||
$participant->setPassword(password_hash($row[7], PASSWORD_BCRYPT));
|
||||
$em->persist($participant);
|
||||
}
|
||||
$em->flush();
|
||||
}
|
||||
return $this->redirectToRoute('participant_index');
|
||||
}
|
||||
#[Route('/admin/user/export', name: 'participant_export')]
|
||||
public function export(ParticipantRepository $participantRepository): Response
|
||||
{
|
||||
$participants = $participantRepository->findAll();
|
||||
$csv = "Nom,Prénom,Téléphone,Email,Administrateur,Actif,Rôles,Password\n";
|
||||
foreach ($participants as $participant) {
|
||||
$csv .= sprintf(
|
||||
"%s,%s,%s,%s,%s,%s,%s,%s\n",
|
||||
$participant->getNom(),
|
||||
$participant->getPrenom(),
|
||||
$participant->getTelephone(),
|
||||
$participant->getMail(),
|
||||
$participant->isAdministrateur() ? '1' : '0',
|
||||
$participant->isActif() ? '1' : '0',
|
||||
implode('|', $participant->getRoles()),
|
||||
$participant->getPassword()
|
||||
);
|
||||
}
|
||||
$response = new Response($csv);
|
||||
$response->headers->set('Content-Type', 'text/csv');
|
||||
$response->headers->set('Content-Disposition', 'attachment;filename="participants.csv"');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
//Gestion des villes
|
||||
#[Route('/admin/city', name: 'app_adminCity')]
|
||||
public function adminCity(VilleRepository $villeRepository): Response
|
||||
{
|
||||
return $this->render('admin/city.html.twig', [
|
||||
'citys' => $villeRepository->findAll(),
|
||||
'controller_name' => 'AdminController',
|
||||
]);
|
||||
}
|
||||
#[Route('/admin/city/add', name: 'app_adminCityAdd', methods: ['POST'])]
|
||||
public function adminCityAdd(Request $request, EntityManagerInterface $entityManager, VilleRepository $villeRepository): RedirectResponse
|
||||
{
|
||||
try {
|
||||
// Récupérer les données envoyées par le formulaire
|
||||
$postalCode = $request->request->get('postalCode');
|
||||
$cityName = $request->request->get('citySelect');
|
||||
|
||||
// Vérifier que les champs ne sont pas vides
|
||||
if (!$postalCode || !$cityName) {
|
||||
return new Response('Tous les champs sont requis.', Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
// Créer une nouvelle entité City et définir ses propriétés
|
||||
$city = new Ville();
|
||||
$city->setNom($cityName);
|
||||
$city->setCodePostal($postalCode);
|
||||
|
||||
// Enregistrer la ville dans la base de données
|
||||
$entityManager->persist($city);
|
||||
$entityManager->flush();
|
||||
$this->addFlash('success', "Ville ajouté !");
|
||||
return $this->redirectToRoute('app_adminCity');
|
||||
} catch(\Exception $e) {
|
||||
$this->addFlash('error', "Erreur : " . $e->getMessage());
|
||||
return $this->redirectToRoute('app_adminCity');
|
||||
}
|
||||
}
|
||||
#[Route('/admin/city/delete', name: 'app_adminCityDelete')]
|
||||
public function adminCityDelete(String $id, EntityManagerInterface $entityManager): RedirectResponse
|
||||
{
|
||||
// Récupérer la ville à supprimer
|
||||
$city = $entityManager->getRepository(Ville::class)->find($id);
|
||||
|
||||
// Vérifier si la ville existe
|
||||
if (!$city) {
|
||||
// Si la ville n'existe pas, rediriger avec un message d'erreur
|
||||
$this->addFlash('error', 'La ville demandée n\'existe pas.');
|
||||
return $this->redirectToRoute('app_adminCity'); // Rediriger vers la liste des villes
|
||||
}
|
||||
|
||||
// Supprimer la ville
|
||||
$entityManager->remove($city);
|
||||
$entityManager->flush();
|
||||
|
||||
// Ajouter un message de succès et rediriger vers la liste des villes
|
||||
$this->addFlash('success', 'Ville supprimée avec succès.');
|
||||
return $this->redirectToRoute('app_adminCity');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user