<?php
namespace App\Controller;
use App\Entity\Form;
use App\Entity\FormLead;
use App\Form\JsonType;
use App\Service\FormLeadService;
use App\Service\JsonFormManager;
use App\Service\ProcessFormService;
use App\Service\Style\FormStyleService;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/form", name="form_")
*/
class FormController extends AbstractController
{
/**
* @Route("/create/{uuid}/{frame}", name="create", requirements={"frame": App\Service\ProcessFormService::STRING_FRAMES})
* @IsGranted("PROCESS_CREATE", subject="formInstance")
* @throws Exception
*/
public function createLead(
Form $formInstance,
string $frame,
Request $request,
JsonFormManager $jsonFormManager,
FormLeadService $service,
FormStyleService $formStyleService
): Response {
$manager = $jsonFormManager
->setForm($formInstance)
->setQueryParams($request->query->all())
->loadFromFilePath($formInstance->getDefinitionFile())
;
$isCreateForm = Form::TYPE_CREATE === $formInstance->getType();
$form = $this->createForm(JsonType::class, null, [
'manager' => $manager,
'disabled' => !$isCreateForm
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (!$isCreateForm) {
throw new Exception('Este formulario no es de creación');
}
$formDefinitionFields = $manager->getFormDefinitionFields();
$formData = $manager->getFormDataInFormDefinitionFields($form->getData(), $formDefinitionFields);
$lead = $service->create($formInstance, $formData, $manager->getFormDefinition());
return $this->redirectToRoute('form_success', [
'form_uuid' => $formInstance->getUuid(),
'lead_uuid' => $lead->getUuid()
]);
}
return $this->render('form/form.html.twig', [
'formInstance' => $formInstance,
'frame' => $frame,
'form' => $form->createView(),
'render_form' => true,
'styles' => $formStyleService->setQueryParams($request->query->all())->buildStyles()
]);
}
/**
* @Route("/edit/{form_uuid}/{lead_uuid}/{frame}", name="edit", requirements={"frame": App\Service\ProcessFormService::STRING_FRAMES})
* @ParamConverter("formInstance", options={"mapping": {"form_uuid": "uuid"}})
* @ParamConverter("formLead", options={"mapping": {"lead_uuid": "uuid"}})
* @IsGranted("PROCESS_EDIT", subject="formInstance")
* @throws Exception
*/
public function editLead(
Form $formInstance,
FormLead $formLead,
string $frame,
Request $request,
ProcessFormService $processFormService,
FormLeadService $service,
FormStyleService $formStyleService
): Response {
if (!$formLead->isActive()) {
return $this->render('form/form.html.twig', [
'formInstance' => $formInstance,
'frame' => $frame,
'render_form' => false,
'error_message' => 'El registro no está activo'
]);
}
$manager = $processFormService
->setForm($formInstance)
->setQueryParams($request->query->all())
->loadFromFilePath($formInstance->getDefinitionFile())
;
$form = $this->createForm(JsonType::class, null, [
'manager' => $manager,
'form_lead' => $formLead
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$formDefinitionFields = $manager->getFormDefinitionFields();
$formData = $manager->getFormDataInFormDefinitionFields($form->getData(), $formDefinitionFields);
switch ($formInstance->getType()) {
case Form::TYPE_EDIT:
$formLead = $service->edit($formInstance, $formLead, $formData, $manager->getFormDefinition());
break;
case Form::TYPE_FULL_CREATE:
$formLead = $service->create($formInstance, $formData, $manager->getFormDefinition());
break;
default:
throw new Exception('Este formulario no es de edición');
}
return $this->redirectToRoute('form_success', [
'form_uuid' => $formInstance->getUuid(),
'lead_uuid' => $formLead->getUuid()
]);
}
return $this->render('form/form.html.twig', [
'formInstance' => $formInstance,
'frame' => $frame,
'form' => $form->createView(),
'not_lead_data_in_form' => $manager->getNotLeadDataInForm($formLead),
'render_form' => $manager->isAllowedRenderForm($formLead),
'styles' => $formStyleService->setQueryParams($request->query->all())->buildStyles()
]);
}
/**
* @Route("/success/{form_uuid}/{lead_uuid}", name="success")
* @ParamConverter("formInstance", options={"mapping": {"form_uuid": "uuid"}})
* @ParamConverter("formLead", options={"mapping": {"lead_uuid": "uuid"}})
* @IsGranted("PROCESS_SUCCESS", subject="formInstance")
*/
public function leadSuccess(Form $formInstance, FormLead $formLead): Response
{
return $this->render('form/success.html.twig', [
'form' => $formInstance,
'formLead' => $formLead
]);
}
}