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
@@ -2,9 +2,9 @@
namespace App\Controller\Administrative\Assignment;
use App\Model\AssignmentFilterDto;
use App\Repository\AssignmentRepository;
use App\Service\Assignment\TimelineService;
use App\Service\Common\TimelineFilterHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
@@ -14,7 +14,8 @@ class TimelineController extends AbstractController
{
public function __construct(
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
{
$period = $this->timelineService->getActiveWindow();
$filterDto = new AssignmentFilterDto();
$filterDto
->setDateFrom($period->first()->toDateTimeImmutable())
->setDateTo($period->last()->toDateTimeImmutable())
->setIncludePast(true)
;
$this->filterHandler->setPeriod($period);
$filterDto = $this->filterHandler->getFilterSettings();
$assignments = $this
->assignmentRepository
@@ -40,6 +38,7 @@ class TimelineController extends AbstractController
return $this->render('administrative/assignment/timeline.html.twig', [
'data' => $timelineData,
'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'])
;
}
}
+7 -1
View File
@@ -17,6 +17,12 @@ abstract class AbstractFilterDto
foreach ($reflection->getProperties() as $property) {
$name = $property->getName();
// don't count properties starting with '_'
if (str_starts_with($name, '_')) {
continue;
}
if ($this->$name !== $freshInstance->$name) {
++$filterCount;
}
@@ -24,4 +30,4 @@ abstract class AbstractFilterDto
return $filterCount;
}
}
}
+1 -4
View File
@@ -2,9 +2,6 @@
namespace App\Model;
use App\Entity\Destination;
use App\Entity\JobProfile;
class AssignmentFilterDto extends AbstractFilterDto
{
public const DURATION_WEEKEND = 1;
@@ -116,4 +113,4 @@ class AssignmentFilterDto extends AbstractFilterDto
return $this;
}
}
}
+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 $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\Teamer;
use App\Model\AssignmentFilterDto;
use App\Model\TimelineFilterDto;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr\Join;
@@ -309,4 +310,29 @@ class AssignmentRepository extends ServiceEntityRepository
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) {
/** @var TimelineItem $entry */
if (
$item->getDateFrom() >= $entry->getDateFrom() && $item->getDateFrom() < $entry->getDateTo()
|| $item->getDateTo() >= $entry->getDateFrom() && $item->getDateTo() < $entry->getDateTo()
$item->getDateFrom() >= $entry->getDateFrom() && $item->getDateFrom() <= $entry->getDateTo()
|| $item->getDateTo() >= $entry->getDateFrom() && $item->getDateTo() <= $entry->getDateTo()
) {
$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(),
]);
}
}