<?php
namespace App\Core\Controller\Admin;
use AndreaSprega\Bundle\BreadcrumbBundle\Annotation\Breadcrumb;
use App\Core\Entity\Competition;
use App\Core\Entity\CompetitionMatch;
use App\Core\Entity\Tournament;
use App\Core\Form\Type\CompetitionMatchFromType;
use App\Core\Repository\CompetitionMatchRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/{_locale}/admin/tournament/{tournament}/competition/{competition}/match", name="app_admin_competition_match_")
*/
class CompetitionMatchAdminController extends AbstractAdminController
{
private CompetitionMatchRepository $competitionMatchRepository;
private EntityManagerInterface $entityManager;
public function __construct(
CompetitionMatchRepository $competitionMatchRepository,
EntityManagerInterface $entityManager
) {
$this->competitionMatchRepository = $competitionMatchRepository;
$this->entityManager = $entityManager;
}
/**
* @Route("/list", name="list", methods={"GET"})
* @Breadcrumb({
* { "label" = "Home", "route" = "app_admin_home" },
* { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
* { "label" = "$tournament.name" },
* { "label" = "Competitions list", "route" = "app_admin_competition_list", "params" = {"tournament" = "$tournament.id"} },
* { "label" = "$competition.name" },
* { "label" = "Matches list" },
* })
*
* @param Request $request
* @param Tournament $tournament
* @param Competition $competition
* @return Response
*/
public function index(Request $request, Tournament $tournament, Competition $competition): Response
{
//TODO: fetch all competitions - show a list. Include action buttons for easier access. Think of CRUD actions.
$matchList = $this->competitionMatchRepository->findBy(['competition' => $competition, 'isLaunched' => 1]);
//TODO: what if competition is null
return $this->render(
'admin/CompetitionMatch/list.html.twig',
[
'user' => $this->getUser(),
'match_list' => $matchList,
'competition' => $competition,
'tournament' => $tournament,
]
);
}
/**
* @Route("/{match}/delete", name="delete", methods={"GET"})
*
* @param Request $request
* @param Tournament $tournament
* @param Competition $competition
* @param CompetitionMatch $match
* @return Response
*/
public function delete(
Request $request,
Tournament $tournament,
Competition $competition,
CompetitionMatch $match
): Response {
$redirectIfEditIsDisabled = $this->disableEditIfTournamentActive($tournament);
if ($redirectIfEditIsDisabled) {
return $redirectIfEditIsDisabled;
}
$this->entityManager->remove($match);
$this->entityManager->flush();
return $this->redirectToRoute(
'app_admin_competition_match_list',
['tournament' => $tournament->getId(), 'competition' => $competition->getId()]
);
}
/**
* @Route("/{match}/edit", name="edit", methods={"GET", "POST"})
* @Breadcrumb({
* { "label" = "Home", "route" = "app_admin_home" },
* { "label" = "Tournaments list", "route" = "app_admin_tournament_list" },
* { "label" = "$tournament.name" },
* { "label" = "Competitions list", "route" = "app_admin_competition_list", "params" = {"tournament" = "$tournament.id"} },
* { "label" = "$competition.name" },
* { "label" = "Matches list", "route" = "app_admin_competition_match_list", "params" = {"tournament" = "$tournament.id", "competition" = "$competition.id"} },
* { "label" = "Edit team" }
* })
*
* @param Request $request
* @param Tournament $tournament
* @param Competition $competition
* @param CompetitionMatch $match
* @return Response
*/
public function edit(
Request $request,
Tournament $tournament,
Competition $competition,
CompetitionMatch $match
): Response {
$redirectIfEditIsDisabled = $this->disableEditIfTournamentActive($tournament);
if ($redirectIfEditIsDisabled) {
return $redirectIfEditIsDisabled;
}
$form = $this->createForm(CompetitionMatchFromType::class, $match);
$form->handleRequest($request);
$editProcessResponse = $this->processCompetitionMatch($form);
if ($editProcessResponse !== null) {
return $editProcessResponse;
}
return $this->render(
'admin/CompetitionMatch/edit.html.twig',
[
'form' => $form->createView(),
'match' => $match,
'tournament' => $competition->getTournament(),
'competition' => $competition,
]
);
}
private function processCompetitionMatch(FormInterface $form): ?Response
{
if ($form->isSubmitted() && $form->isValid()) {
/** @var CompetitionMatch $match */
$match = $form->getData();
$this->entityManager->persist($match);
$this->entityManager->flush();
$this->addFlash(
'notice',
"Your have saved a competition match: {$match->getId()} {$match->getDisplayName()}"
);
return $this->redirectToRoute(
'app_admin_competition_match_list',
[
'tournament' => $match->getCompetition()->getTournament()->getId(),
'competition' => $match->getCompetition()->getId(),
]
);
}
return null;
}
}