feat: redesign the timeline as day columns with a bar per placement

This commit is contained in:
fromme committed 2026-11-12 11:14:25 +01:00
1 parent 4e50f23558
commit e976342c09
8 files changed
+413 -190

No files matched your search

@@ -44,11 +44,11 @@ class TimelineController extends AbstractController
->getListQuery($filterDto, 'destination.dateFrom', $hotelCodes)
->getResult()
;
$timelineData = $this->timelineService->preprocess($assignments);
$period = $filterDto->getPeriod();
return $this->render('administrative/assignment/timeline.html.twig', [
'data' => $timelineData,
'period' => $period,
'houses' => $this->timelineService->preprocess($assignments, $period),
'calendar' => $this->timelineService->getCalendar($period),
'filterDto' => $filterDto,
]);
}
+70
View File
@@ -0,0 +1,70 @@
<?php
namespace App\Model;
/**
* One house of the timeline: its rows of non-overlapping bars and the count of vacancies nobody
* applied for.
*/
class TimelineHouse
{
/** @var list<list<TimelineItem>> */
private array $rows = [[]];
public function __construct(
private readonly string $code,
private readonly ?string $name,
) {
}
public function getCode(): string
{
return $this->code;
}
public function getName(): ?string
{
return $this->name;
}
/**
* @return list<list<TimelineItem>>
*/
public function getRows(): array
{
return $this->rows;
}
public function getOpenSlots(): int
{
$count = 0;
foreach ($this->rows as $row) {
foreach ($row as $item) {
if (TimelineItem::STATE_OPEN === $item->getState()) {
++$count;
}
}
}
return $count;
}
/**
* Puts the item into the first row it does not overlap, or into a new one.
*/
public function addItem(TimelineItem $item): void
{
foreach ($this->rows as $index => $row) {
foreach ($row as $entry) {
if ($entry->getDateTo() >= $item->getDateFrom() && $entry->getDateFrom() <= $item->getDateTo()) {
continue 2;
}
}
$this->rows[$index][] = $item;
return;
}
$this->rows[] = [$item];
}
}
+25 -7
View File
@@ -25,6 +25,8 @@ class TimelineItem
private ?string $jobProfile = null;
private ?string $teamerName = null;
private ?string $teamerUuid = null;
private int $column = 1;
private int $span = 1;
public function __construct(private readonly string $type)
{
@@ -196,17 +198,33 @@ class TimelineItem
return $this;
}
public function getKey(): ?string
/**
* The first day column of the bar, counted from 1.
*/
public function getColumn(): int
{
return $this->getDateFrom()?->format('Ymd');
return $this->column;
}
public function getNumberOfDays(): int
public function setColumn(int $column): static
{
if (null === $this->getDateFrom() || null === $this->getDateTo()) {
return 1;
}
$this->column = $column;
return $this->getDateFrom()->diff($this->getDateTo())->days + 1;
return $this;
}
/**
* The number of day columns the bar covers.
*/
public function getSpan(): int
{
return $this->span;
}
public function setSpan(int $span): static
{
$this->span = $span;
return $this;
}
}
+72 -48
View File
@@ -2,29 +2,40 @@
namespace App\Service\Assignment;
use App\Config\HouseCatalog;
use App\Entity\Assignment;
use App\Model\TimelineHouse;
use App\Model\TimelineItem;
use Carbon\CarbonImmutable;
use Carbon\CarbonPeriodImmutable;
/**
* Service for processing assignments into timeline visualization data.
* Organizes assignments and dispositions by hotel in a timeline format,
* Organizes assignments and dispositions by house in a timeline format,
* handling overlapping dates by distributing items across multiple rows.
*/
class TimelineService
{
public function __construct(private readonly HouseCatalog $houseCatalog)
{
}
/**
* Preprocesses assignments into a timeline structure organized by hotel.
* Preprocesses assignments into a timeline structure organized by house.
* Creates timeline items for both vacant assignment slots and actual dispositions,
* distributing them across multiple rows to avoid date overlaps.
* placed on the period's day columns and distributed across rows to avoid date overlaps.
*
* @param Assignment[] $assignments Array of assignments to process
*
* @return array Multi-dimensional array: [hotelCode][rowIndex][itemKey] => TimelineItem
* @return list<TimelineHouse> sorted by house name
*/
public function preprocess(array $assignments): array
public function preprocess(array $assignments, CarbonPeriodImmutable $period): array
{
$rows = [];
$periodFrom = $period->first()->startOfDay();
$periodTo = $period->last()->startOfDay();
/** @var array<string, TimelineHouse> $houses */
$houses = [];
foreach ($assignments as $assignment) {
// exclude called off assignments
@@ -32,72 +43,70 @@ class TimelineService
if (Assignment::STATUS_CALLED_OFF === $assignment->getStatus()) {
continue;
}
$destination = $assignment->getDestination();
$hotelCode = $destination->getHotelCodeNormalized();
$hotelCode = $assignment->getDestination()->getHotelCodeNormalized();
// add first row for current hotel unless existing
if (false === isset($rows[$hotelCode])) {
$rows[$hotelCode] = [[]];
}
$houses[$hotelCode] ??= new TimelineHouse($hotelCode, $this->houseCatalog->getName($hotelCode));
// called-off dispositions free their slot again
$dispositions = $assignment->getActiveDispositions();
$items = [];
// Create items for the vacant slots, if any
$item = TimelineItem::fromAssignment($assignment);
for ($i = $dispositions->count(); $i < $assignment->getAvailableDispositions(); ++$i) {
$this->addItem($rows[$hotelCode], $item);
$items[] = TimelineItem::fromAssignment($assignment);
}
// Create items for actual dispositions
foreach ($dispositions as $disposition) {
$this->addItem($rows[$hotelCode], TimelineItem::fromDisposition($disposition));
$items[] = TimelineItem::fromDisposition($disposition);
}
foreach ($items as $item) {
// an item without dates has no place on the timeline
if (null === $item->getDateFrom() || null === $item->getDateTo()) {
continue;
}
$this->place($item, $periodFrom, $periodTo);
$houses[$hotelCode]->addItem($item);
}
}
return $rows;
usort($houses, fn (TimelineHouse $a, TimelineHouse $b) => strcmp($a->getName() ?? $a->getCode(), $b->getName() ?? $b->getCode()));
return $houses;
}
/**
* Adds a timeline item to the appropriate row, ensuring no date overlaps.
* Finds the first available row where the item doesn't overlap with existing items,
* or creates a new row if necessary.
* The days of the period with what the header and the background need, and the months
* spanning them. Columns count from 1.
*
* @param array $rows Reference to the rows array for a specific hotel
* @param TimelineItem $item The timeline item to add
* @return array{days: list<array{date: CarbonImmutable, column: int, weekend: bool, today: bool, monthStart: bool}>, months: list<array{label: string, column: int, span: int}>}
*/
private function addItem(array &$rows, TimelineItem $item): void
public function getCalendar(CarbonPeriodImmutable $period): array
{
// The key identifies the item within its row, so one without it cannot be placed.
$key = $item->getKey();
if (null === $key) {
return;
}
$days = [];
$months = [];
$today = CarbonImmutable::today();
$rowIndex = 0;
$fitsRow = false;
foreach ($period as $index => $day) {
$day = $day->startOfDay()->locale('de');
$column = $index + 1;
$monthStart = 1 === $column || 1 === $day->day;
// Find first row where item doesn't overlap with existing items
foreach ($rows as $index => $row) {
$fitsRow = true;
foreach ($row as $entry) {
/** @var TimelineItem $entry */
// Check for date overlap: item overlaps if its end >= entry start AND item start <= entry end
if ($entry->getDateTo() >= $item->getDateFrom() && $entry->getDateFrom() <= $item->getDateTo()) {
$fitsRow = false;
}
}
$rowIndex = $index;
if (true === $fitsRow) {
break;
$days[] = [
'date' => $day,
'column' => $column,
'weekend' => $day->isWeekend(),
'today' => $day->equalTo($today),
'monthStart' => $monthStart,
];
if (true === $monthStart) {
$months[] = ['label' => $day->isoFormat('MMMM YYYY'), 'column' => $column, 'span' => 0];
}
++$months[array_key_last($months)]['span'];
}
// If no existing row fits, create a new row
if (false === $fitsRow) {
++$rowIndex;
}
$rows[$rowIndex][$key] = $item;
return ['days' => $days, 'months' => $months];
}
/**
@@ -114,4 +123,19 @@ class TimelineService
return CarbonPeriodImmutable::create($dateFrom, $dateTo);
}
/**
* Sets the item's day columns, clipped to the period: the timeline also shows trips that
* started before it or end after it.
*/
private function place(TimelineItem $item, CarbonImmutable $periodFrom, CarbonImmutable $periodTo): void
{
$from = max($periodFrom, CarbonImmutable::instance($item->getDateFrom())->startOfDay());
$to = min($periodTo, CarbonImmutable::instance($item->getDateTo())->startOfDay());
$item
->setColumn((int) $periodFrom->diffInDays($from) + 1)
->setSpan(max(1, (int) $from->diffInDays($to) + 1))
;
}
}