From e52753446bb41df7172adbf13a372f09f4b579b7 Mon Sep 17 00:00:00 2001 From: thisfro Date: Fri, 14 Jan 2022 13:52:16 +0100 Subject: [PATCH] Implement filtering functionality --- src/Controller/OfferController.php | 32 ++++++++++++++++++++++++++++-- src/Form/OfferFilterFormType.php | 31 +++++++++++++++++++++++++++++ templates/offer/index.html.twig | 16 +++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/Form/OfferFilterFormType.php diff --git a/src/Controller/OfferController.php b/src/Controller/OfferController.php index 9425e3b..b66f0d4 100644 --- a/src/Controller/OfferController.php +++ b/src/Controller/OfferController.php @@ -4,6 +4,8 @@ namespace App\Controller; use App\Entity\Offering; use App\Form\OfferingFormType; +use App\Form\OfferFilterFormType; + use App\Repository\OfferingRepository; use App\Repository\WishRepository; @@ -32,10 +34,36 @@ class OfferController extends AbstractController } #[Route('/offers', name: 'offers')] - public function index(Environment $twig, OfferingRepository $offerRepository): Response + public function distanceExample(Environment $twig, Request $request, OfferingRepository $offerRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response { + $form = $this->createForm(OfferFilterFormType::class); + $form->handleRequest($request); + + $allOffers = $offerRepository->findAll(); + $filteredOffers = []; + + if ($form->isSubmitted() && $form->isValid()) { + $filterDistance = $form->get('distance')->getData(); + $filterPlz = $form->get('zipCode')->getData(); + $filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz); + + foreach ($allOffers as $offer) { + $offerCoordinate = $offer->getCoordinate(); + + $distance = $distanceCalculator->calculateDistance($offerCoordinate, $filterCoordinate); + + if ($distance < $filterDistance) { + array_push($filteredOffers, $offer); + } + } + } + else { + $filteredOffers = $allOffers; + } + return new Response($twig->render('offer/index.html.twig', [ - 'offers' => $offerRepository->findAll(), + 'offers' => $filteredOffers, + 'filter_form' => $form->createView() ])); } diff --git a/src/Form/OfferFilterFormType.php b/src/Form/OfferFilterFormType.php new file mode 100644 index 0000000..92ecb96 --- /dev/null +++ b/src/Form/OfferFilterFormType.php @@ -0,0 +1,31 @@ +add('zipCode', TextType::class, [ + 'label' => 'ZIP', + ]) + ->add('distance', NumberType::class, [ + 'label' => 'Distance', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + // Configure your form options here + ]); + } +} diff --git a/templates/offer/index.html.twig b/templates/offer/index.html.twig index 5f0d2e7..ff31135 100644 --- a/templates/offer/index.html.twig +++ b/templates/offer/index.html.twig @@ -10,6 +10,22 @@ {% endfor %} +

Filter

+ {{ form_start(filter_form) }} +
+
+ {{ form_row(filter_form.zipCode) }} +
+
+ {{ form_row(filter_form.distance) }} +
+
+ +
+
+ {{ form_end(filter_form) }} + +

Offers

{% if offers|length > 0 %}