engelsystem/includes/view/Shifts_view.php

296 lines
9.6 KiB
PHP
Raw Normal View History

<?php
2017-08-28 16:21:10 +02:00
2020-09-06 23:50:36 +02:00
use Engelsystem\Models\Room;
2018-10-10 03:10:28 +02:00
use Engelsystem\Models\User\User;
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
*
* @param array $shift
2020-09-06 23:50:36 +02:00
* @param Room $room
* @return string HTML
*/
2020-09-06 23:50:36 +02:00
function Shift_view_header($shift, Room $room)
2017-12-25 23:12:52 +01:00
{
return div('row', [
div('col-sm-3 col-xs-6', [
'<h4>' . __('Title') . '</h4>',
2017-12-25 23:12:52 +01:00
'<p class="lead">'
. ($shift['URL'] != ''
? '<a href="' . $shift['URL'] . '">' . $shift['title'] . '</a>'
: $shift['title'])
. '</p>'
]),
div('col-sm-3 col-xs-6', [
'<h4>' . __('Start') . '</h4>',
'<p class="lead' . (time() >= $shift['start'] ? ' text-success' : '') . '">',
2021-07-24 21:08:04 +02:00
icon('calendar3') . date(__('Y-m-d'), $shift['start']),
'<br />',
icon('clock') . date('H:i', $shift['start']),
'</p>'
]),
div('col-sm-3 col-xs-6', [
'<h4>' . __('End') . '</h4>',
'<p class="lead' . (time() >= $shift['end'] ? ' text-success' : '') . '">',
2021-07-24 21:08:04 +02:00
icon('calendar3') . date(__('Y-m-d'), $shift['end']),
'<br />',
icon('clock') . date('H:i', $shift['end']),
'</p>'
]),
div('col-sm-3 col-xs-6', [
'<h4>' . __('Location') . '</h4>',
'<p class="lead">' . Room_name_render($room) . '</p>'
])
]);
}
2017-01-03 03:22:48 +01:00
/**
* @param array $shift
* @return string
*/
2017-01-02 03:57:23 +01:00
function Shift_editor_info_render($shift)
{
$info = [];
2018-01-14 17:47:26 +01:00
if (!empty($shift['created_by_user_id'])) {
2017-01-02 15:43:36 +01:00
$info[] = sprintf(
icon('plus-lg') . __('created at %s by %s'),
2017-01-02 15:43:36 +01:00
date('Y-m-d H:i', $shift['created_at_timestamp']),
2018-10-10 03:10:28 +02:00
User_Nick_render(User::find($shift['created_by_user_id']))
2017-01-02 15:43:36 +01:00
);
2017-01-02 03:57:23 +01:00
}
2018-01-14 17:47:26 +01:00
if (!empty($shift['edited_by_user_id'])) {
2017-01-02 15:43:36 +01:00
$info[] = sprintf(
icon('pencil') . __('edited at %s by %s'),
2017-01-02 15:43:36 +01:00
date('Y-m-d H:i', $shift['edited_at_timestamp']),
2018-10-10 03:10:28 +02:00
User_Nick_render(User::find($shift['edited_by_user_id']))
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
/**
* @param array $shift
* @param array $angeltype
* @param array $user_angeltype
* @return string
*/
2017-01-02 03:57:23 +01:00
function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null)
{
2018-01-14 17:47:26 +01:00
if (empty($user_angeltype)) {
2018-10-17 01:30:10 +02:00
$user_angeltype = UserAngelType_by_User_and_AngelType(auth()->user()->id, $angeltype);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
if (
isset($angeltype['shift_signup_state'])
&& (
$angeltype['shift_signup_state']->isSignupAllowed()
|| User_is_AngelType_supporter(auth()->user(), $angeltype)
)
) {
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(
2017-08-28 16:21:10 +02:00
page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]),
2022-10-18 19:15:22 +02:00
sprintf(
__('Become %s'),
$angeltype['name']
)
2017-01-02 15:43:36 +01:00
);
2017-01-02 03:57:23 +01:00
}
return '';
}
2017-01-03 03:22:48 +01:00
/**
* @param array $shift
* @param array $shifttype
2020-09-06 23:50:36 +02:00
* @param Room $room
2017-01-03 03:22:48 +01:00
* @param array[] $angeltypes_source
* @param ShiftSignupState $shift_signup_state
* @return string
*/
2020-09-06 23:50:36 +02:00
function Shift_view($shift, $shifttype, Room $room, $angeltypes_source, ShiftSignupState $shift_signup_state)
2017-01-02 03:57:23 +01:00
{
$shift_admin = auth()->can('admin_shifts');
$user_shift_admin = auth()->can('user_shifts_admin');
$admin_rooms = auth()->can('admin_rooms');
$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) {
$angeltypes[$angeltype['id']] = $angeltype;
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$needed_angels = '';
$neededAngels = new Collection($shift['NeedAngels']);
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
$shiftEntry = new Collection($shift['ShiftEntry']);
foreach ($shiftEntry->groupBy('TID') as $angelTypes) {
/** @var Collection $angelTypes */
$type = $angelTypes->first()['TID'];
if (!$neededAngels->where('TID', $type)->first()) {
$needed_angels .= Shift_view_render_needed_angeltype([
'TID' => $type,
'count' => 0,
'restricted' => true,
'taken' => $angelTypes->count(),
], $angeltypes, $shift, $user_shift_admin);
}
}
$content = [msg()];
if ($shift_signup_state->getState() == ShiftSignupState::COLLIDES) {
$content[] = info(__('This shift collides with one of your shifts.'), true);
}
if ($shift_signup_state->getState() == ShiftSignupState::SIGNED_UP) {
$content[] = info(__('You are signed up for this shift.'), true);
}
if (config('signup_advance_hours') && $shift['start'] > 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.'),
date(__('Y-m-d') . ' H:i', $shift['start'] - config('signup_advance_hours') * 3600)
), true);
}
$buttons = [];
if ($shift_admin || $admin_shifttypes || $admin_rooms) {
$buttons = [
$shift_admin ? button(shift_edit_link($shift), icon('pencil') . __('edit')) : '',
$shift_admin ? button(shift_delete_link($shift), icon('trash') . __('delete')) : '',
$admin_shifttypes ? button(shifttype_link($shifttype), $shifttype['name']) : '',
$admin_rooms ? button(room_link($room), icon('geo-alt') . $room->name) : '',
];
}
2018-10-17 01:30:10 +02:00
$buttons[] = button(user_link(auth()->user()->id), '<span class="icon-icon_angel"></span> ' . __('My shifts'));
$content[] = buttons($buttons);
$content[] = Shift_view_header($shift, $room);
$content[] = div('row', [
div('col-sm-6', [
'<h2>' . __('Needed angels') . '</h2>',
'<div class="list-group">' . $needed_angels . '</div>'
]),
div('col-sm-6', [
'<h2>' . __('Description') . '</h2>',
2021-12-01 00:27:46 +01:00
$parsedown->parse((string)$shifttype['description']),
$parsedown->parse((string)$shift['description']),
])
]);
if ($shift_admin) {
$content[] = Shift_editor_info_render($shift);
}
2017-01-02 15:43:36 +01:00
return page_with_title(
$shift['name'] . ' <small class="moment-countdown" data-timestamp="' . $shift['start'] . '">%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
/**
* @param array $needed_angeltype
* @param array $angeltypes
* @param array[] $shift
* @param bool $user_shift_admin
* @return string
*/
2017-01-02 03:57:23 +01:00
function Shift_view_render_needed_angeltype($needed_angeltype, $angeltypes, $shift, $user_shift_admin)
{
$angeltype = $angeltypes[$needed_angeltype['TID']];
2018-10-10 03:10:28 +02:00
$angeltype_supporter = User_is_AngelType_supporter(auth()->user(), $angeltype);
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 = [];
foreach ($shift['ShiftEntry'] as $shift_entry) {
if ($shift_entry['TID'] == $needed_angeltype['TID']) {
$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
/**
* @param array $shift_entry
* @param bool $user_shift_admin
* @param bool $angeltype_supporter
* @param array $shift
2017-01-03 03:22:48 +01:00
* @return string
*/
function Shift_view_render_shift_entry($shift_entry, $user_shift_admin, $angeltype_supporter, $shift)
2017-01-02 03:57:23 +01:00
{
2018-10-10 03:10:28 +02:00
$entry = User_Nick_render(User::find($shift_entry['UID']));
2017-01-02 03:57:23 +01:00
if ($shift_entry['freeloaded']) {
2017-01-03 03:22:48 +01:00
$entry = '<del>' . $entry . '</del>';
2017-01-02 03:57:23 +01:00
}
$isUser = $shift_entry['UID'] == 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(
2017-08-28 16:21:10 +02:00
page_link_to('user_myshifts', ['edit' => $shift_entry['id'], 'id' => $shift_entry['UID']]),
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
}
$angeltype = AngelType($shift_entry['TID']);
$disabled = Shift_signout_allowed($shift, $angeltype, $shift_entry['UID']) ? '' : ' 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
*
2017-01-03 03:22:48 +01:00
* @param array $shift
* @return string
*/
2017-01-02 03:57:23 +01:00
function shift_length($shift)
{
2017-01-03 14:12:17 +01:00
$length = floor(($shift['end'] - $shift['start']) / (60 * 60)) . ':';
2017-12-25 23:12:52 +01:00
$length .= str_pad(
2022-10-18 19:15:22 +02:00
(($shift['end'] - $shift['start']) % (60 * 60)) / 60,
2,
'0',
STR_PAD_LEFT
) . 'h';
2017-01-02 03:57:23 +01:00
return $length;
}