Implement filtering functionality

This commit is contained in:
Jannis Portmann 2022-01-14 13:52:16 +01:00
parent d0feff7d74
commit e52753446b
3 changed files with 77 additions and 2 deletions

View file

@ -4,6 +4,8 @@ namespace App\Controller;
use App\Entity\Offering; use App\Entity\Offering;
use App\Form\OfferingFormType; use App\Form\OfferingFormType;
use App\Form\OfferFilterFormType;
use App\Repository\OfferingRepository; use App\Repository\OfferingRepository;
use App\Repository\WishRepository; use App\Repository\WishRepository;
@ -32,10 +34,36 @@ class OfferController extends AbstractController
} }
#[Route('/offers', name: 'offers')] #[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', [ return new Response($twig->render('offer/index.html.twig', [
'offers' => $offerRepository->findAll(), 'offers' => $filteredOffers,
'filter_form' => $form->createView()
])); ]));
} }

View file

@ -0,0 +1,31 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OfferFilterFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->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
]);
}
}

View file

@ -10,6 +10,22 @@
</div> </div>
{% endfor %} {% endfor %}
<h2><i class="fas fa-filter"></i>Filter</h2>
{{ form_start(filter_form) }}
<div class="row">
<div class="col-sm">
{{ form_row(filter_form.zipCode) }}
</div>
<div class="col-sm">
{{ form_row(filter_form.distance) }}
</div>
<div class="col-sm">
<button type="submit" class="btn btn-lg btn-primary">Filter</button>
</div>
</div>
{{ form_end(filter_form) }}
<h1>Offers</h1> <h1>Offers</h1>
{% if offers|length > 0 %} {% if offers|length > 0 %}
<div class="card-deck d-flex justify-content-around justify-content-sm-around justify-content-md-between flex-wrap"> <div class="card-deck d-flex justify-content-around justify-content-sm-around justify-content-md-between flex-wrap">