src/Controller/RegistrationController.php line 47

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use App\Repository\SettingsRepository;
  13. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  14. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  15. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  16. use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  17. use Symfony\Component\Serializer\Serializer;
  18. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  19. use App\Repository\UserRepository;
  20. use Doctrine\DBAL\Types\TimeImmutableType;
  21. use Symfony\Contracts\HttpClient\HttpClientInterface;
  22. use App\Repository\ProjectRepository;
  23. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  24. use Symfony\Component\Mime\Address;
  25. use Symfony\Component\Mailer\MailerInterface;
  26. use Symfony\Bundle\SecurityBundle\Security;
  27. use App\Repository\OrganizationRepository;
  28. use App\Repository\ProvinceRepository;
  29. class RegistrationController extends AbstractController
  30. {
  31.     private $settings;
  32.     private $entityManager;
  33.     private $projectgRepo;
  34.     private $organizationRepo;
  35.     private $provinceRepo;
  36.     private $userRepo;
  37.     private $encoders;
  38.     private $normalizers;
  39.     private $serializer;
  40.     private $client;
  41.     private $projects;
  42.     private $mailer;
  43.     
  44.     public function __construct(
  45.         SettingsRepository $settingsRepository,
  46.         EntityManagerInterface $entityManager,
  47.         UserRepository $userRepository,
  48.         ProjectRepository $projectRepository,
  49.         OrganizationRepository $organizationRepository,
  50.         ProvinceRepository $provinceRepository,
  51.         MailerInterface $mailer)
  52.     {
  53.         $this->settings = $settingsRepository->findOneById(1);
  54.         $this->entityManager = $entityManager;
  55.         $this->userRepo = $userRepository;
  56.         $this->projectgRepo = $projectRepository;
  57.         $this->encoders = [new XmlEncoder(), new JsonEncoder()];
  58.         $this->normalizers = [new DateTimeNormalizer(), new ObjectNormalizer()];
  59.         $this->serializer = new Serializer($this->normalizers, $this->encoders);
  60.         $this->organizationRepo = $organizationRepository;
  61.         $this->provinceRepo = $provinceRepository;
  62.         //         $this->client = $client;
  63.         $this->projects = $projectRepository->findAll();
  64.         $this->mailer = $mailer;
  65.     }
  66.     
  67.     #[Route('/organizations-list')]
  68.     public function organizationsList(Request $request)
  69.     {
  70.         $provinceId = $request->query->get('provinceId');
  71.         $province = $this->provinceRepo->findOneById($provinceId);
  72.         $organizations = $this->organizationRepo->findByProvince($province);
  73.         $jsonOrganizations = $this->serializer->serialize(
  74.             $organizations, 
  75.             'json', 
  76.             [AbstractNormalizer::IGNORED_ATTRIBUTES => [
  77.                 'author', 
  78.                 'users',
  79.                 'videoAuthorOrganizations',
  80.                 'organizations', 
  81.                 'province', 
  82.                 'video'
  83.             ]]
  84.         );
  85.         return new Response(
  86.             json_encode([
  87.                 'organizations' => $jsonOrganizations,
  88.             ]),
  89.             Response::HTTP_OK,
  90.             ['content-type' => 'application/json']
  91.         );
  92.     }
  93.     
  94.     #[Route('/register', name: 'app_register')]
  95.     public function register(
  96.         Request $request, 
  97.         UserPasswordHasherInterface $userPasswordHasher,
  98.         Security $security): Response
  99.     {
  100.         $user = new User();
  101.         $form = $this->createForm(RegistrationFormType::class, $user);
  102.         $form->handleRequest($request);
  103.         if ($form->isSubmitted() && $form->isValid()) {
  104.             // encode the plain password
  105.             $user->setPassword(
  106.                 $userPasswordHasher->hashPassword(
  107.                     $user,
  108.                     $form->get('plainPassword')->getData()
  109.                 )
  110.             )
  111.             ->setCreatedAt(new \DateTimeImmutable('now'))
  112.             ->setLastAccessAt(new \DateTimeImmutable('now'));
  113.             $this->entityManager->persist($user);
  114.             $this->entityManager->flush();
  115.             // do anything else you need here, like send an email
  116.             $email = (new TemplatedEmail())
  117.             ->from(new Address($this->settings->getContactEmail(), strip_tags($this->settings->getTitle())))
  118.             ->to(new Address($user->getEmail()))
  119.             ->subject('Bienvenid@ a '.strip_tags($this->settings->getTitle()))
  120.             ->htmlTemplate('emails/signup.html.twig')
  121.             ->context([
  122.                 'user' => $user,
  123.                 'settings' => $this->settings,
  124.             ]);
  125.             
  126.             $this->mailer->send($email);
  127.             
  128.             $security->login($user, 'form_login');
  129.             
  130.             return $this->redirectToRoute('app_home');
  131.         }
  132.         $specialtyChoices = [
  133.             'Nefrología' => 'Nefrología',
  134.             'Hepatología' => 'Hepatología',
  135.             'Cirugía Hepatobiliar' => 'Cirugía Hepatobiliar',
  136.             'Inmunología' => 'Inmunología',
  137.             'Farmacia Hospitalaria' => 'Farmacia Hospitalaria',
  138.             'Enfermería' => 'Enfermería',
  139.             'UCI' => 'UCI',
  140.             //             'Otro' => 'Otro',
  141.         ];
  142.         
  143.         return $this->render('registration/register.html.twig', [
  144.             'registrationForm' => $form->createView(),
  145.             'settings' => $this->settings,
  146.             'specialtyChoices' => $specialtyChoices,
  147.         ]);
  148.     }
  149. }