src/Jockeyexchange/Controller/JockeyexchangeController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Jockeyexchange\Controller;
  3. use App\Calendar\Calendar;
  4. use App\Calendar\Form\Type\CalendarFilterType;
  5. use App\EventMap\EventMap;
  6. use App\Jockeyexchange\Event\JockeyexchangeRacedayAttendEvent;
  7. use App\Jockeyexchange\Event\JockeyexchangeRacedayAttendEvents;
  8. use App\Jockeyexchange\Form\Type\AttendRacedayType;
  9. use App\Jockeyexchange\Form\Type\AttendRaceType;
  10. use App\Jockeyexchange\Service\JockeyexchangeService;
  11. use App\Profile\Model\ProfileRoles;
  12. use App\Profile\Service\ProfileService;
  13. use Carbon\CarbonImmutable;
  14. use Pimcore\Controller\FrontendController;
  15. use Pimcore\Model\DataObject\Race;
  16. use Pimcore\Model\DataObject\Raceday;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\ExpressionLanguage\Expression;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\UX\Turbo\TurboBundle;
  25. #[Route([
  26.   'name' => 'jockeyexchange_',
  27.   'localizedPaths' => [
  28.     'en' => '/{_locale}/jockeyexchange',
  29.     'de' => '/{_locale}/jockeyboerse',
  30.     'fr' => '/{_locale}/echangedejockeys',
  31.   ],
  32. ])]
  33. #[IsGranted(ProfileRoles::ROLE_USER)]
  34. // #[Security("is_granted('ROLE_USER') and (is_granted('ROLE_CLUB_HORSE') or is_granted('ROLE_CLUB_JOCKEY'))")]
  35. class JockeyexchangeController extends FrontendController
  36. {
  37.   #[Route('/'name'index')]
  38.   public function index(Request $requestEventMap $eventMapCalendar $calendarJockeyexchangeService $jockeyexchangeService): Response
  39.   {
  40.     // $this->denyAccessUnlessGranted(new Expression(
  41.     //   '"ROLE_CLUB_HORSE" in role_names or "ROLE_CLUB_JOCKEY" in role_names'
  42.     // ));
  43.     if (!($this->isGranted('ROLE_CLUB_HORSE') || $this->isGranted('ROLE_CLUB_JOCKEY'))) {
  44.       $e $this->createAccessDeniedException('not allowed');
  45.       $e->setAttributes(['ROLE_CLUB_HORSE''ROLE_CLUB_JOCKEY']);
  46.       throw $e;
  47.     }
  48.     $date $request->query->get('date');
  49.     $year $date['year'] ?? date('Y');
  50.     $month $date['month'] ?? date('m');
  51.     $selectedMonth CarbonImmutable::create($year$month);
  52.     $race $request->get('race'null);
  53.     $period CarbonImmutable::create($year$month) > CarbonImmutable::now() ? 'future' 'past';
  54.     $country $race $race['country'] : null;
  55.     $events $jockeyexchangeService->getSelectedRacedays($selectedMonth$country);
  56.     // Setup Calendar
  57.     $calendar->setMonth($selectedMonth);
  58.     $calendar->generateEvents($events);
  59.     $weeks $calendar->getWeeksWithEvents();
  60.     $days $calendar->getDaysWithEvents();
  61.     // Create Filter for Calendar & Map
  62.     $filter $this->createForm(CalendarFilterType::class, [
  63.        'date' => $selectedMonth,
  64.          'topic' => 'racedays',
  65.      ], [
  66.        'method' => 'GET',
  67.        'period' => $period,
  68.        'country' => $country,
  69.      ]);
  70.     // Generate Marker for Events
  71.     $marker $eventMap->generateMarker($events);
  72.     $timelineposts $jockeyexchangeService->getCurrentJockeyRacedayAttendancePosts();
  73.     return $this->renderTemplate('jockeyexchange/index.html.twig', [
  74.         'topic' => 'racedays',
  75.        'month' => $selectedMonth,
  76.        'weeks' => $weeks,
  77.        'days' => $days,
  78.        'filter' => $filter->createView(),
  79.        'marker' => $marker,
  80.        'timelineposts' => $timelineposts,
  81.      ]);
  82.   }
  83.   #[Route('/{id}'name'detail')]
  84.   public function detail($idRequest $requestProfileService $profileServiceEventDispatcherInterface $eventDispatcherJockeyexchangeService $jockeyexchangeService): Response
  85.   {
  86.     $this->denyAccessUnlessGranted(new Expression(
  87.       '"ROLE_CLUB_HORSE" in role_names or "ROLE_CLUB_JOCKEY" in role_names'
  88.     ));
  89.     $raceday Raceday::getById($id);
  90.     $user $this->getUser();
  91.     $races Race::getByRaceday($raceday);
  92.     $form $this->createForm(AttendRacedayType::class, ['attend' => (bool) $profileService->checkIfAttendEvent($user$id), 'racesoptions' => $races->load(), 'profile' => $user]);
  93.     $form->handleRequest($request);
  94.     $racedayAttendees $jockeyexchangeService->getOtherJockeyRacedayAttendees($raceday);
  95.     if ($form->isSubmitted() && $form->isValid()) {
  96.       $data $form->getData();
  97.       $jockeyexchangeService->changeRacedayAttendanceStatus($data['attend'], $data['races'], $profileService->checkIfAttendEvent($user$id), $id$user);
  98.       $jockeyexchangeService->changeRacedayRaceAttendanceStatus($races$data['races'], $user);
  99.       $event = new JockeyexchangeRacedayAttendEvent($raceday$data$user);
  100.       $request->setRequestFormat(TurboBundle::STREAM_FORMAT);
  101.       $eventDispatcher->dispatch($eventJockeyexchangeRacedayAttendEvents::RACEDAY_ATTEND);
  102.       $response = new Response();
  103.       return $response;
  104.     }
  105.     return $this->renderForm('jockeyexchange/detail.html.twig', [
  106.       'raceday' => $raceday,
  107.       'form' => $form,
  108.       'user' => $user,
  109.       'otherRacedayAttendees' => $racedayAttendees,
  110.     ]);
  111.   }
  112.   #[Route('/detail/attend/race/{id}/{key}'name'detail_race_attend')]
  113.   public function raceAttend($id$keyRequest $requestProfileService $profileService): Response
  114.   {
  115.     $this->denyAccessUnlessGranted(new Expression(
  116.       '"ROLE_CLUB_HORSE" in role_names or "ROLE_CLUB_JOCKEY" in role_names'
  117.     ));
  118.     $race Race::getById($id);
  119.     $user $this->getUser();
  120.     $form $this->createForm(AttendRaceType::class, ['attend' => (bool) $profileService->checkIfAttendRace($user$id)]);
  121.     $form->handleRequest($request);
  122.     $data $form->getData();
  123.     if ($form->isSubmitted() && $form->isValid()) {
  124.       if ($data['attend']) {
  125.         $profileService->attendEvent($user$id);
  126.         if (!$profileService->checkIfAttendEvent($user$race->getRaceday()->getId())) {
  127.           $profileService->attendEvent($user$race->getRaceday()->getId());
  128.         }
  129.       } else {
  130.         $profileService->cancelAttendEvent($user$id);
  131.       }
  132.     }
  133.     return $this->renderTemplate('jockeyexchange/race_attend_frame.html.twig', [
  134.       'form' => $form->createView(),
  135.       'race' => $race,
  136.       'key' => $key,
  137.     ]);
  138.   }
  139.   #[Route('detail/attend/raceday/{id}'name'detail_raceday_stream')]
  140.   public function racedayAttend($idRequest $request): Response
  141.   {
  142.     $this->denyAccessUnlessGranted(new Expression(
  143.       '"ROLE_CLUB_HORSE" in role_names or "ROLE_CLUB_JOCKEY" in role_names'
  144.     ));
  145.     $raceday Raceday::getById($id);
  146.     $form $this->createForm(AttendRacedayType::class);
  147.     $form->handleRequest($request);
  148.     if ($form->isSubmitted() && $form->isValid()) {
  149.     }
  150.     // dd($form->isSubmitted());
  151.     return $this->renderTemplate('jockeyexchange/detail.html.twig', [
  152.       'raceday' => $raceday,
  153.       'form' => $form->createView(),
  154.       'user' => $this->getUser(),
  155.     ]);
  156.   }
  157. }