src/Core/Controller/Admin/TournamentAdminController.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Core\Controller\Admin;
  3. use AndreaSprega\Bundle\BreadcrumbBundle\Annotation\Breadcrumb;
  4. use App\Core\Entity\Tournament;
  5. use App\Core\Form\Type\TournamentFromType;
  6. use App\Core\Repository\CompetitionMatchRepository;
  7. use App\Core\Repository\TournamentRepository;
  8. use App\Core\Service\PluginFinder;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/{_locale}/admin/tournament", name="app_admin_tournament_")
  15.  */
  16. class TournamentAdminController extends AbstractAdminController
  17. {
  18.     private EntityManagerInterface $entityManager;
  19.     private TournamentRepository $tournamentRepository;
  20.     private PluginFinder $pluginFinder;
  21.     private CompetitionMatchRepository $competitionMatchRepository;
  22.     public function __construct(
  23.         EntityManagerInterface $entityManager,
  24.         TournamentRepository $tournamentRepository,
  25.         PluginFinder $pluginFinder,
  26.         CompetitionMatchRepository $competitionMatchRepository
  27.     ) {
  28.         $this->entityManager $entityManager;
  29.         $this->tournamentRepository $tournamentRepository;
  30.         $this->pluginFinder $pluginFinder;
  31.         $this->competitionMatchRepository $competitionMatchRepository;
  32.     }
  33.     /**
  34.      * @Route("/list", name="list", methods={"GET"})
  35.      * @Breadcrumb({
  36.      *   { "label" = "Home", "route" = "app_admin_home" },
  37.      *   { "label" = "Tournaments list" }
  38.      * })
  39.      *
  40.      * @param Request $request
  41.      * @return Response
  42.      */
  43.     public function index(Request $request): Response
  44.     {
  45.         $tournaments $this->tournamentRepository->findAll();
  46.         return $this->render(
  47.             'admin/Tournament/list.html.twig',
  48.             [
  49.                 'user' => $this->getUser(),
  50.                 'tournament_list' => $tournaments,
  51.                 'action_plugin_templates' => $this->pluginFinder->getTemplates('/admin/Tournament/plugins/actions'),
  52.             ]
  53.         );
  54.     }
  55.     /**
  56.      * @Route("/{id}/view", name="view", methods={"GET"})
  57.      * @Breadcrumb({
  58.      *   { "label" = "Home", "route" = "app_admin_home" },
  59.      *   { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
  60.      * })
  61.      *
  62.      * @param Request $request
  63.      * @param Tournament $tournament
  64.      * @return Response
  65.      */
  66.     public function view(Request $requestTournament $tournament): Response
  67.     {
  68.         try {
  69.             $activeMatches $this->competitionMatchRepository->findActiveByTournament($tournament);
  70.         } catch (\Exception $exception) {
  71.             $activeMatches = [];
  72.         }
  73.         return $this->render(
  74.             'admin/Tournament/view.html.twig',
  75.             [
  76.                 'user' => $this->getUser(),
  77.                 'tournament' => $tournament,
  78.                 'active_matches' => $activeMatches,
  79.                 'action_plugin_templates' => $this->pluginFinder->getTemplates('/admin/Tournament/plugins/actions'),
  80.             ]
  81.         );
  82.     }
  83.     /**
  84.      * @Route("/add", name="add", methods={"GET", "POST"})
  85.      * @Breadcrumb({
  86.      *   { "label" = "Home", "route" = "app_admin_home" },
  87.      *   { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
  88.      * })
  89.      *
  90.      * @param Request $request
  91.      * @return Response
  92.      */
  93.     public function add(Request $request): Response
  94.     {
  95.         $tournament = new Tournament();
  96.         $form $this->createForm(TournamentFromType::class, $tournament);
  97.         $form->handleRequest($request);
  98.         $addProcessResponse $this->processTournament($form);
  99.         if ($addProcessResponse !== null) {
  100.             return $addProcessResponse;
  101.         }
  102.         return $this->render(
  103.             'admin/Tournament/add.html.twig',
  104.             [
  105.                 'form' => $form->createView(),
  106.             ]
  107.         );
  108.     }
  109.     /**
  110.      * @Route("/{id}/delete", name="delete", methods={"GET"})
  111.      *
  112.      * @param Request $request
  113.      * @param Tournament $tournament
  114.      * @return Response
  115.      */
  116.     public function delete(Request $requestTournament $tournament): Response
  117.     {
  118.         try {
  119.             $this->entityManager->remove($tournament);
  120.             $this->entityManager->flush();
  121.         } catch (\Exception $exception) {
  122.             $this->addFlash(
  123.                 'error',
  124.                 "Unable to delete tournament!!! Contact admins."
  125.             );
  126.         }
  127.         return $this->redirectToRoute('app_admin_tournament_list');
  128.     }
  129.     /**
  130.      * @Route("/{id}/archive", name="archive", methods={"GET"})
  131.      *
  132.      * @param Request $request
  133.      * @param Tournament $tournament
  134.      * @return Response
  135.      */
  136.     public function archive(Request $requestTournament $tournament): Response
  137.     {
  138.         try {
  139.             $tournament->setIsActive(false);
  140.             $this->entityManager->flush();
  141.         } catch (\Exception $exception) {
  142.             $this->addFlash(
  143.                 'error',
  144.                 "Unable to archive tournament!!! Contact admins."
  145.             );
  146.         }
  147.         return $this->redirectToRoute('app_admin_tournament_list');
  148.     }
  149.     /**
  150.      * @Route("/{id}/unarchive", name="unarchive", methods={"GET"})
  151.      *
  152.      * @param Request $request
  153.      * @param Tournament $tournament
  154.      * @return Response
  155.      */
  156.     public function unarchive(Request $requestTournament $tournament): Response
  157.     {
  158.         try {
  159.             $tournament->setIsActive(true);
  160.             $this->entityManager->flush();
  161.         } catch (\Exception $exception) {
  162.             $this->addFlash(
  163.                 'error',
  164.                 "Unable to unarchive tournament!!! Contact admins."
  165.             );
  166.         }
  167.         return $this->redirectToRoute('app_admin_tournament_list');
  168.     }
  169.     /**
  170.      * @Route("/{id}/edit", name="edit", methods={"GET", "POST"})
  171.      * @Breadcrumb({
  172.      *   { "label" = "Home", "route" = "app_admin_home" },
  173.      *   { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
  174.      * })
  175.      *
  176.      * @param Request $request
  177.      * @param Tournament $tournament
  178.      * @return Response
  179.      */
  180.     public function edit(Request $requestTournament $tournament): Response
  181.     {
  182.         $redirectIfEditIsDisabled $this->disableEditIfTournamentActive($tournament);
  183.         if ($redirectIfEditIsDisabled) {
  184.             return $redirectIfEditIsDisabled;
  185.         }
  186.         $form $this->createForm(TournamentFromType::class, $tournament);
  187.         $form->handleRequest($request);
  188.         $editProcessResponse $this->processTournament($form);
  189.         if ($editProcessResponse !== null) {
  190.             return $editProcessResponse;
  191.         }
  192.         return $this->render(
  193.             'admin/Tournament/edit.html.twig',
  194.             [
  195.                 'form' => $form->createView(),
  196.             ]
  197.         );
  198.     }
  199.     private function processTournament($form): ?Response
  200.     {
  201.         if ($form->isSubmitted() && $form->isValid()) {
  202.             /** @var Tournament $tournament */
  203.             $tournament $form->getData();
  204.             $this->entityManager->persist($tournament);
  205.             $this->entityManager->flush();
  206.             $this->addFlash(
  207.                 'notice',
  208.                 "Your have saved a tournament: {$tournament->getId()} {$tournament->getName()}"
  209.             );
  210.             return $this->redirectToRoute('app_admin_tournament_list');
  211.         }
  212.         return null;
  213.     }
  214. }