<?php
namespace App\Controller;
use App\Entity\GeoContinents;
use App\Entity\GeoCountries;
use App\Entity\Station;
use App\Form\StationType;
use App\Repository\StationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use GeoIp2\Database\Reader;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @Route("/")
*/
class DefaultController extends AbstractController
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @Route("/", name="station_pays", methods={"GET"})
*/
public function index(StationRepository $stationRepository, Request $request): Response
{
require_once('/home2/doulebtn/bestradiolive.online/geoip2.phar');
$reader = new Reader('/home2/doulebtn/bestradiolive.online/GeoLite2-City.mmdb');
$record = $reader->city($_SERVER['REMOTE_ADDR']);
$cca2 = $record->country->isoCode;
$em = $this->getDoctrine()->getManager();
$continent = $em->getRepository(GeoContinents::class)->findOneByCode($record->continent->code);
$stations = $stationRepository->findParPays($cca2);
if(!$stations) {
$stations = $stationRepository->findAll();
//findIfNotLocated($continent->getCode());
}
return $this->render('station/index.html.twig', [
'stations' => $stations,
'continent' => $continent
]);
}
/**
* @Route("/stations/radio/live/{slug}", name="station_pays_show", methods={"GET"})
*/
public function stationPays(string $slug): Response
{
$em = $this->getDoctrine()->getManager();
$country = $em->getRepository(GeoCountries::class)->findOneBySlug($slug);
return $this->render('station/index.html.twig', [
'stations' => $em->getRepository(Station::class)->findParPays($country->getCca2()),
'continent' => $country->getContinent()
]);
}
/**
* @Route("/pays/stations/radio", name="station_liste_pays", methods={"GET"})
*/
public function listepays(): Response
{
$em = $this->getDoctrine()->getManager();
$continents = $em->getRepository(GeoContinents::class)->findAll();
return $this->render('geo_continents/index.html.twig', [
'continents' => $continents,
]);
}
/**
* @Route("/select/languages/{locale}", name="language", methods={"GET"})
*/
public function languages(string $locale, Request $request): Response
{
// On stocke la langue dans la session
$request->getSession()->set('_locale', $locale);
// On revient sur la page précédente
return $this->redirect($request->headers->get('referer'));
/*$browserLocale = '';
$localePreferences = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
if(is_array($localePreferences) && count($localePreferences) > 0) {
$browserLocale = $localePreferences[0];
$_SESSION['browser_locale'] = $browserLocale;
}
$jsonStations = json_encode($browserLocale, JSON_UNESCAPED_UNICODE);
return new Response($jsonStations);
return new Response($browserLocale);*/
}
/**
* @Route("/station/{slug}/stream", name="station_one", methods={"GET"})
*/
public function one(string $slug): Response
{
header("Access-Control-Allow-Origin: *");
$station = $this->getDoctrine()->getManager()->getRepository(Station::class)->transformOne($slug);
$jsonStation = json_encode($station, JSON_UNESCAPED_UNICODE);
return new Response($jsonStation);
}
/**
* @Route("/new", name="station_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$station = new Station();
$form = $this->createForm(StationType::class, $station);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var UploadedFile $brochureFile */
$brochureFile = $form->get('image')->getData();
// this condition is needed because the 'brochure' field is not required
// so the PDF file must be processed only when a file is uploaded
if ($brochureFile) {
$originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
// this is needed to safely include the file name as part of the URL
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
// Move the file to the directory where brochures are stored
try {
$brochureFile->move(
$this->getParameter('brochures_directory'),
$newFilename
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
// updates the 'brochureFilename' property to store the PDF file name
// instead of its contents
$station->setImage($newFilename);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($station);
$entityManager->flush();
return $this->redirectToRoute('station_new');
}
return $this->render('station/new.html.twig', [
'station' => $station,
'form' => $form->createView(),
]);
}
/**
* @Route("/{slug}/stream", name="station_ecouter", methods={"GET"})
*/
public function show(Station $station): Response
{
$stationRepository = $this->getDoctrine()->getManager()->getRepository(Station::class);
$cca2 = $station->getPays()->getCca2();
$stations = $stationRepository->findParPays($cca2);
return $this->render('station/show.html.twig', [
'station' => $station,
'stations' =>$stations
]);
}
/**
* Génère le sitemap du site.
*
* @Route("/sitemap.{_format}", name="front_sitemap", Requirements={"_format" = "xml"})
*/
public function siteMapAction()
{
return $this->render(
'sitemap.xml.twig',
['urls' => $this->generer()]
);
}
/**
* Génère le ads text.
*
* @Route("/ads.{_format}", name="front_ads", Requirements={"_format" = "txt"})
*/
public function adsAction()
{
return $this->render(
'ads.txt.twig'
);
}
/**
* Génère l'ensemble des valeurs des balises <url> du sitemap.
*
* @return array Tableau contenant l'ensemble des balise url du sitemap.
*/
private function generer()
{
$urls = [];
$em = $this->getDoctrine()->getManager();
$articles = $em->getRepository(Station::class)->findAll();
foreach ($articles as $article) {
$urls[] = [
'loc' => $this->generateUrl('station_ecouter', ['slug' => $article->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL)
];
}
return $urls;
}
}