Files
wk-sync-bundle/src/Controller/FrontendModule/BookingFormController.php
T
admin.juergenandClaude Fable 5.1 142afa6e52 Initial version of the W&K sync bundle for Contao 5.7
Import of W&K seminar exports with preview, booking form via form generator
with event-dependent field rules, notification routing, inbox with folders
and CSV export for W&K.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-24 17:42:34 +02:00

89 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Mummert\ContaoWkSyncBundle\Controller\FrontendModule;
use Contao\Controller;
use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
use Contao\CoreBundle\DependencyInjection\Attribute\AsFrontendModule;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Twig\FragmentTemplate;
use Contao\ModuleModel;
use Contao\StringUtil;
use Mummert\ContaoWkSyncBundle\Booking\BookingAvailability;
use Mummert\ContaoWkSyncBundle\Booking\BookingContext;
use Mummert\ContaoWkSyncBundle\Booking\BookingRules;
use Mummert\ContaoWkSyncBundle\Booking\EventResolver;
use Mummert\ContaoWkSyncBundle\Category\CategoryRepository;
use Mummert\ContaoWkSyncBundle\Sync\CalendarConfigRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Anmeldeformular zu einer Veranstaltung (Seite mit auto_item = Alias der Veranstaltung).
*/
#[AsFrontendModule(self::TYPE, category: 'wk_sync', template: 'frontend_module/wk_booking_form')]
class BookingFormController extends AbstractFrontendModuleController
{
public const string TYPE = 'wk_booking_form';
public function __construct(
private readonly EventResolver $events,
private readonly CalendarConfigRepository $calendars,
private readonly BookingAvailability $availability,
private readonly BookingRules $rules,
private readonly CategoryRepository $categories,
) {
}
protected function getResponse(FragmentTemplate $template, ModuleModel $model, Request $request): Response
{
$calendarIds = array_map(intval(...), StringUtil::deserialize($model->cal_calendar, true));
$event = $this->events->resolveFromRequest($request, $calendarIds);
if (null === $event || (!$event->published && !$request->attributes->get('_preview', false))) {
throw new PageNotFoundException('Event not found: '.($request->attributes->get('auto_item') ?? ''));
}
$calendar = $this->calendars->find($event->calendarId);
if (null === $calendar) {
throw new PageNotFoundException('Calendar not found for event ID '.$event->id);
}
$availability = $this->availability->check($event, $calendar);
$rules = $this->rules->rulesFor($event, $calendar);
$context = new BookingContext($event, $calendar, $rules, $availability);
$context->attachTo($request);
$formHtml = '';
if ($availability->isBookable()) {
// Die Hooks des Formulargenerators greifen über den BookingContext auf die Veranstaltung zu
$formHtml = $this->getContaoAdapter(Controller::class)->getForm($calendar->bookingForm);
}
$template->set('event', $event);
$template->set('calendar', $calendar);
$template->set('availability', $availability);
$template->set('form_html', $formHtml);
$template->set('places_left', $availability->placesLeft);
$template->set('deadline', $availability->displayDeadline);
$template->set('categories', $this->categories->findByIds($event->categories));
$template->set('rules_config', json_encode([
'hidden' => $rules->hiddenFields,
'dependencies' => $rules->dependencies,
], JSON_THROW_ON_ERROR));
$this->tagResponse(['contao.db.tl_calendar_events.'.$event->id, 'contao.db.tl_wk_booking']);
$response = $template->getResponse();
$response->setPrivate();
$response->headers->addCacheControlDirective('no-store');
return $response;
}
}