<?php
namespace App\Jockeyexchange\Controller;
use App\Calendar\Calendar;
use App\Calendar\Form\Type\CalendarFilterType;
use App\EventMap\EventMap;
use App\Jockeyexchange\Event\JockeyexchangeRacedayAttendEvent;
use App\Jockeyexchange\Event\JockeyexchangeRacedayAttendEvents;
use App\Jockeyexchange\Form\Type\AttendRacedayType;
use App\Jockeyexchange\Form\Type\AttendRaceType;
use App\Jockeyexchange\Service\JockeyexchangeService;
use App\Profile\Model\ProfileRoles;
use App\Profile\Service\ProfileService;
use Carbon\CarbonImmutable;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Race;
use Pimcore\Model\DataObject\Raceday;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\UX\Turbo\TurboBundle;
#[Route([
'name' => 'jockeyexchange_',
'localizedPaths' => [
'en' => '/{_locale}/jockeyexchange',
'de' => '/{_locale}/jockeyboerse',
'fr' => '/{_locale}/echangedejockeys',
],
])]
#[IsGranted(ProfileRoles::ROLE_USER)]
// #[Security("is_granted('ROLE_USER') and (is_granted('ROLE_CLUB_HORSE') or is_granted('ROLE_CLUB_JOCKEY'))")]
class JockeyexchangeController extends FrontendController
{
#[Route('/', name: 'index')]
public function index(Request $request, EventMap $eventMap, Calendar $calendar, JockeyexchangeService $jockeyexchangeService): Response
{
// $this->denyAccessUnlessGranted(new Expression(
// '"ROLE_CLUB_HORSE" in role_names or "ROLE_CLUB_JOCKEY" in role_names'
// ));
if (!($this->isGranted('ROLE_CLUB_HORSE') || $this->isGranted('ROLE_CLUB_JOCKEY'))) {
$e = $this->createAccessDeniedException('not allowed');
$e->setAttributes(['ROLE_CLUB_HORSE', 'ROLE_CLUB_JOCKEY']);
throw $e;
}
$date = $request->query->get('date');
$year = $date['year'] ?? date('Y');
$month = $date['month'] ?? date('m');
$selectedMonth = CarbonImmutable::create($year, $month);
$race = $request->get('race', null);
$period = CarbonImmutable::create($year, $month) > CarbonImmutable::now() ? 'future' : 'past';
$country = $race ? $race['country'] : null;
$events = $jockeyexchangeService->getSelectedRacedays($selectedMonth, $country);
// Setup Calendar
$calendar->setMonth($selectedMonth);
$calendar->generateEvents($events);
$weeks = $calendar->getWeeksWithEvents();
$days = $calendar->getDaysWithEvents();
// Create Filter for Calendar & Map
$filter = $this->createForm(CalendarFilterType::class, [
'date' => $selectedMonth,
'topic' => 'racedays',
], [
'method' => 'GET',
'period' => $period,
'country' => $country,
]);
// Generate Marker for Events
$marker = $eventMap->generateMarker($events);
$timelineposts = $jockeyexchangeService->getCurrentJockeyRacedayAttendancePosts();
return $this->renderTemplate('jockeyexchange/index.html.twig', [
'topic' => 'racedays',
'month' => $selectedMonth,
'weeks' => $weeks,
'days' => $days,
'filter' => $filter->createView(),
'marker' => $marker,
'timelineposts' => $timelineposts,
]);
}
#[Route('/{id}', name: 'detail')]
public function detail($id, Request $request, ProfileService $profileService, EventDispatcherInterface $eventDispatcher, JockeyexchangeService $jockeyexchangeService): Response
{
$this->denyAccessUnlessGranted(new Expression(
'"ROLE_CLUB_HORSE" in role_names or "ROLE_CLUB_JOCKEY" in role_names'
));
$raceday = Raceday::getById($id);
$user = $this->getUser();
$races = Race::getByRaceday($raceday);
$form = $this->createForm(AttendRacedayType::class, ['attend' => (bool) $profileService->checkIfAttendEvent($user, $id), 'racesoptions' => $races->load(), 'profile' => $user]);
$form->handleRequest($request);
$racedayAttendees = $jockeyexchangeService->getOtherJockeyRacedayAttendees($raceday);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$jockeyexchangeService->changeRacedayAttendanceStatus($data['attend'], $data['races'], $profileService->checkIfAttendEvent($user, $id), $id, $user);
$jockeyexchangeService->changeRacedayRaceAttendanceStatus($races, $data['races'], $user);
$event = new JockeyexchangeRacedayAttendEvent($raceday, $data, $user);
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);
$eventDispatcher->dispatch($event, JockeyexchangeRacedayAttendEvents::RACEDAY_ATTEND);
$response = new Response();
return $response;
}
return $this->renderForm('jockeyexchange/detail.html.twig', [
'raceday' => $raceday,
'form' => $form,
'user' => $user,
'otherRacedayAttendees' => $racedayAttendees,
]);
}
#[Route('/detail/attend/race/{id}/{key}', name: 'detail_race_attend')]
public function raceAttend($id, $key, Request $request, ProfileService $profileService): Response
{
$this->denyAccessUnlessGranted(new Expression(
'"ROLE_CLUB_HORSE" in role_names or "ROLE_CLUB_JOCKEY" in role_names'
));
$race = Race::getById($id);
$user = $this->getUser();
$form = $this->createForm(AttendRaceType::class, ['attend' => (bool) $profileService->checkIfAttendRace($user, $id)]);
$form->handleRequest($request);
$data = $form->getData();
if ($form->isSubmitted() && $form->isValid()) {
if ($data['attend']) {
$profileService->attendEvent($user, $id);
if (!$profileService->checkIfAttendEvent($user, $race->getRaceday()->getId())) {
$profileService->attendEvent($user, $race->getRaceday()->getId());
}
} else {
$profileService->cancelAttendEvent($user, $id);
}
}
return $this->renderTemplate('jockeyexchange/race_attend_frame.html.twig', [
'form' => $form->createView(),
'race' => $race,
'key' => $key,
]);
}
#[Route('detail/attend/raceday/{id}', name: 'detail_raceday_stream')]
public function racedayAttend($id, Request $request): Response
{
$this->denyAccessUnlessGranted(new Expression(
'"ROLE_CLUB_HORSE" in role_names or "ROLE_CLUB_JOCKEY" in role_names'
));
$raceday = Raceday::getById($id);
$form = $this->createForm(AttendRacedayType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
}
// dd($form->isSubmitted());
return $this->renderTemplate('jockeyexchange/detail.html.twig', [
'raceday' => $raceday,
'form' => $form->createView(),
'user' => $this->getUser(),
]);
}
}