Files
ENI-sortir/src/Controller/ProfileController.php
jleroy2023 5b6b44ab9d set V1
2024-11-20 14:49:09 +01:00

111 lines
5.4 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Participant;
use App\Form\ProfileFormType;
use App\Service\FileUploader;
use App\Form\RegistrationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use App\Repository\ParticipantRepository;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ProfileController extends AbstractController
{
private FileUploader $fileUploader;
private ParticipantRepository $profileRepo;
public function __construct(FileUploader $fileUploader, ParticipantRepository $profileRepo) {
$this->fileUploader = $fileUploader;
$this->profileRepo = $profileRepo;
}
#[Route('/profile/{uuid}', name: 'profile_view', methods: ['GET'])]
public function viewProfile(string $uuid, ParticipantRepository $profileRepo, TokenStorageInterface $tokenStorage): Response
{
$token = $tokenStorage->getToken();
$userConnect = $token?->getUser();
$currentProfile = $profileRepo->findOneBy(['idParticipant' => $uuid]);
if ($userConnect->getIdParticipant() !== $currentProfile->getIdParticipant()) {
$this->addFlash('error', "Vous ne pouvez pas consulter un profil qui n'est pas le votre");
return $this->redirectToRoute('home');
}
return $this->render('profile/view.html.twig', [
'profile' => $currentProfile,
]);
}
#[Route('/profile/edit/{uuid}', name: 'profile_edit', methods: ['GET', 'POST'])]
public function editProfile(string $uuid, Request $request, TokenStorageInterface $tokenStorage): Response
{
try {
$token = $tokenStorage->getToken();
$userConnect = $token?->getUser();
$profile = $this->profileRepo->findOneBy(['idParticipant' => $uuid]);
if ($userConnect->getIdParticipant() !== $profile->getIdParticipant()) {
$this->addFlash('error', "Vous ne pouvez pas modifier un profil qui n'est pas le votre");
return $this->redirectToRoute('home');
}
$form = $this->createForm(ProfileFormType::class, $profile);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$imageFile = $form->get('image')->getData();
if (($form->has('deleteImage') && $form['deleteImage']->getData()) || $imageFile) {
$this->fileUploader->delete($profile->getFileName(), '/upload/image/profile');
if ($imageFile) {
$imageFilename = $this->fileUploader->upload($imageFile);
$profile->setFileName($imageFilename);
} else {
$profile->setFileName(null);
}
}
if ($form->has('newPassword') && $form->has('confirmPassword')) {
if ($form->get('newPassword')->getData() !== $form->get('confirmPassword')->getData()) {
$this->addFlash('error', "Les mots de passe ne correspondent pas");
return $this->render('profile/edit.html.twig', [
'profile' => $userConnect,
'formProfile' => $form,
]);
}
$profile->setPassword($form->get('newPassword')->getData());
}
if ($form->has('pseudo')) {
$alreadyExists = $this->profileRepo->findOneBy(['pseudo' => $profile->getPseudo()]);
if ($alreadyExists && $alreadyExists !== $profile) {
$this->addFlash('error', "Ce pseudo existe déjà");
return $this->render('profile/edit.html.twig', [
'profile' => $userConnect,
'formProfile' => $form,
]);
}
}
if ($form->has('email')) {
$alreadyExists = $this->profileRepo->findOneBy(['email' => $profile->getEmail()]);
if ($alreadyExists && $alreadyExists !== $profile) {
$this->addFlash('error', "Cet email existe déjà");
return $this->render('profile/edit.html.twig', [
'profile' => $userConnect,
'formProfile' => $form,
]);
}
}
$profileToUpdate = $this->profileRepo->update($profile);
if (!$profileToUpdate) {
throw $this->createNotFoundException('No profile found');
}
$this->addFlash('success', 'Votre profile est bien à jour');
return $this->redirectToRoute('profile_view',['uuid' => $profile->getIdParticipant()]);
}
return $this->render('profile/edit.html.twig', [
'profile' => $userConnect,
'formProfile' => $form,
]);
} catch(\Exception $e) {
$formProfile = $this->createForm(ProfileFormType::class, $profile);
$this->addFlash('error', $e->getMessage());
return $this->render('profile/edit.html.twig', ['formProfile' => $formProfile]);
}
}
}