src/Controller/Front/HomeController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Year;
  4. use App\Form\ContactType;
  5. use App\Model\ContactDTO;
  6. use App\Repository\BlockTitleDescriptionRepository;
  7. use App\Repository\NextRaceRepository;
  8. use App\Repository\RaceRepository;
  9. use App\Repository\YearRepository;
  10. use App\Services\Mail;
  11. use App\Services\Social;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class HomeController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="front_home")
  21.      */
  22.     public function home(
  23.         BlockTitleDescriptionRepository $blockTitleDescriptionRepository,
  24.         YearRepository $yearRepository,
  25.         NextRaceRepository $nextRaceRepository,
  26.         RaceRepository $raceRepository,
  27.         Request $request,
  28.         Mail $mail,
  29.         Social $social)
  30.     {
  31.         $contactDTO = new ContactDTO();
  32.         $form $this->createForm(ContactType::class, $contactDTO)->handleRequest($request);
  33.         if ($form->isSubmitted() && $form->isValid()) {
  34.             if ($request->get('email-mail') && $request->get('email-mail') !== '') {
  35.                 $this->addFlash('danger''Erreur lors de l’envoi du formulaire');
  36.                 return new RedirectResponse('/#contact');
  37.             }
  38.             $mail->sendMail($contactDTO);
  39.             $this->addFlash('success''Message de contact envoyé');
  40.             return new RedirectResponse('/#contact');
  41.         }
  42.         $images $social->displayInstagram(12);
  43.         
  44.         return $this->render('front/index.html.twig', [
  45.             'aboutDescription' => $blockTitleDescriptionRepository->findOneBy(['section' => 'about''subSection' => 'description']),
  46.             'aboutPresentation' => $blockTitleDescriptionRepository->findOneBy(['section' => 'about''subSection' => 'presentation']),
  47.             'results' => $yearRepository->findBy(['section' => 'results']),
  48.             'galleries' => $yearRepository->findSectionsByDate('gallery', new \DateTime()),
  49.             'yearsGallery' => $yearRepository->findSectionsGroupByData('gallery', new \DateTime()),
  50.             'sponsor' => $yearRepository->findOneBy(['section' => 'sponsors']),
  51.             'nextRace' => $nextRaceRepository->find(1),
  52.             'races' => $raceRepository->findAll(),
  53.             'form' => $form->createView(),
  54.             'instaImages' => $images
  55.         ]);
  56.     }
  57.     /**
  58.      * @Route("/next-race/{raceId<\d+>}", name="front_next_race")
  59.      */
  60.     public function nextRace(int $raceIdRaceRepository $raceRepository)
  61.     {
  62.         if ($raceId >= 3) {
  63.             $nextId 1;
  64.         } else {
  65.             $nextId $raceId+1;
  66.         }
  67.         $nextRace $raceRepository->find($nextId);
  68.         if (!$nextRace) {
  69.             return $this->json([
  70.                 'status' => 'error'
  71.             ], Response::HTTP_NOT_FOUND);
  72.         }
  73.         return $this->json([
  74.             'status' => 'ok',
  75.             'raceId' => $nextId,
  76.             'template' => $this->render('front/html/slider-race.html.twig', ['race' => $nextRace])
  77.         ], Response::HTTP_OK);
  78.     }
  79.     /**
  80.      * @Route("/to-race/{raceId<\d+>}", name="front_to_race")
  81.      */
  82.     public function toRace(int $raceIdRaceRepository $raceRepository)
  83.     {
  84.         $race $raceRepository->find($raceId);
  85.         $raceId $race->getId();
  86.         if ($raceId >= 3) {
  87.             $nextId 1;
  88.         } else {
  89.             $nextId $raceId;
  90.         }
  91.         return $this->json([
  92.             'status' => 'ok',
  93.             'raceId' => $nextId,
  94.             'template' => $this->render('front/html/slider-race.html.twig', ['race' => $race'nextYear' => $race->getYears()->first(), 'previousYear' => $race->getYears()->first()])
  95.         ], Response::HTTP_OK);
  96.     }
  97.     /**
  98.      * @Route("/race/change-year-race/{id<\d+>}", name="front_change_year_race")
  99.      */
  100.     public function changeYearRace(Year $year)
  101.     {
  102.         return $this->json([
  103.             'status' => 'ok',
  104.             'template' => $this->render('front/html/year_race_table.html.twig', [
  105.                 'year' => $year,
  106.                 'nextYear' => $year,
  107.                 'previousYear' => $year
  108.             ])
  109.         ]);
  110.     }
  111.     /**
  112.      * @Route("/race/change-table-race/{id<\d+>}", name="front_changetable_race")
  113.      */
  114.     public function changeTableRace(Year $yearResult)
  115.     {
  116.         $years $yearResult->getRace()->getYears();
  117.         $previous null;
  118.         $next null;
  119.         $counter 1;
  120.         foreach ($years as $key => $year) {
  121.             if ($year->getId() === $yearResult->getId()) {
  122.                 $counter $key;
  123.                 $previous $years[$key-1];
  124.                 $next $years[$key+1];
  125.                 if ($key === 1) {
  126.                     $previous $years[end($years)];
  127.                 }
  128.             }
  129.         }
  130.         if ($next === null) {
  131.             $next $years[0];
  132.             $previous $years[-1];
  133.         }
  134.         if ($previous === null) {
  135.             $previous $years[end($years)];
  136.         }
  137.         return $this->json([
  138.             'status' => 'ok',
  139.             'template' => $this->render('front/html/year_race_table.html.twig', [
  140.                 'year' => $yearResult,
  141.                 'race' => $yearResult->getRace(),
  142.                 'nextYear' => $next,
  143.                 'previousYear' => $previous,
  144.                 'countYears' => count($years)
  145.             ])
  146.         ], Response::HTTP_OK);
  147.     }
  148.     /**
  149.      * @Route("/results/change-year/{id}", name="front_results_change_year")
  150.      */
  151.     public function resultsYear(Year $year)
  152.     {
  153.         return $this->json([
  154.             'status' => 'ok',
  155.             'template' => $this->render('front/html/result_tables.html.twig', [
  156.                 'year' => $year,
  157.                 'race' => $year->getRace()
  158.             ])
  159.         ], Response::HTTP_OK);
  160.     }
  161.     /**
  162.      * @Route("/gallery/gallery-year/{galleryYear}", name="front_gallery_gallery_year")
  163.      */
  164.     public function galleryYear(int $galleryYearYearRepository $yearRepository)
  165.     {
  166.         $newDatetime = new \DateTime($galleryYear.'-01-01');
  167.         $galleries $yearRepository->findSectionsByDate('gallery'$newDatetime);
  168.         return $this->json([
  169.             'status' => 'ok',
  170.             'template' => $this->render('front/html/gallery_year.html.twig', ['galleries' => $galleries])
  171.         ], Response::HTTP_OK);
  172.     }
  173. }