profile update done

This commit is contained in:
Olivier PARPAILLON
2024-11-20 12:51:11 +01:00
parent 8563a74e3b
commit 40019dff72
16 changed files with 498 additions and 24 deletions

View File

@@ -3,6 +3,7 @@
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;
@@ -20,7 +21,7 @@ class ProfileController extends AbstractController
$this->profileRepo = $profileRepo;
}
#[Route('/profile/{uuid}', name: 'profile_view', methods: ['GET'])]
public function viewProfile(string $uuid, ParticipantRepository $profileRepo): Response
public function viewProfile(string $uuid, ParticipantRepository $profileRepo, Request $request): Response
{
$currentProfile = $profileRepo->findOneBy(['idParticipant' => $uuid]);
return $this->render('profile/view.html.twig', [
@@ -37,7 +38,7 @@ class ProfileController extends AbstractController
$this->addFlash('error', "Vous ne pouvez pas modifier un profil qui n'est pas le votre");
return $this->redirectToRoute('profile_view',['uuid' => $profile->getIdParticipant()]);
}
$form = $this->createForm(RegistrationFormType::class, $profile);
$form = $this->createForm(ProfileFormType::class, $profile);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$imageFile = $form->get('image')->getData();
@@ -47,7 +48,34 @@ class ProfileController extends AbstractController
$imageFilename = $this->fileUploader->upload($imageFile);
$profile->setFileName($imageFilename);
} else {
$profile->setFileName('');
$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', [
'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', [
'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', [
'formProfile' => $form,
]);
}
}
$profileToUpdate = $this->profileRepo->update($profile);
@@ -61,7 +89,7 @@ class ProfileController extends AbstractController
'formProfile' => $form,
]);
} catch(\Exception $e) {
$formProfile = $this->createForm(RegistrationFormType::class, $profile);
$formProfile = $this->createForm(ProfileFormType::class, $profile);
$this->addFlash('error', $e->getMessage());
return $this->render('profile/edit.html.twig', ['formProfile' => $formProfile]);
}

View File

@@ -9,7 +9,11 @@ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class UserFixtures extends Fixture
{
public function load(ObjectManager $manager, UserPasswordHasherInterface $userPasswordHasher): void
private UserPasswordHasherInterface $userPasswordHasher;
public function __construct(UserPasswordHasherInterface $userPasswordHasher){
$this->userPasswordHasher = $userPasswordHasher;
}
public function load(ObjectManager $manager): void
{
$olivier = new Participant();
$olivier->setPrenom('Olivier');
@@ -20,7 +24,7 @@ class UserFixtures extends Fixture
$olivier->setRoles(['ROLE_USER', 'ROLE_ADMIN']);
$olivier->setAdministrateur(true);
$olivier->setActif(false);
$olivier->setPassword($userPasswordHasher->hashPassword($olivier, 'test-44'));
$olivier->setPassword($this->userPasswordHasher->hashPassword($olivier, 'test-44'));
$manager->persist($olivier);
$johan = new Participant();
@@ -32,7 +36,7 @@ class UserFixtures extends Fixture
$johan->setRoles(['ROLE_USER', 'ROLE_ADMIN']);
$johan->setAdministrateur(true);
$johan->setActif(false);
$johan->setPassword($userPasswordHasher->hashPassword($johan, 'test-44'));
$johan->setPassword($this->userPasswordHasher->hashPassword($johan, 'test-44'));
$manager->persist($johan);
$marvin = new Participant();
@@ -44,7 +48,7 @@ class UserFixtures extends Fixture
$marvin->setRoles(['ROLE_USER', 'ROLE_ADMIN']);
$marvin->setAdministrateur(true);
$marvin->setActif(false);
$marvin->setPassword($userPasswordHasher->hashPassword($marvin, 'test-44'));
$marvin->setPassword($this->userPasswordHasher->hashPassword($marvin, 'test-44'));
$manager->persist($marvin);
$manager->flush();

View File

@@ -0,0 +1,154 @@
<?php
namespace App\Form;
use App\Entity\Participant;
use App\Entity\Site;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class ProfileFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('nom', TextType::class, [
'label' => 'Nom',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg text-center focus:outline-none focus:border-blue-500',
'placeholder' => 'Nom',
],
])
->add('prenom', TextType::class, [
'label' => 'Prénom',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg text-center focus:outline-none focus:border-blue-500',
'placeholder' => 'Prénom',
],
])
->add('pseudo', TextType::class, [
'label' => 'Pseudo',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg text-center focus:outline-none focus:border-blue-500',
'placeholder' => 'Pseudo',
],
])
->add('email', EmailType::class, [
'label' => 'Email',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg text-center focus:outline-none focus:border-blue-500',
'placeholder' => 'Adresse e-mail',
],
'constraints' => [
new NotBlank([
'message' => 'Please enter an email address',
]),
],
])
->add('telephone', TextType::class, [
'label' => 'Numéro de téléphone',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'class' => 'w-full text-center mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
'placeholder' => 'Numéro de téléphone',
],
])
->add('newPassword', PasswordType::class, [
'mapped' => false,
'label' => 'Nouveau mot de passe',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'autocomplete' => 'new-password',
'class' => 'w-full text-center mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
'placeholder' => 'Nouveau mot de passe',
],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
'max' => 4096,
]),
],
])
->add('confirmPassword', PasswordType::class, [
'mapped' => false,
'label' => 'Confirmer mot de passe',
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'attr' => [
'autocomplete' => 'new-password',
'class' => 'w-full text-center mb-4 px-4 py-2 border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
'placeholder' => 'Confirmer mot de passe',
],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
'max' => 4096,
]),
],
])
->add('image', FileType::class, [
'label' => 'Image',
'mapped' => false,
'required' => false,
'attr' => [
'class' => 'w-full mb-4 px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:border-blue-500',
],
'label_attr' => ['class' => 'text-gray-700 font-bold'],
'constraints' => [
new File([
'maxSize' => '1024k',
'mimeTypes' => [
'image/png',
'image/jpeg',
],
'mimeTypesMessage' => 'Please upload a valid image',
])
],
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$profile = $event->getData();
if ($profile && $profile->getFileName()) {
$form = $event->getForm();
$form->add('deleteImage', CheckboxType::class, [
'required' => false,
'mapped' => false,
'label' => 'Supprimer l\'image',
'attr' => [
'class' => 'w-4 h-4 mb-4 border-gray-300 rounded mx-2',
], 'label_attr' => ['class' => 'text-gray-700 font-bold px-4']
]);
}
})
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Participant::class,
]);
}
}