2016-11-07 20:50:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a single shift for the shift calendar
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
class ShiftCalendarShiftRenderer
|
|
|
|
{
|
2016-11-07 20:50:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a shift
|
|
|
|
*
|
|
|
|
* @param Shift $shift
|
|
|
|
* The shift to render
|
2016-11-12 23:00:46 +01:00
|
|
|
* @param User $user
|
|
|
|
* The user who is viewing the shift calendar
|
2016-11-07 20:50:15 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
public function render($shift, $needed_angeltypes, $shift_entries, $user)
|
|
|
|
{
|
|
|
|
$info_text = "";
|
|
|
|
if ($shift['title'] != '') {
|
|
|
|
$info_text = glyph('info-sign') . $shift['title'] . '<br>';
|
|
|
|
}
|
|
|
|
list($shift_signup_state, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $needed_angeltypes, $shift_entries, $user);
|
2016-12-29 14:51:43 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$class = $this->classForSignupState($shift_signup_state);
|
2016-11-07 20:50:15 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW);
|
|
|
|
$blocks = max(1, $blocks);
|
|
|
|
return [
|
2016-11-07 20:50:15 +01:00
|
|
|
$blocks,
|
2016-12-27 23:02:05 +01:00
|
|
|
div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [
|
2016-11-07 20:50:15 +01:00
|
|
|
$this->renderShiftHead($shift),
|
|
|
|
div('panel-body', [
|
|
|
|
$info_text,
|
|
|
|
Room_name_render([
|
|
|
|
'RID' => $shift['RID'],
|
2017-01-02 03:57:23 +01:00
|
|
|
'Name' => $shift['room_name']
|
|
|
|
])
|
2016-11-07 20:50:15 +01:00
|
|
|
]),
|
|
|
|
$shifts_row,
|
2017-01-02 03:57:23 +01:00
|
|
|
div('shift-spacer')
|
2016-12-27 23:02:05 +01:00
|
|
|
])
|
2016-11-07 20:50:15 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
private function classForSignupState(ShiftSignupState $shiftSignupState)
|
|
|
|
{
|
|
|
|
switch ($shiftSignupState->getState()) {
|
2016-12-29 14:56:09 +01:00
|
|
|
case ShiftSignupState::ADMIN:
|
2016-11-12 23:00:46 +01:00
|
|
|
case ShiftSignupState::OCCUPIED:
|
|
|
|
return 'success';
|
|
|
|
|
|
|
|
case ShiftSignupState::SIGNED_UP:
|
|
|
|
return 'primary';
|
|
|
|
|
|
|
|
case ShiftSignupState::SHIFT_ENDED:
|
|
|
|
return 'default';
|
|
|
|
|
|
|
|
case ShiftSignupState::ANGELTYPE:
|
|
|
|
case ShiftSignupState::COLLIDES:
|
|
|
|
return 'warning';
|
|
|
|
|
|
|
|
case ShiftSignupState::FREE:
|
|
|
|
return 'danger';
|
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-12 23:00:46 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
private function renderShiftNeededAngeltypes($shift, $needed_angeltypes, $shift_entries, $user)
|
|
|
|
{
|
|
|
|
global $privileges;
|
2016-11-07 21:24:05 +01:00
|
|
|
|
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) {
|
|
|
|
$shift_entries_filtered[$shift_entry['TID']][] = $shift_entry;
|
|
|
|
}
|
2016-12-27 23:02:05 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$html = "";
|
|
|
|
$shift_signup_state = null;
|
|
|
|
foreach ($needed_angeltypes as $angeltype) {
|
|
|
|
if ($angeltype['count'] > 0 || count($shift_entries_filtered[$angeltype['id']]) > 0) {
|
|
|
|
list($angeltype_signup_state, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $shift_entries_filtered[$angeltype['id']], $angeltype, $user);
|
|
|
|
if ($shift_signup_state == null) {
|
|
|
|
$shift_signup_state = $angeltype_signup_state;
|
|
|
|
} else {
|
|
|
|
$shift_signup_state->combineWith($angeltype_signup_state);
|
|
|
|
}
|
|
|
|
$html .= $angeltype_html;
|
|
|
|
}
|
|
|
|
}
|
2016-12-27 23:02:05 +01:00
|
|
|
if ($shift_signup_state == null) {
|
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
|
|
|
}
|
2016-12-27 19:13:24 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (in_array('user_shifts_admin', $privileges)) {
|
|
|
|
$html .= '<li class="list-group-item">' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'], _("Add more angels"), 'btn-xs') . '</li>';
|
|
|
|
}
|
|
|
|
if ($html != '') {
|
|
|
|
return [
|
2016-11-12 23:00:46 +01:00
|
|
|
$shift_signup_state,
|
2017-01-02 03:57:23 +01:00
|
|
|
'<ul class="list-group">' . $html . '</ul>'
|
2016-11-07 21:24:05 +01:00
|
|
|
];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return [
|
2016-11-12 23:00:46 +01:00
|
|
|
$shift_signup_state,
|
2017-01-02 03:57:23 +01:00
|
|
|
""
|
2016-11-07 21:24:05 +01:00
|
|
|
];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-11-07 21:24:05 +01:00
|
|
|
|
2016-11-07 20:50:15 +01:00
|
|
|
/**
|
|
|
|
* Renders a list entry containing the needed angels for an angeltype
|
|
|
|
*
|
|
|
|
* @param Shift $shift
|
|
|
|
* The shift which is rendered
|
|
|
|
* @param Angeltype $angeltype
|
|
|
|
* The angeltype, containing informations about needed angeltypes and already signed up angels
|
2016-11-12 23:00:46 +01:00
|
|
|
* @param User $user
|
|
|
|
* The user who is viewing the shift calendar
|
2016-11-07 20:50:15 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
private function renderShiftNeededAngeltype($shift, $shift_entries, $angeltype, $user)
|
|
|
|
{
|
|
|
|
$entry_list = [];
|
|
|
|
foreach ($shift_entries as $entry) {
|
|
|
|
$style = $entry['freeloaded'] ? " text-decoration: line-through;" : '';
|
|
|
|
$entry_list[] = "<span style=\"$style\">" . User_Nick_render($entry) . "</span>";
|
|
|
|
}
|
|
|
|
$shift_signup_state = Shift_signup_allowed($user, $shift, $angeltype, null, null, $angeltype, $shift_entries);
|
|
|
|
$inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $shift_signup_state->getFreeEntries()), $shift_signup_state->getFreeEntries());
|
|
|
|
switch ($shift_signup_state->getState()) {
|
2016-11-12 23:00:46 +01:00
|
|
|
case ShiftSignupState::ADMIN:
|
|
|
|
case ShiftSignupState::FREE:
|
|
|
|
// When admin or free display a link + button for sign up
|
|
|
|
$entry_list[] = '<a href="' . page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'] . '">' . $inner_text . '</a> ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs btn-primary');
|
|
|
|
break;
|
2016-11-07 20:50:15 +01:00
|
|
|
|
2016-11-12 23:00:46 +01:00
|
|
|
case ShiftSignupState::SHIFT_ENDED:
|
|
|
|
// No link and add a text hint, when the shift ended
|
|
|
|
$entry_list[] = $inner_text . ' (' . _('ended') . ')';
|
|
|
|
break;
|
2016-11-07 20:50:15 +01:00
|
|
|
|
2016-11-12 23:00:46 +01:00
|
|
|
case ShiftSignupState::ANGELTYPE:
|
|
|
|
if ($angeltype['restricted'] == 1) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// User has to be confirmed on the angeltype first
|
2016-11-07 20:50:15 +01:00
|
|
|
$entry_list[] = $inner_text . glyph('lock');
|
|
|
|
} else {
|
2017-01-02 03:57:23 +01:00
|
|
|
// Add link to join the angeltype first
|
2016-11-07 20:50:15 +01:00
|
|
|
$entry_list[] = $inner_text . '<br />' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs');
|
|
|
|
}
|
2016-11-12 23:00:46 +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;
|
2016-11-07 20:50:15 +01:00
|
|
|
|
2016-11-12 23:00:46 +01:00
|
|
|
case ShiftSignupState::OCCUPIED:
|
|
|
|
// Shift is full
|
|
|
|
break;
|
2016-11-07 20:50:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$shifts_row = '<li class="list-group-item">';
|
|
|
|
$shifts_row .= '<strong>' . AngelType_name_render($angeltype) . ':</strong> ';
|
|
|
|
$shifts_row .= join(", ", $entry_list);
|
|
|
|
$shifts_row .= '</li>';
|
|
|
|
return [
|
2016-11-12 23:00:46 +01:00
|
|
|
$shift_signup_state,
|
2017-01-02 03:57:23 +01:00
|
|
|
$shifts_row
|
2016-11-07 21:24:05 +01:00
|
|
|
];
|
2016-11-07 20:50:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the shift header
|
|
|
|
*
|
|
|
|
* @param Shift $shift
|
|
|
|
* The shift
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
private function renderShiftHead($shift)
|
|
|
|
{
|
|
|
|
global $privileges;
|
2016-11-07 20:50:15 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$header_buttons = "";
|
|
|
|
if (in_array('admin_shifts', $privileges)) {
|
|
|
|
$header_buttons = '<div class="pull-right">' . table_buttons([
|
2016-11-07 20:50:15 +01:00
|
|
|
button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'),
|
2017-01-02 03:57:23 +01:00
|
|
|
button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs')
|
2016-11-07 20:50:15 +01:00
|
|
|
]) . '</div>';
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
$shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . $shift['name'];
|
|
|
|
return div('panel-heading', [
|
2016-11-07 20:50:15 +01:00
|
|
|
'<a href="' . shift_link($shift) . '">' . $shift_heading . '</a>',
|
2017-01-02 03:57:23 +01:00
|
|
|
$header_buttons
|
2016-11-07 20:50:15 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|