Implement filtering functionality
This commit is contained in:
parent
d0feff7d74
commit
e52753446b
3 changed files with 77 additions and 2 deletions
|
@ -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()
|
||||
]));
|
||||
}
|
||||
|
||||
|
|
31
src/Form/OfferFilterFormType.php
Normal file
31
src/Form/OfferFilterFormType.php
Normal 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
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -10,6 +10,22 @@
|
|||
</div>
|
||||
{% 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>
|
||||
{% if offers|length > 0 %}
|
||||
<div class="card-deck d-flex justify-content-around justify-content-sm-around justify-content-md-between flex-wrap">
|
||||
|
|
Reference in a new issue