src/Controller/ThemesWebsite/Whileresume/GestionCandidates/GenerateController.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ThemesWebsite\Whileresume\GestionCandidates;
  3. use App\Entity\Core\Notifications;
  4. use App\Entity\Cvs\Candidates;
  5. use App\Form\Cvs\CandidatesSettingsForm;
  6. use App\Form\Cvs\CandidatesStep0Form;
  7. use App\Form\Cvs\CandidatesStep1Form;
  8. use App\Services\Core\RequestData;
  9. use App\Services\Core\Users;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\HttpFoundation\Cookie;
  19. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  24. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  25. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  26. use Vich\UploaderBundle\Storage\StorageInterface;
  27. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  28. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  29. /**
  30.  * Générateur de CV
  31.  */
  32. class GenerateController extends AbstractController
  33. {
  34.     private $rd;
  35.     private $em;
  36.     private $ms;
  37.     private $storage;
  38.     public function __construct(RequestData            $rd,
  39.                                 EntityManagerInterface $em,
  40.                                 \App\Services\Mails    $ms,
  41.                                 StorageInterface       $storage
  42.     )
  43.     {
  44.         $this->rd $rd;
  45.         $this->em $em;
  46.         $this->ms $ms;
  47.         $this->storage $storage;
  48.     }
  49.     /**
  50.      * Visualisation du PDF.
  51.      * @param Request $request
  52.      * @return BinaryFileResponse
  53.      * @throws \Exception
  54.      */
  55.     public function previewPdf(Request $request)
  56.     {
  57.         $session $request->getSession();
  58.         $session->set("navbar-section""dashboard");
  59.         $session->set("sidebar-section""dashboard");
  60.         $user $this->getUser();
  61.         if ($user == null) {
  62.             throw new \Exception("Aucun utilisateur");
  63.         }
  64.         $candidate $user->getCandidate();
  65.         $filePath $this->storage->resolvePath($candidate'imageFile');
  66.         if (!$filePath || !file_exists($filePath)) {
  67.             throw $this->createNotFoundException('Fichier PDF introuvable sur le serveur');
  68.         }
  69.         $response = new BinaryFileResponse($filePath);
  70.         // INLINE pour affichage dans le navigateur/iframe
  71.         $response->setContentDisposition(
  72.             ResponseHeaderBag::DISPOSITION_INLINE
  73.         );
  74.         // Headers nécessaires
  75.         $response->headers->set('Content-Type''application/pdf');
  76.         $response->headers->set('X-Frame-Options''SAMEORIGIN');
  77.         return $response;
  78.     }
  79.     /**
  80.      * Première étape.
  81.      * @param Request $request
  82.      * @return Response
  83.      * @throws \Exception
  84.      */
  85.     public function first(Request $requestUsers $us): Response
  86.     {
  87.         $session $request->getSession();
  88.         $session->set("navbar-section""dashboard");
  89.         $session->set("sidebar-section""generate");
  90.         $locale $request->getLocale();
  91.         $user $this->getUser();
  92.         if ($user == null) {
  93.             throw new \Exception("Aucun utilisateur");
  94.         }
  95.         $candidate $user->getCandidate();
  96.         if ($candidate == null) {
  97.             $candidate = new Candidates();
  98.             $candidate->setSalary(true);
  99.             $candidate->setFrench(true);
  100.             $candidate->setEnglish(true);
  101.             $candidate->setEmail($user->getEmail());
  102.             $candidate->setFirst(true);
  103.             $this->em->persist($candidate);
  104.             $this->em->flush();
  105.             $user->setCandidate($candidate);
  106.             $this->em->persist($user);
  107.             $this->em->flush();
  108.         }
  109.         if(empty($candidate->getSlug())) {
  110.             $slug $us->getUniqueCvSlug(10);
  111.             $candidate->setSlug($slug);
  112.         }
  113.         if(empty($candidate->getSlugAnonyme())) {
  114.             $slug $us->getUniqueCvAnonymeSlug(10);
  115.             $candidate->setSlugAnonyme($slug);
  116.         }
  117.         if(empty($candidate->getInvitationCode())) {
  118.             $code $us->getUniqueCvInvitationCode(15);
  119.             $candidate->setInvitationCode($code);
  120.         }
  121.         if(empty($candidate->getInvitationCodeAnonyme())) {
  122.             $code $us->getUniqueCvInvitationCodeAnonyme(15);
  123.             $candidate->setInvitationcodeAnonyme($code);
  124.         }
  125.         $form $this->createForm(CandidatesStep0Form::class, $candidate);
  126.         $form->handleRequest($request);
  127.         if ($form->isSubmitted() && $form->isValid()) {
  128.             $data $request->request->all();
  129.             $data $data['candidates_step0_form'];
  130.             $user->setName($data['name']);
  131.             $user->setLastname($data['lastname']);
  132.             $this->em->persist($user);
  133.             $this->em->flush();
  134.             $newFileUploaded $candidate->getImageFile() !== null;
  135.             if ($newFileUploaded) {
  136.                 $pathPython $_ENV["PYTHON_PATH"];
  137.                 $pathPythonScript $_ENV["PYTHON_SCRIPT_PATH"];
  138.                 $candidate->setAnalyse(false);
  139.                 $this->em->persist($candidate);
  140.                 $this->em->flush();
  141.                 $cvID $candidate->getId();
  142.                 $command escapeshellcmd($pathPython " " $pathPythonScript " " $cvID);
  143.                 shell_exec($command " > /dev/null 2>/dev/null &");
  144.                 if ($locale != "en") {
  145.                     return $this->redirectToRoute('locale_cvs_gestion_candidates_generate_analyse', ['_locale' => $locale]);
  146.                 }
  147.                 return $this->redirectToRoute('cvs_gestion_candidates_generate_analyse');
  148.             } else {
  149.                 if ($locale != "en") {
  150.                     return $this->redirectToRoute('locale_cvs_gestion_candidates_generate_about', ['_locale' => $locale]);
  151.                 }
  152.                 return $this->redirectToRoute('cvs_gestion_candidates_generate_about');
  153.             }
  154.         }
  155.         return $this->render('application/whileresume/gestion/candidates/generate/first.html.twig', [
  156.             'form' => $form->createView(),
  157.             'candidate' => $candidate,
  158.         ]);
  159.     }
  160.     /**
  161.      * Analyse en cours.
  162.      * @param Request $request
  163.      * @return Response
  164.      * @throws \Exception
  165.      */
  166.     public function analyse(Request $request): Response
  167.     {
  168.         $session $request->getSession();
  169.         $session->set("navbar-section""dashboard");
  170.         $session->set("sidebar-section""generate");
  171.         $locale $request->getLocale();
  172.         $user $this->getUser();
  173.         if ($user == null) {
  174.             throw new \Exception("Aucun utilisateur");
  175.         }
  176.         $candidate $user->getCandidate();
  177.         return $this->render('application/whileresume/gestion/candidates/generate/analyse.html.twig', [
  178.             'candidate' => $candidate
  179.         ]);
  180.     }
  181.     /**
  182.      * Retourne si c'est en cours d'analyse.
  183.      * @param Request $request
  184.      * @return Response
  185.      */
  186.     public function analyseWaiting(Request $request): Response
  187.     {
  188.         $session $request->getSession();
  189.         $user $this->getUser();
  190.         if ($user == null) {
  191.             return new JsonResponse(false);
  192.         }
  193.         $candidate $user->getCandidate();
  194.         if ($candidate->getAnalyse() == null) {
  195.             return new JsonResponse(false);
  196.         }
  197.         if ($candidate->getAnalyse() == true) {
  198.             return new JsonResponse(true);
  199.         }
  200.         return new JsonResponse(false);
  201.     }
  202.     /**
  203.      * Informations sur le CV
  204.      * @param Request $request
  205.      * @return Response
  206.      * @throws \Exception
  207.      */
  208.     public function about(Request $request): Response
  209.     {
  210.         $session $request->getSession();
  211.         $session->set("navbar-section""dashboard");
  212.         $session->set("sidebar-section""generate");
  213.         $locale $request->getLocale();
  214.         $user $this->getUser();
  215.         if ($user == null) {
  216.             throw new \Exception("Aucun utilisateur");
  217.         }
  218.         $candidate $user->getCandidate();
  219.         if($candidate->getDefaultLocale() != "fr" and $candidate->getDefaultLocale() != "en") {
  220.             $candidate->setDefault(true);
  221.         } else {
  222.             $candidate->setDefault(false);
  223.         }
  224.         if($candidate->getDefaultLocale() == "fr") {
  225.             $candidate->setFrench(true);
  226.         }
  227.         if($candidate->getDefaultLocale() == "en") {
  228.             $candidate->setEnglish(true);
  229.         }
  230.         $this->em->persist($candidate);
  231.         $this->em->flush();
  232.         $form $this->createForm(CandidatesStep1Form::class, $candidate);
  233.         if($candidate->getDefaultLocale() == "fr") {
  234.             $form->add('french'HiddenType::class);
  235.         }
  236.         if($candidate->getDefaultLocale() == "fr") {
  237.             $form->add('french'HiddenType::class);
  238.         } else {
  239.             $form->add('french'CheckboxType::class, [
  240.                 'required' => false,
  241.                 'error_bubbling' => true,
  242.                 'label' => 'FR',
  243.                 'attr' => ['class' => ''],
  244.                 'label_attr' => ['class' => 'form-label']
  245.             ]);
  246.         }
  247.         if($candidate->getDefaultLocale() == "en") {
  248.             $form->add('english'HiddenType::class);
  249.         } else {
  250.             $form->add('english'CheckboxType::class, [
  251.                 'required' => false,
  252.                 'error_bubbling' => true,
  253.                 'label' => 'EN',
  254.                 'attr' => ['class' => ''],
  255.                 'label_attr' => ['class' => 'form-label']
  256.             ]);
  257.         }
  258.         $form->handleRequest($request);
  259.         if ($form->isSubmitted() && $form->isValid()) {
  260.             $data $request->request->all();
  261.             $candidate->setPointX(null);
  262.             $candidate->setPointY(null);
  263.             $this->em->persist($candidate);
  264.             $this->em->flush();
  265.             $session->getFlashBag()->add('success''Mise à jour des informations');
  266.             if ($data['submit'] == "0") {
  267.                 if ($locale != "en") {
  268.                     return $this->redirectToRoute('locale_cvs_gestion_candidates_generate_first', ['_locale' => $locale]);
  269.                 }
  270.                 return $this->redirectToRoute('cvs_gestion_candidates_generate_first');
  271.             }
  272.             if ($locale != "en") {
  273.                 return $this->redirectToRoute('locale_cvs_gestion_candidates_generate_publish', ['_locale' => $locale]);
  274.             }
  275.             return $this->redirectToRoute('cvs_gestion_candidates_generate_publish');
  276.         }
  277.         return $this->render('application/whileresume/gestion/candidates/generate/about.html.twig', [
  278.             'form' => $form->createView(),
  279.             'candidate' => $candidate
  280.         ]);
  281.     }
  282.     /**
  283.      * Publier le CV
  284.      * @param Request $request
  285.      * @return Response
  286.      * @throws \Exception
  287.      */
  288.     public function publish(Request $request): Response
  289.     {
  290.         $session $request->getSession();
  291.         $session->set("navbar-section""dashboard");
  292.         $session->set("sidebar-section""generate");
  293.         $user $this->getUser();
  294.         $locale $request->getLocale();
  295.         if ($user == null) {
  296.             throw new \Exception("Aucun utilisateur");
  297.         }
  298.         $candidate $user->getCandidate();
  299.         if ($candidate == null) {
  300.             throw new \Exception("Aucun CV");
  301.         }
  302.         $form $this->createForm(CandidatesSettingsForm::class, $candidate);
  303.         $form->handleRequest($request);
  304.         if ($form->isSubmitted() && $form->isValid()) {
  305.             $data $request->request->all();
  306.             if ($data['submit'] == "0") {
  307.                 if ($locale != "en") {
  308.                     $session->getFlashBag()->add('success''Mise à jour du CV');
  309.                     return $this->redirectToRoute('locale_cvs_gestion_candidates_generate_about', ['_locale' => $locale]);
  310.                 }
  311.                 $session->getFlashBag()->add('success''Resume updated');
  312.                 return $this->redirectToRoute('cvs_gestion_candidates_generate_about');
  313.             }
  314.             $candidate->setOnline(true);
  315.             $candidate->setAvailability("open");
  316.             $this->em->persist($candidate);
  317.             $this->em->flush();
  318.             $user->setFirst(false);
  319.             $this->em->persist($user);
  320.             $this->em->flush();
  321.             $notification = new Notifications();
  322.             $notification->setViewed(false);
  323.             $notification->setOnlyAdmin(false);
  324.             $notification->setType("default");
  325.             if ($locale == "fr") {
  326.                 $notification->setDescription("Publication de votre CV");
  327.             } else {
  328.                 $notification->setDescription("Resume published");
  329.             }
  330.             $notification->setUser($user);
  331.             $this->em->persist($notification);
  332.             $this->em->flush();
  333.             if ($locale != "en") {
  334.                 $session->getFlashBag()->add('success''CV publié');
  335.                 return $this->redirectToRoute('locale_cvs_gestion_candidates_dashboard', ['_locale' => $locale]);
  336.             }
  337.             $session->getFlashBag()->add('success''Resume published');
  338.             return $this->redirectToRoute('cvs_gestion_candidates_dashboard');
  339.         }
  340.         return $this->render('application/whileresume/gestion/candidates/generate/publish.html.twig', [
  341.             'form' => $form->createView(),
  342.             'candidate' => $candidate
  343.         ]);
  344.     }
  345. }