src/Controller/Frontend/ProfileController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\Campaign;
  4. use App\Entity\Status;
  5. use App\Misc\CaptchaHelper;
  6. use App\Utilities\ServiceUtil;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\Exception\BadRequestException;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use App\Annotation\Log;
  12. use App\Constant\Common;
  13. use App\Entity\Profile;
  14. use App\Service\ProfileFileService;
  15. use GuzzleHttp\Psr7\UploadedFile;
  16. class ProfileController extends BaseFrontendController
  17. {
  18.     /**
  19.      * @Route("/{_locale}/dossier-inscription/step1", name="profile.step1", methods={"GET"})
  20.      */
  21.     public function profileStep1(TranslatorInterface $translator)
  22.     {
  23.         $siteName $translator->trans('siteName');
  24.         $this->addTitle($translator->trans(
  25.             'profile_step1.title',
  26.             [
  27.                 '%siteName%' => $siteName
  28.             ]
  29.         ));
  30.         $this->setDescription($translator->trans(
  31.             'profile_step1.desc',
  32.             [
  33.                 '%siteName%' => $siteName
  34.             ]
  35.         ));
  36.         // get profile logged in
  37.         $data['formData'] = [
  38.             'errors' => [],
  39.             'inputValues' => []
  40.         ];
  41.         if ($this->getUser()) {
  42.             $data['formData']['inputValues'] = $this->currentService->getProfileByUserId($this->getUser()->getId());
  43.         }
  44.         $data array_merge($data$this->stepData());
  45.         if (isset($data['formData']['inputValues']['year'])) {
  46.             $data['year'] = $data['formData']['inputValues']['year'];
  47.         }
  48.         return $this->render('user/profile/profile_step1.html.twig'$data);
  49.     }
  50.     /**
  51.      * @Route("/{_locale}/dossier-inscription/step2", name="profile.step2", methods={"GET"})
  52.      */
  53.     public function profileStep2(TranslatorInterface $translator)
  54.     {
  55.         if (!$this->getUser()) {
  56.             return $this->redirectToRoute('profile.step1');
  57.         }
  58.         $siteName $translator->trans('siteName');
  59.         $this->addTitle($translator->trans(
  60.             'profile_step1.title',
  61.             [
  62.                 '%siteName%' => $siteName
  63.             ]
  64.         ));
  65.         $this->setDescription($translator->trans(
  66.             'profile_step1.desc',
  67.             [
  68.                 '%siteName%' => $siteName
  69.             ]
  70.         ));
  71.         $data['formData'] = [
  72.             'errors' => [],
  73.             'inputValues' => $this->currentService->getProfileByUserId($this->getUser()->getId())
  74.         ];
  75.         $data array_merge($data$this->stepData());
  76.         if (isset($data['formData']['inputValues']['year'])) {
  77.             $data['year'] = $data['formData']['inputValues']['year'];
  78.         }
  79.         return $this->render('user/profile/profile_step2.html.twig'$data);
  80.     }
  81.     /**
  82.      * @Route("/{_locale}/dossier-inscription/step3", name="profile.step3", methods={"GET"})
  83.      */
  84.     public function profileStep3(TranslatorInterface $translator)
  85.     {
  86.         if (!$this->getUser()) {
  87.             return $this->redirectToRoute('profile.step1');
  88.         }
  89.         $siteName $translator->trans('siteName');
  90.         $this->addTitle($translator->trans(
  91.             'profile_step1.title',
  92.             [
  93.                 '%siteName%' => $siteName
  94.             ]
  95.         ));
  96.         $this->setDescription($translator->trans(
  97.             'profile_step1.desc',
  98.             [
  99.                 '%siteName%' => $siteName
  100.             ]
  101.         ));
  102.         $data['formData'] = [
  103.             'errors' => [],
  104.             'inputValues' => $this->currentService->getProfileByUserId($this->getUser()->getId())
  105.         ];
  106.         $data array_merge($data$this->stepData());
  107.         if (isset($data['formData']['inputValues']['year'])) {
  108.             $data['year'] = $data['formData']['inputValues']['year'];
  109.         }
  110.         return $this->render('user/profile/profile_step3.html.twig'$data);
  111.     }
  112.     /**
  113.      * @Route("/{_locale}/dossier-inscription/step4", name="profile.step4", methods={"GET"})
  114.      */
  115.     public function profileStep4(TranslatorInterface $translator)
  116.     {
  117.         if (!$this->getUser()) {
  118.             return $this->redirectToRoute('profile.step1');
  119.         }
  120.         $siteName $translator->trans('siteName');
  121.         $this->addTitle($translator->trans(
  122.             'profile_step1.title',
  123.             [
  124.                 '%siteName%' => $siteName
  125.             ]
  126.         ));
  127.         $this->setDescription($translator->trans(
  128.             'profile_step1.desc',
  129.             [
  130.                 '%siteName%' => $siteName
  131.             ]
  132.         ));
  133.         $data['formData'] = [
  134.             'errors' => [],
  135.             'inputValues' => $this->currentService->getProfileByUserId($this->getUser()->getId())
  136.         ];
  137.         $data array_merge($data$this->stepData());
  138.         if (isset($data['formData']['inputValues']['year'])) {
  139.             $data['year'] = $data['formData']['inputValues']['year'];
  140.         }
  141.         return $this->render('user/profile/profile_step4.html.twig'$data);
  142.     }
  143.     /**
  144.      * @Route("/{_locale}/profile/step1", name="profile.step1_save", methods={"POST", "PUT"})
  145.      * @Log
  146.      */
  147.     public function profileStep1Save(
  148.         Request $request,
  149.         CaptchaHelper $captchaHelper,
  150.         TranslatorInterface $translator
  151.     ) {
  152.         // new profile
  153.         if ($request->getMethod() === 'POST') {
  154.             // Check captcha is valid
  155.             if ($this->getParameter('captcha_enabled') && !$captchaHelper->isCaptchaValid($request)) {
  156.                 return $this->json(ServiceUtil::processFailed([
  157.                     'errorMessage' => $translator->trans('message.captcha_is_not_valid')
  158.                 ]));
  159.             }
  160.             $campaign $this->campaignRepo->findOneBy(['id' => Campaign::APPLICATION]);
  161.             if ($campaign) {
  162.                 $request->request->set('campaign'$campaign->getId());
  163.             }
  164.             if(!$request->get('programs')){
  165.                 $data $this->stepData();
  166.                 $data['formData'] = [
  167.                     'errors' => [],
  168.                     'inputValues' => $request->request->all()
  169.                 ];
  170.                 $data['valid_programs'] = $translator->trans('message.programme_is_not_valid');
  171.                 return $this->render('user/profile/profile_step1.html.twig'$data);
  172.             }
  173.             try {
  174.                 $this->currentService->addProfile($request);
  175.                 return $this->redirectToRoute('profile.step2', ['new_user'=> 1]);
  176.             } catch (BadRequestException $badRequestException) {
  177.                 $data $this->stepData();
  178.                 $data['formData'] = [
  179.                     'errors' => [],
  180.                     'inputValues' => $request->request->all()
  181.                 ];
  182.                 $error $badRequestException->getMessages();
  183.                 if ($error['entityName'] === 'User') {
  184.                     foreach ($error['data'] as $field => $message) {
  185.                         $data['formData']['errors']['user[' $field ']'] = $message;
  186.                     }
  187.                 }
  188.                 $data['validated'] = true;
  189.                 return $this->render('user/profile/profile_step1.html.twig'$data);
  190.             }
  191.         }
  192.         // update step 1  and ?? go to step 2 ?
  193.         $this->userService->updateProfileUser($request->get('user'));
  194.         $this->currentService->updateProfile($request);
  195.         return $this->redirectToRoute('profile.step2');
  196.     }
  197.     /**
  198.      * @Route("/{_locale}/profile/step2", name="profile.step2_save", methods={"PUT"})
  199.      */
  200.     public function profileStep2Save(Request $request)
  201.     {
  202.         $this->currentService->updateProfile($request'App\DTO\Profile\UpdateProfileStep2Input');
  203.         return $this->redirectToRoute('profile.step3');
  204.     }
  205.     /**
  206.      * @Route("/{_locale}/profile/step3", name="profile.step3_save", methods={"PUT"})
  207.      */
  208.     public function profileStep3Save(Request $request)
  209.     {
  210.         $this->currentService->updateProfile($request'App\DTO\Profile\UpdateProfileStep3Input');
  211.         return $this->redirectToRoute('profile.step4');
  212.     }
  213.     /**
  214.      * @Route("/{_locale}/profile/step4", name="profile.step4_save", methods={"PUT"})
  215.      */
  216.     public function profileStep4Save(Request $requestProfileFileService $profileFileService)
  217.     {   
  218.         if($request->files->has('fileIdentity') and $request->files->get('fileIdentity') instanceof UploadedFile) {
  219.             $file $request->files->get('fileIdentity');
  220.             $result $profileFileService->analyzeFile($file);
  221.             /** @var \App\Entity\User $user */
  222.             $user $this->getUser();
  223.             if(!empty($result['name'])){
  224.                  $user->setFirstName($result['name']);
  225.             }
  226.             if(!empty($result['surname'])){
  227.                 $user->setLastName($result['surname']);
  228.             }
  229.             if(!empty($result['date_of_birth'])){
  230.                 $user->setBirthDay(new \DateTime($result['date_of_birth']));
  231.             }
  232.             if(!empty($result['place_of_birth'])){
  233.                 $user->setBirthPlace($result['place_of_birth']);
  234.             }
  235.             if(!empty($result['gender'])){
  236.                 $user->setGender($result['gender'] === 'female' 0);
  237.             }
  238.             if(!empty($result['country_id'])){
  239.                 $user->setNationality($profileFileService->getEntityManager()->getReference('App\Entity\Country'$result['country_id']));
  240.             }
  241.             $this->userRepository->save($user);
  242.             if(!empty($result['document_expiry'])){
  243.                 $profile $this->currentRepo->findOneBy(['user' => $user->getId()]);
  244.                 $profile->setExpireIdentity(new \DateTime($result['document_expiry']));
  245.                 $this->profileRepository->save($profile);
  246.                 $request->request->remove('expireIdentity');
  247.             }
  248.             
  249.         }
  250.         $isSent $request->get('isSent');
  251.         $this->userService->updateProfileUser($request->files->get('user'));
  252.         $request->files->remove('user');
  253.         $profile $this->currentService->updateProfile($request'App\DTO\Profile\UpdateProfileStep4Input'true);
  254.         $param = [];
  255.         if(!$isSent){
  256.             $param = ['new_user'=> 1];
  257.             //send email to admin campus
  258.             if(isset(Common::listEmailAdmission()[$profile->getCampus1()->getId()])){
  259.                 $this->userService->sendCampusEmail($profileCommon::listEmailAdmission()[$profile->getCampus1()->getId()]);
  260.             }
  261.         }
  262.         return $this->redirectToRoute('user.application'$param);
  263.     }
  264.     public function stepData()
  265.     {
  266.         $data = [];
  267.         $data['studyLevels'] = $this->studyLevelRepository->findAll();
  268.         $data['cursuses'] = $this->cursusRepository->findAll();
  269.         $data['programsByLevel'] = $this->programService->getActiveProgramGroupByLevel();
  270.         $france $this->countryRepo->find(1);
  271.         $data['countries'] = array_merge([$france], $this->countryRepo->getAll(['except_id' => 1'sort_asc' => 'name'], 'ENTITY'));
  272.         $data['departments'] = array_map(function ($department) {
  273.             return ['value' => $department'text' => $department];
  274.         }, $this->commonService->frenchDepartments);
  275.         $data['campuses'] = $this->campusRepository->getAll(['filter_status' => 1'memberOf_programs' => 'notNull','sort_asc' => 'ordering'], 'ENTITY');
  276.         $year date("Y");
  277.         if (date("Y-m-d") > date("Y") . '-10-01') {
  278.             $data['year'] = date('Y'strtotime('+1 year')) . '-' date('Y'strtotime('+2 year'));
  279.         } else {
  280.             $data['year'] = $year '-' date('Y'strtotime('+1 year'));
  281.         }
  282.         return $data;
  283.     }
  284.     /**
  285.      * @Route("/{_locale}/fiche-de-renseigenement-alternance", name="fiche_de_renseigenement", methods={"GET"})
  286.      */
  287.     public function ficheDeRenseigenement(Request $request)
  288.     {
  289.         $token $request->get('token');
  290.         if (!$token) {
  291.             return $this->render('pages/404.html.twig');
  292.         }
  293.         $tokenStorage explode('_MpTKRipu85cN'$token);
  294.         if (is_array($tokenStorage)) {
  295.             $profile $this->profileRepo->find(base64_decode($tokenStorage[0]));
  296.         } else {
  297.             return $this->render('pages/404.html.twig');
  298.         }
  299.         $data = [];
  300.         $data['profile'] = $this->currentService->autoMapper->map($profile'App\DTO\Profile\ProfileOutput');
  301.         // dd($data['profile']);
  302.         $data['session'] = (int) $profile->getYear() ? explode('-'$profile->getYear())[0] : null;
  303.         $data['contractTypes'] = Common::contractTypes();
  304.         $data['specificPrivate'] = Common::CONTRACT_SPECIFIC_TYPES;
  305.         $data['specificPublic'] = Common::CONTRACT_SPECIFIC_PUBLIC_TYPES;
  306.         $data['employers'] = Common::CONTRACT_SPECIFIC_EMPLOYERS;
  307.         //set default list specifics
  308.         $data['specifics'] =  Common::CONTRACT_SPECIFIC_TYPES;
  309.         if (isset($data['profile']->entreprise['entreprise']['type']) && $data['profile']->entreprise['entreprise']['type'] == 2) {
  310.             $data['specifics'] =  Common::CONTRACT_SPECIFIC_PUBLIC_TYPES;
  311.         }
  312.         $data['sendMail'] = 0;
  313.         if ($request->get('mode') == 'public') {
  314.             $data['sendMail'] = 1;
  315.         }
  316.         return $this->render('fiche/index.html.twig'$data);
  317.     }
  318.     /**
  319.      * @Route("/{_locale}/fiche-save", name="fiche.save", methods={"PUT"})
  320.      */
  321.     public function ficheSave(Request $request)
  322.     {
  323.         $contract $request->get('contract');
  324.         if ($contract) {
  325.             $fullSupport 0;
  326.             $annualCompanyTotal 0;
  327.             if (isset($contract['contract']['annualSupport1'])) {
  328.                 if ($contract['contract']['annualSupport1']) {
  329.                     $fullSupport $fullSupport $contract['contract']['annualSupport1'];
  330.                 }
  331.                 if ($contract['contract']['annualCompany1']) {
  332.                     $annualCompanyTotal $annualCompanyTotal $contract['contract']['annualCompany1'];
  333.                 }
  334.             }
  335.             if (isset($contract['contract']['annualSupport2'])) {
  336.                 if ($contract['contract']['annualSupport2']) {
  337.                     $fullSupport $fullSupport $contract['contract']['annualSupport2'];
  338.                 }
  339.                 if ($contract['contract']['annualCompany2']) {
  340.                     $annualCompanyTotal $annualCompanyTotal $contract['contract']['annualCompany2'];
  341.                 }
  342.             }
  343.             if (isset($contract['contract']['annualSupport3'])) {
  344.                 if ($contract['contract']['annualSupport3']) {
  345.                     $fullSupport $fullSupport $contract['contract']['annualSupport3'];
  346.                 }
  347.                 if ($contract['contract']['annualCompany3']) {
  348.                     $annualCompanyTotal $annualCompanyTotal $contract['contract']['annualCompany3'];
  349.                 }
  350.             }
  351.             $contract['contract']['fullSupport'] = $fullSupport;
  352.             $contract['contract']['annualCompanyTotal'] = $annualCompanyTotal;
  353.             $contract['contract']['totalTrainingCost'] = $fullSupport $annualCompanyTotal;
  354.         }
  355.         $conditions $request->get('entreprise');
  356.         $trainingHours 0;
  357.         if ($conditions) {
  358.             if (isset($conditions['contract']['conditions'])) {
  359.                 foreach ($conditions['contract']['conditions'] as $condition) {
  360.                     $trainingHours $trainingHours $condition['hour'];
  361.                 }
  362.             }
  363.             $contract['contract']['trainingHours'] = $trainingHours;
  364.         }
  365.         $request->request->set('contract'$contract);
  366.         $profile $this->currentService->updateProfile($request'App\DTO\Profile\UpdateProfileFicheInput');
  367.         if ($request->get('sendMail')) {
  368.             $this->userService->sendFicheEmail($this->currentService->autoMapper->map($profile'App\DTO\Profile\ProfileOutput'), 'private');
  369.         }
  370.         return $this->json(ServiceUtil::processSuccessful());
  371.     }
  372.     /**
  373.      * @Route("/qcm-result", name="qcm_result.save", methods={"POST"})
  374.      */
  375.     public function QcmResult(Request $request)
  376.     {
  377.         $response json_decode($request->getContent(), 1);
  378.         if ($response['token'] && $response['email']) {
  379.             $profile $this->currentRepo->getProfileByToken($response['email'], $response['token']);
  380.             if ($profile) {
  381.                 $dataQcmResults = [
  382.                     'isDoneTest' => $response['isDoneTest'],
  383.                     'questions' => $response['questions']
  384.                 ];
  385.                 $profile->setQcmResults($dataQcmResults);
  386.                 $this->currentRepo->save($profile);
  387.             } else {
  388.                 return $this->json(ServiceUtil::processFailed([BadRequestException::NO_ENTITY]));
  389.             }
  390.         } else {
  391.             return $this->json(ServiceUtil::processFailed([BadRequestException::WRONG_INPUT]));
  392.         }
  393.         return $this->json(ServiceUtil::processSuccessful());
  394.     }
  395.     /**
  396.      * @Route("/{_locale}/scolarite", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="user.scolarite")
  397.      */
  398.     public function scolarite(TranslatorInterface $translator)
  399.     {
  400.         if (!$this->getUser()) return $this->redirectToRoute('home');
  401.         $userId $this->getUser()->getId();
  402.         $profile $this->profileRepo->getProfileByUserId(['filter_id' => $userId]);
  403.         if($profile['status']['id'] != Status::STATUS_STUDENT_ENROLLED) return $this->redirectToRoute('home');
  404.         $siteName $this->getParameter('site_name');
  405.         $this->addTitle($translator->trans(
  406.             'scolarite.title',
  407.             [
  408.                 '%siteName%' => $siteName
  409.             ]
  410.         ));
  411.         $this->setDescription($translator->trans(
  412.             'scolarite.desc',
  413.             [
  414.                 '%siteName%' => $siteName
  415.             ]
  416.         ));
  417.         return $this->render('scolarite/index.html.twig', ['profile' => $profile]);
  418.     }
  419.     /**
  420.      * @Route("/{_locale}/inscription", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="user.inscription")
  421.      */
  422.     public function inscription(TranslatorInterface $translator)
  423.     {
  424.         if (!$this->getUser()) return $this->redirectToRoute('home');
  425.         $userId $this->getUser()->getId();
  426.         $profile $this->profileRepo->getProfileByUserId(['filter_id' => $userId]);
  427.         if($profile['status']['id'] != Status::STATUS_STUDENT_ELIGIBLE) return $this->redirectToRoute('home');
  428.         $siteName $this->getParameter('site_name');
  429.         $this->addTitle($translator->trans(
  430.             'inscription.title',
  431.             [
  432.                 '%siteName%' => $siteName
  433.             ]
  434.         ));
  435.         $this->setDescription($translator->trans(
  436.             'inscription.desc',
  437.             [
  438.                 '%siteName%' => $siteName
  439.             ]
  440.         ));
  441.         if($profile['selectedProgram']['level'] == 3){
  442.             $fileInscription 'Contrat_d_inscription_MASTER_ESTIAM_2026-2027_Paris_new.docx';
  443.         } else{
  444.             $fileInscription 'Contrat_d_inscription_BACHELOR_ESTIAM_2026-2027_Paris_new.docx';
  445.         }
  446.         return $this->render('inscription/index.html.twig', ['fileInscription' => $fileInscription]);
  447.     }
  448.     /**
  449.      * @Route("/profile/upload-file", name="profile.upload_file")
  450.      */
  451.     public function uploadFile(Request $request)
  452.     {
  453.         if (!$this->getUser())  return $this->json(ServiceUtil::processFailed(['message' => "Unauthorized"]));
  454.         $userId $this->getUser()->getId();
  455.         $profile $this->profileRepo->findOneBy(['user' => $userId]);
  456.         $result $this->profileFileService->saveFiles($profile$request);
  457.         $renderedMacro $this->render('scolarite/wapper.macro.twig.html', ['item' => $result[0]]);
  458.         return $this->json(ServiceUtil::processSuccessful([$renderedMacro]));
  459.     }
  460.     /**
  461.      * @Route("/profile/remove-file", name="profile.remove_file")
  462.      */
  463.     public function removeFile(Request $requestTranslatorInterface $translator)
  464.     {
  465.         if (!$this->getUser())  return $this->json(ServiceUtil::processFailed(['message' => "Unauthorized"]));
  466.         $profileFile $this->profileFileService->get($request->request->get('id'));
  467.         $this->profileFileService->removeProfileFilesByProfileFilesId($profileFile);
  468.         return $this->json(ServiceUtil::processSuccessful());
  469.     }
  470.     /**
  471.      * @Route("/api/analyze-file", name="profile.analyze_file", methods={"POST"})
  472.      */
  473.     public function analyzeFile(Request $requestProfileFileService $profileFileService)
  474.     {
  475.         $file $request->files->get('file');
  476.         if (!$file) {
  477.             return $this->json(ServiceUtil::processFailed(['message' => "No file uploaded"]));
  478.         }
  479.         try {
  480.             $result $profileFileService->analyzeFile($file);
  481.             //$result = $profileFileService->getResponseFromOpenAi('resp_0080a9e8ab1c4cfa0069a11484eb1881958f9a2e4781957dfe');
  482.             return $this->json(ServiceUtil::processSuccessful([$result]));
  483.         } catch (\Exception $e) {
  484.             return $this->json(ServiceUtil::processFailed(['message' => "Error analyzing file: " $e->getMessage()]));
  485.         }
  486.     }
  487. }