1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
<?php
namespace Engelsystem;
/**
* Renders a single shift for the shift calendar
*/
class ShiftCalendarShiftRenderer {
/**
* Renders a shift
*
* @param Shift $shift
* The shift to render
* @param User $user
* The user who is viewing the shift calendar
*/
public function render($shift, $user) {
$info_text = "";
if ($shift['title'] != '') {
$info_text = glyph('info-sign') . $shift['title'] . '<br>';
}
list($shift_signup_state, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $user);
$class = $this->classForSignupState($shift_signup_state);
$blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW);
$blocks = max(1, $blocks);
return [
$blocks,
'<td class="shift" rowspan="' . $blocks . '">' . div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [
$this->renderShiftHead($shift),
div('panel-body', [
$info_text,
Room_name_render([
'RID' => $shift['RID'],
'Name' => $shift['room_name']
])
]),
$shifts_row,
div('shift-spacer')
]) . '</td>'
];
}
private function classForSignupState(ShiftSignupState $shiftSignupState) {
switch ($shiftSignupState->getState()) {
case ShiftSignupState::ADMIN:
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';
}
}
private function renderShiftNeededAngeltypes($shift, $user) {
global $privileges;
$html = "";
$shift_signup_state = null;
$angeltypes = NeededAngelTypes_by_shift($shift['SID']);
foreach ($angeltypes as $angeltype) {
list($angeltype_signup_state, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $angeltype, $user);
if ($shift_signup_state == null) {
$shift_signup_state = $angeltype_signup_state;
} else {
$shift_signup_state->combineWith($angeltype_signup_state);
}
$html .= $angeltype_html;
}
if (in_array('user_shifts_admin', $privileges)) {
$html .= '<li class="list-group-item">' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '</li>';
}
if ($html != '') {
return [
$shift_signup_state,
'<ul class="list-group">' . $html . '</ul>'
];
}
return [
$shift_signup_state,
""
];
}
/**
* 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
* @param User $user
* The user who is viewing the shift calendar
*/
private function renderShiftNeededAngeltype($shift, $angeltype, $user) {
$entry_list = [];
foreach ($angeltype['shift_entries'] as $entry) {
$style = $entry['freeloaded'] ? " text-decoration: line-through;" : '';
$entry_list[] = "<span style=\"$style\">" . User_Nick_render(User($entry['UID'])) . "</span>";
}
$shift_signup_state = Shift_signup_allowed($user, $shift, $angeltype);
$inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $shift_signup_state->getFreeEntries()), $shift_signup_state->getFreeEntries());
switch ($shift_signup_state->getState()) {
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;
case ShiftSignupState::SHIFT_ENDED:
// No link and add a text hint, when the shift ended
$entry_list[] = $inner_text . ' (' . _('ended') . ')';
break;
case ShiftSignupState::ANGELTYPE:
if ($angeltype['restricted'] == 1) {
// User has to be confirmed on the angeltype first
$entry_list[] = $inner_text . glyph('lock');
} else {
// Add link to join the angeltype first
$entry_list[] = $inner_text . '<br />' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs');
}
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> ';
$shifts_row .= join(", ", $entry_list);
$shifts_row .= '</li>';
return [
$shift_signup_state,
$shifts_row
];
}
/**
* Renders the shift header
*
* @param Shift $shift
* The shift
*/
private function renderShiftHead($shift) {
global $privileges;
$header_buttons = "";
if (in_array('admin_shifts', $privileges)) {
$header_buttons = '<div class="pull-right">' . table_buttons([
button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'),
button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs')
]) . '</div>';
}
$shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . ShiftType($shift['shifttype_id'])['name'];
return div('panel-heading', [
'<a href="' . shift_link($shift) . '">' . $shift_heading . '</a>',
$header_buttons
]);
}
}
?>
|