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
+382 -159

No files matched your search

@@ -1,22 +1,26 @@
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = [ 'today' ]
static targets = [ 'scroller', 'today' ]
connect() {
if (false === this.hasTodayTarget) {
requestAnimationFrame(() => this.scrollToToday({ behavior: 'instant' }))
}
// centres today's column; the house column stays in front of the left part
scrollToToday({ behavior = 'smooth' } = {}) {
if (false === this.hasScrollerTarget || false === this.hasTodayTarget) {
return
}
requestAnimationFrame(() => {
const containerRect = this.element.getBoundingClientRect()
const scroller = this.scrollerTarget
const scrollerRect = scroller.getBoundingClientRect()
const todayRect = this.todayTarget.getBoundingClientRect()
const scrollLeft = todayRect.left - containerRect.left + this.element.scrollLeft
const centered = scrollLeft - (this.element.clientWidth / 2) + (todayRect.width / 2)
const maxScrollLeft = this.element.scrollWidth - this.element.clientWidth
const scrollLeft = todayRect.left - scrollerRect.left + scroller.scrollLeft
const centered = scrollLeft - (scroller.clientWidth / 2) + (todayRect.width / 2)
const maxScrollLeft = scroller.scrollWidth - scroller.clientWidth
this.element.scrollLeft = Math.min(Math.max(0, centered), maxScrollLeft)
})
scroller.scrollTo({ left: Math.min(Math.max(0, centered), maxScrollLeft), behavior })
}
}
+54 -2
View File
@@ -1,4 +1,56 @@
.scroll-container {
/*
* The timeline (administrative/assignment/timeline.html.twig): one grid per house with a fixed
* house column and one column per day. The header, the background and every house share the
* column template, so their columns line up; --timeline-days is set on .timeline.
*/
.timeline {
--timeline-label: 11rem;
--timeline-day: 1.75rem;
@apply overflow-auto;
max-height: calc(100vh - 320px);
max-height: calc(100vh - 260px);
}
.timeline-grid {
display: grid;
grid-template-columns: var(--timeline-label) repeat(var(--timeline-days), var(--timeline-day));
}
.timeline-background {
@apply pointer-events-none absolute inset-0;
grid-template-rows: 100%;
}
.timeline-header {
@apply sticky top-0 z-20 border-b-2 border-brand bg-white;
}
.timeline-corner {
@apply sticky left-0 z-10 border-r border-line bg-white;
}
.timeline-house {
@apply relative border-b border-line py-1;
grid-auto-rows: 1.75rem;
}
.timeline-house-label {
@apply sticky left-0 z-10 -my-1 border-r border-line bg-white px-3 py-2;
}
.timeline-bar {
@apply relative z-[1] mx-px my-0.5 flex min-w-0 items-center gap-x-1 overflow-hidden whitespace-nowrap px-1.5 text-xs leading-none;
}
/* the states differ in shape too, not only in colour: dashed, outlined, filled */
.timeline-bar--open {
@apply border border-dashed border-muted bg-white text-muted;
}
.timeline-bar--pending {
@apply border border-brand bg-brand-light text-brand-dark;
}
.timeline-bar--staffed {
@apply border border-emerald-800 bg-emerald-700 text-white;
}
@@ -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];
}
}
+24 -6
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;
}
return $this->getDateFrom()->diff($this->getDateTo())->days + 1;
/**
* 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;
}
}
+75 -51
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();
foreach ($period as $index => $day) {
$day = $day->startOfDay()->locale('de');
$column = $index + 1;
$monthStart = 1 === $column || 1 === $day->day;
$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'];
}
$rowIndex = 0;
$fitsRow = false;
// 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;
}
}
// 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))
;
}
}
@@ -2,51 +2,48 @@
{% block title %}Zeitleiste{% endblock %}
{% macro cell(item, colspan) %}
<td class="{{ html_classes('border border-gray-700', {
'bg-gray-100': item.state == 'open',
'bg-blue-500 text-gray-50': item.state == 'pending',
'bg-green-500 text-gray-50': item.state == 'staffed',
}) }}" colspan="{{ colspan }}">
{% if item.type == 'disposition' %}
<div class="flex items-center space-x-2">
{% set state_labels = { open: 'offen', pending: 'in Bewerbung', staffed: 'besetzt' } %}
{% macro bar(item, row, state_labels) %}
{% set is_disposition = item.type == 'disposition' %}
<div class="timeline-bar timeline-bar--{{ item.state }}"
style="grid-row: {{ row }}; grid-column: {{ item.column + 1 }} / span {{ item.span }}"
title="{{ item.jobProfile }}{{ is_disposition ? ' · ' ~ item.teamerName }} · {{ item.dateRange }} · {{ state_labels[item.state] }}">
<span class="sr-only">{{ state_labels[item.state] }}:</span>
{% if not is_disposition and item.state == 'open' %}
<span class="font-bold uppercase">offen</span>
{% endif %}
{% if is_granted('ROLE_HOUSE_MANAGER') %}
<span class="inline-block text-xs font-semibold">{{ item.jobProfile }}</span>
<span class="truncate">{{ item.jobProfile }}</span>
{% else %}
<a href="{{ path('app_administrative_assignment_detail', { 'uuid': item.assignmentUuid, 'r': return_url() }) }}"
class="inline-block text-xs font-semibold">
{{ item.jobProfile }}
</a>
class="truncate hover:underline">{{ item.jobProfile }}</a>
{% endif %}
<span>|</span>
{% if is_disposition %}
<span aria-hidden="true">·</span>
<button type="button"
class="flex items-center space-x-2"
class="truncate font-bold hover:underline"
title="Teamer:inneninfo {{ item.teamerName }}"
hx-get="{{ path('app_administrative_teamer_info', { 'uuid': item.teamerUuid, 'r': return_url() }) }}"
hx-target="body"
hx-swap="beforeend">
<span class="text-xs font-semibold">{{ item.teamerName }}</span>
</button>
hx-swap="beforeend">{{ item.teamerName }}</button>
{% endif %}
</div>
{% else %}
{% if is_granted('ROLE_HOUSE_MANAGER') %}
<span class="text-xs font-semibold">{{ item.jobProfile }}</span>
{% else %}
<a href="{{ path('app_administrative_assignment_detail', { 'uuid': item.assignmentUuid, 'r': return_url() }) }}"
class="text-xs font-semibold">
{{ item.jobProfile }}
</a>
{% endif %}
{% endif %}
</td>
{% endmacro %}
{% block content %}
<div {{ stimulus_controller('timeline-scroll') }}>
<div class="flex items-start justify-between pb-4">
<h1 class="text-2xl font-bold">
Zeitleiste
</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="btn btn--small btn--secondary"
{{ stimulus_action('timeline-scroll', 'scrollToToday') }}>
{{ icon('calendar', 'w-4 h-4 shrink-0') }}
<span>Heute</span>
</button>
<button type="button"
class="{{ html_classes('btn btn--small', { 'btn--secondary': filterDto.active }) }}"
title="Filter"
@@ -67,64 +64,76 @@
{% endif %}
</div>
</div>
<div class="scroll-container border border-gray-300 mb-4" {{ stimulus_controller('timeline-scroll') }}>
<table>
<tr class="[&_th]:sticky [&_th]:top-0 [&_th]:z-10">
<th></th>
{% for day in filterDto.period %}
<th{% if day|date('Y-m-d') == 'now'|date('Y-m-d') %} {{ stimulus_target('timeline-scroll', 'today') }}{% endif %}>
{{ day | date('d.m.Y') }}
</th>
{% endfor %}
</tr>
{% for hotelCode, rows in data %}
<tr class="border-t-2 border-gray-700">
<td class="border border-gray-700 bg-gray-200 align-middle font-bold sticky left-0 z-10" rowspan="{{ rows | length }}">
{{ hotelCode }}
</td>
{% for day in filterDto.period %}
{% if colspan is defined and colspan > 1 %}
{% set colspan = colspan - 1 %}
{% elseif rows[0][day|date('Ymd')] is defined %}
{% set item = rows[0][day|date('Ymd')] %}
{% set colspan = item.numberOfDays %}
{{ _self.cell(item, colspan) }}
{% if houses is empty %}
<div class="card p-4 mb-4 text-muted">Keine Ergebnisse</div>
{% else %}
<td class="border border-gray-700"></td>
<div class="timeline card mb-4"
style="--timeline-days: {{ calendar.days|length }}"
{{ stimulus_target('timeline-scroll', 'scroller') }}>
<div class="relative w-max min-w-full">
{# weekends, month boundaries and today, behind the bars #}
<div class="timeline-grid timeline-background" aria-hidden="true">
{% for day in calendar.days %}
{% if day.weekend or day.today or day.monthStart %}
<div class="{{ html_classes({
'bg-page': day.weekend,
'border-l border-slate-300': day.monthStart and day.column > 1,
}) }}" style="grid-column: {{ day.column + 1 }}">
{% if day.today %}
<div class="mx-auto h-full w-0.5 bg-brand-bright"></div>
{% endif %}
</div>
{% endif %}
{% endfor %}
</tr>
{% for row in rows[1:] %}
<tr>
{% for day in filterDto.period %}
{% if colspan is defined and colspan > 1 %}
{% set colspan = colspan - 1 %}
{% elseif row[day|date('Ymd')] is defined %}
{% set item = row[day|date('Ymd')] %}
{% set colspan = item.numberOfDays %}
{{ _self.cell(item, colspan) }}
{% else %}
<td class="border border-gray-700"></td>
</div>
<div class="timeline-grid timeline-header">
<div class="timeline-corner" style="grid-row: 1 / span 2"></div>
{% for month in calendar.months %}
<div class="{{ html_classes('truncate px-2 py-1 text-xs font-bold uppercase tracking-wide text-brand', { 'border-l border-slate-300': not loop.first }) }}"
style="grid-row: 1; grid-column: {{ month.column + 1 }} / span {{ month.span }}">
{{ month.label }}
</div>
{% endfor %}
{% for day in calendar.days %}
<div class="{{ html_classes('flex flex-col items-center py-1 text-[11px] leading-tight', {
'text-muted': day.weekend and not day.today,
'bg-brand-bright text-white font-bold': day.today,
}) }}"
style="grid-row: 2; grid-column: {{ day.column + 1 }}"
{% if day.today %}{{ stimulus_target('timeline-scroll', 'today') }}{% endif %}>
<span class="font-bold">{{ day.date.day }}</span>
<span>{{ day.date.isoFormat('dd') }}</span>
</div>
{% endfor %}
</div>
{% for house in houses %}
<div class="timeline-grid timeline-house">
<div class="timeline-house-label" style="grid-row: 1 / span {{ house.rows|length }}">
<span class="block truncate font-bold">{{ house.name ?? house.code }}</span>
<span class="block text-xs text-muted">
{{ house.code }}{% if house.openSlots > 0 %} · <span class="font-bold text-ink">{{ house.openSlots }} offen</span>{% endif %}
</span>
</div>
{% for row in house.rows %}
{% set row_number = loop.index %}
{% for item in row %}
{{ _self.bar(item, row_number, state_labels) }}
{% endfor %}
{% endfor %}
</div>
{% endfor %}
</div>
</div>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
{% else %}
<td colspan="{{ filterDto.period | length }}">
Keine Ergebnisse
</td>
{% endfor %}
</table>
</div>
<div class="flex space-x-2">
<div class="flex items-center space-x-2 bg-gray-100 px-4 py-1">
<span>Bedarf</span>
</div>
<div class="flex items-center space-x-2 bg-blue-500 text-gray-50 px-4 py-1">
<span>in Bewerbung</span>
</div>
<div class="flex items-center space-x-2 bg-green-500 text-gray-50 px-4 py-1">
<span>besetzt</span>
</div>
<ul class="flex flex-wrap gap-x-6 gap-y-2 text-xs" aria-label="Legende">
<li class="flex items-center gap-x-2"><span class="timeline-bar timeline-bar--staffed w-10 h-5"></span> besetzt</li>
<li class="flex items-center gap-x-2"><span class="timeline-bar timeline-bar--pending w-10 h-5"></span> in Bewerbung oder Vertrag in Prüfung</li>
<li class="flex items-center gap-x-2"><span class="timeline-bar timeline-bar--open w-10 h-5"></span> offen, noch keine Bewerbung</li>
<li class="flex items-center gap-x-2"><span class="inline-block h-5 w-0.5 bg-brand-bright"></span> heute</li>
</ul>
</div>
{% endblock %}
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Tests\Service\Assignment;
use App\Config\HouseCatalog;
use App\Entity\Application;
use App\Entity\Assignment;
use App\Entity\Destination;
@@ -12,6 +13,7 @@ use App\Entity\JobProfile;
use App\Entity\Teamer;
use App\Model\TimelineItem;
use App\Service\Assignment\TimelineService;
use Carbon\CarbonPeriodImmutable;
use PHPUnit\Framework\TestCase;
class TimelineServiceTest extends TestCase
@@ -82,14 +84,58 @@ class TimelineServiceTest extends TestCase
}
}
/**
* Trips running into or out of the window are clipped to its columns.
*/
public function testItemsArePlacedOnTheDayColumnsAndClipped(): void
{
$inside = $this->assignment(1);
$startsBefore = $this->assignment(1, '2026-11-28', '2026-12-03');
$endsAfter = $this->assignment(1, '2027-01-28', '2027-02-03');
$columns = array_map(
fn (Assignment $assignment) => [$this->items($assignment)[0]->getColumn(), $this->items($assignment)[0]->getSpan()],
[$inside, $startsBefore, $endsAfter],
);
// window 2026-12-01 to 2027-01-31: 5 December is column 5, eight days
$this->assertSame([[5, 8], [1, 3], [59, 4]], $columns);
}
public function testHousesCarryNameRowsAndOpenSlots(): void
{
$first = $this->assignment(2);
$overlapping = $this->assignment(1, '2026-12-10', '2026-12-14');
$overlapping->addApplication(new Application($overlapping, $this->teamer('Carla')));
$other = $this->assignment(1);
$other->getDestination()->setHotelCode('AGR02');
$houses = $this->service()->preprocess([$first, $overlapping, $other], $this->period());
// sorted by name: Rotbach (AGR) before Spinabad (DGS)
$this->assertSame(['Rotbach', 'Spinabad'], array_map(fn ($house) => $house->getName(), $houses));
$this->assertCount(3, $houses[1]->getRows());
$this->assertSame(2, $houses[1]->getOpenSlots());
}
/**
* @return list<TimelineItem>
*/
private function items(Assignment $assignment): array
{
$rows = (new TimelineService())->preprocess([$assignment]);
$houses = $this->service()->preprocess([$assignment], $this->period());
return array_merge(...array_map('array_values', $rows['DGS']));
return array_merge(...$houses[0]->getRows());
}
private function service(): TimelineService
{
return new TimelineService(new HouseCatalog(['AGR' => 'Rotbach', 'DGS' => 'Spinabad']));
}
private function period(): CarbonPeriodImmutable
{
return CarbonPeriodImmutable::create('2026-12-01', '2027-01-31');
}
/**
@@ -104,12 +150,12 @@ class TimelineServiceTest extends TestCase
return $items;
}
private function assignment(int $slots): Assignment
private function assignment(int $slots, string $from = '2026-12-05', string $to = '2026-12-12'): Assignment
{
$destination = (new Destination())
->setHotelCode('DGS01')
->setDateFrom(new \DateTimeImmutable('2026-12-05'))
->setDateTo(new \DateTimeImmutable('2026-12-12'))
->setDateFrom(new \DateTimeImmutable($from))
->setDateTo(new \DateTimeImmutable($to))
;
return (new Assignment())