feat: filtering of custom destinations by hotel

This commit is contained in:
Björn Fromme
2025-07-22 17:42:06 +02:00
parent ea574427fb
commit e0ce495b86
8 changed files with 233 additions and 1 deletions
@@ -3,6 +3,7 @@
namespace App\Controller\Administrative\System\Destination;
use App\Repository\DestinationRepository;
use App\Service\Common\DestinationFilterHandler;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@@ -15,6 +16,7 @@ class IndexController extends AbstractController
public function __construct(
private readonly DestinationRepository $destinationRepository,
private readonly PaginatorInterface $paginator,
private readonly DestinationFilterHandler $filterHandler
) {
}
@@ -22,9 +24,11 @@ class IndexController extends AbstractController
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Request $request): Response
{
$filterDto = $this->filterHandler->getFilterSettings();
$query = $this
->destinationRepository
->getListQueryForCustom()
->getListQuery($filterDto)
;
$pagination = $this->paginator->paginate(
@@ -39,6 +43,7 @@ class IndexController extends AbstractController
return $this->render('administrative/system/destination/index.html.twig', [
'pagination' => $pagination,
'filterDto' => $filterDto,
]);
}
}
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Controller\Common;
use App\Controller\Traits\ReturnUrlTrait;
use App\Form\DestinationFilterType;
use App\Htmx\HxRedirectResponse;
use App\Service\Common\DestinationFilterHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class DestinationFilterController extends AbstractController
{
use ReturnUrlTrait;
public function __construct(
private readonly DestinationFilterHandler $filterHandler
) {
}
#[Route('/common/destination/filter', name: 'app_common_destination_filter')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Request $request): Response
{
$formData = $this->filterHandler->getFilterSettings();
$form = $this->createForm(DestinationFilterType::class, $formData);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->filterHandler->handleRequest($form);
$returnUrl = $this->getReturnUrl($request, 'app_administrative_system_destination_index');
return new HxRedirectResponse($returnUrl);
}
return $this->render('common/modal_destination_filter.html.twig', [
'filterForm' => $form->createView(),
'filterDto' => $form->getData(),
]);
}
#[Route('/common/destination/filter/reset', name: 'app_common_destination_filter_reset')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function reset(Request $request): Response
{
$this->filterHandler->resetFilterSettings();
$returnUrl = $this->getReturnUrl($request, 'app_administrative_system_destination_index');
return $this->redirect($returnUrl);
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace App\Form;
use App\Model\DestinationFilterDto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DestinationFilterType extends AbstractType
{
private array $hotelChoices = [];
public function __construct(private readonly array $destinations)
{
$destinations = $this->destinations;
sort($destinations);
foreach ($destinations as $item) {
$this->hotelChoices[$item] = $item;
}
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('hotels', MultiselectType::class, [
'label' => 'Haus/Destination',
'required' => false,
'empty_label' => 'nicht filtern',
'choices' => $this->hotelChoices,
])
->add('apply', SubmitType::class, [
'label' => 'filtern',
])
->add('reset', SubmitType::class, [
'label' => 'reset',
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => DestinationFilterDto::class,
]);
}
}
+22
View File
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Model;
class DestinationFilterDto extends AbstractFilterDto
{
protected array $hotels = [];
public function getHotels(): ?array
{
return $this->hotels;
}
public function setHotels(array $hotels): static
{
$this->hotels = $hotels;
return $this;
}
}
+26
View File
@@ -3,6 +3,7 @@
namespace App\Repository;
use App\Entity\Destination;
use App\Model\DestinationFilterDto;
use App\Repository\Traits\QueryHelperTrait;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
@@ -25,6 +26,31 @@ class DestinationRepository extends ServiceEntityRepository
parent::__construct($registry, Destination::class);
}
public function getListQuery(DestinationFilterDto $filterDto): Query
{
$qb = $this->createQueryBuilder('destination');
$qb->where(
$qb->expr()->andX(
$qb->expr()->isNull('destination.deletedAt'),
$qb->expr()->isNull('destination.busProId')
)
);
if (0 < count($filterDto->getHotels())) {
$constraints = [];
foreach ($filterDto->getHotels() as $index => $hotel) {
$constraints[] = $qb->expr()->like('destination.hotel', ':hotel'.$index);
}
$qb->andWhere($qb->expr()->orX(...$constraints));
foreach ($filterDto->getHotels() as $index => $hotel) {
$qb->setParameter('hotel'.$index, '%'.$hotel.'%');
}
}
return $qb->getQuery();
}
public function getListQueryForCustom(): Query
{
$qb = $this->createQueryBuilder('destination');
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Service\Common;
use App\Model\AbstractFilterDto;
use App\Model\DestinationFilterDto;
class DestinationFilterHandler extends AbstractFilterHandler
{
protected string $namespace = 'filter:destination';
protected string $modelClass = DestinationFilterDto::class;
public function getFilterSettings(): DestinationFilterDto
{
$filterDto = new $this->modelClass();
if (null === $data = $this->getSession()->get($this->namespace)) {
return $filterDto;
}
if (isset($data['hotels']) && 0 < count($data['hotels'])) {
$filterDto->setHotels($data['hotels']);
}
return $filterDto;
}
protected function saveFilterSettings(AbstractFilterDto $filterDto): void
{
/** @var DestinationFilterDto $filterDto */
$this->getSession()->set($this->namespace, [
'hotels' => $filterDto->getHotels(),
]);
}
}
@@ -7,6 +7,26 @@
<h1 class="text-2xl font-bold">
Manuell angelegte Reisen
</h1>
<div class="flex flex-col items-end space-y-2 md:flex-row md:items-center md:space-x-2 md:space-y-0">
<button type="button"
class="{{ html_classes('btn btn--small', { 'btn--secondary': filterDto.active }) }}"
title="Filter"
hx-get="{{ path('app_common_destination_filter', { 'r': return_url() }) }}"
hx-target="body"
hx-swap="beforeend">
{{ icon('filter', 'w-4 h-4 shrink-0') }}
{% if filterDto.active %}
<span class="whitespace-nowrap">{{ filterDto.activeFiltersCount }} Filter aktiv</span>
{% else %}
<span>Filter</span>
{% endif %}
</button>
{% if filterDto.active %}
<a href="{{ path('app_common_destination_filter_reset', { 'r': return_url() }) }}" class="btn btn--small btn--secondary">
Reset
</a>
{% endif %}
</div>
</div>
<div class="data-table-wrapper">
@@ -0,0 +1,16 @@
{% extends 'htmx_modal.html.twig' %}
{% block title %}Destinationen filtern{% endblock %}
{% block content %}
{{ form_start(filterForm, { 'attr': { 'hx-post': app.request.uri, 'hx-target': '#htmx-modal', 'hx-swap': 'outerHTML' } }) }}
<div class="flex flex-col space-y-2 pb-4">
{{ form_row(filterForm.hotels) }}
</div>
<div class="flex items-center space-x-2">
{{ form_widget(filterForm.apply, { 'attr': { 'class': 'btn' } }) }}
{{ form_widget(filterForm.reset, { 'attr': { 'class': 'btn btn--secondary' } }) }}
</div>
{{ form_rest(filterForm) }}
{{ form_end(filterForm) }}
{% endblock %}