<?php
namespace App\Controller\Front;
use App\Entity\AdditionalService;
use App\Entity\Offer;
use App\Entity\User;
use App\Form\SearchFormMobileType;
use App\Form\SearchFormType;
use App\Repository\OfferRepository;
use App\Services\OfferManager;
use App\Services\Utils;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Knp\Component\Pager\PaginatorInterface;
class HuntsController extends AbstractController
{
private OfferRepository $offerRepository;
public function __construct(OfferRepository $offerRepository)
{
$this->offerRepository = $offerRepository;
}
#[Route('/resultats-de-recherche', name: 'hunt_search')]
#[Route('/liste', name: 'our_hunts')]
#[Route('/', name: 'homepage')]
public function index(Request $request, PaginatorInterface $paginator, OfferManager $offerManager): Response
{
if($this->getUser() !== null && $request->query->get('action', '') === 'login') {
return $this->redirectToRoute('user_informations');
}
$limit = 15;
$formDesktop = $this->createForm(SearchFormType::class);
$formMobile = $this->createForm(SearchFormType::class, ['isMobile' => true]);
$formDesktop->get('orderBy')->setData('isTuchassouSelected');
$formDesktop->get('orderSort')->setData('DESC');
$formMobile->get('orderBy')->setData('isTuchassouSelected');
$formMobile->get('orderSort')->setData('DESC');
$formDesktop->handleRequest($request);
$formMobile->handleRequest($request);
if ($formDesktop->isSubmitted()) {
$data = $formDesktop->getData();
$offers = $this->offerRepository->search($data);
if ($formDesktop->get('priceMax')->getData() !== null) {
$offers = $offerManager->filterOffersByPriceWithTaxes($offers, $formDesktop->get('priceMax')->getData());
}
$pagination = $paginator->paginate(
$offers,
$request->query->getInt('page', 1),
$limit
);
} else {
$data = $formMobile->getData();
$offers = $this->offerRepository->search($data);
if ($formMobile->get('priceMax')->getData() !== null) {
$offers = $offerManager->filterOffersByPriceWithTaxes($offers, $formMobile->get('priceMax')->getData());
}
$pagination = $paginator->paginate(
$offers,
$request->query->getInt('page', 1),
$limit
);
}
$offerForMap = $offerManager->getOffersForMaps($pagination);
return $this->render('front/hunts.html.twig', [
'formDesktop' => $formDesktop->createView(),
'formMobile' => $formMobile->createView(),
'pagination' => $pagination,
'mapOffers' => $offerForMap,
'offerManager' => $offerManager,
'order' => (isset($data['orderBy'])) ? $data['orderBy'] : 'isTuchassouSelected',
'sort' => (isset($data['orderSort'])) ? $data['orderSort'] : 'ASC',
'limit' => $limit,
]);
}
#[Route('/annonce/{slug}', name: 'user_offer_details', options: ['expose' => true])]
#[ParamConverter('offer', class: Offer::class, options: ['slug' => 'slug'])]
public function show(Request $request, Offer $offer = null, OfferManager $offerManager): Response
{
if ($offer === null) {
return $this->redirectToRoute('our_hunts');
}
$em = $this->getDoctrine()->getManager();
$similarOffers = $em->getRepository(Offer::class)->getOffersByDistance($offer, 50);
if (is_null($similarOffers) || empty($similarOffers)) {
$similarOffers = $em->getRepository(Offer::class)->getOffersByDistance($offer, 100);
}
//$offer->getAvailableDates();
return $this->render('front/hunt/show.html.twig', [
'offer' => $offer,
'similarOffers' => $similarOffers,
'offerManager' => $offerManager,
]);
}
#[Route('/ajax/maps/marker/{id}/show', name: 'google_maps_marker_show', options: ['expose' => true])]
#[ParamConverter('offer', class: Offer::class, options: ['id' => 'id'])]
public function mapMarkerShow(Offer $offer, OfferManager $offerManager): Response
{
return $this->render('front/hunt/offer_in_map.html.twig', [
'offer' => $offer,
'offerManager' => $offerManager
]);
}
#[Route('/ajax/hunt/regions-list', name: 'regions_list', options: ['expose' => true])]
public function regionsList(Utils $utils, Request $request): JsonResponse
{
$regions = $utils->getRegionList($request->query->get('term'));
sort($regions);
return new JsonResponse(array_values($regions));
}
#[Route('/ajax/hunt/departements-list', name: 'departements_list_by_region', options: ['expose' => true])]
public function departementsList(Utils $utils, Request $request): JsonResponse
{
if($request->query->get('region', null) && $request->query->get('region', null) !== '') {
$departements = $utils->getDepartementByRegion($request->query->get('region'), $request->query->get('term'));
} else {
$departements = array_merge_recursive(...$utils->getDepartementList());
$term = $request->query->get('term');
if($term !== '') {
$departements = array_filter(array_map(function ($departement) use ($term) {
return (str_contains(strtolower($departement), strtolower($term)) !== FALSE) ? $departement : null;
}, $departements));
}
}
sort($departements);
return new JsonResponse((array_values($departements)));
}
#[Route('/ajax/hunt/departements-list-to-choice', name: 'departements_list_to_choice_by_region', options: ['expose' => true])]
public function departementsListToChoice(Utils $utils, Request $request): Response
{
$departements = [];
if($request->query->get('regions', null) && !empty($request->query->get('regions', null))) {
foreach ($request->query->get('regions', null) as $region) {
$departements = array_merge($departements, $utils->getDepartementByRegion($region, $request->query->get('term')));
sort($departements);
}
} else {
$departements = array_merge_recursive(...$utils->getDepartementList());
sort($departements);
$term = $request->query->get('term');
if($term !== '') {
$departements = array_filter(array_map(function ($departement) use ($term) {
return (str_contains(strtolower($departement), strtolower($term)) !== FALSE) ? $departement : null;
}, $departements));
}
}
$formName = 'search_form';
$html = '';
foreach ($departements as $key => $departement) {
$form = 'search_form_department_';
$html .= '<div class="form-check"><input type="checkbox" id="'.$form.$key.'" name="'.$formName.'[departement][]" class="form-check-input" value="'.$departement.'"><label class="form-check-label" for="'.$form.$key.'">'.$departement.'</label></div>';
}
return new Response($html);
}
}