summaryrefslogtreecommitdiff
path: root/includes/view/ShiftCalendarShiftRenderer.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2016-11-09 17:43:56 +0100
committerGitHub <noreply@github.com>2016-11-09 17:43:56 +0100
commitd43eb41d25d0d5c0509417247030dd6c21118cf6 (patch)
tree2c3f78bf8fbd4215e70af7dbc2ce30cfef674816 /includes/view/ShiftCalendarShiftRenderer.php
parentd5d2acc7d80920eef5f0ed779a3738a12d5db348 (diff)
parent22520532c78b3a032aec6ececb7623ba094da8de (diff)
Merge pull request #274 from engelsystem/task-164-shift-view
Task 164 shift view
Diffstat (limited to 'includes/view/ShiftCalendarShiftRenderer.php')
-rw-r--r--includes/view/ShiftCalendarShiftRenderer.php188
1 files changed, 188 insertions, 0 deletions
diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php
new file mode 100644
index 00000000..e86f7baa
--- /dev/null
+++ b/includes/view/ShiftCalendarShiftRenderer.php
@@ -0,0 +1,188 @@
+<?php
+
+namespace Engelsystem;
+
+/**
+ * Renders a single shift for the shift calendar
+ */
+class ShiftCalendarShiftRenderer {
+
+ /**
+ * Renders a shift
+ *
+ * @param Shift $shift
+ * The shift to render
+ */
+ public function render($shift) {
+ global $privileges;
+
+ $collides = $this->collides();
+ $info_text = "";
+ if ($shift['title'] != '') {
+ $info_text = glyph('info-sign') . $shift['title'] . '<br>';
+ }
+ list($is_free, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $collides);
+
+ if (isset($shift['own']) && $shift['own'] && ! in_array('user_shifts_admin', $privileges)) {
+ $class = 'primary';
+ } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) {
+ $class = 'default';
+ } elseif ($is_free) {
+ $class = 'danger';
+ } else {
+ $class = 'success';
+ }
+
+ $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 renderShiftNeededAngeltypes($shift, $collides) {
+ global $privileges;
+
+ $html = "";
+ $is_free = false;
+ $angeltypes = NeededAngelTypes_by_shift($shift['SID']);
+ foreach ($angeltypes as $angeltype) {
+ list($angeltype_free, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $angeltype, $collides);
+ $is_free |= $angeltype_free;
+ $html .= $angeltype_html;
+ }
+ if (in_array('user_shifts_admin', $privileges)) {
+ $html .= '<li class="list-group-item">' . button(page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '</li>';
+ }
+ if ($html != '') {
+ return [
+ $is_free,
+ '<ul class="list-group">' . $html . '</ul>'
+ ];
+ }
+ return [
+ $is_free,
+ ""
+ ];
+ }
+
+ /**
+ * 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 boolean $collides
+ * true if the shift collides with the users shifts
+ */
+ private function renderShiftNeededAngeltype($shift, $angeltype, $collides) {
+ global $privileges;
+
+ $is_free = false;
+ $entry_list = [];
+ $freeloader = 0;
+ foreach ($angeltype['shift_entries'] as $entry) {
+ $style = '';
+ if ($entry['freeloaded']) {
+ $freeloader ++;
+ $style = " text-decoration: line-through;";
+ }
+ $entry_list[] = "<span style=\"$style\">" . User_Nick_render(User($entry['UID'])) . "</span>";
+ }
+ if ($angeltype['count'] - count($angeltype['shift_entries']) - $freeloader > 0) {
+ $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($angeltype['shift_entries'])), $angeltype['count'] - count($angeltype['shift_entries']));
+ // is the shift still running or alternatively is the user shift admin?
+ $user_may_join_shift = true;
+
+ // you cannot join if user alread joined a parallel or this shift
+ $user_may_join_shift &= ! $collides;
+
+ // you cannot join if user is not of this angel type
+ $user_may_join_shift &= isset($angeltype['user_id']);
+
+ // you cannot join if you are not confirmed
+ if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) {
+ $user_may_join_shift &= isset($angeltype['confirm_user_id']);
+ }
+
+ // you can only join if the shift is in future or running
+ $user_may_join_shift &= time() < $shift['start'];
+
+ // User shift admins may join anybody in every shift
+ $user_may_join_shift |= in_array('user_shifts_admin', $privileges);
+ if ($user_may_join_shift) {
+ $entry_list[] = '<a href="' . page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'] . '">' . $inner_text . '</a> ' . button(page_link_to('user_shifts') . '&amp;shift_id=' . $shift['SID'] . '&amp;type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs btn-primary');
+ } else {
+ if (time() > $shift['start']) {
+ $entry_list[] = $inner_text . ' (' . _('ended') . ')';
+ } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) {
+ $entry_list[] = $inner_text . glyph('lock');
+ } elseif ($angeltype['restricted'] == 1) {
+ $entry_list[] = $inner_text;
+ } elseif ($collides) {
+ $entry_list[] = $inner_text;
+ } else {
+ $entry_list[] = $inner_text . '<br />' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs');
+ }
+ }
+
+ unset($inner_text);
+ $is_free = true;
+ }
+
+ $shifts_row = '<li class="list-group-item">';
+ $shifts_row .= '<strong>' . AngelType_name_render($angeltype) . ':</strong> ';
+ $shifts_row .= join(", ", $entry_list);
+ $shifts_row .= '</li>';
+ return [
+ $is_free,
+ $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']) . ' &dash; ' . date('H:i', $shift['end']) . ' &mdash; ' . ShiftType($shift['shifttype_id'])['name'];
+ return div('panel-heading', [
+ '<a href="' . shift_link($shift) . '">' . $shift_heading . '</a>',
+ $header_buttons
+ ]);
+ }
+
+ /**
+ * Does the shift collide with the user's shifts
+ */
+ private function collides() {
+ // TODO
+ return false;
+ }
+}
+
+?> \ No newline at end of file