src/Controller/RankingsController.php line 30

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Nation;
  4. use App\Entity\PeopleId;
  5. use App\Form\JoinRankingsType;
  6. use App\Form\RankingsType;
  7. use App\Repository\EventRepository;
  8. use App\Repository\NationRepository;
  9. use App\Repository\PeopleIdRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Bundle\SecurityBundle\Security;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Http\Attribute\IsGranted;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. #[Route('/{_locale}/rankings')]
  20. class RankingsController extends AbstractController
  21. {
  22.     #[Route(
  23.         path'/results/{country}/{event}/{type}',
  24.         name'rankings',
  25. //        defaults: ['country' => 'bzh', 'event' => '333', 'type' => 'average']
  26.     )]
  27.     public function rankings(Request $requestEntityManagerInterface $entityManagerEventRepository $eventRepositoryNationRepository $nationRepositoryFormFactoryInterface $formFactorystring $country nullstring $event nullstring $type null): Response
  28.     {
  29.         $form $formFactory->create(RankingsType::class, null, [
  30.             'country' => $country,
  31.             'event' => $event,
  32.             'type' => $type
  33.         ]);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             $formData $form->getData();
  37.             return $this->redirectToRoute('rankings', ['country' => $formData['country'], 'event' => $formData['event'], 'type' => $formData['type']]);
  38.         }
  39.         if ($country == null) {
  40.             return $this->render('rankings_explanations.html.twig', [
  41.                 'countriesList' => $entityManager->getRepository(Nation::class)->findAll(),
  42.                 'form' => $form
  43.             ]);
  44.         }
  45.         if ($event == null) {
  46.             $event '333';
  47.             if ($type == null) {
  48.                 $type 'single';
  49.             }
  50.             return $this->redirectToRoute('rankings', [
  51.                 'country' => $country,
  52.                 'event' => $event,
  53.                 'type' => $type
  54.             ]);
  55.         }
  56.         $nation $entityManager->getRepository(Nation::class)->findOneBy(['short' => $country]);
  57.         $rankings $nationRepository->getResults($country$event$type);
  58.         return $this->render('rankings.html.twig', [
  59.             'country' => $nation,
  60.             'rankingsEvent' => $eventRepository->find($event),
  61.             'type' => $type,
  62.             'rankings' => $rankings,
  63.             'form' => $form
  64.         ]);
  65.     }
  66.     #[Route(
  67.         path'/join',
  68.         name'join_rankings'
  69.     )]
  70.     #[IsGranted('ROLE_USER')]
  71.     public function askToJoinRankings(Request $requestEntityManagerInterface $entityManagerSecurity $securityPeopleIdRepository $peopleIdRepositoryTranslatorInterface $translatorFormFactoryInterface $formFactory)
  72.     {
  73.         $user $security->getUser();
  74.         $peopleIdCountries $entityManager->getRepository(PeopleId::class)->findBy(['wcaId' => $user->getWcaId()]);
  75.         $countries = [];
  76.         foreach ($peopleIdCountries as $peopleIdCountry) {
  77.             $countries[$translator->trans('rankings.country_names.' $peopleIdCountry->getCountryShort())] = $peopleIdCountry->getCountryShort();
  78.         }
  79.         ksort($countries);
  80.         $form $formFactory->create(JoinRankingsType::class);
  81.         $form->handleRequest($request);
  82.         if ($form->isSubmitted() && $form->isValid()) {
  83.             $maxCountries 10;
  84.             $personCountriesNb count($peopleIdCountries);
  85.             if ($personCountriesNb >= $maxCountries) {
  86.                 $message $translator->trans('join.too_many_countries', ['%person_nb_of_countries%' => $personCountriesNb'%max_nb_of_countries%' => $maxCountries]);
  87.                 $this->addFlash('danger'$message);
  88.             } else {
  89.                 $formData $form->getData();
  90.                 $countryToAdd $formData['country'];
  91.                 $countryName $translator->trans('rankings.country_names.' $countryToAdd);
  92.                 $isCountryAlreadyThere false;
  93.                 foreach ($peopleIdCountries as $peopleIdCountry) {
  94.                     if ($peopleIdCountry->getCountryShort() === $countryToAdd) {
  95.                         $isCountryAlreadyThere true;
  96.                         $message $translator->trans('join.already_here', ['%country%' => $countryName]);
  97.                         $this->addFlash('danger'$message);
  98.                         break;
  99.                     }
  100.                 }
  101.                 if (!$isCountryAlreadyThere) {
  102.                     $peopleId = new PeopleId();
  103.                     $peopleId->setWcaId($user->getWCAId());
  104.                     $peopleId->setCountryCode($countryToAdd);
  105.                     $peopleIdRepository->insertNewPeopleId($peopleIdtrue);
  106.                     $message $translator->trans('join.added', ['%country%' => $countryName]);
  107.                     $this->addFlash('success'$message);
  108.                 }
  109.             }
  110.             return $this->redirectToRoute('join_rankings');
  111.         }
  112.         return $this->render('joinrankings.html.twig', [
  113.             'form' => $form,
  114.             'peopleIdCountries' => $countries
  115.         ]);
  116.     }
  117.     #[Route('/unjoin/{countryShort}'name'unjoin_rankings'methods: ['POST'])]
  118.     #[IsGranted('ROLE_USER')]
  119.     public function deleteFromRankings(Request $request$countryShortEntityManagerInterface $entityManagerSecurity $securityPeopleIdRepository $peopleIdRepository): Response
  120.     {
  121.         $user $security->getUser();
  122.         if ($this->isCsrfTokenValid('delete'.$countryShort$request->request->get('_token'))) {
  123.             $peopleInRanking $peopleIdRepository->findOneBy(['wcaId' => $user->getWcaId(), 'countryShort' => $countryShort]);
  124.             if ($peopleInRanking) {
  125.                 $peopleIdRepository->remove($peopleInRankingtrue);
  126.             }
  127.         }
  128.         return $this->redirectToRoute('join_rankings', [], Response::HTTP_SEE_OTHER);
  129.     }
  130. }