src/Controller/CreateFormsController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\EventCustomDate;
  4. use App\Entity\EventFormEntity;
  5. use App\Entity\MobileAppEvent;
  6. use App\Helper\ColorHelper;
  7. use Exception;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. class CreateFormsController extends AbstractController
  13. {
  14.     private $translator;
  15.     public function __construct(TranslatorInterface $translator) {
  16.         $this->translator $translator;
  17.     }
  18.     /**
  19.      * Vista del formulario para que la gente se registre.
  20.      *
  21.      * @Route("/register/form/{key}", name="create_forms_register")
  22.      */
  23.     public function registerAction($keyRequest $request)
  24.     {
  25.         $entityManager $this->getDoctrine()->getManager();
  26.         $event null;
  27.         $eventForm null;
  28.         try {
  29.             // Buscar evento por keyword
  30.             $event $entityManager->getRepository(MobileAppEvent::class)->findOneByKeyword($key);
  31.             if (!$event) {
  32.                 throw new \Exception($this->translator->trans('Error, event not found'), 404);
  33.             }
  34.             
  35.             // Convertir color HEX a RGB si existe
  36.             if ($event->getRegisterBgColor()) {
  37.                 $event->setRegisterBgColor(ColorHelper::hexToRgb($event->getRegisterBgColor()));
  38.             }
  39.             
  40.             // Buscar el último formulario vinculado al evento
  41.             $eventForms $entityManager->getRepository(EventFormEntity::class)->findBy(
  42.                 ['event' => $event],        // Relación correcta (no por ID)
  43.                 ['id' => 'DESC'],         // Ordenar descendente
  44.                 1                         // Solo el más reciente
  45.             );
  46.             if (!$eventForms) {
  47.                 throw new \Exception($this->translator->trans('Error, form not found'), 404);
  48.             }
  49.             $eventForm $eventForms[count($eventForms) - 1];
  50.         } catch (\Exception $e) {
  51.             $this->addFlash('mensajeerror'$e->getMessage());
  52.         }
  53.         // Lógica de fecha personalizada
  54.         $customDate null;
  55.         if ($event) {
  56.             $hoy = new \DateTimeImmutable('today');
  57.             $customDates $entityManager->getRepository(EventCustomDate::class)->findBy(
  58.                 ['eid' => $event->getId()],
  59.                 ['date' => 'ASC']
  60.             );
  61.             foreach ($customDates as $dateObj) {
  62.                 $eventDate $dateObj->getDate();
  63.                 // Fecha exacta de hoy
  64.                 if ($eventDate->format('Y-m-d') === $hoy->format('Y-m-d')) {
  65.                     $customDate $eventDate;
  66.                     break;
  67.                 }
  68.                 // Próxima fecha futura (si no hay coincidencia exacta)
  69.                 if ($eventDate $hoy && $customDate === null) {
  70.                     $customDate $eventDate;
  71.                 }
  72.             }
  73.         }
  74.         // Token desde query string (opcional)
  75.         $token $request->query->get('token');
  76.         // Detectar HTTP/HTTPS correctamente
  77.         $isHttps = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;
  78.         $httpHost = ($isHttps 'https://' 'http://') . $_SERVER['HTTP_HOST'] . '/';
  79.         $bgStyle 'background: linear-gradient(180deg, grey, white)';
  80.         if ($event && $event->getImageBackground()) {
  81.             $bgStyle sprintf(
  82.                 "background: url('%s%s') center / cover no-repeat fixed",
  83.                 $httpHost,
  84.                 $event->getImageBackground()
  85.             );
  86.         }
  87. //dd($eventForm);
  88.         return $this->render('create-forms/register.html.twig', [
  89.             'bgStyle' => $bgStyle,
  90.             'event' => $event,
  91.             'eventForm' => $eventForm,
  92.             'httpHost' => $httpHost,
  93.             'customDate' => $customDate,
  94.             'token' => $token,
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/create-forms/{eid}", name="create_forms")
  99.      */
  100.     public function createAction($eid)
  101.     {
  102.         return $this->render('create-forms/pruebaDragAndDrops.html.twig',[
  103.             'eid' => $eid,
  104.             'url' => '/api/create-forms/save/metadata',
  105.         ]);
  106.     }
  107.     /**
  108.      * @Route("/create-forms/edit/{id}", name="create_forms_edit")
  109.      */
  110.     public function editAction($id)
  111.     {
  112.         $entityManager $this->getDoctrine()->getManager();
  113.         $eventForm $entityManager->getRepository(EventFormEntity::class)->findOneById($id);
  114.         return $this->render('create-forms/pruebaDragAndDrops.html.twig',[
  115.             'eid' => $eventForm->getEid()->getId(),
  116.             'data' => json_encode($eventForm->getDataForm()),
  117.             'url' => '/api/create-forms/save/metadata',
  118.         ]);
  119.     }
  120. }