src/Controller/Website/API/CruiseApiController.php line 84

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Website\API;
  4. use App\Controller\Website\CruiseListSearchFormConfiguration;
  5. use App\Helper\Partner\PartnerHelper;
  6. use Croisiland\CommonBundle\Entity\Cruise\Cruise;
  7. use Croisiland\CommonBundle\Form\CruiseSearchFullFormType;
  8. use Croisiland\CommonBundle\Model\CruiseSearchModel;
  9. use Croisiland\CommonBundle\Serializer\CruiseSearchModelSerializer;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. // À adapter selon ton bundle
  17. use Twig\Environment;
  18. #[Route('/api/cruise', name: 'api_cruise_')]
  19. class CruiseApiController extends AbstractController
  20. {
  21. public function __construct(
  22. private readonly EntityManagerInterface $entityManager,
  23. private readonly CruiseSearchModelSerializer $cruiseSearchModelSerializer,
  24. private readonly PartnerHelper $partnerHelper,
  25. private readonly Environment $twig
  26. )
  27. {
  28. }
  29. /**
  30. * Récupère une cruiseCard pour une croisière donnée (via AJAX).
  31. * Le type de carte est passé en paramètre (ex: mini, full, summary) pour être concatené avec '_snippets/components/cards/%s.html.twig'
  32. */
  33. #[Route('/card/{type}/{id}', name: 'get_cruise_card', methods: ['GET'], requirements: ['id' => '\d+', 'type' => '[a-zA-Z0-9_]+'])]
  34. public function getCruiseCard(Request $request, string $type, int $id): JsonResponse
  35. {
  36. // 1. Récupérer la croisière depuis la base de données
  37. // TODO
  38. // Récuperée les filtres a mettre pour le retourner que les cruise compatibles:
  39. // cruise futur, cruise actives, cruise, compatible partenaire, etc...
  40. $cruise = $this->entityManager->getRepository(Cruise::class)->find($id);
  41. if (!$cruise) {
  42. return new JsonResponse(
  43. ['error' => 'Cruise not found'],
  44. Response::HTTP_NOT_FOUND
  45. );
  46. }
  47. // 2. Déterminer le template Twig en fonction du type
  48. $templatePath = sprintf('_snippets/components/cards/%s.html.twig', $type);
  49. // 3. Vérifier si le template existe
  50. if (!$this->twig->getLoader()->exists($templatePath)) {
  51. return new JsonResponse(
  52. ['error' => sprintf('Template for card type "%s" not found.', $type)],
  53. Response::HTTP_BAD_REQUEST
  54. );
  55. }
  56. // 4. Rendre le template Twig pour la cruiseCard
  57. $cruiseCardHtml = $this->twig->render($templatePath, [
  58. 'cruise' => $cruise,
  59. ]);
  60. // 5. Retourner la réponse en JSON avec le HTML
  61. return new JsonResponse([
  62. 'success' => true,
  63. 'cruiseCard' => $cruiseCardHtml,
  64. ]);
  65. }
  66. /**
  67. * Récupère les options dynamiques pour les champs du formulaire de recherche de croisières. incluant les parametres d'URL
  68. */
  69. #[Route('/cruise-search-form-dynamic-options', name: 'get_cruise_serach_dynamic_options', methods: ['GET'])]
  70. public function getDynamicOptions(Request $request): Response
  71. {
  72. $partner = $this->partnerHelper->getCurrentPartner();
  73. $cruiseSearch = $this->cruiseSearchModelSerializer->fromRequest($request);
  74. $cruiseSearch->setPartner($partner);
  75. $form = $this->createForm(CruiseSearchFullFormType::class, $cruiseSearch, [
  76. 'visible_fields' => CruiseListSearchFormConfiguration::visibleFields(),
  77. ]);
  78. $form->handleRequest($request);
  79. return $this->render('@CroisilandCommon/updated_form_fields.html.twig', [
  80. 'form' => $form->createView(),
  81. ]);
  82. }
  83. }