src/Controller/FormController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Form;
  4. use App\Entity\FormLead;
  5. use App\Form\JsonType;
  6. use App\Service\FormLeadService;
  7. use App\Service\JsonFormManager;
  8. use App\Service\ProcessFormService;
  9. use App\Service\Style\FormStyleService;
  10. use Exception;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. /**
  18.  * @Route("/form", name="form_")
  19.  */
  20. class FormController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/create/{uuid}/{frame}", name="create", requirements={"frame": App\Service\ProcessFormService::STRING_FRAMES})
  24.      * @IsGranted("PROCESS_CREATE", subject="formInstance")
  25.      * @throws Exception
  26.      */
  27.     public function createLead(
  28.         Form $formInstance,
  29.         string $frame,
  30.         Request $request,
  31.         JsonFormManager $jsonFormManager,
  32.         FormLeadService $service,
  33.         FormStyleService $formStyleService
  34.     ): Response {
  35.         $manager $jsonFormManager
  36.             ->setForm($formInstance)
  37.             ->setQueryParams($request->query->all())
  38.             ->loadFromFilePath($formInstance->getDefinitionFile())
  39.         ;
  40.         $isCreateForm Form::TYPE_CREATE === $formInstance->getType();
  41.         $form $this->createForm(JsonType::class, null, [
  42.             'manager' => $manager,
  43.             'disabled' => !$isCreateForm
  44.         ]);
  45.         $form->handleRequest($request);
  46.         if ($form->isSubmitted() && $form->isValid()) {
  47.             if (!$isCreateForm) {
  48.                 throw new Exception('Este formulario no es de creación');
  49.             }
  50.             $formDefinitionFields $manager->getFormDefinitionFields();
  51.             $formData $manager->getFormDataInFormDefinitionFields($form->getData(), $formDefinitionFields);
  52.             $lead $service->create($formInstance$formData$manager->getFormDefinition());
  53.             return $this->redirectToRoute('form_success', [
  54.                 'form_uuid' => $formInstance->getUuid(),
  55.                 'lead_uuid' => $lead->getUuid()
  56.             ]);
  57.         }
  58.         return $this->render('form/form.html.twig', [
  59.             'formInstance' => $formInstance,
  60.             'frame' => $frame,
  61.             'form' => $form->createView(),
  62.             'render_form' => true,
  63.             'styles' => $formStyleService->setQueryParams($request->query->all())->buildStyles()
  64.         ]);
  65.     }
  66.     /**
  67.      * @Route("/edit/{form_uuid}/{lead_uuid}/{frame}", name="edit", requirements={"frame": App\Service\ProcessFormService::STRING_FRAMES})
  68.      * @ParamConverter("formInstance", options={"mapping": {"form_uuid": "uuid"}})
  69.      * @ParamConverter("formLead", options={"mapping": {"lead_uuid": "uuid"}})
  70.      * @IsGranted("PROCESS_EDIT", subject="formInstance")
  71.      * @throws Exception
  72.      */
  73.     public function editLead(
  74.         Form $formInstance,
  75.         FormLead $formLead,
  76.         string $frame,
  77.         Request $request,
  78.         ProcessFormService $processFormService,
  79.         FormLeadService $service,
  80.         FormStyleService $formStyleService
  81.     ): Response {
  82.         if (!$formLead->isActive()) {
  83.             return $this->render('form/form.html.twig', [
  84.                 'formInstance' => $formInstance,
  85.                 'frame' => $frame,
  86.                 'render_form' => false,
  87.                 'error_message' => 'El registro no está activo'
  88.             ]);
  89.         }
  90.         $manager $processFormService
  91.             ->setForm($formInstance)
  92.             ->setQueryParams($request->query->all())
  93.             ->loadFromFilePath($formInstance->getDefinitionFile())
  94.         ;
  95.         $form $this->createForm(JsonType::class, null, [
  96.             'manager' => $manager,
  97.             'form_lead' => $formLead
  98.         ]);
  99.         $form->handleRequest($request);
  100.         if ($form->isSubmitted() && $form->isValid()) {
  101.             $formDefinitionFields $manager->getFormDefinitionFields();
  102.             $formData $manager->getFormDataInFormDefinitionFields($form->getData(), $formDefinitionFields);
  103.             switch ($formInstance->getType()) {
  104.                 case Form::TYPE_EDIT:
  105.                     $formLead $service->edit($formInstance$formLead$formData$manager->getFormDefinition());
  106.                     break;
  107.                 case Form::TYPE_FULL_CREATE:
  108.                     $formLead $service->create($formInstance$formData$manager->getFormDefinition());
  109.                     break;
  110.                 default:
  111.                     throw new Exception('Este formulario no es de edición');
  112.             }
  113.             return $this->redirectToRoute('form_success', [
  114.                 'form_uuid' => $formInstance->getUuid(),
  115.                 'lead_uuid' => $formLead->getUuid()
  116.             ]);
  117.         }
  118.         return $this->render('form/form.html.twig', [
  119.             'formInstance' => $formInstance,
  120.             'frame' => $frame,
  121.             'form' => $form->createView(),
  122.             'not_lead_data_in_form' => $manager->getNotLeadDataInForm($formLead),
  123.             'render_form' => $manager->isAllowedRenderForm($formLead),
  124.             'styles' => $formStyleService->setQueryParams($request->query->all())->buildStyles()
  125.         ]);
  126.     }
  127.     /**
  128.      * @Route("/success/{form_uuid}/{lead_uuid}", name="success")
  129.      * @ParamConverter("formInstance", options={"mapping": {"form_uuid": "uuid"}})
  130.      * @ParamConverter("formLead", options={"mapping": {"lead_uuid": "uuid"}})
  131.      * @IsGranted("PROCESS_SUCCESS", subject="formInstance")
  132.      */
  133.     public function leadSuccess(Form $formInstanceFormLead $formLead): Response
  134.     {
  135.         return $this->render('form/success.html.twig', [
  136.             'form' => $formInstance,
  137.             'formLead' => $formLead
  138.         ]);
  139.     }
  140. }