src/Controller/Frontend/DocumentController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\Campaign;
  4. use App\Misc\CaptchaHelper;
  5. use App\Utilities\ServiceUtil;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use FOS\RestBundle\Controller\Annotations as Rest;
  10. use App\Exception\BadRequestException;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use App\Annotation\Log;
  14. class DocumentController extends BaseFrontendController
  15. {
  16.     /**
  17.      * @Route("/{_locale}/telechargez-la-brochure-estiam", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="demande")
  18.      */
  19.     public function demande(Request $requestTranslatorInterface $translator)
  20.     {
  21.         $siteName $translator->trans('siteName');
  22.         $this->addTitle($translator->trans(
  23.             'demande.title',
  24.             [
  25.                 '%siteName%' => $siteName
  26.             ]
  27.         ));
  28.         $this->setDescription($translator->trans(
  29.             'demande.desc',
  30.             [
  31.                 '%siteName%' => $siteName
  32.             ]
  33.         ));
  34.         $data = [];
  35.         $data['campaign'] = $this->campaignRepo->findOneBy(['id' => Campaign::DOCUMENT]);
  36.         $data['programId'] = $request->query->get('id') ? $request->query->get('id') : null;
  37.         $data['campuses'] = $this->campusRepo->findBy(['status' => 1]);
  38.         $data['programs'] = $this->programRepo->findBy(['isRegister' => 1], ['ordering' => 'ASC']);
  39.         return $this->render('demande/index.html.twig'$data);
  40.     }
  41.     /**
  42.      * @Rest\Post("/save-document", name="document.save_apply")
  43.      * @Log
  44.      */
  45.     public function saveDocument(
  46.         Request $request,
  47.         CaptchaHelper $captchaHelper,
  48.         TranslatorInterface $translator
  49.     ) {
  50.         try {
  51.             // Check captcha is valid
  52.             // if ($this->getParameter('captcha_enabled') && !$captchaHelper->isCaptchaValid($request)) {
  53.             //     return $this->json(ServiceUtil::processFailed([
  54.             //         'errorMessage' => $translator->trans('message.captcha_is_not_valid')
  55.             //     ]));
  56.             // }
  57.             $information['previousSchool'] = '';
  58.             if ($request->get('schoolName')) {
  59.                 $information['previousSchool'] = $request->get('schoolName');
  60.                 if ($request->get('schoolPostal')) {
  61.                     $information['previousSchool'] = $information['previousSchool'] . ' / ' $request->get('schoolPostal');
  62.                 }
  63.                 if ($request->get('schoolCity')) {
  64.                     $information['previousSchool'] = $information['previousSchool'] . ' / ' $request->get('schoolCity');
  65.                 }
  66.                 $request->query->set('information'$information);
  67.             }
  68.             //check email
  69.             $userRequest $request->get('user');
  70.             $user $this->userRepo->findOneBy(['email' => $userRequest['email']]);
  71.             if ($user) {
  72.                 $this->userService->updateProfileUser($userRequest$user);
  73.                 $profile $this->profileRepo->findOneBy(['user' => $user]);
  74.                 $request->query->set('profileId'$profile->getId());
  75.                 $programs $request->get('programs');
  76.                 foreach ($profile->getPrograms() as $program) {
  77.                     if (!in_array($program->getId(), $programs)) {
  78.                         $programs[] = $program->getId();
  79.                     }
  80.                 }
  81.                 $request->query->set('programs'$programs);
  82.                 $this->profileService->updateProfile($request);
  83.             } else {
  84.                 $this->profileService->addProfile($request);
  85.             }
  86.             return $this->json(ServiceUtil::processSuccessful());
  87.         } catch (BadRequestException $badRequestException) {
  88.             $messagesArr $badRequestException->getMessages();
  89.             foreach ($messagesArr['data'] as $field => $message) {
  90.                 $errorMessage $message;
  91.                 break;
  92.             }
  93.         }
  94.         $data['errorMessage'] = $errorMessage;
  95.         return $this->json(ServiceUtil::processFailed($data));
  96.     }
  97. }