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