<?php
namespace App\Controller\Front;
use App\Entity\Comment;
use App\Entity\Masjid;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route({"en" : "/masjid","fr" : "/Mosquees", "ar" : "/مساجد"})
*/
class MasjidController extends AbstractController
{
/**
* @Route("/", name="masjid_index", methods={"GET"})
*/
public function index(Request $request, PaginatorInterface $paginator): Response
{
$masjidsAll = $this->getDoctrine()
->getRepository(Masjid::class)
->findAll();
$pagination = $paginator->paginate(
$masjidsAll, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
10 /*limit per page*/
);
return $this->render('masjid/index.html.twig', [
'masjids' => $pagination,
]);
}
/**
* @Route("/{slug}", name="masjid_show", methods={"GET"})
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function show(Request $request, Masjid $masjid,PaginatorInterface $paginator): Response
{
$prevMasjid = $this->getDoctrine()
->getRepository(Masjid::class)
->getPreviousMasjid($masjid->getId(),'prev');
$nextMasjid = $this->getDoctrine()
->getRepository(Masjid::class)
->getPreviousMasjid($masjid->getId(),'next');
$listcomment = $this->getDoctrine()
->getRepository(Comment::class)
->findBy(array("masjid"=>$masjid, "isEnabled" => true));
$masjidRandom = $this->getDoctrine()
->getRepository(Masjid::class)
->findByMasjidRandom(2);
//$masjidRandom = array_slice($masjidRandom, 0, 2);
$pagination = $paginator->paginate(
$listcomment, /* query NOT result */
$request->query->getInt('page', 1), /* page number */
10 /* limit per page */
);
return $this->render('masjid/show.html.twig', [
'masjid' => $masjid,
'pagination' => $pagination,
'masjidRandom' => $masjidRandom,
'masjidPrev' => $prevMasjid,
'masjidNext' => $nextMasjid,
]);
}
/**
* @Route({"en": "/query/search", "fr": "/recherche/mosquee", "ar" : "/query/search"}, name="search", methods={"GET","POST"})
*/
public function search(Request $request,PaginatorInterface $paginator):Response
{
if ($request->isMethod('post')) {
///////////gets searchs/////////////////////
$term = $request->request->get('term');
$result = $this->getDoctrine()
->getRepository(Masjid::class)
->search($term);
if($result == null){
return $this->render('masjid/no-result.html.twig', [
]);
}
$pagination = $paginator->paginate(
$result, /* query NOT result */
$request->query->getInt('page', 1), /* page number */
10 /* limit per page */
);
return $this->render('masjid/result.html.twig', [
'masjids' => $pagination,
]);
}
return $this->render('masjid/no-result.html.twig', [
]);
}
/**
* @Route("/findNearestToClientLocation/{lat}/{lng}", name="findNearestToClientLocation", methods={"POST"})
*/
public function findNearestToClientLocation(Request $request, $lat, $lng, int $km = 1)
{
/*** ICI je récupère les mosquées les plus proches des coorfddonnées GPS transmises
puis je limite à 1 dqns lq requete
Ensuite cette mosquée je lq récupère en objet
dans une autre requete pour avoir les translations
car setmaxresult DOCTRINE limite aussi les relations***/
$masjidNearestToClientLocation = $this->getDoctrine()
->getRepository(Masjid::class)
->findNearestToClientLocation($lat, $lng);
$masjidId = $masjidNearestToClientLocation[0][0]['id'];
//var_dump($masjidId);
$masjid = $this->getDoctrine()
->getRepository(Masjid::class)
->getMasjidOne($masjidId);
$response = new Response(json_encode($masjid));
$response->headers->set('Content-Type', 'Application/json');
return $response;
}
}