feat: filter timeline by date range and hotels

This commit is contained in:
Björn Fromme
2025-03-21 11:52:29 +01:00
parent 9ffada111c
commit 06c32fd597
14 changed files with 316 additions and 28 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
.scroll-container { .scroll-container {
@apply overflow-scroll; @apply overflow-scroll;
max-height: calc(100vh - 140px); max-height: calc(100vh - 240px);
} }
@@ -2,9 +2,9 @@
namespace App\Controller\Administrative\Assignment; namespace App\Controller\Administrative\Assignment;
use App\Model\AssignmentFilterDto;
use App\Repository\AssignmentRepository; use App\Repository\AssignmentRepository;
use App\Service\Assignment\TimelineService; use App\Service\Assignment\TimelineService;
use App\Service\Common\TimelineFilterHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
@@ -14,7 +14,8 @@ class TimelineController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly AssignmentRepository $assignmentRepository, private readonly AssignmentRepository $assignmentRepository,
private readonly TimelineService $timelineService private readonly TimelineService $timelineService,
private readonly TimelineFilterHandler $filterHandler
) { ) {
} }
@@ -23,12 +24,9 @@ class TimelineController extends AbstractController
public function index(): Response public function index(): Response
{ {
$period = $this->timelineService->getActiveWindow(); $period = $this->timelineService->getActiveWindow();
$filterDto = new AssignmentFilterDto(); $this->filterHandler->setPeriod($period);
$filterDto
->setDateFrom($period->first()->toDateTimeImmutable()) $filterDto = $this->filterHandler->getFilterSettings();
->setDateTo($period->last()->toDateTimeImmutable())
->setIncludePast(true)
;
$assignments = $this $assignments = $this
->assignmentRepository ->assignmentRepository
@@ -40,6 +38,7 @@ class TimelineController extends AbstractController
return $this->render('administrative/assignment/timeline.html.twig', [ return $this->render('administrative/assignment/timeline.html.twig', [
'data' => $timelineData, 'data' => $timelineData,
'period' => $period, 'period' => $period,
'filterDto' => $filterDto,
]); ]);
} }
} }
@@ -0,0 +1,62 @@
<?php
namespace App\Controller\Common;
use App\Controller\Traits\ReturnUrlTrait;
use App\Form\TimelineFilterType;
use App\Htmx\HxRedirectResponse;
use App\Service\Assignment\TimelineService;
use App\Service\Common\TimelineFilterHandler;
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 TimelineFilterController extends AbstractController
{
use ReturnUrlTrait;
public function __construct(
private readonly TimelineFilterHandler $filterHandler,
private readonly TimelineService $timelineService,
) {
$period = $this->timelineService->getActiveWindow();
$this->filterHandler->setPeriod($period);
}
#[Route('/common/timeline/filter', name: 'app_common_timeline_filter')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Request $request): Response
{
$formData = $this->filterHandler->getFilterSettings();
$formOptions = [
'min_date' => $formData->getPeriod()->first()->toDateTimeImmutable(),
'max_date' => $formData->getPeriod()->last()->toDateTimeImmutable(),
];
$form = $this->createForm(TimelineFilterType::class, $formData, $formOptions);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->filterHandler->handleRequest($form);
$returnUrl = $this->getReturnUrl($request, 'app_administrative_assignment_timeline');
return new HxRedirectResponse($returnUrl);
}
return $this->render('common/modal_timeline_filter.html.twig', [
'filterForm' => $form->createView(),
'filterDto' => $form->getData(),
]);
}
#[Route('/common/timeline/filter/reset', name: 'app_common_timeline_filter_reset')]
public function reset(Request $request): Response
{
$this->filterHandler->resetFilterSettings();
$returnUrl = $this->getReturnUrl($request, 'app_administrative_assignment_timeline');
return $this->redirect($returnUrl);
}
}
+75
View File
@@ -0,0 +1,75 @@
<?php
namespace App\Form;
use App\Model\TimelineFilterDto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TimelineFilterType 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('dateFrom', DatepickerType::class, [
'label' => 'Zeitraum von',
'required' => false,
'min_date' => $options['min_date'],
'max_date' => $options['max_date'],
'attr' => [
'placeholder' => 'nicht filtern',
],
])
->add('dateTo', DatepickerType::class, [
'label' => 'Zeitraum bis',
'required' => false,
'min_date' => $options['min_date'],
'max_date' => $options['max_date'],
'attr' => [
'placeholder' => 'nicht filtern',
],
])
->add('apply', SubmitType::class, [
'label' => 'filtern',
])
->add('reset', SubmitType::class, [
'label' => 'reset',
])
;
$builder
->add('hotels', MultiselectType::class, [
'label' => 'Haus/Destination',
'required' => false,
'empty_label' => 'nicht filtern',
'choices' => $this->hotelChoices,
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'data_class' => TimelineFilterDto::class,
'min_date' => null,
'max_date' => null,
])
->setAllowedTypes('min_date', [\DateTimeImmutable::class, 'null'])
->setAllowedTypes('max_date', [\DateTimeImmutable::class, 'null'])
;
}
}
+6
View File
@@ -17,6 +17,12 @@ abstract class AbstractFilterDto
foreach ($reflection->getProperties() as $property) { foreach ($reflection->getProperties() as $property) {
$name = $property->getName(); $name = $property->getName();
// don't count properties starting with '_'
if (str_starts_with($name, '_')) {
continue;
}
if ($this->$name !== $freshInstance->$name) { if ($this->$name !== $freshInstance->$name) {
++$filterCount; ++$filterCount;
} }
-3
View File
@@ -2,9 +2,6 @@
namespace App\Model; namespace App\Model;
use App\Entity\Destination;
use App\Entity\JobProfile;
class AssignmentFilterDto extends AbstractFilterDto class AssignmentFilterDto extends AbstractFilterDto
{ {
public const DURATION_WEEKEND = 1; public const DURATION_WEEKEND = 1;
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace App\Model;
use Carbon\CarbonPeriod;
class TimelineFilterDto extends AssignmentFilterDto
{
protected ?CarbonPeriod $_period = null;
public function getPeriod(): ?CarbonPeriod
{
return $this->_period;
}
public function setPeriod(?CarbonPeriod $_period): static
{
$this->_period = $_period;
return $this;
}
}
+1 -1
View File
@@ -159,6 +159,6 @@ class TimelineItem
return 1; return 1;
} }
return $this->getDateFrom()->diff($this->getDateTo())->days; return $this->getDateFrom()->diff($this->getDateTo())->days + 1;
} }
} }
+26
View File
@@ -6,6 +6,7 @@ use App\Entity\Application;
use App\Entity\Assignment; use App\Entity\Assignment;
use App\Entity\Teamer; use App\Entity\Teamer;
use App\Model\AssignmentFilterDto; use App\Model\AssignmentFilterDto;
use App\Model\TimelineFilterDto;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
@@ -309,4 +310,29 @@ class AssignmentRepository extends ServiceEntityRepository
return array_column($availableAssignments, 'id'); return array_column($availableAssignments, 'id');
} }
public function getSelectableHotelCodes(): array
{
$qb = $this->createQueryBuilder('assignment');
$codes = [];
$result = $qb
->select('assignment.id', 'destination.hotelCode')
->innerJoin('assignment.destination', 'destination')
->groupBy('destination.hotelCode')
->orderBy('destination.hotelCode', 'ASC')
->getQuery()
->getArrayResult();
foreach ($result as $row) {
if (str_starts_with($row['hotelCode'], 'SER')) {
$codes[] = substr($row['hotelCode'], 2, 3);
} else {
$codes[] = substr($row['hotelCode'], 0, 3);
}
}
return array_unique($codes);
}
} }
+2 -2
View File
@@ -49,8 +49,8 @@ class TimelineService
foreach ($row as $entry) { foreach ($row as $entry) {
/** @var TimelineItem $entry */ /** @var TimelineItem $entry */
if ( if (
$item->getDateFrom() >= $entry->getDateFrom() && $item->getDateFrom() < $entry->getDateTo() $item->getDateFrom() >= $entry->getDateFrom() && $item->getDateFrom() <= $entry->getDateTo()
|| $item->getDateTo() >= $entry->getDateFrom() && $item->getDateTo() < $entry->getDateTo() || $item->getDateTo() >= $entry->getDateFrom() && $item->getDateTo() <= $entry->getDateTo()
) { ) {
$addToRow = false; $addToRow = false;
} }
@@ -0,0 +1,60 @@
<?php
namespace App\Service\Common;
use App\Model\AbstractFilterDto;
use App\Model\TimelineFilterDto;
use Carbon\CarbonPeriod;
class TimelineFilterHandler extends AbstractFilterHandler
{
protected string $namespace = 'filter:timeline';
protected ?CarbonPeriod $period = null;
public function setPeriod(CarbonPeriod $period): void
{
$this->period = $period;
}
public function getFilterSettings(): TimelineFilterDto
{
if (null === $data = $this->getSession()->get($this->namespace)) {
$filterDto = new TimelineFilterDto();
$filterDto->setPeriod($this->period);
return $filterDto;
}
$filterDto = new TimelineFilterDto();
// override dates from form in case outside of selectable period
$periodFrom = $this->period->first()->toDateTimeImmutable();
$periodTo = $this->period->last()->toDateTimeImmutable();
if (isset($data['date_from'])) {
$dateFrom = max($periodFrom, $data['date_from']);
$periodFrom = $dateFrom;
}
if (isset($data['date_to'])) {
$dateTo = max($periodTo, $data['date_to']);
$periodTo = $dateTo;
}
if (isset($data['hotels']) && 0 < count($data['hotels'])) {
$filterDto->setHotels($data['hotels']);
}
$filterDto->setPeriod(new CarbonPeriod($periodFrom, $periodTo));
return $filterDto;
}
protected function saveFilterSettings(AbstractFilterDto $filterDto): void
{
/** @var TimelineFilterDto $filterDto */
$this->getSession()->set($this->namespace, [
'date_from' => $filterDto->getDateFrom(),
'date_to' => $filterDto->getDateTo(),
'hotels' => $filterDto->getHotels(),
]);
}
}
@@ -1,13 +1,38 @@
{% extends 'administrative/layout.html.twig' %} {% extends 'administrative/layout.html.twig' %}
{% block title %}Einsatzübersicht{% endblock %} {% block title %}Timeline{% endblock %}
{% block content %} {% block content %}
<div class="flex items-start justify-between pb-4">
<h1 class="text-2xl font-bold">
Timeline
</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_timeline_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_timeline_filter_reset', { 'r': return_url() }) }}" class="btn btn--small btn--secondary">
Reset
</a>
{% endif %}
</div>
</div>
<div class="scroll-container border border-gray-300 mb-16"> <div class="scroll-container border border-gray-300 mb-16">
<table> <table>
<tr class="[&_th]:sticky [&_th]:top-0 [&_th]:z-10"> <tr class="[&_th]:sticky [&_th]:top-0 [&_th]:z-10">
<th></th> <th></th>
{% for day in period %} {% for day in filterDto.period %}
<th> <th>
{{ day | date('d.m.Y') }} {{ day | date('d.m.Y') }}
</th> </th>
@@ -18,7 +43,7 @@
<td class="border border-gray-700 bg-gray-200 align-middle font-bold sticky left-0 z-10" rowspan="{{ rows | length }}"> <td class="border border-gray-700 bg-gray-200 align-middle font-bold sticky left-0 z-10" rowspan="{{ rows | length }}">
{{ hotelCode }} {{ hotelCode }}
</td> </td>
{% for day in period %} {% for day in filterDto.period %}
{% if colspan is defined and colspan > 1 %} {% if colspan is defined and colspan > 1 %}
{% set colspan = colspan - 1 %} {% set colspan = colspan - 1 %}
{% elseif rows[0][day|date('Ymd')] is defined %} {% elseif rows[0][day|date('Ymd')] is defined %}
@@ -39,10 +64,8 @@
<span class="text-xs">{{ item.teamerName | raw }}</span> <span class="text-xs">{{ item.teamerName | raw }}</span>
{{ icon('info', 'w-4 h-4') }} {{ icon('info', 'w-4 h-4') }}
</button> </button>
<span class="text-xs">{{ item.jobProfile }}</span>
{% else %}
<span class="text-xs">{{ item.jobProfile }}</span>
{% endif %} {% endif %}
<span class="text-xs">{{ item.jobProfile }}</span>
</td> </td>
{% else %} {% else %}
<td class="border border-gray-700"></td> <td class="border border-gray-700"></td>
@@ -51,7 +74,7 @@
</tr> </tr>
{% for row in rows[1:] %} {% for row in rows[1:] %}
<tr> <tr>
{% for day in period %} {% for day in filterDto.period %}
{% if colspan is defined and colspan > 1 %} {% if colspan is defined and colspan > 1 %}
{% set colspan = colspan - 1 %} {% set colspan = colspan - 1 %}
{% elseif row[day|date('Ymd')] is defined %} {% elseif row[day|date('Ymd')] is defined %}
@@ -72,10 +95,8 @@
<span class="text-xs">{{ item.teamerName | raw }}</span> <span class="text-xs">{{ item.teamerName | raw }}</span>
{{ icon('info', 'w-4 h-4') }} {{ icon('info', 'w-4 h-4') }}
</button> </button>
<span class="text-xs">{{ item.jobProfile }}</span>
{% else %}
<span class="text-xs">{{ item.jobProfile }}</span>
{% endif %} {% endif %}
<span class="text-xs">{{ item.jobProfile }}</span>
</td> </td>
{% else %} {% else %}
<td class="border border-gray-700"></td> <td class="border border-gray-700"></td>
@@ -0,0 +1,20 @@
{% extends 'htmx_modal.html.twig' %}
{% block title %}Timeline 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 flex-1">
<div class="grid grid-cols-2 gap-x-4 gap-y-2">
{{ form_row(filterForm.dateFrom) }}
{{ form_row(filterForm.dateTo) }}
</div>
{{ form_row(filterForm.hotels) }}
<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>
</div>
{{ form_rest(filterForm) }}
{{ form_end(filterForm) }}
{% endblock %}
+1 -1
View File
@@ -12,7 +12,7 @@
{{ icon('close', 'w-6 h-6')}} {{ icon('close', 'w-6 h-6')}}
</button> </button>
</div> </div>
<div class="max-h-96 overflow-y-scroll"> <div class="h-96 overflow-y-scroll">
<div class="px-8"> <div class="px-8">
{% block content %}{% endblock %} {% block content %}{% endblock %}
</div> </div>