src/Controller/Frontend/ProfileController.php line 21

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