fileUploader + edit profile

This commit is contained in:
Olivier PARPAILLON
2024-11-20 10:53:29 +01:00
parent caa78c634b
commit 7091bd4094
10 changed files with 259 additions and 30 deletions

View File

@@ -2,6 +2,8 @@
namespace App\Controller;
use App\Entity\Participant;
use App\Service\FileUploader;
use App\Form\RegistrationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@@ -11,26 +13,57 @@ use App\Repository\ParticipantRepository;
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 $participantRepository): Response
public function viewProfile(string $uuid, ParticipantRepository $profileRepo): Response
{
$currentProfile = $participantRepository->findOneBy(['idParticipant' => $uuid]);
$currentProfile = $profileRepo->findOneBy(['idParticipant' => $uuid]);
return $this->render('profile/view.html.twig', [
'profile' => $currentProfile,
]);
}
#[Route('/profile/edit/{uuid}', name: 'profile_edit', methods: ['GET', 'POST'])]
public function editProfile(string $uuid, ParticipantRepository $participantRepository, Request $request): Response
public function editProfile(string $uuid, Request $request): Response
{
$currentProfile = $participantRepository->findOneBy(['idParticipant' => $uuid]);
$form = $this->createForm(RegistrationFormType::class, $currentProfile);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$profile = $this->profileRepo->findOneBy(['idParticipant' => $uuid]);
if (!$profile === $this->getUser()) {
$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->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('');
}
}
$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', [
'formProfile' => $form,
]);
} catch(\Exception $e) {
$formProfile = $this->createForm(RegistrationFormType::class, $profile);
$this->addFlash('error', $e->getMessage());
return $this->render('profile/edit.html.twig', ['formProfile' => $formProfile]);
}
return $this->render('profile/view.html.twig', [
'profile' => $currentProfile,
]);
}
}