engelsystem/includes/view/ShiftCalendarRenderer.php

327 lines
9.2 KiB
PHP
Raw Permalink Normal View History

2016-10-03 17:41:14 +02:00
<?php
2017-08-30 00:07:01 +02:00
namespace Engelsystem;
2017-07-28 19:28:11 +02:00
2023-08-27 19:40:33 +02:00
use Engelsystem\Helpers\Carbon;
2023-01-03 22:19:03 +01:00
use Engelsystem\Models\Shifts\Shift;
2023-01-18 13:02:11 +01:00
use Engelsystem\Models\Shifts\ShiftEntry;
use Illuminate\Support\Collection;
2020-09-06 23:50:36 +02:00
2017-01-02 03:57:23 +01:00
class ShiftCalendarRenderer
{
2017-01-02 15:43:36 +01:00
/**
* 15m * 60s/m = 900s
*/
public const SECONDS_PER_ROW = 900;
2017-01-02 15:43:36 +01:00
/**
* Height of a block in pixel.
* Do not change - corresponds with theme/css
*/
public const BLOCK_HEIGHT = 30;
2017-01-02 15:43:36 +01:00
/**
* Distance between two shifts in pixels
*/
public const MARGIN = 5;
2017-01-02 15:43:36 +01:00
/**
* Seconds added to the start and end time
*/
public const TIME_MARGIN = 1800;
2016-10-07 17:22:48 +02:00
2017-01-03 03:22:48 +01:00
/** @var array */
2017-01-02 03:57:23 +01:00
private $lanes;
2017-01-03 03:22:48 +01:00
/** @var ShiftsFilter */
2017-01-02 03:57:23 +01:00
private $shiftsFilter;
2017-01-03 03:22:48 +01:00
/** @var int */
private $firstBlockStartTime;
2016-11-14 17:58:15 +01:00
2017-01-03 03:22:48 +01:00
/** @var int */
private $lastBlockEndTime;
2016-11-05 10:06:06 +01:00
2017-01-03 03:22:48 +01:00
/** @var int */
2017-01-02 03:57:23 +01:00
private $blocksPerSlot = null;
2016-11-05 10:06:06 +01:00
2017-01-03 03:22:48 +01:00
/**
* ShiftCalendarRenderer constructor.
*
2023-01-18 13:02:11 +01:00
* @param Shift[] $shifts
* @param array[] $needed_angeltypes
* @param ShiftEntry[][]|Collection $shift_entries
* @param ShiftsFilter $shiftsFilter
2017-01-03 03:22:48 +01:00
*/
public function __construct($shifts, private $needed_angeltypes, private $shift_entries, ShiftsFilter $shiftsFilter)
2017-01-02 03:57:23 +01:00
{
$this->shiftsFilter = $shiftsFilter;
$this->firstBlockStartTime = $this->calcFirstBlockStartTime($shifts);
$this->lastBlockEndTime = $this->calcLastBlockEndTime($shifts);
$this->lanes = $this->assignShiftsToLanes($shifts);
}
2017-01-02 15:43:36 +01:00
/**
2023-10-15 19:25:55 +02:00
* Assigns the shifts to different lanes per location if they collide
2017-01-02 15:43:36 +01:00
*
2023-01-03 22:19:03 +01:00
* @param Shift[] $shifts The shifts to assign
2023-10-15 19:25:55 +02:00
* @return array Returns an array that assigns a location_id to an array of ShiftCalendarLane containing the shifts
2017-01-02 15:43:36 +01:00
*/
private function assignShiftsToLanes($shifts)
{
2023-10-15 19:25:55 +02:00
// array that assigns a location id to a list of lanes (per location)
2017-01-02 15:43:36 +01:00
$lanes = [];
2017-08-30 00:07:01 +02:00
2017-01-02 15:43:36 +01:00
foreach ($shifts as $shift) {
2023-10-15 19:25:55 +02:00
$location = $shift->location;
$header = location_name_render($location);
if (!isset($lanes[$location->id])) {
// initialize location with one lane
$lanes[$location->id] = [
new ShiftCalendarLane($header),
2017-01-02 15:43:36 +01:00
];
}
2023-10-15 19:25:55 +02:00
// Try to add the shift to the existing lanes for this location
2017-01-02 15:43:36 +01:00
$shift_added = false;
2023-10-15 19:25:55 +02:00
foreach ($lanes[$location->id] as $lane) {
2017-01-03 03:22:48 +01:00
/** @var ShiftCalendarLane $lane */
2017-08-30 00:07:01 +02:00
if ($lane->shiftFits($shift)) {
$lane->addShift($shift);
2017-07-28 19:28:11 +02:00
$shift_added = true;
2017-01-02 15:43:36 +01:00
break;
}
}
2023-10-15 19:25:55 +02:00
// If all lanes for this location are busy, create a new lane and add shift to it
if (!$shift_added) {
2023-01-03 22:19:03 +01:00
$newLane = new ShiftCalendarLane($header);
$newLane->addShift($shift);
2023-10-15 19:25:55 +02:00
$lanes[$location->id][] = $newLane;
2017-01-02 15:43:36 +01:00
}
}
2017-08-30 00:07:01 +02:00
2017-01-02 15:43:36 +01:00
return $lanes;
}
2016-11-05 10:06:06 +01:00
2017-01-03 03:22:48 +01:00
/**
* @return int
*/
2017-01-02 03:57:23 +01:00
public function getFirstBlockStartTime()
{
return $this->firstBlockStartTime;
}
2016-11-14 17:58:15 +01:00
2017-01-03 03:22:48 +01:00
/**
* @return int
*/
2017-01-02 03:57:23 +01:00
public function getLastBlockEndTime()
{
return $this->lastBlockEndTime;
}
2016-10-07 17:22:48 +02:00
2017-01-03 03:22:48 +01:00
/**
* @return float
*/
2017-01-02 03:57:23 +01:00
public function getBlocksPerSlot()
{
2018-01-14 17:47:26 +01:00
if (is_null($this->blocksPerSlot)) {
2017-01-02 03:57:23 +01:00
$this->blocksPerSlot = $this->calcBlocksPerSlot();
}
return $this->blocksPerSlot;
2016-10-07 17:22:48 +02:00
}
2017-01-02 15:43:36 +01:00
/**
* Renders the whole calendar
*
2017-01-03 03:22:48 +01:00
* @return string the generated html
2017-01-02 15:43:36 +01:00
*/
public function render()
{
if (count($this->lanes) == 0) {
return info(__('No shifts found.'), true);
2017-01-02 15:43:36 +01:00
}
return div('shift-calendar table-responsive', [
2017-08-30 00:07:01 +02:00
$this->renderTimeLane(),
$this->renderShiftLanes(),
2017-08-30 00:07:01 +02:00
]) . $this->renderLegend();
2017-01-02 15:43:36 +01:00
}
/**
* Renders the lanes containing the shifts
2017-01-03 03:22:48 +01:00
*
* @return string
2017-01-02 15:43:36 +01:00
*/
private function renderShiftLanes()
{
2017-01-03 14:12:17 +01:00
$html = '';
2023-10-15 19:25:55 +02:00
foreach ($this->lanes as $location_lanes) {
foreach ($location_lanes as $lane) {
2017-01-02 15:43:36 +01:00
$html .= $this->renderLane($lane);
}
}
2017-08-30 00:07:01 +02:00
2017-01-02 15:43:36 +01:00
return $html;
}
/**
* Renders a single lane
*
2017-08-30 00:07:01 +02:00
* @param ShiftCalendarLane $lane The lane to render
2017-01-03 03:22:48 +01:00
* @return string
2017-01-02 15:43:36 +01:00
*/
private function renderLane(ShiftCalendarLane $lane)
{
$shift_renderer = new ShiftCalendarShiftRenderer();
2017-01-03 14:12:17 +01:00
$html = '';
2017-01-02 15:43:36 +01:00
$rendered_until = $this->getFirstBlockStartTime();
2017-08-30 00:07:01 +02:00
2017-01-02 15:43:36 +01:00
foreach ($lane->getShifts() as $shift) {
2023-01-03 22:19:03 +01:00
while ($rendered_until + ShiftCalendarRenderer::SECONDS_PER_ROW <= $shift->start->timestamp) {
2017-01-02 15:43:36 +01:00
$html .= $this->renderTick($rendered_until);
$rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW;
}
2017-08-30 00:07:01 +02:00
list ($shift_height, $shift_html) = $shift_renderer->render(
$shift,
2023-01-03 22:19:03 +01:00
$this->needed_angeltypes[$shift->id],
$this->shift_entries[$shift->id],
2018-10-10 03:10:28 +02:00
auth()->user()
2017-08-30 00:07:01 +02:00
);
2017-01-02 15:43:36 +01:00
$html .= $shift_html;
$rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW;
}
2017-08-30 00:07:01 +02:00
2017-01-02 15:43:36 +01:00
while ($rendered_until < $this->getLastBlockEndTime()) {
$html .= $this->renderTick($rendered_until);
$rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW;
}
2017-08-30 00:07:01 +02:00
2021-09-11 10:46:21 +02:00
$bg = 'bg-' . theme_type();
2017-01-02 15:43:36 +01:00
return div('lane', [
div('header ' . $bg, $lane->getHeader()),
$html,
2017-01-02 15:43:36 +01:00
]);
}
/**
* Renders a tick/block for given time
*
2023-01-18 13:02:11 +01:00
* @param int $time unix timestamp
2017-08-30 00:07:01 +02:00
* @param boolean $label Should time labels be generated?
2017-01-03 03:22:48 +01:00
* @return string rendered tick html
2017-01-02 15:43:36 +01:00
*/
private function renderTick($time, $label = false)
{
2023-08-27 19:40:33 +02:00
$time = Carbon::createFromTimestamp($time);
$class = $label ? 'tick bg-' . theme_type() : 'tick ';
2024-02-21 17:36:23 +01:00
$diffNow = $time->diffInMinutes(null, false) * 60;
if ($diffNow >= 0 && $diffNow < self::SECONDS_PER_ROW) {
$class .= ' now';
}
2023-08-27 19:40:33 +02:00
if ($time->isStartOfDay()) {
2017-08-30 00:07:01 +02:00
if (!$label) {
return div($class . ' day');
2017-01-02 15:43:36 +01:00
}
return div($class . ' day', [
2023-08-27 19:40:33 +02:00
$time->format(__('m-d')) . '<br>' . $time->format(__('H:i')),
2017-01-02 15:43:36 +01:00
]);
2023-08-27 19:40:33 +02:00
} elseif ($time->isStartOfHour()) {
2017-08-30 00:07:01 +02:00
if (!$label) {
return div($class . ' hour');
2017-01-02 15:43:36 +01:00
}
return div($class . ' hour', [
2023-08-27 19:40:33 +02:00
$time->format(__('m-d')) . '<br>' . $time->format(__('H:i')),
2017-01-02 15:43:36 +01:00
]);
}
return div($class);
2017-01-02 15:43:36 +01:00
}
/**
* Renders the left time lane including hour/day ticks
2017-01-03 03:22:48 +01:00
*
* @return string
2017-01-02 15:43:36 +01:00
*/
private function renderTimeLane()
{
$bg = 'bg-' . theme_type();
2017-01-02 15:43:36 +01:00
$time_slot = [
div('header ' . $bg, [
2023-10-03 18:59:46 +02:00
__('log.time'),
]),
2017-01-02 15:43:36 +01:00
];
2017-08-30 00:07:01 +02:00
for ($block = 0; $block < $this->getBlocksPerSlot(); $block++) {
2017-01-02 15:43:36 +01:00
$thistime = $this->getFirstBlockStartTime() + ($block * ShiftCalendarRenderer::SECONDS_PER_ROW);
$time_slot[] = $this->renderTick($thistime, true);
}
return div('lane time', $time_slot);
}
2016-10-07 17:22:48 +02:00
2017-01-03 03:22:48 +01:00
/**
2023-01-03 22:19:03 +01:00
* @param Shift[] $shifts
2017-01-03 03:22:48 +01:00
* @return int
*/
2017-01-02 03:57:23 +01:00
private function calcFirstBlockStartTime($shifts)
{
$start_time = $this->shiftsFilter->getEndTime();
foreach ($shifts as $shift) {
2023-01-03 22:19:03 +01:00
if ($shift->start->timestamp < $start_time) {
$start_time = $shift->start->timestamp;
2017-01-02 03:57:23 +01:00
}
}
2017-12-25 23:12:52 +01:00
return ShiftCalendarRenderer::SECONDS_PER_ROW * floor(
2022-10-18 19:15:22 +02:00
($start_time - ShiftCalendarRenderer::TIME_MARGIN)
2017-12-25 23:12:52 +01:00
/ ShiftCalendarRenderer::SECONDS_PER_ROW
2022-10-18 19:15:22 +02:00
);
2016-10-07 17:22:48 +02:00
}
2017-01-03 03:22:48 +01:00
/**
2023-01-03 22:19:03 +01:00
* @param Shift[] $shifts
2017-01-03 03:22:48 +01:00
* @return int
*/
2017-01-02 03:57:23 +01:00
private function calcLastBlockEndTime($shifts)
{
$end_time = $this->shiftsFilter->getStartTime();
foreach ($shifts as $shift) {
2023-01-03 22:19:03 +01:00
if ($shift->end->timestamp > $end_time) {
$end_time = $shift->end->timestamp;
2017-01-02 03:57:23 +01:00
}
}
2017-12-25 23:12:52 +01:00
return ShiftCalendarRenderer::SECONDS_PER_ROW * ceil(
2022-10-18 19:15:22 +02:00
($end_time + ShiftCalendarRenderer::TIME_MARGIN)
2017-12-25 23:12:52 +01:00
/ ShiftCalendarRenderer::SECONDS_PER_ROW
2022-10-18 19:15:22 +02:00
);
}
2017-01-03 03:22:48 +01:00
/**
* @return int
*/
2017-01-02 03:57:23 +01:00
private function calcBlocksPerSlot()
{
2024-03-25 00:03:39 +01:00
return (int) ceil(
2017-12-25 23:12:52 +01:00
($this->getLastBlockEndTime() - $this->getFirstBlockStartTime())
/ ShiftCalendarRenderer::SECONDS_PER_ROW
);
2017-01-02 03:57:23 +01:00
}
2016-11-14 17:58:15 +01:00
2017-01-02 15:43:36 +01:00
/**
* Renders a legend explaining the shift coloring
2017-01-03 03:22:48 +01:00
*
* @return string
2017-01-02 15:43:36 +01:00
*/
private function renderLegend()
{
return div('legend mt-3', [
2021-07-23 15:37:11 +02:00
badge(__('Your shift'), 'primary'),
badge(__('Help needed'), 'danger'),
badge(__('Other angeltype needed / collides with my shifts'), 'warning'),
badge(__('Shift is full'), 'success'),
2023-10-03 18:59:46 +02:00
badge(__('Shift is running/ended or you have not arrived'), 'secondary'),
2017-01-02 15:43:36 +01:00
]);
}
2016-10-03 17:41:14 +02:00
}