src/Controller/DefaultController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\GeoContinents;
  4. use App\Entity\GeoCountries;
  5. use App\Entity\Station;
  6. use App\Form\StationType;
  7. use App\Repository\StationRepository;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use GeoIp2\Database\Reader;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. /**
  16.  * @Route("/")
  17.  */
  18. class DefaultController extends AbstractController
  19. {
  20.     private $translator;
  21.     public function __construct(TranslatorInterface $translator)
  22.     {
  23.         $this->translator $translator;
  24.     }
  25.     /**
  26.      * @Route("/", name="station_pays", methods={"GET"})
  27.      */
  28.     public function index(StationRepository $stationRepositoryRequest $request): Response
  29.     {
  30.         require_once('/home2/doulebtn/bestradiolive.online/geoip2.phar');
  31.         $reader = new Reader('/home2/doulebtn/bestradiolive.online/GeoLite2-City.mmdb');
  32.         $record $reader->city($_SERVER['REMOTE_ADDR']);
  33.         $cca2 $record->country->isoCode;
  34.         $em $this->getDoctrine()->getManager();
  35.         $continent $em->getRepository(GeoContinents::class)->findOneByCode($record->continent->code);
  36.         $stations $stationRepository->findParPays($cca2);
  37.         if(!$stations) {
  38.             $stations $stationRepository->findAll();
  39.             //findIfNotLocated($continent->getCode());
  40.         }
  41.         return $this->render('station/index.html.twig', [
  42.             'stations'      => $stations,
  43.             'continent'     => $continent
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/stations/radio/live/{slug}", name="station_pays_show", methods={"GET"})
  48.      */
  49.     public function stationPays(string $slug): Response
  50.     {
  51.         $em $this->getDoctrine()->getManager();
  52.         $country $em->getRepository(GeoCountries::class)->findOneBySlug($slug);
  53.         return $this->render('station/index.html.twig', [
  54.             'stations' => $em->getRepository(Station::class)->findParPays($country->getCca2()),
  55.             'continent' => $country->getContinent()
  56.         ]);
  57.     }
  58.     /**
  59.      * @Route("/pays/stations/radio", name="station_liste_pays", methods={"GET"})
  60.      */
  61.     public function listepays(): Response
  62.     {
  63.         $em $this->getDoctrine()->getManager();
  64.         $continents $em->getRepository(GeoContinents::class)->findAll();
  65.         return $this->render('geo_continents/index.html.twig', [
  66.             'continents' => $continents,
  67.         ]);
  68.     }
  69.     
  70.     /**
  71.      * @Route("/select/languages/{locale}", name="language", methods={"GET"})
  72.      */
  73.     public function languages(string $localeRequest $request): Response
  74.     {
  75.         // On stocke la langue dans la session
  76.         $request->getSession()->set('_locale'$locale);
  77.         // On revient sur la page précédente
  78.         return $this->redirect($request->headers->get('referer'));
  79.         /*$browserLocale = '';
  80.         $localePreferences = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
  81.         if(is_array($localePreferences) && count($localePreferences) > 0) {
  82.         $browserLocale = $localePreferences[0];
  83.             $_SESSION['browser_locale'] = $browserLocale;
  84.         }
  85.      $jsonStations = json_encode($browserLocale, JSON_UNESCAPED_UNICODE);
  86.         return new Response($jsonStations);
  87.         return new Response($browserLocale);*/
  88.     }
  89.     
  90.     /**
  91.      * @Route("/station/{slug}/stream", name="station_one", methods={"GET"})
  92.      */
  93.     public function one(string $slug): Response
  94.     {
  95.         header("Access-Control-Allow-Origin: *");
  96.         
  97.         $station $this->getDoctrine()->getManager()->getRepository(Station::class)->transformOne($slug);
  98.         $jsonStation json_encode($stationJSON_UNESCAPED_UNICODE);
  99.         return new Response($jsonStation);
  100.     }
  101.     /**
  102.      * @Route("/new", name="station_new", methods={"GET","POST"})
  103.      */
  104.     public function new(Request $request): Response
  105.     {
  106.         $station = new Station();
  107.         $form $this->createForm(StationType::class, $station);
  108.         $form->handleRequest($request);
  109.         if ($form->isSubmitted() && $form->isValid()) {
  110.             /** @var UploadedFile $brochureFile */
  111.             $brochureFile $form->get('image')->getData();
  112.             // this condition is needed because the 'brochure' field is not required
  113.             // so the PDF file must be processed only when a file is uploaded
  114.             if ($brochureFile) {
  115.                 $originalFilename pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
  116.                 // this is needed to safely include the file name as part of the URL
  117.                 $safeFilename transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()'$originalFilename);
  118.                 $newFilename $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
  119.                 // Move the file to the directory where brochures are stored
  120.                 try {
  121.                     $brochureFile->move(
  122.                         $this->getParameter('brochures_directory'),
  123.                         $newFilename
  124.                     );
  125.                 } catch (FileException $e) {
  126.                     // ... handle exception if something happens during file upload
  127.                 }
  128.                 // updates the 'brochureFilename' property to store the PDF file name
  129.                 // instead of its contents
  130.                 $station->setImage($newFilename);
  131.             }
  132.             $entityManager $this->getDoctrine()->getManager();
  133.             $entityManager->persist($station);
  134.             $entityManager->flush();
  135.             return $this->redirectToRoute('station_new');
  136.         }
  137.         return $this->render('station/new.html.twig', [
  138.             'station' => $station,
  139.             'form' => $form->createView(),
  140.         ]);
  141.     }
  142.     /**
  143.      * @Route("/{slug}/stream", name="station_ecouter", methods={"GET"})
  144.      */
  145.     public function show(Station $station): Response
  146.     {
  147.         $stationRepository $this->getDoctrine()->getManager()->getRepository(Station::class);
  148.         $cca2 $station->getPays()->getCca2();
  149.         $stations $stationRepository->findParPays($cca2);
  150.         return $this->render('station/show.html.twig', [
  151.             'station'   => $station,
  152.             'stations'  =>$stations
  153.         ]);
  154.     }
  155.     
  156.     /**
  157.      * Génère le sitemap du site.
  158.      *
  159.      * @Route("/sitemap.{_format}", name="front_sitemap", Requirements={"_format" = "xml"})
  160.      */
  161.     public function siteMapAction()
  162.     {
  163.         return $this->render(
  164.             'sitemap.xml.twig',
  165.             ['urls' => $this->generer()]
  166.         );
  167.     }
  168.     
  169.     /**
  170.      * Génère le ads text.
  171.      *
  172.      * @Route("/ads.{_format}", name="front_ads", Requirements={"_format" = "txt"})
  173.      */
  174.     public function adsAction()
  175.     {
  176.         return $this->render(
  177.             'ads.txt.twig'
  178.         );
  179.     }
  180.     
  181.     /**
  182.      * Génère l'ensemble des valeurs des balises <url> du sitemap.
  183.      *
  184.      * @return array Tableau contenant l'ensemble des balise url du sitemap.
  185.      */
  186.     private function generer()
  187.     {
  188.         $urls = [];        
  189.         $em $this->getDoctrine()->getManager();
  190.         
  191.         $articles $em->getRepository(Station::class)->findAll();
  192.         foreach ($articles as $article) {
  193.             $urls[] = [
  194.                 'loc' => $this->generateUrl('station_ecouter', ['slug' => $article->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL)
  195.             ];
  196.         }
  197.         return $urls;
  198.     }
  199. }