2016-11-07 20:50:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem;
|
|
|
|
|
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-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
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $shift The shift to render
|
|
|
|
* @param array $needed_angeltypes
|
|
|
|
* @param array $shift_entries
|
2018-10-10 03:10:28 +02: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
|
|
|
*/
|
|
|
|
public function render($shift, $needed_angeltypes, $shift_entries, $user)
|
|
|
|
{
|
2017-01-03 14:12:17 +01:00
|
|
|
$info_text = '';
|
2017-01-02 15:43:36 +01:00
|
|
|
if ($shift['title'] != '') {
|
2021-07-22 21:22:21 +02:00
|
|
|
$info_text = icon('info') . $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);
|
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
$blocks = ceil(($shift['end'] - $shift['start']) / ShiftCalendarRenderer::SECONDS_PER_ROW);
|
2017-01-02 15:43:36 +01:00
|
|
|
$blocks = max(1, $blocks);
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2020-09-06 23:50:36 +02:00
|
|
|
$room = new Room();
|
|
|
|
$room->name = $shift['room_name'];
|
|
|
|
$room->setAttribute('id', $shift['RID']);
|
|
|
|
|
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(
|
2019-10-20 14:54:00 +02:00
|
|
|
'shift panel panel-' . $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()),
|
2018-12-21 16:18:47 +01:00
|
|
|
div('panel-body', [
|
|
|
|
$info_text,
|
2020-09-06 23:50:36 +02:00
|
|
|
Room_name_render($room)
|
2018-12-21 16:18:47 +01:00
|
|
|
]),
|
2019-10-20 14:54:00 +02:00
|
|
|
$shifts_row
|
2018-12-21 16:18:47 +01:00
|
|
|
]
|
|
|
|
)
|
2019-10-20 14:54:00 +02: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)
|
|
|
|
{
|
|
|
|
switch ($shiftSignupState->getState()) {
|
2017-01-02 15:43:36 +01:00
|
|
|
case ShiftSignupState::ADMIN:
|
|
|
|
case ShiftSignupState::OCCUPIED:
|
|
|
|
return 'success';
|
|
|
|
|
|
|
|
case ShiftSignupState::SIGNED_UP:
|
|
|
|
return 'primary';
|
|
|
|
|
2017-12-20 23:05:46 +01:00
|
|
|
case ShiftSignupState::NOT_ARRIVED:
|
2017-12-03 04:05:02 +01:00
|
|
|
case ShiftSignupState::NOT_YET:
|
2017-01-02 15:43:36 +01:00
|
|
|
case ShiftSignupState::SHIFT_ENDED:
|
|
|
|
return 'default';
|
|
|
|
|
|
|
|
case ShiftSignupState::ANGELTYPE:
|
|
|
|
case ShiftSignupState::COLLIDES:
|
|
|
|
return 'warning';
|
|
|
|
|
|
|
|
case ShiftSignupState::FREE:
|
|
|
|
return 'danger';
|
2017-01-03 03:22:48 +01:00
|
|
|
default:
|
|
|
|
return '';
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-12 23:00:46 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param array $shift
|
|
|
|
* @param array[] $needed_angeltypes
|
|
|
|
* @param array[] $shift_entries
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param User $user
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
private function renderShiftNeededAngeltypes($shift, $needed_angeltypes, $shift_entries, $user)
|
|
|
|
{
|
|
|
|
$shift_entries_filtered = [];
|
|
|
|
foreach ($needed_angeltypes as $needed_angeltype) {
|
|
|
|
$shift_entries_filtered[$needed_angeltype['id']] = [];
|
|
|
|
}
|
|
|
|
foreach ($shift_entries as $shift_entry) {
|
|
|
|
$shift_entries_filtered[$shift_entry['TID']][] = $shift_entry;
|
|
|
|
}
|
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)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$shift_signup_state = new ShiftSignupState(ShiftSignupState::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')) {
|
2017-09-16 17:14:45 +02:00
|
|
|
$html .= '<li class="list-group-item">';
|
2017-12-20 00:42:58 +01: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,
|
|
|
|
'<ul class="list-group">' . $html . '</ul>'
|
|
|
|
];
|
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,
|
2017-01-03 14:12:17 +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
|
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param array $shift The shift which is rendered
|
|
|
|
* @param array[] $shift_entries
|
2017-12-25 21:29:00 +01:00
|
|
|
* @param array[] $angeltype The angeltype, containing information about needed angeltypes
|
2017-01-03 03:22:48 +01:00
|
|
|
* and already signed up angels
|
2018-10-10 03:10:28 +02: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
|
|
|
*/
|
|
|
|
private function renderShiftNeededAngeltype($shift, $shift_entries, $angeltype, $user)
|
|
|
|
{
|
|
|
|
$entry_list = [];
|
|
|
|
foreach ($shift_entries as $entry) {
|
2017-01-03 14:12:17 +01:00
|
|
|
$style = $entry['freeloaded'] ? ' text-decoration: line-through;' : '';
|
|
|
|
$entry_list[] = '<span style="' . $style . '">' . User_Nick_render($entry) . '</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
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
switch ($shift_signup_state->getState()) {
|
|
|
|
case ShiftSignupState::ADMIN:
|
|
|
|
case ShiftSignupState::FREE:
|
|
|
|
// When admin or free display a link + button for sign up
|
2017-08-28 16:21:10 +02:00
|
|
|
$entry_list[] = '<a href="'
|
2017-12-20 00:42:58 +01:00
|
|
|
. shift_entry_create_link($shift, $angeltype)
|
2017-08-28 16:21:10 +02:00
|
|
|
. '">'
|
2017-01-02 15:43:36 +01:00
|
|
|
. $inner_text
|
|
|
|
. '</a> '
|
|
|
|
. button(
|
2017-12-20 00:42:58 +01:00
|
|
|
shift_entry_create_link($shift, $angeltype),
|
2021-07-23 01:52:19 +02:00
|
|
|
__('Sign up'), 'btn-sm btn-primary hidden-print'
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
|
|
|
break;
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
case ShiftSignupState::SHIFT_ENDED:
|
|
|
|
// No link and add a text hint, when the shift ended
|
2018-08-29 21:55:32 +02:00
|
|
|
$entry_list[] = $inner_text . ' (' . __('ended') . ')';
|
2017-01-02 15:43:36 +01:00
|
|
|
break;
|
2017-12-25 23:12:52 +01:00
|
|
|
|
2017-12-20 23:05:46 +01:00
|
|
|
case ShiftSignupState::NOT_ARRIVED:
|
|
|
|
// No link and add a text hint, when the shift ended
|
2018-08-29 21:55:32 +02:00
|
|
|
$entry_list[] = $inner_text . ' (' . __('please arrive for signup') . ')';
|
2017-12-20 23:05:46 +01:00
|
|
|
break;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-03 04:05:02 +01:00
|
|
|
case ShiftSignupState::NOT_YET:
|
2019-08-21 22:11:20 +02:00
|
|
|
$entry_list[] = $inner_text . ' (' . __('not yet') . ')';
|
2017-12-03 04:05:02 +01:00
|
|
|
break;
|
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
case ShiftSignupState::ANGELTYPE:
|
|
|
|
if ($angeltype['restricted'] == 1) {
|
|
|
|
// User has to be confirmed on the angeltype first
|
2021-07-22 21:22:21 +02:00
|
|
|
$entry_list[] = $inner_text . icon('book');
|
2017-01-02 15:43:36 +01:00
|
|
|
} else {
|
|
|
|
// Add link to join the angeltype first
|
|
|
|
$entry_list[] = $inner_text . '<br />'
|
|
|
|
. button(
|
2017-12-25 23:12:52 +01:00
|
|
|
page_link_to(
|
|
|
|
'user_angeltypes',
|
|
|
|
['action' => 'add', 'angeltype_id' => $angeltype['id']]
|
|
|
|
),
|
2018-08-29 21:55:32 +02:00
|
|
|
sprintf(__('Become %s'), $angeltype['name']),
|
2021-07-23 01:52:19 +02:00
|
|
|
'btn-sm'
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ShiftSignupState::COLLIDES:
|
|
|
|
case ShiftSignupState::SIGNED_UP:
|
|
|
|
// Shift collides or user is already signed up: No signup allowed
|
|
|
|
$entry_list[] = $inner_text;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ShiftSignupState::OCCUPIED:
|
|
|
|
// Shift is full
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$shifts_row = '<li class="list-group-item">';
|
|
|
|
$shifts_row .= '<strong>' . 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,
|
|
|
|
$shifts_row
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the shift header
|
|
|
|
*
|
2019-08-21 22:11:20 +02:00
|
|
|
* @param array $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
|
|
|
*/
|
2019-11-03 12:08:46 +01:00
|
|
|
private function renderShiftHead($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')) {
|
2019-07-24 20:57:27 +02:00
|
|
|
$header_buttons = '<div class="pull-right hidden-print">' . table_buttons([
|
2017-12-25 23:12:52 +01:00
|
|
|
button(
|
|
|
|
page_link_to('user_shifts', ['edit_shift' => $shift['SID']]),
|
2021-07-22 21:22:21 +02:00
|
|
|
icon('pencil'),
|
2021-07-23 01:52:19 +02:00
|
|
|
"btn-$class btn-sm"
|
2017-12-25 23:12:52 +01:00
|
|
|
),
|
|
|
|
button(
|
|
|
|
page_link_to('user_shifts', ['delete_shift' => $shift['SID']]),
|
2021-07-22 21:22:21 +02:00
|
|
|
icon('trash'),
|
2021-07-23 01:52:19 +02:00
|
|
|
"btn-$class btn-sm"
|
2017-12-25 23:12:52 +01:00
|
|
|
)
|
2017-01-02 15:43:36 +01:00
|
|
|
]) . '</div>';
|
2016-11-07 20:50:15 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
$shift_heading = date('H:i', $shift['start']) . ' ‐ '
|
|
|
|
. date('H:i', $shift['end']) . ' — '
|
|
|
|
. $shift['name'];
|
2019-11-03 12:08:46 +01:00
|
|
|
|
|
|
|
if($needed_angeltypes_count > 0) {
|
|
|
|
$shift_heading = '<span class="badge">' . $needed_angeltypes_count . '</span> ' . $shift_heading;
|
|
|
|
}
|
|
|
|
|
2017-01-02 15:43:36 +01:00
|
|
|
return div('panel-heading', [
|
|
|
|
'<a href="' . shift_link($shift) . '">' . $shift_heading . '</a>',
|
|
|
|
$header_buttons
|
|
|
|
]);
|
2016-11-07 20:50:15 +01:00
|
|
|
}
|
|
|
|
}
|