You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
engelsystem/includes/view/ShiftCalendarRenderer.php

333 lines
9.2 KiB
PHTML

<?php
7 years ago
namespace Engelsystem;
4 years ago
use Engelsystem\Models\Room;
8 years ago
class ShiftCalendarRenderer
{
8 years ago
/**
* 15m * 60s/m = 900s
*/
const SECONDS_PER_ROW = 900;
/**
* Height of a block in pixel.
* Do not change - corresponds with theme/css
*/
const BLOCK_HEIGHT = 30;
/**
* Distance between two shifts in pixels
*/
const MARGIN = 5;
/**
* Seconds added to the start and end time
*/
const TIME_MARGIN = 1800;
/** @var array */
8 years ago
private $lanes;
/** @var ShiftsFilter */
8 years ago
private $shiftsFilter;
/** @var int */
private $firstBlockStartTime = 0;
/** @var int */
private $lastBlockEndTime = 0;
/** @var int */
8 years ago
private $blocksPerSlot = null;
/** @var array[] */
private $needed_angeltypes = [];
/** @var array[] */
private $shift_entries = [];
/**
* ShiftCalendarRenderer constructor.
*
7 years ago
* @param array[] $shifts
* @param array[] $needed_angeltypes
* @param array[] $shift_entries
* @param ShiftsFilter $shiftsFilter
*/
8 years ago
public function __construct($shifts, $needed_angeltypes, $shift_entries, ShiftsFilter $shiftsFilter)
{
$this->shiftsFilter = $shiftsFilter;
$this->firstBlockStartTime = $this->calcFirstBlockStartTime($shifts);
$this->lastBlockEndTime = $this->calcLastBlockEndTime($shifts);
$this->lanes = $this->assignShiftsToLanes($shifts);
$this->needed_angeltypes = $needed_angeltypes;
$this->shift_entries = $shift_entries;
}
8 years ago
/**
* Assigns the shifts to different lanes per room if they collide
*
7 years ago
* @param array[] $shifts The shifts to assign
* @return array Returns an array that assigns a room_id to an array of ShiftCalendarLane containing the shifts
8 years ago
*/
private function assignShiftsToLanes($shifts)
{
// array that assigns a room id to a list of lanes (per room)
$lanes = [];
7 years ago
8 years ago
foreach ($shifts as $shift) {
$room_id = $shift['RID'];
4 years ago
$room = new Room();
$room->name = $shift['room_name'];
$room->setAttribute('id', $room_id);
$header = Room_name_render($room);
7 years ago
if (!isset($lanes[$room_id])) {
8 years ago
// initialize room with one lane
$lanes[$room_id] = [
new ShiftCalendarLane($header, $this->getFirstBlockStartTime(), $this->getBlocksPerSlot())
];
}
// Try to add the shift to the existing lanes for this room
$shift_added = false;
foreach ($lanes[$room_id] as $lane) {
/** @var ShiftCalendarLane $lane */
7 years ago
if ($lane->shiftFits($shift)) {
$lane->addShift($shift);
$shift_added = true;
8 years ago
break;
}
}
// If all lanes for this room are busy, create a new lane and add shift to it
if ($shift_added == false) {
$newLane = new ShiftCalendarLane($header, $this->getFirstBlockStartTime(), $this->getBlocksPerSlot());
$newLane->addShift($shift);
8 years ago
$lanes[$room_id][] = $newLane;
}
}
7 years ago
8 years ago
return $lanes;
}
/**
* @return int
*/
8 years ago
public function getFirstBlockStartTime()
{
return $this->firstBlockStartTime;
}
/**
* @return int
*/
8 years ago
public function getLastBlockEndTime()
{
return $this->lastBlockEndTime;
}
/**
* @return float
*/
8 years ago
public function getBlocksPerSlot()
{
if (is_null($this->blocksPerSlot)) {
8 years ago
$this->blocksPerSlot = $this->calcBlocksPerSlot();
}
return $this->blocksPerSlot;
}
8 years ago
/**
* Renders the whole calendar
*
* @return string the generated html
8 years ago
*/
public function render()
{
if (count($this->lanes) == 0) {
return info(__('No shifts found.'), true);
8 years ago
}
return div('shift-calendar table-responsive', [
7 years ago
$this->renderTimeLane(),
$this->renderShiftLanes()
]) . $this->renderLegend();
8 years ago
}
/**
* Renders the lanes containing the shifts
*
* @return string
8 years ago
*/
private function renderShiftLanes()
{
$html = '';
8 years ago
foreach ($this->lanes as $room_lanes) {
foreach ($room_lanes as $lane) {
$html .= $this->renderLane($lane);
}
}
7 years ago
8 years ago
return $html;
}
/**
* Renders a single lane
*
7 years ago
* @param ShiftCalendarLane $lane The lane to render
* @return string
8 years ago
*/
private function renderLane(ShiftCalendarLane $lane)
{
$shift_renderer = new ShiftCalendarShiftRenderer();
$html = '';
8 years ago
$rendered_until = $this->getFirstBlockStartTime();
7 years ago
8 years ago
foreach ($lane->getShifts() as $shift) {
while ($rendered_until + ShiftCalendarRenderer::SECONDS_PER_ROW <= $shift['start']) {
$html .= $this->renderTick($rendered_until);
$rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW;
}
7 years ago
list ($shift_height, $shift_html) = $shift_renderer->render(
$shift,
$this->needed_angeltypes[$shift['SID']],
$this->shift_entries[$shift['SID']],
auth()->user()
7 years ago
);
8 years ago
$html .= $shift_html;
$rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW;
}
7 years ago
8 years ago
while ($rendered_until < $this->getLastBlockEndTime()) {
$html .= $this->renderTick($rendered_until);
$rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW;
}
7 years ago
$bg = '';
if (theme_type() === 'light') {
$bg = 'bg-light';
}
8 years ago
return div('lane', [
div('header ' . $bg, $lane->getHeader()),
8 years ago
$html
]);
}
/**
* Renders a tick/block for given time
*
7 years ago
* @param int $time unix timestamp
* @param boolean $label Should time labels be generated?
* @return string rendered tick html
8 years ago
*/
private function renderTick($time, $label = false)
{
if ($time % (24 * 60 * 60) == 23 * 60 * 60) {
7 years ago
if (!$label) {
8 years ago
return div('tick day');
}
return div('tick day', [
date(__('m-d'), $time) .'<br>'.date(__('H:i'), $time)
8 years ago
]);
} elseif ($time % (60 * 60) == 0) {
7 years ago
if (!$label) {
8 years ago
return div('tick hour');
}
return div('tick hour', [
date(__('m-d'), $time) .'<br>'.date(__('H:i'), $time)
8 years ago
]);
}
return div('tick');
}
/**
* Renders the left time lane including hour/day ticks
*
* @return string
8 years ago
*/
private function renderTimeLane()
{
$bg = '';
if (theme_type() === 'light') {
$bg = 'bg-light';
}
8 years ago
$time_slot = [
div('header ' . $bg, [
__('Time')
8 years ago
])
];
7 years ago
for ($block = 0; $block < $this->getBlocksPerSlot(); $block++) {
8 years ago
$thistime = $this->getFirstBlockStartTime() + ($block * ShiftCalendarRenderer::SECONDS_PER_ROW);
$time_slot[] = $this->renderTick($thistime, true);
}
return div('lane time', $time_slot);
}
/**
7 years ago
* @param array[] $shifts
* @return int
*/
8 years ago
private function calcFirstBlockStartTime($shifts)
{
$start_time = $this->shiftsFilter->getEndTime();
foreach ($shifts as $shift) {
if ($shift['start'] < $start_time) {
$start_time = $shift['start'];
}
}
return ShiftCalendarRenderer::SECONDS_PER_ROW * floor(
($start_time - ShiftCalendarRenderer::TIME_MARGIN)
/ ShiftCalendarRenderer::SECONDS_PER_ROW
);
}
/**
7 years ago
* @param array[] $shifts
* @return int
*/
8 years ago
private function calcLastBlockEndTime($shifts)
{
$end_time = $this->shiftsFilter->getStartTime();
foreach ($shifts as $shift) {
if ($shift['end'] > $end_time) {
$end_time = $shift['end'];
}
}
return ShiftCalendarRenderer::SECONDS_PER_ROW * ceil(
($end_time + ShiftCalendarRenderer::TIME_MARGIN)
/ ShiftCalendarRenderer::SECONDS_PER_ROW
);
}
/**
* @return int
*/
8 years ago
private function calcBlocksPerSlot()
{
return ceil(
($this->getLastBlockEndTime() - $this->getFirstBlockStartTime())
/ ShiftCalendarRenderer::SECONDS_PER_ROW
);
8 years ago
}
8 years ago
/**
* Renders a legend explaining the shift coloring
*
* @return string
8 years ago
*/
private function renderLegend()
{
return div('legend mt-3', [
badge(__('Your shift'), 'primary'),
badge(__('Help needed'), 'danger'),
badge(__('Other angeltype needed / collides with my shifts'), 'warning'),
badge(__('Shift is full'), 'success'),
badge(__('Shift running/ended or user not arrived/allowed'), 'secondary')
8 years ago
]);
}
}