src/Core/Controller/Admin/TeamAdminController.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Core\Controller\Admin;
  3. use AndreaSprega\Bundle\BreadcrumbBundle\Annotation\Breadcrumb;
  4. use App\Core\Entity\Competition;
  5. use App\Core\Entity\Team;
  6. use App\Core\Entity\Tournament;
  7. use App\Core\Form\Type\TeamFormType;
  8. use App\Core\Repository\TeamRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("/{_locale}/admin/tournament/{tournament}/competition/{competition}/team", name="app_admin_team_")
  16.  */
  17. class TeamAdminController extends AbstractAdminController
  18. {
  19.     private TeamRepository $teamRepository;
  20.     private EntityManagerInterface $entityManager;
  21.     public function __construct(TeamRepository $teamRepositoryEntityManagerInterface $entityManager)
  22.     {
  23.         $this->teamRepository $teamRepository;
  24.         $this->entityManager $entityManager;
  25.     }
  26.     /**
  27.      * @Route("/list", name="list", methods={"GET"})
  28.      * @Breadcrumb({
  29.      *   { "label" = "Home", "route" = "app_admin_home" },
  30.      *   { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
  31.      *   { "label" = "$tournament.name" },
  32.      *   { "label" = "Competitions list", "route" = "app_admin_competition_list", "params" = {"tournament" = "$tournament.id"} },
  33.      *   { "label" = "$competition.name" },
  34.      *   { "label" = "Teams list" }
  35.      * })
  36.      *
  37.      * @param Request $request
  38.      * @param Tournament $tournament
  39.      * @param Competition $competition
  40.      * @return Response
  41.      */
  42.     public function index(Request $requestTournament $tournamentCompetition $competition): Response
  43.     {
  44.         //TODO: fetch all competitions - show a list. Include action buttons for easier access. Think of CRUD actions.
  45.         $teamList $this->teamRepository->findTeamsByCompetition($competition);
  46.         //TODO: what if competition is null
  47.         return $this->render(
  48.             'admin/Team/list.html.twig',
  49.             [
  50.                 'user' => $this->getUser(),
  51.                 'team_list' => $teamList,
  52.                 'competition' => $competition,
  53.                 'tournament' => $tournament,
  54.             ]
  55.         );
  56.     }
  57.     /**
  58.      * @Route("/add", name="add", methods={"GET", "POST"})
  59.      * @Breadcrumb({
  60.      *   { "label" = "Home", "route" = "app_admin_home" },
  61.      *   { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
  62.      *   { "label" = "$tournament.name" },
  63.      *   { "label" = "Competitions list", "route" = "app_admin_competition_list", "params" = {"tournament" = "$tournament.id"} },
  64.      *   { "label" = "$competition.name" },
  65.      *   { "label" = "Teams list", "route" = "app_admin_team_list", "params" = {"tournament" = "$tournament.id", "competition" = "$competition.id"} },
  66.      *   { "label" = "Add team" }
  67.      * })
  68.      *
  69.      * @param Request $request
  70.      * @param Tournament $tournament
  71.      * @param Competition $competition
  72.      * @return Response
  73.      */
  74.     public function add(Request $requestTournament $tournamentCompetition $competition): Response
  75.     {
  76.         $redirectIfEditIsDisabled $this->disableEditIfTournamentActive($tournament);
  77.         if ($redirectIfEditIsDisabled) {
  78.             return $redirectIfEditIsDisabled;
  79.         }
  80.         $team = new Team();
  81.         $form $this->createForm(TeamFormType::class, $team);
  82.         $form->handleRequest($request);
  83.         $addProcessResponse $this->processTeam($form$competition);
  84.         if ($addProcessResponse !== null) {
  85.             return $addProcessResponse;
  86.         }
  87.         return $this->render(
  88.             'admin/Team/add.html.twig',
  89.             [
  90.                 'form' => $form->createView(),
  91.                 'tournament' => $tournament,
  92.                 'competition' => $competition,
  93.             ]
  94.         );
  95.     }
  96.     /**
  97.      * @Route("/{team}/delete", name="delete", methods={"GET"})
  98.      *
  99.      * @param Request $request
  100.      * @param Tournament $tournament
  101.      * @param Competition $competition
  102.      * @param Team $team
  103.      * @return Response
  104.      */
  105.     public function delete(Request $requestTournament $tournamentCompetition $competitionTeam $team): Response
  106.     {
  107.         $redirectIfEditIsDisabled $this->disableEditIfTournamentActive($tournament);
  108.         if ($redirectIfEditIsDisabled) {
  109.             return $redirectIfEditIsDisabled;
  110.         }
  111.         $this->entityManager->remove($team);
  112.         $this->entityManager->flush();
  113.         return $this->redirectToRoute(
  114.             'app_admin_team_list',
  115.             ['tournament' => $tournament->getId(), 'competition' => $competition->getId()]
  116.         );
  117.     }
  118.     /**
  119.      * @Route("/{team}/edit", name="edit", methods={"GET", "POST"})
  120.      * @Breadcrumb({
  121.      *   { "label" = "Home", "route" = "app_admin_home" },
  122.      *   { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
  123.      *   { "label" = "$tournament.name" },
  124.      *   { "label" = "Competitions list", "route" = "app_admin_competition_list", "params" = {"tournament" = "$tournament.id"} },
  125.      *   { "label" = "$competition.name" },
  126.      *   { "label" = "Teams list", "route" = "app_admin_team_list", "params" = {"tournament" = "$tournament.id", "competition" = "$competition.id"} },
  127.      *   { "label" = "Add team" }
  128.      * })
  129.      *
  130.      * @param Request $request
  131.      * @param Tournament $tournament
  132.      * @param Competition $competition
  133.      * @param Team $team
  134.      * @return Response
  135.      */
  136.     public function edit(Request $requestTournament $tournamentCompetition $competitionTeam $team): Response
  137.     {
  138.         $redirectIfEditIsDisabled $this->disableEditIfTournamentActive($tournament);
  139.         if ($redirectIfEditIsDisabled) {
  140.             return $redirectIfEditIsDisabled;
  141.         }
  142.         $form $this->createForm(TeamFormType::class, $team);
  143.         $form->handleRequest($request);
  144.         $editProcessResponse $this->processTeam($form$competition);
  145.         if ($editProcessResponse !== null) {
  146.             return $editProcessResponse;
  147.         }
  148.         return $this->render(
  149.             'admin/Team/edit.html.twig',
  150.             [
  151.                 'form' => $form->createView(),
  152.                 'tournament' => $tournament,
  153.                 'competition' => $competition,
  154.                 'team' => $team,
  155.             ]
  156.         );
  157.     }
  158.     private function processTeam(FormInterface $formCompetition $competition): ?Response
  159.     {
  160.         if ($form->isSubmitted() && $form->isValid()) {
  161.             /** @var Team $team */
  162.             $team $form->getData();
  163.             $team->addCompetition($competition);
  164.             $this->entityManager->persist($team);
  165.             $this->entityManager->flush();
  166.             $this->addFlash(
  167.                 'notice',
  168.                 "Your have saved a team: {$team->getId()} {$team->getName()}"
  169.             );
  170.             return $this->redirectToRoute(
  171.                 'app_admin_team_list',
  172.                 [
  173.                     'tournament' => $competition->getTournament()->getId(),
  174.                     'competition' => $competition->getId(),
  175.                 ]
  176.             );
  177.         }
  178.         return null;
  179.     }
  180. }