diff --git a/assets/controllers/timeline_scroll_controller.js b/assets/controllers/timeline_scroll_controller.js index 6c65e57..e9b76d9 100644 --- a/assets/controllers/timeline_scroll_controller.js +++ b/assets/controllers/timeline_scroll_controller.js @@ -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 todayRect = this.todayTarget.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 }) } } diff --git a/assets/styles/components/timeline.css b/assets/styles/components/timeline.css index 02b0255..4f88626 100644 --- a/assets/styles/components/timeline.css +++ b/assets/styles/components/timeline.css @@ -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; } diff --git a/src/Controller/Administrative/Assignment/TimelineController.php b/src/Controller/Administrative/Assignment/TimelineController.php index f6bbb6e..f124bbb 100644 --- a/src/Controller/Administrative/Assignment/TimelineController.php +++ b/src/Controller/Administrative/Assignment/TimelineController.php @@ -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, ]); } diff --git a/src/Model/TimelineHouse.php b/src/Model/TimelineHouse.php new file mode 100644 index 0000000..fa4f4e0 --- /dev/null +++ b/src/Model/TimelineHouse.php @@ -0,0 +1,70 @@ +> */ + 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> + */ + 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]; + } +} diff --git a/src/Model/TimelineItem.php b/src/Model/TimelineItem.php index f09c659..8447555 100644 --- a/src/Model/TimelineItem.php +++ b/src/Model/TimelineItem.php @@ -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; } } diff --git a/src/Service/Assignment/TimelineService.php b/src/Service/Assignment/TimelineService.php index 320c8df..bab46ce 100644 --- a/src/Service/Assignment/TimelineService.php +++ b/src/Service/Assignment/TimelineService.php @@ -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 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 $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, months: list} */ - 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)) + ; + } } diff --git a/templates/administrative/assignment/timeline.html.twig b/templates/administrative/assignment/timeline.html.twig index ff4e192..5764ac2 100644 --- a/templates/administrative/assignment/timeline.html.twig +++ b/templates/administrative/assignment/timeline.html.twig @@ -2,129 +2,138 @@ {% block title %}Zeitleiste{% endblock %} -{% macro cell(item, colspan) %} - - {% if item.type == 'disposition' %} -
- {% if is_granted('ROLE_HOUSE_MANAGER') %} - {{ item.jobProfile }} - {% else %} - - {{ item.jobProfile }} - - {% endif %} - | - -
- {% else %} - {% if is_granted('ROLE_HOUSE_MANAGER') %} - {{ item.jobProfile }} - {% else %} - - {{ item.jobProfile }} - - {% endif %} +{% set state_labels = { open: 'offen', pending: 'in Bewerbung', staffed: 'besetzt' } %} + +{% macro bar(item, row, state_labels) %} + {% set is_disposition = item.type == 'disposition' %} +
+ {{ state_labels[item.state] }}: + {% if not is_disposition and item.state == 'open' %} + offen {% endif %} - + {% if is_granted('ROLE_HOUSE_MANAGER') %} + {{ item.jobProfile }} + {% else %} + {{ item.jobProfile }} + {% endif %} + {% if is_disposition %} + + + {% endif %} +
{% endmacro %} {% block content %} -
-

- Zeitleiste -

-
- + {% if filterDto.active %} - {{ filterDto.activeFiltersCount }} Filter aktiv - {% else %} - Filter + + Reset + {% endif %} - - {% if filterDto.active %} - - Reset - - {% endif %} +
- -
- - - - {% for day in filterDto.period %} - - {{ day | date('d.m.Y') }} - - {% endfor %} - - {% for hotelCode, rows in data %} - - - {% 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) }} - {% else %} - - {% endif %} - {% endfor %} - - {% for row in rows[1:] %} - - {% 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 %} - + + {% if houses is empty %} +
Keine Ergebnisse
+ {% else %} +
+
+ {# weekends, month boundaries and today, behind the bars #} +
- {% endfor %} - {% else %} - - {% endfor %} -
- {{ hotelCode }} -
- Keine Ergebnisse -
-
-
-
- Bedarf -
-
- in Bewerbung -
-
- besetzt -
+
+ +
+
+ {% for month in calendar.months %} +
+ {{ month.label }} +
+ {% endfor %} + {% for day in calendar.days %} +
+ {{ day.date.day }} + {{ day.date.isoFormat('dd') }} +
+ {% endfor %} +
+ + {% for house in houses %} +
+
+ {{ house.name ?? house.code }} + + {{ house.code }}{% if house.openSlots > 0 %} · {{ house.openSlots }} offen{% endif %} + +
+ {% for row in house.rows %} + {% set row_number = loop.index %} + {% for item in row %} + {{ _self.bar(item, row_number, state_labels) }} + {% endfor %} + {% endfor %} +
+ {% endfor %} + + + {% endif %} + +
    +
  • besetzt
  • +
  • in Bewerbung oder Vertrag in Prüfung
  • +
  • offen, noch keine Bewerbung
  • +
  • heute
  • +
{% endblock %} diff --git a/tests/Service/Assignment/TimelineServiceTest.php b/tests/Service/Assignment/TimelineServiceTest.php index b37d786..5150bd3 100644 --- a/tests/Service/Assignment/TimelineServiceTest.php +++ b/tests/Service/Assignment/TimelineServiceTest.php @@ -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 */ 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())