diff --git a/src/Controller/OfferController.php b/src/Controller/OfferController.php index 5468814..5edc4af 100644 --- a/src/Controller/OfferController.php +++ b/src/Controller/OfferController.php @@ -33,15 +33,21 @@ class OfferController extends AbstractController } #[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->handleRequest($request); - $allOffers = $offerRepository->findAll(); $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(); $filterPlz = $form->get('zipCode')->getData(); $filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz); @@ -144,7 +150,7 @@ class OfferController extends AbstractController } #[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); $user = $this->getUser(); @@ -209,4 +215,14 @@ class OfferController extends AbstractController '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, + ]); + } } diff --git a/src/Form/OfferFilterFormType.php b/src/Form/OfferFilterFormType.php index 45cfff7..5a6c36a 100644 --- a/src/Form/OfferFilterFormType.php +++ b/src/Form/OfferFilterFormType.php @@ -14,7 +14,10 @@ class OfferFilterFormType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('zipCode', TextType::class, [ + ->add('search', TextType::class, [ + 'label' => 'Search', + ]) + ->add('zipCode', NumberType::class, [ 'label' => 'ZIP', ]) ->add('distance', NumberType::class, [ diff --git a/src/Repository/OfferRepository.php b/src/Repository/OfferRepository.php index b2dbdf7..492095b 100644 --- a/src/Repository/OfferRepository.php +++ b/src/Repository/OfferRepository.php @@ -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 { diff --git a/templates/offer/index.html.twig b/templates/offer/index.html.twig index d61a397..76cca68 100644 --- a/templates/offer/index.html.twig +++ b/templates/offer/index.html.twig @@ -20,7 +20,7 @@