src/Controller/Page/ApplicantSnippetController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Page;
  3. use App\Component\Configuration\Util\Config;
  4. use App\Decorator\ApplicantDecorator as ApplicantFormDecorator;
  5. use App\Entity\Applicant;
  6. use App\Entity\ApplicantForm;
  7. use App\Event\PreOpenApplicationEvent;
  8. use App\Templating\Decorator as ThemeTemplateDecorator;
  9. use App\Util\StringUtil;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  17. #[Route(path'/applicant_snippet'condition"env('APP_ENV') != 'prod' || request.isXmlHttpRequest()")]
  18. class ApplicantSnippetController extends AbstractController
  19. {
  20.     private ApplicantFormDecorator $applicantFormDecorator;
  21.     private ThemeTemplateDecorator $themeTemplateDecorator;
  22.     private Config $config;
  23.     public function __construct(
  24.         ApplicantFormDecorator $applicantFormDecorator,
  25.         ThemeTemplateDecorator $themeTemplateDecorator,
  26.         Config $config,
  27.         private readonly EventDispatcherInterface $eventDispatcher,
  28.     ) {
  29.         $this->applicantFormDecorator $applicantFormDecorator;
  30.         $this->themeTemplateDecorator $themeTemplateDecorator;
  31.         $this->config $config;
  32.     }
  33.     /**
  34.      * @throws \JsonException
  35.      */
  36.     #[Route(path'/open'name'applicant_open_modal_snippet')]
  37.     public function modalSnippetAction(Request $request): Response
  38.     {
  39.         $applicant = new Applicant();
  40.         $formId $request->query->get('form_id');
  41.         if (empty($formId) || !StringUtil::validateUniqId($formId)) {
  42.             $formId uniqid();
  43.         }
  44.         $form $this->applicantFormDecorator->getOpenForm(
  45.             $applicant,
  46.             $this->generateUrl('applicant_open_modal_snippet', ['form_id' => $formId]),
  47.             $formId,
  48.             $request->getLocale()
  49.         );
  50.         if (!$form instanceof FormInterface) {
  51.             throw $this->createNotFoundException('Could not find open application form');
  52.         }
  53.         $applicantForm $this->applicantFormDecorator->fetchOpenApplicationForm();
  54.         $form->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             $applicant->setLocale($request->getLocale());
  57.             $this->applicantFormDecorator->handleOpenForm($form$applicant);
  58.             return $this->redirect($this->generateUrl('vacancy_apply_thanks', [
  59.                 $this->config->get('site_google_applicant_query_name') => $applicant->getId(),
  60.             ]));
  61.         }
  62.         return $this->render(
  63.             $this->themeTemplateDecorator->getTemplate('admin/Form/open_apply_modal.html.twig'),
  64.             [
  65.                 'form' => $form->createView(),
  66.                 'form_title' => $applicantForm?->getTitle(),
  67.             ]
  68.         );
  69.     }
  70.     /**
  71.      * @throws \JsonException
  72.      */
  73.     #[Route(path'/open/form'name'applicant_open_snippet')]
  74.     public function snippetAction(Request $request): Response
  75.     {
  76.         return $this->handleSnippetAction($request);
  77.     }
  78.     /**
  79.      * @throws \JsonException
  80.      */
  81.     #[Route(path'/open/form-headless'name'applicant_open_snippet_headless')]
  82.     public function headlessSnippetAction(Request $request): Response
  83.     {
  84.         return $this->handleSnippetAction(
  85.             $request,
  86.             '@default/pages/form/open_apply_headless.html.twig'
  87.         );
  88.     }
  89.     /**
  90.      * @throws \JsonException
  91.      */
  92.     private function handleSnippetAction(
  93.         Request $request,
  94.         $template '@default/pages/form/open_apply.html.twig'
  95.     ): JsonResponse|Response {
  96.         $applicant = new Applicant();
  97.         $formId $request->query->get('form_id');
  98.         $applicantForm $this->applicantFormDecorator->fetchOpenApplicationForm(
  99.             $request->query->getInt('applicant-form')
  100.         );
  101.         if (!$applicantForm instanceof ApplicantForm) {
  102.             return new Response('');
  103.         }
  104.         $event = new PreOpenApplicationEvent($applicantForm);
  105.         $this->eventDispatcher->dispatch($event);
  106.         if ($event->getResponse() instanceof Response) {
  107.             return $event->getResponse();
  108.         }
  109.         $applicant->setApplicantForm($applicantForm);
  110.         if (empty($formId) || !StringUtil::validateUniqId($formId)) {
  111.             $formId uniqid();
  112.         }
  113.         $form $this->applicantFormDecorator->getOpenForm(
  114.             $applicant,
  115.             $this->generateUrl(
  116.                 'applicant_open_snippet',
  117.                 ['form_id' => $formId'applicant-form' => $applicantForm->getId() ?? 0]
  118.             ),
  119.             $formId
  120.         );
  121.         if (!$form instanceof FormInterface) {
  122.             throw $this->createNotFoundException();
  123.         }
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             $applicant->setLocale($request->getLocale());
  127.             $referer $request->headers->get('referer');
  128.             if (!empty($referer)) {
  129.                 $applicant->setApplyUrl($referer);
  130.             }
  131.             $this->applicantFormDecorator->handleOpenForm($form$applicant);
  132.             $redirectUri $this->generateUrl('vacancy_apply_thanks', [
  133.                 $this->config->get('site_google_applicant_query_name') => $applicant->getId(),
  134.             ]);
  135.             if ($request->isXmlHttpRequest()) {
  136.                 return new JsonResponse([
  137.                     'redirect' => $redirectUri,
  138.                 ], 200);
  139.             }
  140.             return $this->redirect($redirectUri);
  141.         }
  142.         return $this->render(
  143.             $this->themeTemplateDecorator->getTemplate($template),
  144.             [
  145.                 'applicant_form' => $applicant->getApplicantForm(),
  146.                 'form' => $form->createView(),
  147.                 'form_title' => $applicantForm->getTitle(),
  148.             ]
  149.         );
  150.     }
  151. }