260 lines
9.3 KiB
PHP
260 lines
9.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Form\Service;
|
|
|
|
use App\BusProNet\Constants;
|
|
use App\BusProNet\Model\Service;
|
|
use App\Form\Model\BookingDto;
|
|
use App\Form\Model\ParticipantDto;
|
|
use App\Form\Service\Abstract\AbstractParticipantFieldHandler;
|
|
|
|
/**
|
|
* Handles processing of the rentals field for booking participants.
|
|
*
|
|
* This handler manages rental equipment selections for participants in the booking
|
|
* creation process. It processes the rentals field from form submissions,
|
|
* filters out age-inappropriate options and duration-inappropriate options, and
|
|
* updates the participant DTO with only valid selections.
|
|
*
|
|
* Dependencies: dateOfBirth (for age evaluation) and skiPass (for duration filtering)
|
|
*/
|
|
class ParticipantRentalsFieldHandler extends AbstractParticipantFieldHandler
|
|
{
|
|
public function getFieldName(): string
|
|
{
|
|
return 'rentals';
|
|
}
|
|
|
|
/**
|
|
* Returns the field dependencies for proper processing order.
|
|
*
|
|
* This handler depends on both dateOfBirth (for age evaluation) and skiPass
|
|
* (for duration filtering and field visibility logic).
|
|
*
|
|
* @return string[] Array containing 'dateOfBirth' and 'skiPass' dependencies
|
|
*/
|
|
public function getDependencies(): array
|
|
{
|
|
return ['dateOfBirth', 'skiPass'];
|
|
}
|
|
|
|
/**
|
|
* Determines if this handler should process the field based on submitted data.
|
|
*
|
|
* For service selection fields like rentals, we always need to process
|
|
* to handle cases where all selections are cleared (field not present in data).
|
|
* This ensures the participant DTO is updated with an empty array when no
|
|
* services are selected.
|
|
*
|
|
* @param array<string, mixed> $submittedData The submitted participant form data
|
|
* @param string $mode The booking mode (BookingDto::MODE_CREATE or MODE_EDIT)
|
|
* @param int $participantIndex The index of the participant being processed
|
|
*
|
|
* @return bool Always returns true for service selection fields
|
|
*/
|
|
public function shouldProcess(array $submittedData, string $mode, int $participantIndex): bool
|
|
{
|
|
return true; // Always process to handle deselection cases
|
|
}
|
|
|
|
public function processField(array $submittedData, BookingDto $bookingDto, int $participantIndex): void
|
|
{
|
|
$participant = $this->getParticipant($bookingDto, $participantIndex);
|
|
if (null === $participant) {
|
|
return;
|
|
}
|
|
|
|
if (false === $participant instanceof ParticipantDto) {
|
|
return;
|
|
}
|
|
|
|
// Store previous rental state BEFORE checking skipass
|
|
$previousRentals = $participant->rentals;
|
|
|
|
// Check if rentals should be available based on skipass selection
|
|
if (null === $participant->skiPass) {
|
|
// No skipass selected = clear all rental selections
|
|
if ([] !== $previousRentals) {
|
|
$participant->addNotification(
|
|
'warning',
|
|
'Ausrüstung wurde entfernt (kein Skipass ausgewählt)'
|
|
);
|
|
}
|
|
$participant->rentals = [];
|
|
|
|
return;
|
|
}
|
|
|
|
$selectedRentals = $this->getFieldValue($submittedData, $this->getFieldName()) ?? [];
|
|
|
|
// Get available rentals from travel data (includes booked rentals in edit mode)
|
|
$availableRentals = $this->getAvailableServicesWithBooked(
|
|
$bookingDto,
|
|
$participantIndex,
|
|
Constants::TOKEN_RENTALS,
|
|
true
|
|
);
|
|
|
|
// Filter rentals by skipass duration to ensure only matching rentals are available
|
|
$durationFilteredRentals = $this->filterRentalsBySkiPassDuration($availableRentals, $participant);
|
|
|
|
$validSelections = $this->filterValidServiceSelections(
|
|
$selectedRentals,
|
|
$durationFilteredRentals,
|
|
$bookingDto,
|
|
$participantIndex
|
|
);
|
|
|
|
$this->updateAutoBookOptOutRentals(
|
|
$participant,
|
|
$durationFilteredRentals,
|
|
$validSelections,
|
|
$bookingDto,
|
|
$participantIndex
|
|
);
|
|
|
|
// Notify if rentals were cleared due to skipass duration change
|
|
// Only show notification if user had submitted rentals that got filtered out,
|
|
// not if the user intentionally cleared the selection (empty submission)
|
|
if ([] !== $previousRentals && [] === $validSelections && [] !== $selectedRentals) {
|
|
$participant->addNotification(
|
|
'warning',
|
|
'Ausrüstung wurde entfernt (andere Skipass-Dauer ausgewählt)'
|
|
);
|
|
}
|
|
|
|
$participant->rentals = $validSelections;
|
|
}
|
|
|
|
/**
|
|
* @param array<int, Service> $availableRentals
|
|
* @param list<Service> $validSelections
|
|
*/
|
|
private function updateAutoBookOptOutRentals(
|
|
ParticipantDto $participant,
|
|
array $availableRentals,
|
|
array $validSelections,
|
|
BookingDto $bookingDto,
|
|
int $participantIndex,
|
|
): void {
|
|
$currentlySelectedAutoBookIds = [];
|
|
foreach ($participant->rentals as $selectedService) {
|
|
if (null === $selectedService->id || true === $selectedService->mandatory || false === $selectedService->autoBook) {
|
|
continue;
|
|
}
|
|
|
|
if (false === $this->isServiceValidForParticipant($selectedService, $availableRentals, $bookingDto, $participantIndex)) {
|
|
continue;
|
|
}
|
|
|
|
$currentlySelectedAutoBookIds[] = $selectedService->id;
|
|
}
|
|
|
|
$newlySelectedAutoBookIds = [];
|
|
foreach ($validSelections as $selectedService) {
|
|
if (null === $selectedService->id || true === $selectedService->mandatory || false === $selectedService->autoBook) {
|
|
continue;
|
|
}
|
|
|
|
$newlySelectedAutoBookIds[] = $selectedService->id;
|
|
}
|
|
|
|
foreach ($currentlySelectedAutoBookIds as $serviceId) {
|
|
if (false === in_array($serviceId, $newlySelectedAutoBookIds, true)
|
|
&& false === in_array($serviceId, $participant->autoBookOptOutRentalIds, true)) {
|
|
$participant->autoBookOptOutRentalIds[] = $serviceId;
|
|
}
|
|
}
|
|
|
|
$participant->autoBookOptOutRentalIds = array_values(array_filter(
|
|
$participant->autoBookOptOutRentalIds,
|
|
static fn (int $serviceId): bool => false === in_array($serviceId, $newlySelectedAutoBookIds, true)
|
|
));
|
|
}
|
|
|
|
/**
|
|
* @param list<mixed> $selectedServices
|
|
* @param array<int, Service> $availableServices
|
|
*
|
|
* @return list<Service>
|
|
*/
|
|
private function filterValidServiceSelections(
|
|
array $selectedServices,
|
|
array $availableServices,
|
|
BookingDto $bookingDto,
|
|
int $participantIndex,
|
|
): array {
|
|
$validSelections = [];
|
|
|
|
foreach ($selectedServices as $selectedService) {
|
|
if ($this->isServiceValidForParticipant($selectedService, $availableServices, $bookingDto, $participantIndex)) {
|
|
// Convert the selected service ID/data back to the actual Service object
|
|
$serviceObject = $this->findServiceInAvailableServices($selectedService, $availableServices);
|
|
if (null !== $serviceObject) {
|
|
$validSelections[] = $serviceObject;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $validSelections;
|
|
}
|
|
|
|
/**
|
|
* @param array<int, Service> $availableServices
|
|
*/
|
|
private function isServiceValidForParticipant(
|
|
mixed $selectedService,
|
|
array $availableServices,
|
|
BookingDto $bookingDto,
|
|
int $participantIndex,
|
|
): bool {
|
|
$service = $this->findServiceInAvailableServices($selectedService, $availableServices);
|
|
|
|
if (null === $service) {
|
|
return false;
|
|
}
|
|
|
|
$ageEvaluator = new ServiceAgeEvaluator();
|
|
if (!$ageEvaluator->canEvaluate($service)) {
|
|
return true;
|
|
}
|
|
|
|
return $ageEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex);
|
|
}
|
|
|
|
/**
|
|
* Filters rentals to only include those matching the skipass duration.
|
|
*
|
|
* Rentals must have exact date matching with the selected skipass:
|
|
* - rental.dateFrom === skipass.dateFrom
|
|
* - rental.dateTo === skipass.dateTo
|
|
*
|
|
* @param array<int, Service> $rentals All available rental services
|
|
* @param ParticipantDto $participant The participant with skipass selection
|
|
*
|
|
* @return array<int, Service> Filtered rentals matching the skipass duration
|
|
*/
|
|
private function filterRentalsBySkiPassDuration(array $rentals, ParticipantDto $participant): array
|
|
{
|
|
$selectedSkiPass = $participant->skiPass;
|
|
|
|
// If skipass has no valid dates, return empty rentals
|
|
if (null === $selectedSkiPass->dateFrom || null === $selectedSkiPass->dateTo) {
|
|
return [];
|
|
}
|
|
|
|
return array_filter($rentals, function (Service $rental) use ($selectedSkiPass) {
|
|
// Rental must have valid dates to be considered
|
|
if (null === $rental->dateFrom || null === $rental->dateTo) {
|
|
return false;
|
|
}
|
|
|
|
// Exact date matching: rental dates must match skipass dates exactly
|
|
return $rental->dateFrom == $selectedSkiPass->dateFrom
|
|
&& $rental->dateTo == $selectedSkiPass->dateTo;
|
|
});
|
|
}
|
|
}
|