wip: booking process, room assignment

This commit is contained in:
Björn Fromme
2025-07-18 15:40:23 +02:00
parent eecea0abd1
commit 6feb8178b5
12 changed files with 454 additions and 201 deletions
@@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
namespace App\Controller\Booking;
use App\Form\BookingCreateStep2Type;
use App\Form\Model\BookingCreateDto;
use App\Form\Model\ParticipantDto;
use App\Service\BookingService;
use App\Validator\Constraints\Participant;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
/**
* Handles the second step of the booking creation process.
*
* This controller manages participant information collection including
* dynamic room assignment functionality with HTMX-based form updates.
*/
class CreateStep2Controller extends AbstractController
{
use BookingCreateTrait;
public function __construct(
private readonly BookingService $bookingService,
) {
}
/**
* Displays and processes the participant information form.
*/
#[Route('/bookings/create/participants', name: 'app_booking_create_step_2')]
public function participants(Request $request): Response
{
$bookingCreateDto = $this
->bookingService
->getOrCreateBookingCreateDto($request);
// Validate step access
$this->validateStepAccess($bookingCreateDto, 2);
// Ensure correct number of participants
$this->ensureCorrectNumberOfParticipants($bookingCreateDto);
$participantsCount = $this->getParticipantsCount($bookingCreateDto);
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
$form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto, [
'attr' => ['novalidate' => 'novalidate'],
'validation_groups' => ['booking_create_step_2'],
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$bookingCreateDto->currentStep = 3;
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
return $this->redirectToRoute('app_booking_create_step_3');
}
return $this->render('booking/create_step_2.html.twig', [
'bookingCreateDto' => $bookingCreateDto,
'participantsCount' => $participantsCount,
'form' => $form->createView(),
]);
}
/**
* Handles HTMX requests for dynamic form updates when room selections change.
*/
#[Route('/bookings/create/participants/refresh', name: 'app_booking_create_step_2_refresh', methods: ['POST'])]
public function refreshParticipantForm(Request $request): Response
{
$bookingCreateDto = $this->bookingService->getOrCreateBookingCreateDto($request);
// Process form data without validation to capture current state
$form = $this->createForm(BookingCreateStep2Type::class, $bookingCreateDto, [
'attr' => ['novalidate' => 'novalidate'],
'validation_groups' => false,
]);
$form->handleRequest($request);
$this->bookingService->saveBookingCreateDto($request, $bookingCreateDto);
return $this->renderBlock('booking/create_step_2.html.twig', 'participants_form', [
'form' => $form->createView(),
]);
}
private function getParticipantsCount(BookingCreateDto $bookingCreateDto): int
{
return $this
->bookingService
->getParticipantsCount($bookingCreateDto->roomSelections, $bookingCreateDto->travelData);
}
private function ensureCorrectNumberOfParticipants(BookingCreateDto $bookingCreateDto): void
{
$participantsCount = $this->getParticipantsCount($bookingCreateDto);
$participants = $bookingCreateDto->participants;
$bookingCreateDto->participants = [];
for ($i = 0; $i < $participantsCount; ++$i) {
$participant = $participants[$i] ?? new ParticipantDto();
$participant->index = $i;
$bookingCreateDto->participants[$i] = $participant;
}
}
}