<?php
namespace App\Controller\Page;
use App\Component\Configuration\Util\Config;
use App\Decorator\ApplicantDecorator as ApplicantFormDecorator;
use App\Entity\Applicant;
use App\Entity\ApplicantForm;
use App\Event\PreOpenApplicationEvent;
use App\Templating\Decorator as ThemeTemplateDecorator;
use App\Util\StringUtil;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
#[Route(path: '/applicant_snippet', condition: "env('APP_ENV') != 'prod' || request.isXmlHttpRequest()")]
class ApplicantSnippetController extends AbstractController
{
private ApplicantFormDecorator $applicantFormDecorator;
private ThemeTemplateDecorator $themeTemplateDecorator;
private Config $config;
public function __construct(
ApplicantFormDecorator $applicantFormDecorator,
ThemeTemplateDecorator $themeTemplateDecorator,
Config $config,
private readonly EventDispatcherInterface $eventDispatcher,
) {
$this->applicantFormDecorator = $applicantFormDecorator;
$this->themeTemplateDecorator = $themeTemplateDecorator;
$this->config = $config;
}
/**
* @throws \JsonException
*/
#[Route(path: '/open', name: 'applicant_open_modal_snippet')]
public function modalSnippetAction(Request $request): Response
{
$applicant = new Applicant();
$formId = $request->query->get('form_id');
if (empty($formId) || !StringUtil::validateUniqId($formId)) {
$formId = uniqid();
}
$form = $this->applicantFormDecorator->getOpenForm(
$applicant,
$this->generateUrl('applicant_open_modal_snippet', ['form_id' => $formId]),
$formId,
$request->getLocale()
);
if (!$form instanceof FormInterface) {
throw $this->createNotFoundException('Could not find open application form');
}
$applicantForm = $this->applicantFormDecorator->fetchOpenApplicationForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$applicant->setLocale($request->getLocale());
$this->applicantFormDecorator->handleOpenForm($form, $applicant);
return $this->redirect($this->generateUrl('vacancy_apply_thanks', [
$this->config->get('site_google_applicant_query_name') => $applicant->getId(),
]));
}
return $this->render(
$this->themeTemplateDecorator->getTemplate('admin/Form/open_apply_modal.html.twig'),
[
'form' => $form->createView(),
'form_title' => $applicantForm?->getTitle(),
]
);
}
/**
* @throws \JsonException
*/
#[Route(path: '/open/form', name: 'applicant_open_snippet')]
public function snippetAction(Request $request): Response
{
return $this->handleSnippetAction($request);
}
/**
* @throws \JsonException
*/
#[Route(path: '/open/form-headless', name: 'applicant_open_snippet_headless')]
public function headlessSnippetAction(Request $request): Response
{
return $this->handleSnippetAction(
$request,
'@default/pages/form/open_apply_headless.html.twig'
);
}
/**
* @throws \JsonException
*/
private function handleSnippetAction(
Request $request,
$template = '@default/pages/form/open_apply.html.twig'
): JsonResponse|Response {
$applicant = new Applicant();
$formId = $request->query->get('form_id');
$applicantForm = $this->applicantFormDecorator->fetchOpenApplicationForm(
$request->query->getInt('applicant-form')
);
if (!$applicantForm instanceof ApplicantForm) {
return new Response('');
}
$event = new PreOpenApplicationEvent($applicantForm);
$this->eventDispatcher->dispatch($event);
if ($event->getResponse() instanceof Response) {
return $event->getResponse();
}
$applicant->setApplicantForm($applicantForm);
if (empty($formId) || !StringUtil::validateUniqId($formId)) {
$formId = uniqid();
}
$form = $this->applicantFormDecorator->getOpenForm(
$applicant,
$this->generateUrl(
'applicant_open_snippet',
['form_id' => $formId, 'applicant-form' => $applicantForm->getId() ?? 0]
),
$formId
);
if (!$form instanceof FormInterface) {
throw $this->createNotFoundException();
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$applicant->setLocale($request->getLocale());
$referer = $request->headers->get('referer');
if (!empty($referer)) {
$applicant->setApplyUrl($referer);
}
$this->applicantFormDecorator->handleOpenForm($form, $applicant);
$redirectUri = $this->generateUrl('vacancy_apply_thanks', [
$this->config->get('site_google_applicant_query_name') => $applicant->getId(),
]);
if ($request->isXmlHttpRequest()) {
return new JsonResponse([
'redirect' => $redirectUri,
], 200);
}
return $this->redirect($redirectUri);
}
return $this->render(
$this->themeTemplateDecorator->getTemplate($template),
[
'applicant_form' => $applicant->getApplicantForm(),
'form' => $form->createView(),
'form_title' => $applicantForm->getTitle(),
]
);
}
}