src/Controller/RegistrationController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\UserImage;
  5. use App\Form\RegistrationType;
  6. use App\Form\RecruiterFormType;
  7. use App\Repository\UserRepository;
  8. use App\Service\Mailer;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. use Symfony\Component\String\Slugger\SluggerInterface;
  16. use Symfony\Component\HttpFoundation\File\File;
  17. use Symfony\Component\Validator\Validator\ValidatorInterface;
  18. class RegistrationController extends AbstractController
  19. {
  20.     /** 
  21.      * @var UserPasswordEncoderInterface
  22.      */
  23.     private $passwordEncoder;
  24.     /**
  25.      * @var Mailer
  26.      */
  27.     private $mailer;
  28.     /**
  29.      * @var UserRepository
  30.      */
  31.     private $userRepository;
  32.     public function __construct(UserPasswordEncoderInterface $passwordEncoderMailer $mailerUserRepository $userRepository)
  33.     {
  34.         $this->passwordEncoder $passwordEncoder;
  35.         $this->mailer $mailer;
  36.         $this->userRepository $userRepository;
  37.     }
  38.     /**
  39.      * @Route("/inscription", name="register")
  40.      * @param Request $request
  41.      * @return Response
  42.      */
  43.     public function register(Request $requestSluggerInterface $sluggerValidatorInterface $validator): Response
  44.     {
  45.         $userimage = new UserImage();
  46.         $user = new User();
  47.         $form $this->createForm(RegistrationType::class, $user);
  48.         $form->handleRequest($request);
  49.         $errors $validator->validate($user);
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             $user->setPassword(
  52.                 $this->passwordEncoder->encodePassword($user$form->get("password")->getData())
  53.             );
  54.             /** @var UploadedFile $cv */
  55.             $cvFile $form->get('cv')->getData();
  56.             // this condition is needed because the 'brochure' field is not required
  57.             // so the PDF file must be processed only when a file is uploaded
  58.             if ($cvFile) {
  59.                 $originalFilename pathinfo($cvFile->getClientOriginalName(), PATHINFO_FILENAME);
  60.                 // this is needed to safely include the file name as part of the URL
  61.                 $safeFilename $slugger->slug($originalFilename);
  62.                 $newFilename $safeFilename '-' uniqid() . '.' $cvFile->guessExtension();
  63.                 // Move the file to the directory where brochures are stored
  64.                 try {
  65.                     $cvFile->move(
  66.                         $this->getParameter('cvs_directory'),
  67.                         $newFilename
  68.                     );
  69.                 } catch (FileException $e) {
  70.                     // ... handle exception if something happens during file upload
  71.                 }
  72.                 // updates the 'cv' property to store the PDF file name
  73.                 // instead of its contents
  74.                 $user->setCv($newFilename);
  75.             }
  76.             $user->setToken($this->generateToken());
  77.             $user->setRoles(['ROLE_EMPLOYEE']);
  78.             $em $this->getDoctrine()->getManager();
  79.             $em->persist($user);
  80.             $em->flush();
  81.             $uploadedFile $form->get('userImage')->getData();
  82.             if($uploadedFile) {
  83.                 $destination $this->getParameter('kernel.project_dir') . '/public/uploads/images_users/imageprofil';
  84.                 $originalFilename pathinfo($uploadedFile->getClientOriginalName(), PATHINFO_FILENAME);
  85.                 $newFilename time() . $originalFilename uniqid() . '.' $uploadedFile->guessExtension();
  86.                 $uploadedFile->move(
  87.                     $destination,
  88.                     $newFilename
  89.                 );
  90.                 $userimage $em->getRepository(UserImage::class)->findOneByUserImages($user);
  91.                 if (!$userimage) {
  92.                     $userimage = new UserImage();
  93.                 }
  94.                 $userimage->setImageName($newFilename);
  95.                 $userimage->setUpdatedAt(new \DateTime());
  96.                 $em->persist($userimage);
  97.                 $em->flush();
  98.                 $user->setUserImage($userimage);
  99.                 $em->persist($user);
  100.                 $em->flush();
  101.             }
  102.             $this->mailer->sendEmail($user->getEmail(), $user->getToken());
  103.             $this->addFlash("success-registration""Inscription rĂ©ussie !");
  104.             return $this->redirectToRoute("msgverif", ['id' => $user->getId()]);
  105.         }
  106.         return $this->render('registration/register1.html.twig', [
  107.             'form' => $form->createView(),
  108.             'controller_name' => 'Inscrivez-Vous',
  109.             'errors' => $errors
  110.         ]);
  111.     }
  112.     /**
  113.      * @Route("/confirmer-mon-compte/{token}", name="confirm_account")
  114.      * @param string $token
  115.      */
  116.     public function confirmAccount(string $token)
  117.     {
  118.         $user $this->userRepository->findOneBy(["token" => $token]);
  119.         if ($user) {
  120.             $user->setToken(null);
  121.             $user->setEnabled(true);
  122.             $em $this->getDoctrine()->getManager();
  123.             $em->persist($user);
  124.             $em->flush();
  125.             $this->addFlash("success-activation""Compte actif !");
  126.             return $this->redirectToRoute("app_login");
  127.         } else {
  128.             $this->addFlash("error""Ce compte n'exsite pas !");
  129.             return $this->redirectToRoute('register');
  130.         }
  131.     }
  132.     /**
  133.      * @return string
  134.      * @throws \Exception
  135.      */
  136.     private function generateToken()
  137.     {
  138.         return rtrim(strtr(base64_encode(random_bytes(32)), '+/''-_'), '=');
  139.     }
  140.     /**
  141.      * @Route("/inscription/recruiter", name="recruiter_register")
  142.      * @param Request $request
  143.      * @return Response
  144.      */
  145.     public function registerRecruiter(Request $requestSluggerInterface $sluggerValidatorInterface $validator): Response
  146.     {
  147.         $userimage = new UserImage();
  148.         $user = new User();
  149.         $form $this->createForm(RecruiterFormType::class, $user);
  150.         $form->handleRequest($request);
  151.         $errors $validator->validate($user);
  152.         if ($form->isSubmitted() && $form->isValid()) {
  153.             $user->setPassword(
  154.                 $this->passwordEncoder->encodePassword($user$form->get("password")->getData())
  155.             );
  156.             $roles = ['ROLE_RECRUITER'];
  157.             /** @var UploadedFile $cv */
  158.             $logoFile $form->get('logo')->getData();
  159.             // this condition is needed because the 'brochure' field is not required
  160.             // so the PDF file must be processed only when a file is uploaded
  161.             if ($logoFile) {
  162.                 $originalFilename pathinfo($logoFile->getClientOriginalName(), PATHINFO_FILENAME);
  163.                 // this is needed to safely include the file name as part of the URL
  164.                 $safeFilename $slugger->slug($originalFilename);
  165.                 $newFilename $safeFilename '-' uniqid() . '.' $logoFile->guessExtension();
  166.                 // Move the file to the directory where brochures are stored
  167.                 try {
  168.                     $logoFile->move(
  169.                         $this->getParameter('logos_directory'),
  170.                         $newFilename
  171.                     );
  172.                 } catch (FileException $e) {
  173.                     // ... handle exception if something happens during file upload
  174.                 }
  175.                 // updates the 'cv' property to store the PDF file name
  176.                 // instead of its contents
  177.                 $user->setLogo($newFilename);
  178.             }
  179.             $user->setRoles($roles);
  180.             $user->setToken($this->generateToken());
  181.             $em $this->getDoctrine()->getManager();
  182.             $em->persist($user);
  183.             $em->flush();
  184.             $this->mailer->sendEmail($user->getEmail(), $user->getToken());
  185.             $this->addFlash("success-registration""Inscription rĂ©ussie !");
  186.             return $this->redirectToRoute("msgverif", ['id' => $user->getId()]);
  187.         }
  188.         return $this->render('registration/register_rec.html.twig', [
  189.             'form' => $form->createView(),
  190.             'errors' => $errors,
  191.             'controller_name' => 'Inscrivez-Vous'
  192.         ]);
  193.     }
  194.     /**
  195.      * @Route("/download/{id}/{cv}",name="pdf_download")
  196.      */
  197.     public function downloadAction($cv$id null)
  198.     {
  199.         $cvv $this->userRepository->find($id);
  200.         // dd($cvv);
  201.         $downloadedFile $cvv->getCv();
  202.         // dd($downloadedFile);
  203.         $response = new Response();
  204.         // $response = new Response();
  205.         $response->headers->set('Content-type''application/octet-stream');
  206.         $response->headers->set('Content-Disposition'sprintf('attachment; filename="%s"'$downloadedFile));
  207.         $response->setContent($downloadedFile);
  208.         $response->setStatusCode(200);
  209.         $response->headers->set('Content-Transfer-Encoding''binary');
  210.         $response->headers->set('Pragma''no-cache');
  211.         $response->headers->set('Expires''0');
  212.         return $response;
  213.     }
  214. }