src/Controller/Admin/TournamentController.php line 28
<?php
namespace App\Controller\Admin;
use App\Entity\Club;
use App\Entity\ClubNameHistory;
use App\Entity\Season;
use App\Entity\Tournament;
use App\Entity\TournamentClub;
use App\Entity\TournamentStage;
use App\Entity\TournamentType;
use App\Services\ChangeListServices;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
class TournamentController extends AbstractController
{
public EntityManagerInterface $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
public function index(Request $request)
{
$tournaments = $this->em->getRepository(Tournament::class)->findAll();
return $this->render('Admin/Tournament/index.html.twig', [
'tournaments' => $tournaments
]);
}
public function add(Request $request, ChangeListServices $changeListServices)
{
$data = $request->request->all();
$files = $request->files->all();
if (isset($data['name'])){
try{
$dateStart = new \DateTime($data['date_start']);
$dateEnd = new \DateTime($data['date_end']);
}catch(\Exception){
$dateStart = new \DateTime("now");
$dateEnd = new \DateTime("now");
}
$tournament = new Tournament();
$tournament->setName($data['name']);
$tournament->setStartDate($dateStart);
$tournament->setEndDate($dateEnd);
$tournament->setLogo('');
if(isset($data['season']) and $data['season']>0 ){
$season = $this->em->getRepository(Season::class)->find($data['season']);
$tournament->setSeason($season);
}
if(isset($data['tournamentTypes'])){
$tournament->setTournamentType($data['tournamentTypes']);
}
if(isset($data['format']) and $data['format']){
$tournament->setFormat(true);
}else{
$tournament->setFormat(false);
}
if(isset($data['api_id'])){
$tournament->setApiId($data['api_id']);
}
$this->em->persist($tournament);
$this->em->flush();
if ($files){
// Удалим старый файл
if($tournament->getLogo() != ""){
unlink($_SERVER['DOCUMENT_ROOT'].$tournament->getLogo());
}
$time = new \DateTime("now");
$filePath = 'upload';
foreach ($files as $file){
$fileName = 'tournament' . $tournament->getId() .$time->getTimestamp(). '.' . $file->guessExtension();
$file->move($filePath, $fileName);
$url = '/'.$filePath.'/'.$fileName;
$tournament->setLogo($url);
}
$this->em->persist($tournament);
$this->em->flush();
}
$tournamentId = $tournament->getId();
$changeListServices->add($tournamentId, ChangeListServices::add, 'Tournament');
return $this->redirectToRoute('admin_tournament_edit', ['id' => $tournament->getId()]);
}
$seasons = $this->em->getRepository(Season::class)->findAll();
$types = $this->em->getRepository(TournamentType::class)->findAll();
return $this->render('Admin/Tournament/add.html.twig', [
'seasons' => $seasons,
'tournamentTypes' => $types
]);
}
public function edit($id, Request $request, ChangeListServices $changeListServices)
{
$clubsName = [];
$clubsIds = [];
$data = $request->request->all();
$files = $request->files->all();
$tournament = $this->em->getRepository(Tournament::class)->find($id);
if (!empty($tournament) and isset($data['name'])){
try{
$dateStart = new \DateTime($data['date_start']);
$dateEnd = new \DateTime($data['date_end']);
}catch(\Exception){
$dateStart = new \DateTime("now");
$dateEnd = new \DateTime("now");
}
$tournament->setName($data['name']);
$tournament->setStartDate($dateStart);
$tournament->setEndDate($dateEnd);
if (isset($data['season']) and $data['season'] > 0){
$season = $this->em->getRepository(Season::class)->find($data['season']);
if(!empty($season)){
$tournament->setSeason($season);
}
}
if(isset($data['tournamentTypes'])){
$tournament->setTournamentType($data['tournamentTypes']);
}
if(isset($data['api_id'])){
$tournament->setApiId($data['api_id']);
}
if(isset($data['format']) and $data['format']){
$tournament->setFormat(true);
}else{
$tournament->setFormat(false);
}
$this->em->persist($tournament);
$this->em->flush();
if(isset($files['logo_file'])){
// Удалим старый файл
if($tournament->getLogo() != ""){
unlink($_SERVER['DOCUMENT_ROOT'].$tournament->getLogo());
}
$time = new \DateTime("now");
$filePath = 'upload';
$fileName = 'tournament' . $tournament->getId() .$time->getTimestamp(). '.' . $files['logo_file']->guessExtension();
$files['logo_file']->move($filePath, $fileName);
$url = '/'.$filePath.'/'.$fileName;
$tournament->setLogo($url);
$this->em->persist($tournament);
$this->em->flush();
}
$changeListServices->add($tournament->getId(), ChangeListServices::update, 'Tournament');
return $this->redirectToRoute('admin_tournament_edit', ['id' => $tournament->getId()]);
}
$tournament = $this->em->getRepository(Tournament::class)->find($id);
$clubs = $this->em->getRepository(TournamentClub::class)->findBy(["tournament"=>$tournament->getId()]);
foreach($clubs as $club){
$clubsIds[] = $club->getClub()->getId();
}
$names = $this->em->getRepository(ClubNameHistory::class)->findAllNameByDate($tournament->getStartDate());
foreach($names as $name){
$clubsName[$name->getClubId()] = [
'logo'=>$name->getLogo(),
'name'=>$name->getClubName(),
];
}
$allClubs = $this->em->getRepository(Club::class)->findAll();
$types = $this->em->getRepository(TournamentType::class)->findAll();
$seasons = $this->em->getRepository(Season::class)->findAll();
$stages = $this->em->getRepository(TournamentStage::class)->findBy(array("tournament"=>$tournament->getId()));
return $this->render('Admin/Tournament/edit.html.twig', [
'tournament' => $tournament,
'clubs' => $clubs,
'stages' => $stages,
'allClubs' => $allClubs,
'seasons' => $seasons,
'tournamentTypes' => $types,
'clubsIds' => $clubsIds,
'name'=>$clubsName,
]);
}
public function del($id, ChangeListServices $changeListServices){
$item = $this->em->getRepository(Tournament::class)->find($id);
$changeListServices->add($item->getId(), ChangeListServices::del, 'Tournament');
$this->em->remove($item);
$this->em->flush();
return $this->redirectToRoute('admin_index', []);
}
}