From a07c7ab91a7e416508dc65262eb27370f2ede8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Pileck=C3=BD?= Date: Mon, 27 Nov 2023 17:01:22 +0100 Subject: [PATCH] added nice landing route for event --- src/Application/Route.php | 3 +++ src/Event/EventController.php | 12 +++++++++++- src/Event/EventRepository.php | 5 +++++ src/Middleware/EventInfoMiddleware.php | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Application/Route.php b/src/Application/Route.php index 3a676b36..f3158284 100755 --- a/src/Application/Route.php +++ b/src/Application/Route.php @@ -283,6 +283,9 @@ public function addRoutesInto(App $app): App })->add(AdminsOnlyMiddleware::class)->add(LoggedOnlyMiddleware::class); }); }); + + $app->get($app->getBasePath() . '/{eventSlug}', EventController::class . '::redirectEvent') + ->setName('redirectEvent'); return $app; } diff --git a/src/Event/EventController.php b/src/Event/EventController.php index acafa744..48f4223d 100644 --- a/src/Event/EventController.php +++ b/src/Event/EventController.php @@ -21,6 +21,17 @@ public function list(Response $response): Response ['events' => $this->eventRepository->findActiveNontestEvents()], ); } + + public function redirectEvent(string $eventSlug, Request $request, Response $response): Response + { + $event = $this->eventRepository->findBySlug($eventSlug); + if ($event === null) { + return $this->redirect($request, $response, 'eventList'); + } + + return $this->redirect($request, $response, 'getDashboard', ['eventSlug' => $event->slug]); + } + /* public function createEvent(Request $request, Response $response, array $args) { $params = $request->getParams(); @@ -67,6 +78,5 @@ public function createEvent(Request $request, Response $response, array $args) { $this->flashMessages->warning('Některé údaje nebyly validní - prosím zkus zadání údajů znovu.'); return $response->withRedirect($this->router->urlFor('createEvent')); - // TODO add event-admins (roles table?) }*/ } diff --git a/src/Event/EventRepository.php b/src/Event/EventRepository.php index a8367e93..ae60f919 100755 --- a/src/Event/EventRepository.php +++ b/src/Event/EventRepository.php @@ -40,4 +40,9 @@ public function findActiveNontestAutopaymentsOnEvents(): array 'end_day' => new Relation(DateTimeUtils::getDateTime('-3 month')->format(DATE_ATOM), '>'), ]); } + + public function findBySlug(string $eventSlug): ?Event + { + return $this->findOneBy(['slug' => $eventSlug]); + } } diff --git a/src/Middleware/EventInfoMiddleware.php b/src/Middleware/EventInfoMiddleware.php index a3f6d2b7..295dc955 100755 --- a/src/Middleware/EventInfoMiddleware.php +++ b/src/Middleware/EventInfoMiddleware.php @@ -30,7 +30,7 @@ public function process(Request $request, ResponseHandler $handler): Response $route = $routeContext->getRoute(); $eventSlug = $route?->getArgument('eventSlug') ?? ''; - $event = $this->eventRepository->findOneBy(['slug' => $eventSlug]); + $event = $this->eventRepository->findBySlug($eventSlug); if ($event instanceof Event) { $request = $request->withAttribute('event', $event); $this->view->getEnvironment()->addGlobal('event', $event); // used in templates