Implement basic title text search

This commit is contained in:
Jannis Portmann 2022-01-20 17:51:02 +01:00
parent 71ca261cab
commit 865f8ec94f
4 changed files with 38 additions and 6 deletions

View file

@ -33,15 +33,21 @@ class OfferController extends AbstractController
} }
#[Route('/offers', name: 'offers', options: ["sitemap" => true])] #[Route('/offers', name: 'offers', options: ["sitemap" => true])]
public function showAll(Environment $twig, Request $request, OfferRepository $offerRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response public function showAll(Request $request, OfferRepository $offerRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response
{ {
$form = $this->createForm(OfferFilterFormType::class); $form = $this->createForm(OfferFilterFormType::class);
$form->handleRequest($request); $form->handleRequest($request);
$allOffers = $offerRepository->findAll();
$filteredOffers = []; $filteredOffers = [];
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid() && $form->get('search')->getData() != null) {
$allOffers = $offerRepository->findBySearchLiteral($form->get('search')->getData());
}
else {
$allOffers = $offerRepository->findAll();
}
if ($form->isSubmitted() && $form->isValid() && $form->get('distance')->getData() != null && $form->get('zipCode')->getData() != null) {
$filterDistance = $form->get('distance')->getData(); $filterDistance = $form->get('distance')->getData();
$filterPlz = $form->get('zipCode')->getData(); $filterPlz = $form->get('zipCode')->getData();
$filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz); $filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz);
@ -144,7 +150,7 @@ class OfferController extends AbstractController
} }
#[Route('/offer/edit/{urlId}', name: 'edit_offer')] #[Route('/offer/edit/{urlId}', name: 'edit_offer')]
public function editOffer(Offer $offer, OfferRepository $offerRepository, Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response public function editOffer(Offer $offer, Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response
{ {
$form = $this->createForm(OfferFormType::class, $offer); $form = $this->createForm(OfferFormType::class, $offer);
$user = $this->getUser(); $user = $this->getUser();
@ -209,4 +215,14 @@ class OfferController extends AbstractController
'offers' => $offerRepository->findByUser($user), 'offers' => $offerRepository->findByUser($user),
]); ]);
} }
#[Route('/offers/search', name: 'search', options: ["sitemap" => false])]
public function search(OfferRepository $offerRepository): Response
{
$offers = $offerRepository->findBySearchLiteral('');
return $this->render('offer/search.html.twig', [
'offers' => $offers,
]);
}
} }

View file

@ -14,7 +14,10 @@ class OfferFilterFormType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options): void
{ {
$builder $builder
->add('zipCode', TextType::class, [ ->add('search', TextType::class, [
'label' => 'Search',
])
->add('zipCode', NumberType::class, [
'label' => 'ZIP', 'label' => 'ZIP',
]) ])
->add('distance', NumberType::class, [ ->add('distance', NumberType::class, [

View file

@ -44,6 +44,19 @@ class OfferRepository extends ServiceEntityRepository
; ;
} }
public function findBySearchLiteral(string $literal)
{
$qb = $this->createQueryBuilder('o');
$qb->andWhere($qb->expr()->like('o.title', ':lit'))
->setParameter('lit', '%' . $literal . '%')
->orderBy('o.id', 'ASC')
;
$qb = $qb->getQuery()->getResult();
return $qb;
}
/* /*
public function findOneBySomeField($value): ?Offer public function findOneBySomeField($value): ?Offer
{ {

View file

@ -20,7 +20,7 @@
<div class="btn btn-primary"><i class="fas fa-filter mr-3"></i>Filter<i class="fas fa-chevron-down ml-3 dropdown-collapse"></i></div> <div class="btn btn-primary"><i class="fas fa-filter mr-3"></i>Filter<i class="fas fa-chevron-down ml-3 dropdown-collapse"></i></div>
</a> </a>
<div class="collapse" id="collapseExample"> <div class="collapse" id="collapseExample">
{{ form(filter_form) }} {{ form(filter_form, {attr: {novalidate: 'novalidate'}}) }}
</div> </div>
</div> </div>