2016-11-07 20:50:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem;
|
|
|
|
|
2022-11-09 00:02:30 +01:00
|
|
|
use Engelsystem\Models\AngelType;
|
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;
|
2023-01-24 19:23:57 +01:00
|
|
|
use Engelsystem\Models\Shifts\ShiftSignupStatus;
|
2018-10-10 03:10:28 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2023-01-18 13:02:11 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2018-10-10 03:10:28 +02:00
|
|
|
|
2021-07-24 20:42:19 +02:00
|
|
|
use function theme_type;
|
|
|
|
|
2016-11-07 20:50:15 +01:00
|
|
|
/**
|
|
|
|
* Renders a single shift for the shift calendar
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
class ShiftCalendarShiftRenderer
|
|
|
|
{
|
2017-01-02 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* Renders a shift
|
|
|
|
*
|
2023-01-18 13:02:11 +01:00
|
|
|
* @param Shift $shift The shift to render
|
|
|
|
* @param array[] $needed_angeltypes
|
|
|
|
* @param ShiftEntry[]|Collection $shift_entries
|
|
|
|
* @param User $user The user who is viewing the shift calendar
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return array
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
2023-01-03 22:19:03 +01:00
|
|
|
public function render(Shift $shift, $needed_angeltypes, $shift_entries, $user)
|
2017-01-02 15:43:36 +01:00
|
|
|
{
|
2017-01-03 14:12:17 +01:00
|
|
|
$info_text = '';
|
2023-01-03 22:19:03 +01:00
|
|
|
if ($shift->title != '') {
|
2023-12-04 23:33:07 +01:00
|
|
|
$info_text = icon('info-circle') . htmlspecialchars($shift->title) . '<br>';
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
list($shift_signup_state, $shifts_row) = $this->renderShiftNeededAngeltypes(
|
|
|
|
$shift,
|
|
|
|
$needed_angeltypes,
|
|
|
|
$shift_entries,
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
|
|
|
|
$class = $this->classForSignupState($shift_signup_state);
|
|
|
|
|
2023-01-03 22:19:03 +01:00
|
|
|
$blocks = ceil(($shift->end->timestamp - $shift->start->timestamp) / ShiftCalendarRenderer::SECONDS_PER_ROW);
|
2017-01-02 15:43:36 +01:00
|
|
|
$blocks = max(1, $blocks);
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
return [
|
|
|
|
$blocks,
|
2020-09-06 23:50:36 +02:00
|
|
|
div(
|
|
|
|
'shift-card" style="height: '
|
2017-12-25 23:12:52 +01:00
|
|
|
. ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN)
|
2018-12-27 02:47:41 +01:00
|
|
|
. 'px;',
|
2018-12-21 16:18:47 +01:00
|
|
|
div(
|
2021-07-24 20:42:19 +02:00
|
|
|
'shift card bg-' . $class,
|
2018-12-21 16:18:47 +01:00
|
|
|
[
|
2019-11-03 12:08:46 +01:00
|
|
|
$this->renderShiftHead($shift, $class, $shift_signup_state->getFreeEntries()),
|
2021-07-24 20:42:19 +02:00
|
|
|
div('card-body ' . $this->classBg(), [
|
2018-12-21 16:18:47 +01:00
|
|
|
$info_text,
|
2023-10-15 19:25:55 +02:00
|
|
|
location_name_render($shift->location),
|
2018-12-21 16:18:47 +01:00
|
|
|
]),
|
2023-02-05 18:03:00 +01:00
|
|
|
$shifts_row,
|
2018-12-21 16:18:47 +01:00
|
|
|
]
|
|
|
|
)
|
2023-02-05 18:03:00 +01:00
|
|
|
),
|
2017-01-02 15:43:36 +01:00
|
|
|
];
|
|
|
|
}
|
2016-11-07 20:50:15 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param ShiftSignupState $shiftSignupState
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
private function classForSignupState(ShiftSignupState $shiftSignupState)
|
|
|
|
{
|
2022-12-22 00:08:54 +01:00
|
|
|
return match ($shiftSignupState->getState()) {
|
2023-01-24 19:23:57 +01:00
|
|
|
ShiftSignupStatus::ADMIN, ShiftSignupStatus::OCCUPIED => 'success',
|
|
|
|
ShiftSignupStatus::SIGNED_UP => 'primary',
|
|
|
|
ShiftSignupStatus::NOT_ARRIVED, ShiftSignupStatus::NOT_YET, ShiftSignupStatus::SHIFT_ENDED => 'secondary',
|
|
|
|
ShiftSignupStatus::ANGELTYPE, ShiftSignupStatus::COLLIDES => 'warning',
|
|
|
|
ShiftSignupStatus::FREE => 'danger',
|
2022-12-22 00:08:54 +01:00
|
|
|
default => 'light',
|
|
|
|
};
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-12 23:00:46 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2023-01-18 13:02:11 +01:00
|
|
|
* @param Shift $shift
|
|
|
|
* @param array[] $needed_angeltypes
|
|
|
|
* @param ShiftEntry[]|Collection $shift_entries
|
|
|
|
* @param User $user
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2023-01-03 22:19:03 +01:00
|
|
|
private function renderShiftNeededAngeltypes(Shift $shift, $needed_angeltypes, $shift_entries, $user)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
|
|
|
$shift_entries_filtered = [];
|
|
|
|
foreach ($needed_angeltypes as $needed_angeltype) {
|
|
|
|
$shift_entries_filtered[$needed_angeltype['id']] = [];
|
|
|
|
}
|
|
|
|
foreach ($shift_entries as $shift_entry) {
|
2023-01-18 13:02:11 +01:00
|
|
|
$shift_entries_filtered[$shift_entry->angel_type_id][] = $shift_entry;
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
$html = '';
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var ShiftSignupState $shift_signup_state */
|
2017-01-02 03:57:23 +01:00
|
|
|
$shift_signup_state = null;
|
|
|
|
foreach ($needed_angeltypes as $angeltype) {
|
|
|
|
if ($angeltype['count'] > 0 || count($shift_entries_filtered[$angeltype['id']]) > 0) {
|
2017-01-02 15:43:36 +01:00
|
|
|
list($angeltype_signup_state, $angeltype_html) = $this->renderShiftNeededAngeltype(
|
|
|
|
$shift,
|
|
|
|
$shift_entries_filtered[$angeltype['id']],
|
|
|
|
$angeltype,
|
|
|
|
$user
|
|
|
|
);
|
2018-01-14 17:47:26 +01:00
|
|
|
if (is_null($shift_signup_state)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$shift_signup_state = $angeltype_signup_state;
|
|
|
|
} else {
|
|
|
|
$shift_signup_state->combineWith($angeltype_signup_state);
|
|
|
|
}
|
|
|
|
$html .= $angeltype_html;
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 17:47:26 +01:00
|
|
|
if (is_null($shift_signup_state)) {
|
2023-01-24 19:23:57 +01:00
|
|
|
$shift_signup_state = new ShiftSignupState(ShiftSignupStatus::SHIFT_ENDED, 0);
|
2016-12-27 23:02:05 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-11-12 14:41:23 +01:00
|
|
|
if (auth()->can('user_shifts_admin')) {
|
2021-07-24 20:42:19 +02:00
|
|
|
$html .= '<li class="list-group-item d-flex align-items-center ' . $this->classBg() . '">';
|
2022-10-18 19:15:22 +02:00
|
|
|
$html .= button(
|
|
|
|
shift_entry_create_link_admin($shift),
|
2021-07-22 21:22:21 +02:00
|
|
|
icon('plus-lg') . __('Add more angels'),
|
2021-07-23 01:52:19 +02:00
|
|
|
'btn-sm'
|
2017-12-25 23:12:52 +01:00
|
|
|
);
|
2017-08-30 14:59:27 +02:00
|
|
|
$html .= '</li>';
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
if ($html != '') {
|
|
|
|
return [
|
2017-01-02 15:43:36 +01:00
|
|
|
$shift_signup_state,
|
2023-02-05 18:03:00 +01:00
|
|
|
'<ul class="list-group list-group-flush">' . $html . '</ul>',
|
2017-01-02 15:43:36 +01:00
|
|
|
];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return [
|
2017-01-02 15:43:36 +01:00
|
|
|
$shift_signup_state,
|
2023-02-05 18:03:00 +01:00
|
|
|
'',
|
2017-01-02 15:43:36 +01:00
|
|
|
];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-07 21:24:05 +01:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* Renders a list entry containing the needed angels for an angeltype
|
|
|
|
*
|
2023-01-18 13:02:11 +01:00
|
|
|
* @param Shift $shift The shift which is rendered
|
|
|
|
* @param ShiftEntry[]|Collection $shift_entries
|
|
|
|
* @param array $angeltype The angeltype, containing information about needed angeltypes
|
2017-01-03 03:22:48 +01:00
|
|
|
* and already signed up angels
|
2023-01-18 13:02:11 +01:00
|
|
|
* @param User $user The user who is viewing the shift calendar
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return array
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
2023-01-03 22:19:03 +01:00
|
|
|
private function renderShiftNeededAngeltype(Shift $shift, $shift_entries, $angeltype, $user)
|
2017-01-02 15:43:36 +01:00
|
|
|
{
|
2022-11-09 00:02:30 +01:00
|
|
|
$angeltype = (new AngelType())->forceFill($angeltype);
|
2017-01-02 15:43:36 +01:00
|
|
|
$entry_list = [];
|
|
|
|
foreach ($shift_entries as $entry) {
|
2023-01-18 13:02:11 +01:00
|
|
|
$class = $entry->freeloaded ? 'text-decoration-line-through' : '';
|
|
|
|
$entry_list[] = '<span class="text-nowrap ' . $class . '">' . User_Nick_render($entry->user) . '</span>';
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
2017-12-25 23:12:52 +01:00
|
|
|
$shift_signup_state = Shift_signup_allowed(
|
|
|
|
$user,
|
|
|
|
$shift,
|
|
|
|
$angeltype,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$angeltype,
|
|
|
|
$shift_entries
|
|
|
|
);
|
2018-10-18 23:34:18 +02:00
|
|
|
$freeEntriesCount = $shift_signup_state->getFreeEntries();
|
|
|
|
$inner_text = _e('%d helper needed', '%d helpers needed', $freeEntriesCount, [$freeEntriesCount]);
|
2017-01-03 03:22:48 +01:00
|
|
|
|
2023-01-24 19:23:57 +01:00
|
|
|
$entry = match ($shift_signup_state->getState()) {
|
|
|
|
// When admin or free display a link + button for sign up
|
|
|
|
ShiftSignupStatus::ADMIN, ShiftSignupStatus::FREE =>
|
|
|
|
'<a class="me-1 text-nowrap" href="'
|
|
|
|
. shift_entry_create_link($shift, $angeltype)
|
|
|
|
. '">'
|
|
|
|
. $inner_text
|
|
|
|
. '</a> '
|
|
|
|
. button(
|
|
|
|
shift_entry_create_link($shift, $angeltype),
|
|
|
|
__('Sign up'),
|
|
|
|
'btn-sm btn-primary text-nowrap d-print-none'
|
|
|
|
),
|
|
|
|
// No link and add a text hint, when the shift ended
|
|
|
|
ShiftSignupStatus::SHIFT_ENDED => $inner_text . ' (' . __('ended') . ')',
|
|
|
|
// No link and add a text hint, when the shift ended
|
|
|
|
ShiftSignupStatus::NOT_ARRIVED => $inner_text . ' (' . __('please arrive for signup') . ')',
|
|
|
|
ShiftSignupStatus::NOT_YET => $inner_text . ' (' . __('not yet') . ')',
|
2023-09-25 21:36:10 +02:00
|
|
|
ShiftSignupStatus::ANGELTYPE => $angeltype->restricted || !$angeltype->shift_self_signup
|
|
|
|
// User has to be confirmed on the angeltype first or can't sign up by themselves
|
2023-01-24 19:23:57 +01:00
|
|
|
? $inner_text . icon('mortarboard-fill')
|
|
|
|
// Add link to join the angeltype first
|
|
|
|
: $inner_text . '<br />'
|
2023-02-05 16:34:16 +01:00
|
|
|
. button(
|
2023-11-13 16:56:52 +01:00
|
|
|
url('/user-angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype->id]),
|
2023-12-04 23:33:07 +01:00
|
|
|
sprintf(__('Become %s'), htmlspecialchars($angeltype->name)),
|
2023-02-05 16:34:16 +01:00
|
|
|
'btn-sm'
|
|
|
|
),
|
2023-01-24 19:23:57 +01:00
|
|
|
// Shift collides or user is already signed up: No signup allowed
|
|
|
|
ShiftSignupStatus::COLLIDES, ShiftSignupStatus::SIGNED_UP => $inner_text,
|
|
|
|
// Shift is full
|
|
|
|
ShiftSignupStatus::OCCUPIED => null,
|
|
|
|
default => null,
|
|
|
|
};
|
|
|
|
if (!is_null($entry)) {
|
|
|
|
$entry_list[] = $entry;
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
|
2021-07-24 20:42:19 +02:00
|
|
|
$shifts_row = '<li class="list-group-item d-flex flex-wrap align-items-center ' . $this->classBg() . '">';
|
|
|
|
$shifts_row .= '<strong class="me-1">' . AngelType_name_render($angeltype) . ':</strong> ';
|
2017-01-03 14:12:17 +01:00
|
|
|
$shifts_row .= join(', ', $entry_list);
|
2017-01-02 15:43:36 +01:00
|
|
|
$shifts_row .= '</li>';
|
|
|
|
return [
|
|
|
|
$shift_signup_state,
|
2023-02-05 18:03:00 +01:00
|
|
|
$shifts_row,
|
2017-01-02 15:43:36 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-07-24 20:42:19 +02:00
|
|
|
/**
|
|
|
|
* Return the corresponding bg class
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function classBg(): string
|
|
|
|
{
|
|
|
|
if (theme_type() === 'light') {
|
|
|
|
return 'bg-white';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'bg-dark';
|
|
|
|
}
|
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* Renders the shift header
|
|
|
|
*
|
2023-01-03 22:19:03 +01:00
|
|
|
* @param Shift $shift The shift
|
2018-11-25 15:40:53 +01:00
|
|
|
* @param string $class The shift state class
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
2017-01-02 15:43:36 +01:00
|
|
|
*/
|
2023-01-03 22:19:03 +01:00
|
|
|
private function renderShiftHead(Shift $shift, $class, $needed_angeltypes_count)
|
2017-01-02 15:43:36 +01:00
|
|
|
{
|
2017-01-03 14:12:17 +01:00
|
|
|
$header_buttons = '';
|
2018-11-12 14:41:23 +01:00
|
|
|
if (auth()->can('admin_shifts')) {
|
2021-07-24 20:42:19 +02:00
|
|
|
$header_buttons = '<div class="ms-auto d-print-none">' . table_buttons([
|
2017-12-25 23:12:52 +01:00
|
|
|
button(
|
2023-11-13 16:56:52 +01:00
|
|
|
url('/user-shifts', ['edit_shift' => $shift->id]),
|
2021-07-22 21:22:21 +02:00
|
|
|
icon('pencil'),
|
2023-02-05 16:34:16 +01:00
|
|
|
'btn-' . $class . ' btn-sm border-light text-white'
|
2017-12-25 23:12:52 +01:00
|
|
|
),
|
|
|
|
button(
|
2023-11-13 16:56:52 +01:00
|
|
|
url('/user-shifts', ['delete_shift' => $shift->id]),
|
2021-07-22 21:22:21 +02:00
|
|
|
icon('trash'),
|
2023-02-05 16:34:16 +01:00
|
|
|
'btn-' . $class . ' btn-sm border-light text-white'
|
2023-02-05 18:03:00 +01:00
|
|
|
),
|
2017-01-02 15:43:36 +01:00
|
|
|
]) . '</div>';
|
2016-11-07 20:50:15 +01:00
|
|
|
}
|
2023-01-03 22:19:03 +01:00
|
|
|
$shift_heading = $shift->start->format('H:i') . ' ‐ '
|
|
|
|
. $shift->end->format('H:i') . ' — '
|
2023-12-04 23:33:07 +01:00
|
|
|
. htmlspecialchars($shift->shiftType->name);
|
2019-11-03 12:08:46 +01:00
|
|
|
|
2021-07-24 20:42:19 +02:00
|
|
|
if ($needed_angeltypes_count > 0) {
|
|
|
|
$shift_heading = '<span class="badge bg-light text-danger me-1">' . $needed_angeltypes_count . '</span> ' . $shift_heading;
|
2019-11-03 12:08:46 +01:00
|
|
|
}
|
|
|
|
|
2021-07-24 20:42:19 +02:00
|
|
|
return div('card-header d-flex align-items-center', [
|
|
|
|
'<a class="d-flex align-items-center text-white" href="' . shift_link($shift) . '">' . $shift_heading . '</a>',
|
2023-02-05 18:03:00 +01:00
|
|
|
$header_buttons,
|
2017-01-02 15:43:36 +01:00
|
|
|
]);
|
2016-11-07 20:50:15 +01:00
|
|
|
}
|
|
|
|
}
|