<?php
namespace App\Controller\Front;
use App\Entity\Picture;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Masjid;
use App\Entity\Community;
use Stripe\Stripe;
use Stripe\Checkout;
use Stripe\PaymentIntent;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Form\DonateType;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Form\FormInterface;
class MainController extends AbstractController
{
/**
* @Route("/")
*/
public function index(): Response
{
/*$masjidRandom = $this->getDoctrine()
->getRepository(Masjid::class)
->findByMasjidRandom();*/
return $this->render('Main/index.html.twig', [
]);
}
public function header(Request $request)
{
$masterRequest = $this->container->get('request_stack')->getMasterRequest();
$ip = $_SERVER['REMOTE_ADDR'];
/*var_dump($ip);die;
$json = file_get_contents("http://ipinfo.io/".$ip."/geo?token=bf523ea023e9c0");
$json = json_decode($json, true);
$country = $json['country'];
//This variable is the visitor's region
$region = $json['region'];
//This variable is the visitor's city
$city = $json['city'];*/
// Valeurs par défaut en cas d'erreur
$json = [];
$country = 'AE'; // UAE par défaut
$region = '';
$city = '';
try {
// Tentative d'appel à l'API ipinfo.io
$context = stream_context_create([
'http' => [
'timeout' => 2, // Timeout de 2 secondes
'ignore_errors' => true
]
]);
$response = @file_get_contents("http://ipinfo.io/".$ip."/geo?token=bf523ea023e9c0", false, $context);
if ($response !== false) {
$json = json_decode($response, true);
if ($json && isset($json['country'])) {
$country = $json['country'];
$region = $json['region'] ?? '';
$city = $json['city'] ?? '';
}
}
} catch (\Exception $e) {
// En cas d'erreur, on utilise les valeurs par défaut
// L'erreur est silencieuse pour ne pas casser le site
}
return $this->render('Main/header.html.twig', [
'ip' => $ip,
'json' => $json,
'country' => $country,
'region' => $region,
'city' => $city,
'route' => $masterRequest->attributes->get('_route'),
'route_params' => $masterRequest->attributes->get('_route_params'),
]);
}
/**
* @Route("/about-us")
*/
public function about(): Response
{
$countMasjid = $this->getDoctrine()
->getRepository(Masjid::class)
->countEntity();
$countUser = $this->getDoctrine()
->getRepository(User::class)
->countEntity();
$countPictures = $this->getDoctrine()
->getRepository(Picture::class)
->countEntity();
$masjidRandom = $this->getDoctrine()
->getRepository(Masjid::class)
->findByMasjidRandom(5);
return $this->render('Main/about.html.twig', [
'masjidRandom' => $masjidRandom,
'countMasjid' => $countMasjid,
'countUser' => $countUser,
'countPictures' => $countPictures,
]);
}
public function aboutFragment(): Response
{
$countMasjid = $this->getDoctrine()
->getRepository(Masjid::class)
->countEntity();
$countUser = $this->getDoctrine()
->getRepository(User::class)
->countEntity();
$countPictures = $this->getDoctrine()
->getRepository(Picture::class)
->countEntity();
return $this->render('Main/_about-us.html.twig', [
'countMasjid' => $countMasjid,
'countUser' => $countUser,
'countPictures' => $countPictures,
]);
}
public function sidebar($masjidId): Response
{
$masjid = $this->getDoctrine()
->getRepository(Masjid::class)
->find($masjidId);
$currentRoute = $this->container->get('request_stack')->getMasterRequest()->get('_route');
$nearestMasjid = $this->getDoctrine()
->getRepository(Masjid::class)
->nearestMasjids($masjid->getLatitude(),$masjid->getLongitude());
$randomCommunities = $this->getDoctrine()
->getRepository(Community::class)
->randomCommunities();
$masjidsSameCommunity = $this->getDoctrine()
->getRepository(Masjid::class)
->masjidsSameCommunity($masjid->getCommunity());
$randomCommunities = array_slice($randomCommunities, 0, 10);
return $this->render('Main/sidebar.html.twig', [
'nearestMasjid' => $nearestMasjid,
'randomCommunities' => $randomCommunities,
'masjidsSameCommunity' => $masjidsSameCommunity,
'masjidId' =>$masjidId,
'masjid' =>$masjid,
'currentRoute' =>$currentRoute
]);
}
public function footer(): Response
{
$randomMasjids = $this->getDoctrine()
->getRepository(Masjid::class)
->randomMasjids();
$randomMasjids = array_slice($randomMasjids, 0, 3);
return $this->render('Main/footer.html.twig', [
'randomMasjids' => $randomMasjids,
]);
}
public function newsletter(): Response
{
return $this->render('Main/newsletter.html.twig', [
]);
}
}