src/Controller/Admin/TournamentController.php line 28

  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Club;
  4. use App\Entity\ClubNameHistory;
  5. use App\Entity\Season;
  6. use App\Entity\Tournament;
  7. use App\Entity\TournamentClub;
  8. use App\Entity\TournamentStage;
  9. use App\Entity\TournamentType;
  10. use App\Services\ChangeListServices;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. class TournamentController extends AbstractController
  15. {
  16.     public EntityManagerInterface $em;
  17.     public function __construct(EntityManagerInterface $entityManager)
  18.     {
  19.         $this->em $entityManager;
  20.     }
  21.     
  22.     public function index(Request $request)
  23.     {
  24.         $tournaments $this->em->getRepository(Tournament::class)->findAll();
  25.         return $this->render('Admin/Tournament/index.html.twig', [
  26.             'tournaments' => $tournaments
  27.         ]);
  28.     }
  29.     public function add(Request $requestChangeListServices $changeListServices)
  30.     {
  31.         $data $request->request->all();
  32.         $files $request->files->all();
  33.         if (isset($data['name'])){
  34.             try{
  35.                 $dateStart = new \DateTime($data['date_start']);
  36.                 $dateEnd = new \DateTime($data['date_end']);
  37.             }catch(\Exception){
  38.                 $dateStart = new \DateTime("now");
  39.                 $dateEnd = new \DateTime("now");
  40.             }
  41.             $tournament = new Tournament();
  42.             $tournament->setName($data['name']);
  43.             $tournament->setStartDate($dateStart);
  44.             $tournament->setEndDate($dateEnd);
  45.             $tournament->setLogo('');
  46.             if(isset($data['season']) and $data['season']>){
  47.                 $season $this->em->getRepository(Season::class)->find($data['season']);
  48.                 $tournament->setSeason($season);
  49.             }
  50.             if(isset($data['tournamentTypes'])){
  51.                 $tournament->setTournamentType($data['tournamentTypes']);
  52.             }
  53.             if(isset($data['format']) and $data['format']){
  54.                 $tournament->setFormat(true);
  55.             }else{
  56.                 $tournament->setFormat(false);
  57.             }
  58.             if(isset($data['api_id'])){
  59.                 $tournament->setApiId($data['api_id']);
  60.             }
  61.             $this->em->persist($tournament);
  62.             $this->em->flush();
  63.             if ($files){
  64.                 // Удалим старый файл
  65.                 if($tournament->getLogo() != ""){
  66.                     unlink($_SERVER['DOCUMENT_ROOT'].$tournament->getLogo());
  67.                 }
  68.                 $time     = new \DateTime("now");
  69.                 $filePath 'upload';
  70.                 foreach ($files as $file){
  71.                     $fileName 'tournament' $tournament->getId() .$time->getTimestamp(). '.' $file->guessExtension();
  72.                     $file->move($filePath$fileName);
  73.                     $url '/'.$filePath.'/'.$fileName;
  74.                     $tournament->setLogo($url);
  75.                 }
  76.                 $this->em->persist($tournament);
  77.                 $this->em->flush();
  78.             }
  79.             $tournamentId $tournament->getId();
  80.             $changeListServices->add($tournamentIdChangeListServices::add'Tournament');
  81.             return $this->redirectToRoute('admin_tournament_edit', ['id' => $tournament->getId()]);
  82.         }
  83.         $seasons $this->em->getRepository(Season::class)->findAll();
  84.         $types $this->em->getRepository(TournamentType::class)->findAll();
  85.         return $this->render('Admin/Tournament/add.html.twig', [
  86.             'seasons' => $seasons,
  87.             'tournamentTypes' => $types
  88.         ]);
  89.     }
  90.     public function edit($idRequest $requestChangeListServices $changeListServices)
  91.     {
  92.         $clubsName = [];
  93.         $clubsIds  = [];
  94.         $data       $request->request->all();
  95.         $files      $request->files->all();
  96.         $tournament $this->em->getRepository(Tournament::class)->find($id);
  97.         if (!empty($tournament) and isset($data['name'])){
  98.             try{
  99.                 $dateStart = new \DateTime($data['date_start']);
  100.                 $dateEnd = new \DateTime($data['date_end']);
  101.             }catch(\Exception){
  102.                 $dateStart = new \DateTime("now");
  103.                 $dateEnd = new \DateTime("now");
  104.             }
  105.             $tournament->setName($data['name']);
  106.             $tournament->setStartDate($dateStart);
  107.             $tournament->setEndDate($dateEnd);
  108.             if (isset($data['season']) and $data['season'] > 0){
  109.                 $season $this->em->getRepository(Season::class)->find($data['season']);
  110.                 if(!empty($season)){
  111.                     $tournament->setSeason($season);
  112.                 }
  113.             }
  114.             if(isset($data['tournamentTypes'])){
  115.                 $tournament->setTournamentType($data['tournamentTypes']);
  116.             }
  117.             if(isset($data['api_id'])){
  118.                 $tournament->setApiId($data['api_id']);
  119.             }
  120.             if(isset($data['format']) and $data['format']){
  121.                 $tournament->setFormat(true);
  122.             }else{
  123.                 $tournament->setFormat(false);
  124.             }
  125.             $this->em->persist($tournament);
  126.             $this->em->flush();
  127.             if(isset($files['logo_file'])){
  128.                 // Удалим старый файл
  129.                 if($tournament->getLogo() != ""){
  130.                     unlink($_SERVER['DOCUMENT_ROOT'].$tournament->getLogo());
  131.                 }
  132.                 $time     = new \DateTime("now");
  133.                 $filePath 'upload';
  134.                 $fileName 'tournament' $tournament->getId() .$time->getTimestamp(). '.' $files['logo_file']->guessExtension();
  135.                 $files['logo_file']->move($filePath$fileName);
  136.                 $url '/'.$filePath.'/'.$fileName;
  137.                 $tournament->setLogo($url);
  138.                 $this->em->persist($tournament);
  139.                 $this->em->flush();
  140.             }
  141.             $changeListServices->add($tournament->getId(), ChangeListServices::update'Tournament');
  142.             return $this->redirectToRoute('admin_tournament_edit', ['id' => $tournament->getId()]);
  143.         }
  144.         $tournament $this->em->getRepository(Tournament::class)->find($id);
  145.         $clubs      $this->em->getRepository(TournamentClub::class)->findBy(["tournament"=>$tournament->getId()]);
  146.         foreach($clubs as $club){
  147.             $clubsIds[] = $club->getClub()->getId();
  148.         }
  149.         $names $this->em->getRepository(ClubNameHistory::class)->findAllNameByDate($tournament->getStartDate());
  150.         foreach($names as $name){
  151.             $clubsName[$name->getClubId()] = [
  152.                 'logo'=>$name->getLogo(),
  153.                 'name'=>$name->getClubName(),
  154.             ];
  155.         }
  156.         $allClubs $this->em->getRepository(Club::class)->findAll();
  157.         $types    $this->em->getRepository(TournamentType::class)->findAll();
  158.         $seasons  $this->em->getRepository(Season::class)->findAll();
  159.         $stages   $this->em->getRepository(TournamentStage::class)->findBy(array("tournament"=>$tournament->getId()));
  160.         return $this->render('Admin/Tournament/edit.html.twig', [
  161.             'tournament' => $tournament,
  162.             'clubs' => $clubs,
  163.             'stages' => $stages,
  164.             'allClubs' => $allClubs,
  165.             'seasons' => $seasons,
  166.             'tournamentTypes' => $types,
  167.             'clubsIds' => $clubsIds,
  168.             'name'=>$clubsName,
  169.         ]);
  170.     }
  171.     public function del($idChangeListServices $changeListServices){
  172.         $item $this->em->getRepository(Tournament::class)->find($id);
  173.         $changeListServices->add($item->getId(), ChangeListServices::del'Tournament');
  174.         $this->em->remove($item);
  175.         $this->em->flush();
  176.         
  177.         return $this->redirectToRoute('admin_index', []);
  178.     }
  179. }