Merge branch 'Olivier'
This commit is contained in:
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user