<?php
namespace App\Controller\Front;
use App\Entity\Year;
use App\Form\ContactType;
use App\Model\ContactDTO;
use App\Repository\BlockTitleDescriptionRepository;
use App\Repository\NextRaceRepository;
use App\Repository\RaceRepository;
use App\Repository\YearRepository;
use App\Services\Mail;
use App\Services\Social;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
/**
* @Route("/", name="front_home")
*/
public function home(
BlockTitleDescriptionRepository $blockTitleDescriptionRepository,
YearRepository $yearRepository,
NextRaceRepository $nextRaceRepository,
RaceRepository $raceRepository,
Request $request,
Mail $mail,
Social $social)
{
$contactDTO = new ContactDTO();
$form = $this->createForm(ContactType::class, $contactDTO)->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($request->get('email-mail') && $request->get('email-mail') !== '') {
$this->addFlash('danger', 'Erreur lors de l’envoi du formulaire');
return new RedirectResponse('/#contact');
}
$mail->sendMail($contactDTO);
$this->addFlash('success', 'Message de contact envoyé');
return new RedirectResponse('/#contact');
}
$images = $social->displayInstagram(12);
return $this->render('front/index.html.twig', [
'aboutDescription' => $blockTitleDescriptionRepository->findOneBy(['section' => 'about', 'subSection' => 'description']),
'aboutPresentation' => $blockTitleDescriptionRepository->findOneBy(['section' => 'about', 'subSection' => 'presentation']),
'results' => $yearRepository->findBy(['section' => 'results']),
'galleries' => $yearRepository->findSectionsByDate('gallery', new \DateTime()),
'yearsGallery' => $yearRepository->findSectionsGroupByData('gallery', new \DateTime()),
'sponsor' => $yearRepository->findOneBy(['section' => 'sponsors']),
'nextRace' => $nextRaceRepository->find(1),
'races' => $raceRepository->findAll(),
'form' => $form->createView(),
'instaImages' => $images
]);
}
/**
* @Route("/next-race/{raceId<\d+>}", name="front_next_race")
*/
public function nextRace(int $raceId, RaceRepository $raceRepository)
{
if ($raceId >= 3) {
$nextId = 1;
} else {
$nextId = $raceId+1;
}
$nextRace = $raceRepository->find($nextId);
if (!$nextRace) {
return $this->json([
'status' => 'error'
], Response::HTTP_NOT_FOUND);
}
return $this->json([
'status' => 'ok',
'raceId' => $nextId,
'template' => $this->render('front/html/slider-race.html.twig', ['race' => $nextRace])
], Response::HTTP_OK);
}
/**
* @Route("/to-race/{raceId<\d+>}", name="front_to_race")
*/
public function toRace(int $raceId, RaceRepository $raceRepository)
{
$race = $raceRepository->find($raceId);
$raceId = $race->getId();
if ($raceId >= 3) {
$nextId = 1;
} else {
$nextId = $raceId;
}
return $this->json([
'status' => 'ok',
'raceId' => $nextId,
'template' => $this->render('front/html/slider-race.html.twig', ['race' => $race, 'nextYear' => $race->getYears()->first(), 'previousYear' => $race->getYears()->first()])
], Response::HTTP_OK);
}
/**
* @Route("/race/change-year-race/{id<\d+>}", name="front_change_year_race")
*/
public function changeYearRace(Year $year)
{
return $this->json([
'status' => 'ok',
'template' => $this->render('front/html/year_race_table.html.twig', [
'year' => $year,
'nextYear' => $year,
'previousYear' => $year
])
]);
}
/**
* @Route("/race/change-table-race/{id<\d+>}", name="front_changetable_race")
*/
public function changeTableRace(Year $yearResult)
{
$years = $yearResult->getRace()->getYears();
$previous = null;
$next = null;
$counter = 1;
foreach ($years as $key => $year) {
if ($year->getId() === $yearResult->getId()) {
$counter = $key;
$previous = $years[$key-1];
$next = $years[$key+1];
if ($key === 1) {
$previous = $years[end($years)];
}
}
}
if ($next === null) {
$next = $years[0];
$previous = $years[-1];
}
if ($previous === null) {
$previous = $years[end($years)];
}
return $this->json([
'status' => 'ok',
'template' => $this->render('front/html/year_race_table.html.twig', [
'year' => $yearResult,
'race' => $yearResult->getRace(),
'nextYear' => $next,
'previousYear' => $previous,
'countYears' => count($years)
])
], Response::HTTP_OK);
}
/**
* @Route("/results/change-year/{id}", name="front_results_change_year")
*/
public function resultsYear(Year $year)
{
return $this->json([
'status' => 'ok',
'template' => $this->render('front/html/result_tables.html.twig', [
'year' => $year,
'race' => $year->getRace()
])
], Response::HTTP_OK);
}
/**
* @Route("/gallery/gallery-year/{galleryYear}", name="front_gallery_gallery_year")
*/
public function galleryYear(int $galleryYear, YearRepository $yearRepository)
{
$newDatetime = new \DateTime($galleryYear.'-01-01');
$galleries = $yearRepository->findSectionsByDate('gallery', $newDatetime);
return $this->json([
'status' => 'ok',
'template' => $this->render('front/html/gallery_year.html.twig', ['galleries' => $galleries])
], Response::HTTP_OK);
}
}