ProfilController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Profile;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use App\Form\UserEditProfilType;
  6. use App\Form\ProfileType;
  7. use App\Repository\ProfileRepository;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class ProfilController extends AbstractController
  14. {
  15. #[Route("/profil", name: "user_profil")]
  16. public function monProfilAction(Request $request, EntityManagerInterface $em): Response
  17. {
  18. $user = $this->getUser();
  19. $form = $this->createForm(UserEditProfilType::class , $user);
  20. $form->handleRequest($request);
  21. if ($form->isSubmitted() && $form->isValid())
  22. {
  23. $user = $form->getData();
  24. $em->flush();
  25. $this->addFlash('success', "Votre profil a été modifié");
  26. }
  27. return $this->render('profil/monprofil.html.twig', array (
  28. 'form' => $form
  29. ));
  30. }
  31. #[Route("/preferences", name: "user_preferences")]
  32. public function mesPreferencesAction(Request $request, EntityManagerInterface $em, ProfileRepository $profileRepo): Response
  33. {
  34. $profile = $profileRepo->findByUser($this->getUser());
  35. $form = $this->createForm(ProfileType::class , $profile);
  36. $form->handleRequest($request);
  37. if ($form->isSubmitted() && $form->isValid())
  38. {
  39. $profile = $form->getData();
  40. $em->flush();
  41. $this->addFlash('success', "Les préférences ont été modifiées");
  42. }
  43. return $this->render('profil/mespreferences.html.twig', [
  44. 'form' => $form
  45. ]);
  46. }
  47. #[Route("/changeview", name: "user_changeview")]
  48. public function changeView(EntityManagerInterface $em): JsonResponse
  49. {
  50. /** @var \App\Entity\User $user */
  51. $user = $this->getUser();
  52. $profile = $user->getProfile();
  53. if ($profile->getView() == Profile::$VIEW['liste']) {
  54. $profile->setView(Profile::$VIEW['vignette']);
  55. } else {
  56. $profile->setView(Profile::$VIEW['liste']);
  57. }
  58. $em->flush();
  59. return $this->json($profile->getView());
  60. }
  61. #[Route("/getview", name: "user_getview")]
  62. public function getView(): JsonResponse
  63. {
  64. /** @var \App\Entity\User $user */
  65. $user = $this->getUser();
  66. return $this->json($user->getProfile()->getView());
  67. }
  68. }