src/Controller/Front/HuntsController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\AdditionalService;
  4. use App\Entity\Offer;
  5. use App\Entity\User;
  6. use App\Form\SearchFormMobileType;
  7. use App\Form\SearchFormType;
  8. use App\Repository\OfferRepository;
  9. use App\Services\OfferManager;
  10. use App\Services\Utils;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Knp\Component\Pager\PaginatorInterface;
  17. class HuntsController extends AbstractController
  18. {
  19.     private OfferRepository $offerRepository;
  20.     public function __construct(OfferRepository $offerRepository)
  21.     {
  22.         $this->offerRepository $offerRepository;
  23.     }
  24.     #[Route('/resultats-de-recherche'name'hunt_search')]
  25.     #[Route('/liste'name'our_hunts')]
  26.     #[Route('/'name'homepage')]
  27.     public function index(Request $requestPaginatorInterface $paginatorOfferManager $offerManager): Response
  28.     {
  29.         if($this->getUser() !== null && $request->query->get('action''') === 'login') {
  30.             return $this->redirectToRoute('user_informations');
  31.         }
  32.         $limit 15;
  33.         $formDesktop $this->createForm(SearchFormType::class);
  34.         $formMobile $this->createForm(SearchFormType::class, ['isMobile' => true]);
  35.         $formDesktop->get('orderBy')->setData('isTuchassouSelected');
  36.         $formDesktop->get('orderSort')->setData('DESC');
  37.         $formMobile->get('orderBy')->setData('isTuchassouSelected');
  38.         $formMobile->get('orderSort')->setData('DESC');
  39.         $formDesktop->handleRequest($request);
  40.         $formMobile->handleRequest($request);
  41.         if ($formDesktop->isSubmitted()) {
  42.             $data $formDesktop->getData();
  43.             $offers $this->offerRepository->search($data);
  44.             if ($formDesktop->get('priceMax')->getData() !== null) {
  45.                 $offers $offerManager->filterOffersByPriceWithTaxes($offers$formDesktop->get('priceMax')->getData());
  46.             }
  47.             $pagination $paginator->paginate(
  48.                 $offers,
  49.                 $request->query->getInt('page'1),
  50.                 $limit
  51.             );
  52.         } else {
  53.             $data $formMobile->getData();
  54.             $offers $this->offerRepository->search($data);
  55.             if ($formMobile->get('priceMax')->getData() !== null) {
  56.                 $offers $offerManager->filterOffersByPriceWithTaxes($offers$formMobile->get('priceMax')->getData());
  57.             }
  58.             $pagination $paginator->paginate(
  59.                 $offers,
  60.                 $request->query->getInt('page'1),
  61.                 $limit
  62.             );
  63.         }
  64.         $offerForMap $offerManager->getOffersForMaps($pagination);
  65.         return $this->render('front/hunts.html.twig', [
  66.             'formDesktop' => $formDesktop->createView(),
  67.             'formMobile' => $formMobile->createView(),
  68.             'pagination' => $pagination,
  69.             'mapOffers' => $offerForMap,
  70.             'offerManager' => $offerManager,
  71.             'order' => (isset($data['orderBy'])) ? $data['orderBy'] : 'isTuchassouSelected',
  72.             'sort' => (isset($data['orderSort'])) ? $data['orderSort'] : 'ASC',
  73.             'limit' => $limit,
  74.         ]);
  75.     }
  76.     #[Route('/annonce/{slug}'name'user_offer_details'options: ['expose' => true])]
  77.     #[ParamConverter('offer', class: Offer::class, options: ['slug' => 'slug'])]
  78.     public function show(Request $requestOffer $offer nullOfferManager $offerManager): Response
  79.     {
  80.         if ($offer === null) {
  81.             return $this->redirectToRoute('our_hunts');
  82.         }
  83.         $em $this->getDoctrine()->getManager();
  84.         $similarOffers $em->getRepository(Offer::class)->getOffersByDistance($offer50);
  85.         if (is_null($similarOffers) || empty($similarOffers)) {
  86.             $similarOffers $em->getRepository(Offer::class)->getOffersByDistance($offer100);
  87.         }
  88.         //$offer->getAvailableDates();
  89.         return $this->render('front/hunt/show.html.twig', [
  90.             'offer' => $offer,
  91.             'similarOffers' => $similarOffers,
  92.             'offerManager' => $offerManager,
  93.         ]);
  94.     }
  95.     #[Route('/ajax/maps/marker/{id}/show'name'google_maps_marker_show'options: ['expose' => true])]
  96.     #[ParamConverter('offer', class: Offer::class, options: ['id' => 'id'])]
  97.     public function mapMarkerShow(Offer $offerOfferManager $offerManager): Response
  98.     {
  99.         return $this->render('front/hunt/offer_in_map.html.twig', [
  100.             'offer' => $offer,
  101.             'offerManager' => $offerManager
  102.         ]);
  103.     }
  104.     #[Route('/ajax/hunt/regions-list'name'regions_list'options: ['expose' => true])]
  105.     public function regionsList(Utils $utilsRequest $request): JsonResponse
  106.     {
  107.         $regions $utils->getRegionList($request->query->get('term'));
  108.         sort($regions);
  109.         return new JsonResponse(array_values($regions));
  110.     }
  111.     #[Route('/ajax/hunt/departements-list'name'departements_list_by_region'options: ['expose' => true])]
  112.     public function departementsList(Utils $utilsRequest $request): JsonResponse
  113.     {
  114.         if($request->query->get('region'null) && $request->query->get('region'null) !== '') {
  115.             $departements $utils->getDepartementByRegion($request->query->get('region'), $request->query->get('term'));
  116.         } else {
  117.             $departements array_merge_recursive(...$utils->getDepartementList());
  118.             $term $request->query->get('term');
  119.             if($term !== '') {
  120.                 $departements array_filter(array_map(function ($departement) use ($term) {
  121.                     return (str_contains(strtolower($departement), strtolower($term)) !== FALSE) ? $departement null;
  122.                 }, $departements));
  123.             }
  124.         }
  125.         sort($departements);
  126.         return new JsonResponse((array_values($departements)));
  127.     }
  128.     #[Route('/ajax/hunt/departements-list-to-choice'name'departements_list_to_choice_by_region'options: ['expose' => true])]
  129.     public function departementsListToChoice(Utils $utilsRequest $request): Response
  130.     {
  131.         $departements = [];
  132.         if($request->query->get('regions'null) && !empty($request->query->get('regions'null))) {
  133.             foreach ($request->query->get('regions'null) as $region) {
  134.                 $departements array_merge($departements$utils->getDepartementByRegion($region$request->query->get('term')));
  135.                 sort($departements);
  136.             }
  137.         } else {
  138.             $departements array_merge_recursive(...$utils->getDepartementList());
  139.             sort($departements);
  140.             $term $request->query->get('term');
  141.             if($term !== '') {
  142.                 $departements array_filter(array_map(function ($departement) use ($term) {
  143.                     return (str_contains(strtolower($departement), strtolower($term)) !== FALSE) ? $departement null;
  144.                 }, $departements));
  145.             }
  146.         }
  147.         $formName 'search_form';
  148.         $html '';
  149.         foreach ($departements as $key => $departement) {
  150.             $form 'search_form_department_';
  151.             $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>';
  152.         }
  153.         return new Response($html);
  154.     }
  155. }