From db7f8c1ab3d4c53ce747db707f5b9480f5f0c024 Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 7 Nov 2016 20:50:15 +0100 Subject: [PATCH] split shift calendar renderer into different classes --- includes/engelsystem_provider.php | 1 + includes/view/ShiftCalendarRenderer.php | 127 +------------- includes/view/ShiftCalendarShiftRenderer.php | 171 +++++++++++++++++++ 3 files changed, 175 insertions(+), 124 deletions(-) create mode 100644 includes/view/ShiftCalendarShiftRenderer.php diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 0f8aa425..f3d4f5b1 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -31,6 +31,7 @@ require_once realpath(__DIR__ . '/../includes/view/Questions_view.php'); require_once realpath(__DIR__ . '/../includes/view/Rooms_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarLane.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarRenderer.php'); +require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarShiftRenderer.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftsFilterRenderer.php'); require_once realpath(__DIR__ . '/../includes/view/Shifts_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftEntry_view.php'); diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index 85aedfa0..70948ec5 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -107,7 +107,7 @@ class ShiftCalendarRenderer { */ private function renderShiftLanes() { $html = ""; - foreach ($this->lanes as $room_id => $room_lanes) { + foreach ($this->lanes as $room_lanes) { foreach ($room_lanes as $lane) { $html .= $this->renderLane($lane); } @@ -122,6 +122,7 @@ class ShiftCalendarRenderer { * The lane to render */ private function renderLane(ShiftCalendarLane $lane) { + $shift_renderer = new ShiftCalendarShiftRenderer(); $html = ""; $rendered_until = $this->getFirstBlockStartTime(); foreach ($lane->getShifts() as $shift) { @@ -130,7 +131,7 @@ class ShiftCalendarRenderer { $rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW; } - list($shift_height, $shift_html) = $this->renderShift($shift); + list($shift_height, $shift_html) = $shift_renderer->render($shift); $html .= $shift_html; $rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW; } @@ -189,128 +190,6 @@ class ShiftCalendarRenderer { return div('lane time', $time_slot); } - private function collides() { - // TODO - return false; - } - - private function renderShift($shift) { - global $privileges; - - $collides = $this->collides(); - $is_free = false; - $shifts_row = ''; - $header_buttons = ""; - if (in_array('admin_shifts', $privileges)) { - $header_buttons = '
' . 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') - ]) . '
'; - } - $info_text = ""; - if ($shift['title'] != '') { - $info_text = glyph('info-sign') . $shift['title'] . '
'; - } - - $angeltypes = NeededAngelTypes_by_shift($shift['SID']); - foreach ($angeltypes as $angeltype) { - $entry_list = []; - $freeloader = 0; - foreach ($angeltype['shift_entries'] as $entry) { - $style = ''; - if ($entry['freeloaded']) { - $freeloader ++; - $style = " text-decoration: line-through;"; - } - $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ""; - } - 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[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&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 . '
' . 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 .= '
  • '; - $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; - $shifts_row .= join(", ", $entry_list); - $shifts_row .= '
  • '; - } - if (in_array('user_shifts_admin', $privileges)) { - $shifts_row .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '
  • '; - } - if ($shifts_row != '') { - $shifts_row = ''; - } - 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); - if ($blocks < 1) { - $blocks = 1; - } - $shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . ShiftType($shift['shifttype_id'])['name']; - return [ - $blocks, - '' . div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [ - div('panel-heading', [ - '' . $shift_heading . '', - $header_buttons - ]), - div('panel-body', [ - $info_text, - Room_name_render([ - 'RID' => $shift['RID'], - 'Name' => $shift['room_name'] - ]) - ]), - $shifts_row, - div('shift-spacer') - ]) . '' - ]; - } - private function calcFirstBlockStartTime($shifts) { $start_time = $this->shiftsFilter->getEndTime(); foreach ($shifts as $shift) { diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php new file mode 100644 index 00000000..ee6ebcf0 --- /dev/null +++ b/includes/view/ShiftCalendarShiftRenderer.php @@ -0,0 +1,171 @@ +collides(); + $is_free = false; + $shifts_row = ''; + $info_text = ""; + if ($shift['title'] != '') { + $info_text = glyph('info-sign') . $shift['title'] . '
    '; + } + + $angeltypes = NeededAngelTypes_by_shift($shift['SID']); + foreach ($angeltypes as $angeltype) { + $shifts_row .= $this->renderShiftNeededAngeltype($shift, $angeltype, $collides); + } + if (in_array('user_shifts_admin', $privileges)) { + $shifts_row .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '
  • '; + } + if ($shifts_row != '') { + $shifts_row = ''; + } + 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); + if ($blocks < 1) { + $blocks = 1; + } + return [ + $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') + ]) . '' + ]; + } + + /** + * 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; + + $entry_list = []; + $freeloader = 0; + foreach ($angeltype['shift_entries'] as $entry) { + $style = ''; + if ($entry['freeloaded']) { + $freeloader ++; + $style = " text-decoration: line-through;"; + } + $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ""; + } + 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[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&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 . '
    ' . 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 = '
  • '; + $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; + $shifts_row .= join(", ", $entry_list); + $shifts_row .= '
  • '; + return $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 = '
    ' . 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') + ]) . '
    '; + } + $shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . ShiftType($shift['shifttype_id'])['name']; + return div('panel-heading', [ + '' . $shift_heading . '', + $header_buttons + ]); + } + + /** + * Does the shift collide with the user's shifts + */ + private function collides() { + // TODO + return false; + } +} + +?> \ No newline at end of file