src/Controller/SiteController.php line 34

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Contribution;
  4. use App\Entity\ContributionType;
  5. use App\Form\ChangePasswordFormType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Exception;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  14. use Symfony\Component\Mailer\Mailer;
  15. use Symfony\Component\Mailer\MailerInterface;
  16. use Symfony\Component\Mime\Address;
  17. use Symfony\Component\Mime\Email;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. use Symfony\Component\PasswordHasher\PasswordHasherInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  22. class SiteController extends AbstractController
  23. {
  24.     public function __construct(
  25.         private EntityManagerInterface $em,
  26.         private UserPasswordHasherInterface $passwordHasher,
  27.     )
  28.     {
  29.     }
  30.     public function index(): Response
  31.     {
  32.         return $this->redirectToRoute('admin');
  33.         /*return $this->render('site/index.html.twig', [
  34.             'controller_name' => 'SiteController',
  35.         ]);*/
  36.     }
  37.     public function showProfile(): Response
  38.     {
  39.         return $this->render('site/profile.html.twig', [
  40.             'user' => $this->getUser(),
  41.         ]);
  42.     }
  43.     public function modifyPassword(Request $requestMailerInterface $mailer): Response
  44.     {
  45.         $user $this->getUser();
  46.         $form $this->createForm(ChangePasswordFormType::class);
  47.         $form->handleRequest($request);
  48.         if ($form->isSubmitted() && $form->isValid()) {
  49.             // Encode(hash) the plain password, and set it.
  50.             $encodedPassword $this->passwordHasher->hashPassword(
  51.                 $user,
  52.                 $form->get('plainPassword')->getData()
  53.             );
  54.             $user->setPassword($encodedPassword);
  55.             $this->em->flush();
  56.             $this->addFlash('success''Le mot de passe à bien été réinitialisé');
  57.             return $this->redirectToRoute('app_profile');
  58.         }
  59.         return $this->render('reset_password/reset.html.twig', [
  60.             'user' => $this->getUser(),
  61.             'resetForm' => $form->createView(),
  62.         ]);
  63.     }
  64.     public function getContributionTypes(Request $request): Response
  65.     {
  66.         if ($request->isXmlHttpRequest())
  67.         {
  68.             try {
  69.                 $contributionIdSelected $request->request->get('contribution');
  70.                 $contribution $this->em->getRepository(Contribution::class)->find(intval($contributionIdSelected));
  71.                 $contributionTypes $this->em->getRepository(ContributionType::class)->findBy(['contribution' => $contribution]);
  72.                 $contributionTypesArray = [];
  73.                 /* @var ContributionType $contributionType  */
  74.                 foreach ($contributionTypes as $contributionType) {
  75.                     $contributionTypesArray[] = [
  76.                         'id' => $contributionType->getId(),
  77.                         'rate' => $contributionType->getRate(),
  78.                         'year' => $contribution->getYear(),
  79.                         'name' => $contributionType->getName(),
  80.                     ];
  81.                 }
  82.                 $data = [
  83.                     'success' => true,
  84.                     'contributionTypes' => $contributionTypesArray
  85.                 ];
  86.             } catch (Exception $e) {
  87.                 $data = [
  88.                     'success' => false,
  89.                     'message' => $e,
  90.                 ];
  91.             }
  92.         } else {
  93.             die();
  94.         }
  95.         return new JsonResponse($data);
  96.     }
  97. }