engelsystem/includes/view/Shifts_view.php

313 lines
10 KiB
PHP
Raw Normal View History

<?php
2017-08-28 16:21:10 +02:00
2022-11-09 00:02:30 +01:00
use Engelsystem\Models\AngelType;
2023-10-15 19:25:55 +02:00
use Engelsystem\Models\Location;
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 Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\Models\Shifts\ShiftSignupStatus;
2022-12-03 00:57:04 +01:00
use Engelsystem\Models\UserAngelType;
2016-11-15 17:22:15 +01:00
use Engelsystem\ShiftSignupState;
use Illuminate\Support\Collection;
2014-12-19 22:41:55 +01:00
/**
* Renders the basic shift view header.
2017-12-25 23:12:52 +01:00
*
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2023-10-15 19:25:55 +02:00
* @param Location $location
* @return string HTML
*/
2023-10-15 19:25:55 +02:00
function Shift_view_header(Shift $shift, Location $location)
2017-12-25 23:12:52 +01:00
{
return div('row', [
div('col-sm-3 col-xs-6', [
2023-10-01 22:33:58 +02:00
'<h4>' . __('title.title') . '</h4>',
2017-12-25 23:12:52 +01:00
'<p class="lead">'
2023-01-03 22:19:03 +01:00
. ($shift->url != ''
? '<a href="' . $shift->url . '">' . $shift->title . '</a>'
: $shift->title)
. '</p>',
]),
div('col-sm-3 col-xs-6', [
2023-10-22 18:37:29 +02:00
'<h4>' . __('shifts.start') . '</h4>',
2023-01-03 22:19:03 +01:00
'<p class="lead' . (time() >= $shift->start->timestamp ? ' text-success' : '') . '">',
2023-11-23 14:30:46 +01:00
icon('calendar-event') . $shift->start->format(__('general.date')),
'<br />',
2023-01-03 22:19:03 +01:00
icon('clock') . $shift->start->format('H:i'),
'</p>',
]),
div('col-sm-3 col-xs-6', [
2023-10-22 18:37:29 +02:00
'<h4>' . __('shifts.end') . '</h4>',
2023-01-03 22:19:03 +01:00
'<p class="lead' . (time() >= $shift->end->timestamp ? ' text-success' : '') . '">',
2023-11-23 14:30:46 +01:00
icon('calendar-event') . $shift->end->format(__('general.date')),
'<br />',
2023-01-03 22:19:03 +01:00
icon('clock') . $shift->end->format('H:i'),
'</p>',
]),
div('col-sm-3 col-xs-6', [
'<h4>' . __('Location') . '</h4>',
2023-10-15 19:25:55 +02:00
'<p class="lead">' . location_name_render($location) . '</p>',
]),
]);
}
2017-01-03 03:22:48 +01:00
/**
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-01-03 22:19:03 +01:00
function Shift_editor_info_render(Shift $shift)
2017-01-02 03:57:23 +01:00
{
$info = [];
2023-01-03 22:19:03 +01:00
if (!empty($shift->created_by)) {
2017-01-02 15:43:36 +01:00
$info[] = sprintf(
icon('plus-lg') . __('created at %s by %s'),
2023-11-23 14:30:46 +01:00
$shift->created_at->format(__('general.datetime')),
2023-01-03 22:19:03 +01:00
User_Nick_render($shift->createdBy)
2017-01-02 15:43:36 +01:00
);
2017-01-02 03:57:23 +01:00
}
2023-01-03 22:19:03 +01:00
if (!empty($shift->updated_by)) {
2017-01-02 15:43:36 +01:00
$info[] = sprintf(
icon('pencil') . __('edited at %s by %s'),
2023-11-23 14:30:46 +01:00
$shift->updated_at->format(__('general.datetime')),
2023-01-03 22:19:03 +01:00
User_Nick_render($shift->updatedBy)
2017-01-02 15:43:36 +01:00
);
2017-01-02 03:57:23 +01:00
}
return join('<br />', $info);
2015-08-15 23:36:38 +02:00
}
2017-01-03 03:22:48 +01:00
/**
2023-01-18 13:02:11 +01:00
* @param Shift $shift
* @param AngelType $angeltype
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-01-03 22:19:03 +01:00
function Shift_signup_button_render(Shift $shift, AngelType $angeltype)
2017-01-02 03:57:23 +01:00
{
2022-12-03 00:57:04 +01:00
/** @var UserAngelType|null $user_angeltype */
$user_angeltype = UserAngelType::whereUserId(auth()->user()->id)
->where('angel_type_id', $angeltype->id)
->first();
2017-01-02 15:43:36 +01:00
if (
$angeltype->shift_signup_state?->isSignupAllowed()
|| auth()->user()->isAngelTypeSupporter($angeltype)
|| auth()->can('admin_user_angeltypes')
) {
return button(shift_entry_create_link($shift, $angeltype), __('Sign up'));
2018-01-14 17:47:26 +01:00
} elseif (empty($user_angeltype)) {
2017-01-02 15:43:36 +01:00
return button(
2023-11-13 16:56:52 +01:00
url('/angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id]),
2022-10-18 19:15:22 +02:00
sprintf(
__('Become %s'),
2022-11-09 00:02:30 +01:00
$angeltype->name
2022-10-18 19:15:22 +02:00
)
2017-01-02 15:43:36 +01:00
);
2017-01-02 03:57:23 +01:00
}
2022-12-03 00:57:04 +01:00
2017-01-02 03:57:23 +01:00
return '';
}
2017-01-03 03:22:48 +01:00
/**
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2022-11-09 00:02:30 +01:00
* @param ShiftType $shifttype
2023-10-15 19:25:55 +02:00
* @param Location $location
2022-11-09 00:02:30 +01:00
* @param AngelType[]|Collection $angeltypes_source
* @param ShiftSignupState $shift_signup_state
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-10-15 19:25:55 +02:00
function Shift_view(
Shift $shift,
ShiftType $shifttype,
Location $location,
$angeltypes_source,
ShiftSignupState $shift_signup_state
) {
$shift_admin = auth()->can('admin_shifts');
$user_shift_admin = auth()->can('user_shifts_admin');
2023-10-15 19:25:55 +02:00
$admin_locations = auth()->can('admin_locations');
$admin_shifttypes = auth()->can('shifttypes');
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$parsedown = new Parsedown();
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$angeltypes = [];
foreach ($angeltypes_source as $angeltype) {
2022-11-09 00:02:30 +01:00
$angeltypes[$angeltype->id] = $angeltype;
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$needed_angels = '';
2023-01-03 22:19:03 +01:00
$neededAngels = new Collection($shift->neededAngels);
foreach ($neededAngels as $needed_angeltype) {
2017-01-02 03:57:23 +01:00
$needed_angels .= Shift_view_render_needed_angeltype($needed_angeltype, $angeltypes, $shift, $user_shift_admin);
}
2017-01-02 15:43:36 +01:00
2023-01-18 13:02:11 +01:00
$shiftEntry = $shift->shiftEntries;
foreach ($shiftEntry->groupBy('angel_type_id') as $angelTypes) {
/** @var Collection $angelTypes */
2023-01-18 13:02:11 +01:00
$type = $angelTypes->first()['angel_type_id'];
if (!$neededAngels->where('angel_type_id', $type)->first()) {
$needed_angels .= Shift_view_render_needed_angeltype([
2023-01-18 13:02:11 +01:00
'angel_type_id' => $type,
'count' => 0,
'restricted' => true,
'taken' => $angelTypes->count(),
], $angeltypes, $shift, $user_shift_admin);
}
}
$content = [msg()];
if ($shift_signup_state->getState() === ShiftSignupStatus::COLLIDES) {
$content[] = info(__('This shift collides with one of your shifts.'), true);
}
if ($shift_signup_state->getState() === ShiftSignupStatus::SIGNED_UP) {
$content[] = info(__('You are signed up for this shift.'), true);
}
2023-01-03 22:19:03 +01:00
if (config('signup_advance_hours') && $shift->start->timestamp > time() + config('signup_advance_hours') * 3600) {
$content[] = info(sprintf(
2019-08-21 22:11:20 +02:00
__('This shift is in the far future and becomes available for signup at %s.'),
2023-11-23 14:30:46 +01:00
date(__('general.datetime'), $shift->start->timestamp - config('signup_advance_hours') * 3600)
), true);
}
$buttons = [];
2023-10-15 19:25:55 +02:00
if ($shift_admin || $admin_shifttypes || $admin_locations) {
$buttons = [
$shift_admin ? button(shift_edit_link($shift), icon('pencil') . __('edit')) : '',
$shift_admin ? button(shift_delete_link($shift), icon('trash') . __('delete')) : '',
2023-10-25 02:42:11 +02:00
$admin_shifttypes ? button(url('/admin/shifttypes/' . $shifttype->id), $shifttype->name) : '',
2023-10-15 19:25:55 +02:00
$admin_locations ? button(location_link($location), icon('pin-map-fill') . $location->name) : '',
];
}
$buttons[] = button(
user_link(auth()->user()->id),
'<span class="icon-icon_angel"></span> ' . __('profile.my-shifts')
);
$content[] = buttons($buttons);
2023-10-15 19:25:55 +02:00
$content[] = Shift_view_header($shift, $location);
$content[] = div('row', [
div('col-sm-6', [
'<h2>' . __('Needed angels') . '</h2>',
'<div class="list-group">' . $needed_angels . '</div>',
]),
div('col-sm-6', [
2023-10-02 16:15:25 +02:00
'<h2>' . __('general.description') . '</h2>',
$parsedown->parse($shifttype->description),
2023-01-03 22:19:03 +01:00
$parsedown->parse($shift->description),
]),
]);
if ($shift_admin) {
$content[] = Shift_editor_info_render($shift);
}
2023-11-23 14:30:46 +01:00
$start = $shift->start->format(__('general.datetime'));
2023-11-13 16:56:52 +01:00
$link = button(url('/user-shifts'), icon('chevron-left'), 'btn-sm');
2017-01-02 15:43:36 +01:00
return page_with_title(
2023-11-03 15:02:54 +01:00
$link . ' ' . $shift->shiftType->name . ' <small title="' . $start . '" data-countdown-ts="' . $shift->start->timestamp . '">%c</small>',
$content
2017-01-02 15:43:36 +01:00
);
2014-12-19 22:41:55 +01:00
}
2017-01-03 03:22:48 +01:00
/**
2022-11-09 00:02:30 +01:00
* @param array $needed_angeltype
* @param AngelType[]|Collection $angeltypes
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2022-11-09 00:02:30 +01:00
* @param bool $user_shift_admin
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-01-03 22:19:03 +01:00
function Shift_view_render_needed_angeltype($needed_angeltype, $angeltypes, Shift $shift, $user_shift_admin)
2017-01-02 03:57:23 +01:00
{
2023-01-18 13:02:11 +01:00
$angeltype = $angeltypes[$needed_angeltype['angel_type_id']];
2022-12-03 00:57:04 +01:00
$angeltype_supporter = auth()->user()->isAngelTypeSupporter($angeltype)
|| auth()->can('admin_user_angeltypes');
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$needed_angels = '';
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$class = 'progress-bar-warning';
if ($needed_angeltype['taken'] == 0) {
$class = 'progress-bar-danger';
}
if ($needed_angeltype['taken'] >= $needed_angeltype['count']) {
$class = 'progress-bar-success';
}
$needed_angels .= '<div class="list-group-item">';
2017-01-02 15:43:36 +01:00
2021-07-24 18:19:53 +02:00
$needed_angels .= '<div class="float-end m-3">' . Shift_signup_button_render($shift, $angeltype) . '</div>';
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$needed_angels .= '<h3>' . AngelType_name_render($angeltype) . '</h3>';
$bar_max = max($needed_angeltype['count'] * 10, $needed_angeltype['taken'] * 10, 10);
2019-08-21 22:11:20 +02:00
$bar_value = max($bar_max / 10, $needed_angeltype['taken'] * 10);
2017-01-02 15:43:36 +01:00
$needed_angels .= progress_bar(
0,
$bar_max,
$bar_value,
$class,
$needed_angeltype['taken'] . ' / ' . $needed_angeltype['count']
);
2017-01-02 03:57:23 +01:00
$angels = [];
2023-01-18 13:02:11 +01:00
foreach ($shift->shiftEntries as $shift_entry) {
if ($shift_entry->angel_type_id == $needed_angeltype['angel_type_id']) {
$angels[] = Shift_view_render_shift_entry($shift_entry, $user_shift_admin, $angeltype_supporter, $shift);
2017-01-02 03:57:23 +01:00
}
2016-11-15 17:54:59 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$needed_angels .= join(', ', $angels);
$needed_angels .= '</div>';
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return $needed_angels;
2016-11-15 17:54:59 +01:00
}
2017-01-03 03:22:48 +01:00
/**
2023-01-18 13:02:11 +01:00
* @param ShiftEntry $shift_entry
2017-01-03 03:22:48 +01:00
* @param bool $user_shift_admin
* @param bool $angeltype_supporter
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-01-18 13:02:11 +01:00
function Shift_view_render_shift_entry(ShiftEntry $shift_entry, $user_shift_admin, $angeltype_supporter, Shift $shift)
2017-01-02 03:57:23 +01:00
{
2023-01-18 13:02:11 +01:00
$entry = User_Nick_render($shift_entry->user);
if ($shift_entry->freeloaded) {
2017-01-03 03:22:48 +01:00
$entry = '<del>' . $entry . '</del>';
2017-01-02 03:57:23 +01:00
}
2023-01-18 13:02:11 +01:00
$isUser = $shift_entry->user_id == auth()->user()->id;
if ($user_shift_admin || $angeltype_supporter || $isUser) {
2021-07-24 18:19:53 +02:00
$entry .= ' <div class="btn-group m-1">';
if ($user_shift_admin || $isUser) {
$entry .= button_icon(
2023-11-13 16:56:52 +01:00
url('/user-myshifts', ['edit' => $shift_entry->id, 'id' => $shift_entry->user_id]),
2017-01-02 15:43:36 +01:00
'pencil',
2021-07-23 01:52:19 +02:00
'btn-sm'
2017-01-02 15:43:36 +01:00
);
2017-01-02 03:57:23 +01:00
}
2023-01-18 13:02:11 +01:00
$angeltype = $shift_entry->angelType;
$disabled = Shift_signout_allowed($shift, $angeltype, $shift_entry->user_id) ? '' : ' btn-disabled';
$entry .= button_icon(shift_entry_delete_link($shift_entry), 'trash', 'btn-sm' . $disabled);
2017-01-02 03:57:23 +01:00
$entry .= '</div>';
}
2017-01-02 03:57:23 +01:00
return $entry;
2016-11-15 17:54:59 +01:00
}
/**
* Calc shift length in format 12:23h.
2014-12-19 22:41:55 +01:00
*
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-01-03 22:19:03 +01:00
function shift_length(Shift $shift)
2017-01-02 03:57:23 +01:00
{
2023-01-03 22:19:03 +01:00
$length = floor(($shift->end->timestamp - $shift->start->timestamp) / (60 * 60)) . ':';
2017-12-25 23:12:52 +01:00
$length .= str_pad(
2023-01-03 22:19:03 +01:00
(($shift->end->timestamp - $shift->start->timestamp) % (60 * 60)) / 60,
2022-10-18 19:15:22 +02:00
2,
'0',
STR_PAD_LEFT
2022-11-09 00:02:30 +01:00
);
$length .= 'h';
2017-01-02 03:57:23 +01:00
return $length;
}