src/Controller/Front/MainController.php line 164

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Picture;
  4. use App\Entity\User;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\Entity\Masjid;
  10. use App\Entity\Community;
  11. use Stripe\Stripe;
  12. use Stripe\Checkout;
  13. use Stripe\PaymentIntent;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use App\Form\DonateType;
  16. use Symfony\Component\Validator\Validation;
  17. use Symfony\Component\Form\FormInterface;
  18. class MainController extends AbstractController
  19. {
  20.     /**
  21.     * @Route("/")
  22.     */
  23.     public function index(): Response
  24.     {
  25.         /*$masjidRandom = $this->getDoctrine()
  26.             ->getRepository(Masjid::class)
  27.             ->findByMasjidRandom();*/
  28.         return $this->render('Main/index.html.twig', [
  29.         ]);
  30.     }
  31.     public function header(Request $request)
  32.     {
  33.          
  34.       
  35.         $masterRequest $this->container->get('request_stack')->getMasterRequest();
  36.         
  37.         $ip $_SERVER['REMOTE_ADDR'];
  38.         /*var_dump($ip);die;
  39.         $json = file_get_contents("http://ipinfo.io/".$ip."/geo?token=bf523ea023e9c0");
  40.         $json     = json_decode($json, true);
  41.         $country  = $json['country'];
  42.         //This variable is the visitor's region
  43.         $region   = $json['region'];
  44.         //This variable is the visitor's city
  45.         $city     = $json['city'];*/
  46.          // Valeurs par défaut en cas d'erreur
  47.         $json = [];
  48.         $country 'AE'// UAE par défaut
  49.         $region '';
  50.         $city '';
  51.         $city $json['city'] ?? 'Sharjah';
  52.         
  53.         try {
  54.             // Tentative d'appel à l'API ipinfo.io
  55.             $context stream_context_create([
  56.                 'http' => [
  57.                     'timeout' => 2// Timeout de 2 secondes
  58.                     'ignore_errors' => true
  59.                 ]
  60.             ]);
  61.             
  62.             $response = @file_get_contents("http://ipinfo.io/".$ip."/geo?token=bf523ea023e9c0"false$context);
  63.             
  64.             if ($response !== false) {
  65.                 $json json_decode($responsetrue);
  66.                 if ($json && isset($json['country'])) {
  67.                     $country $json['country'];
  68.                     $region $json['region'] ?? 'Sharjah';
  69.                     $city $json['city'] ?? 'Sharjah';
  70.                 }
  71.             }
  72.         } catch (\Exception $e) {
  73.             // En cas d'erreur, on utilise les valeurs par défaut
  74.             // L'erreur est silencieuse pour ne pas casser le site
  75.         }
  76.         return $this->render('Main/header.html.twig', [
  77.             'ip'           => $ip,
  78.             'json'         => $json,
  79.             'country'      => $country,
  80.             'region'       => $region,
  81.             'city'         => $city,
  82.             'route'        => $masterRequest->attributes->get('_route'),
  83.             'route_params' => $masterRequest->attributes->get('_route_params'),
  84.         ]);
  85.     }
  86.     /**
  87.     * @Route("/about-us")
  88.     */
  89.     public function about(): Response
  90.     {
  91.         $countMasjid $this->getDoctrine()
  92.             ->getRepository(Masjid::class)
  93.             ->countEntity();
  94.         $countUser $this->getDoctrine()
  95.             ->getRepository(User::class)
  96.             ->countEntity();
  97.         $countPictures $this->getDoctrine()
  98.             ->getRepository(Picture::class)
  99.             ->countEntity();
  100.         $masjidRandom $this->getDoctrine()
  101.             ->getRepository(Masjid::class)
  102.             ->findByMasjidRandom(5);
  103.         return $this->render('Main/about.html.twig', [
  104.             'masjidRandom' => $masjidRandom,
  105.             'countMasjid' => $countMasjid,
  106.             'countUser' => $countUser,
  107.             'countPictures' => $countPictures,
  108.         ]);
  109.     }
  110.     public function aboutFragment(): Response
  111.     {
  112.         $countMasjid $this->getDoctrine()
  113.             ->getRepository(Masjid::class)
  114.             ->countEntity();
  115.         $countUser $this->getDoctrine()
  116.             ->getRepository(User::class)
  117.             ->countEntity();
  118.         $countPictures $this->getDoctrine()
  119.             ->getRepository(Picture::class)
  120.             ->countEntity();
  121.         return $this->render('Main/_about-us.html.twig', [
  122.             'countMasjid' => $countMasjid,
  123.             'countUser' => $countUser,
  124.             'countPictures' => $countPictures,
  125.         ]);
  126.     }
  127.     public function sidebar($masjidId): Response
  128.     {
  129.         $masjid $this->getDoctrine()
  130.             ->getRepository(Masjid::class)
  131.             ->find($masjidId);
  132.         $currentRoute $this->container->get('request_stack')->getMasterRequest()->get('_route');
  133.         $nearestMasjid $this->getDoctrine()
  134.             ->getRepository(Masjid::class)
  135.             ->nearestMasjids($masjid->getLatitude(),$masjid->getLongitude());
  136.         $randomCommunities $this->getDoctrine()
  137.             ->getRepository(Community::class)
  138.             ->randomCommunities();
  139.         $masjidsSameCommunity $this->getDoctrine()
  140.             ->getRepository(Masjid::class)
  141.             ->masjidsSameCommunity($masjid->getCommunity());
  142.         $randomCommunities array_slice($randomCommunities010);
  143.         return $this->render('Main/sidebar.html.twig', [
  144.             'nearestMasjid' => $nearestMasjid,
  145.             'randomCommunities' => $randomCommunities,
  146.             'masjidsSameCommunity' => $masjidsSameCommunity,
  147.             'masjidId' =>$masjidId,
  148.             'masjid' =>$masjid,
  149.             'currentRoute' =>$currentRoute
  150.         ]);
  151.     }
  152.     public function footer(): Response
  153.     {
  154.         $randomMasjids $this->getDoctrine()
  155.             ->getRepository(Masjid::class)
  156.             ->randomMasjids();
  157.         $randomMasjids array_slice($randomMasjids03);
  158.         return $this->render('Main/footer.html.twig', [
  159.             'randomMasjids' => $randomMasjids,
  160.         ]);
  161.     }
  162.     public function newsletter(): Response
  163.     {
  164.         return $this->render('Main/newsletter.html.twig', [
  165.         ]);
  166.     }
  167. }