title != '') { $info_text = icon('info-circle') . htmlspecialchars($shift->title) . '
'; } list($shift_signup_state, $shifts_row) = $this->renderShiftNeededAngeltypes( $shift, $needed_angeltypes, $shift_entries, $user ); $class = $this->classForSignupState($shift_signup_state); $blocks = ceil(($shift->end->timestamp - $shift->start->timestamp) / ShiftCalendarRenderer::SECONDS_PER_ROW); $blocks = max(1, $blocks); return [ $blocks, div( 'shift-card" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px;', div( 'shift card bg-' . $class, [ $this->renderShiftHead($shift, $class, $shift_signup_state->getFreeEntries()), div('card-body ' . $this->classBg(), [ $info_text, location_name_render($shift->location), ]), $shifts_row, ] ) ), ]; } /** * @param ShiftSignupState $shiftSignupState * @return string */ private function classForSignupState(ShiftSignupState $shiftSignupState) { return match ($shiftSignupState->getState()) { ShiftSignupStatus::ADMIN, ShiftSignupStatus::OCCUPIED => 'success', ShiftSignupStatus::SIGNED_UP => 'primary', ShiftSignupStatus::NOT_ARRIVED, ShiftSignupStatus::NOT_YET, ShiftSignupStatus::SHIFT_ENDED => 'secondary', ShiftSignupStatus::ANGELTYPE, ShiftSignupStatus::COLLIDES => 'warning', ShiftSignupStatus::FREE => 'danger', default => 'light', }; } /** * @param Shift $shift * @param array[] $needed_angeltypes * @param ShiftEntry[]|Collection $shift_entries * @param User $user * @return array */ private function renderShiftNeededAngeltypes(Shift $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->angel_type_id][] = $shift_entry; } $html = ''; /** @var ShiftSignupState $shift_signup_state */ $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 (is_null($shift_signup_state)) { $shift_signup_state = $angeltype_signup_state; } else { $shift_signup_state->combineWith($angeltype_signup_state); } $html .= $angeltype_html; } } if (is_null($shift_signup_state)) { $shift_signup_state = new ShiftSignupState(ShiftSignupStatus::SHIFT_ENDED, 0); } if (auth()->can('user_shifts_admin')) { $html .= '
  • '; $html .= button( shift_entry_create_link_admin($shift), icon('plus-lg') . __('Add more angels'), 'btn-sm' ); $html .= '
  • '; } if ($html != '') { return [ $shift_signup_state, '', ]; } return [ $shift_signup_state, '', ]; } /** * Renders a list entry containing the needed angels for an angeltype * * @param Shift $shift The shift which is rendered * @param ShiftEntry[]|Collection $shift_entries * @param array $angeltype The angeltype, containing information about needed angeltypes * and already signed up angels * @param User $user The user who is viewing the shift calendar * @return array */ private function renderShiftNeededAngeltype(Shift $shift, $shift_entries, $angeltype, $user) { $angeltype = (new AngelType())->forceFill($angeltype); $entry_list = []; foreach ($shift_entries as $entry) { $class = $entry->freeloaded ? 'text-decoration-line-through' : ''; $entry_list[] = '' . User_Nick_render($entry->user) . ''; } $shift_signup_state = Shift_signup_allowed( $user, $shift, $angeltype, null, null, $angeltype, $shift_entries ); $freeEntriesCount = $shift_signup_state->getFreeEntries(); $inner_text = _e('%d helper needed', '%d helpers needed', $freeEntriesCount, [$freeEntriesCount]); $entry = match ($shift_signup_state->getState()) { // When admin or free display a link + button for sign up ShiftSignupStatus::ADMIN, ShiftSignupStatus::FREE => '' . $inner_text . ' ' . button( shift_entry_create_link($shift, $angeltype), __('Sign up'), 'btn-sm btn-primary text-nowrap d-print-none' ), // No link and add a text hint, when the shift ended ShiftSignupStatus::SHIFT_ENDED => $inner_text . ' (' . __('ended') . ')', // No link and add a text hint, when the shift ended ShiftSignupStatus::NOT_ARRIVED => $inner_text . ' (' . __('please arrive for signup') . ')', ShiftSignupStatus::NOT_YET => $inner_text . ' (' . __('not yet') . ')', ShiftSignupStatus::ANGELTYPE => $angeltype->restricted || !$angeltype->shift_self_signup // User has to be confirmed on the angeltype first or can't sign up by themselves ? $inner_text . icon('mortarboard-fill') // Add link to join the angeltype first : $inner_text . '
    ' . button( url('/user-angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype->id]), sprintf(__('Become %s'), htmlspecialchars($angeltype->name)), 'btn-sm' ), // Shift collides or user is already signed up: No signup allowed ShiftSignupStatus::COLLIDES, ShiftSignupStatus::SIGNED_UP => $inner_text, // Shift is full ShiftSignupStatus::OCCUPIED => null, default => null, }; if (!is_null($entry)) { $entry_list[] = $entry; } $shifts_row = '
  • '; $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; $shifts_row .= join(', ', $entry_list); $shifts_row .= '
  • '; return [ $shift_signup_state, $shifts_row, ]; } /** * Return the corresponding bg class * * @return string */ private function classBg(): string { if (theme_type() === 'light') { return 'bg-white'; } return 'bg-dark'; } /** * Renders the shift header * * @param Shift $shift The shift * @param string $class The shift state class * @return string */ private function renderShiftHead(Shift $shift, $class, $needed_angeltypes_count) { $nightShiftsConfig = config('night_shifts'); $goodie = GoodieType::from(config('goodie_type')); $goodie_enabled = $goodie !== GoodieType::None; $header_buttons = ''; if (auth()->can('admin_shifts')) { $header_buttons = div('ms-auto d-print-none d-flex', [ button( url('/user-shifts', ['edit_shift' => $shift->id]), icon('pencil'), 'btn-' . $class . ' btn-sm border-light text-white', '', __('form.edit') ), form([ form_hidden('delete_shift', $shift->id), form_submit( 'delete', icon('trash'), 'btn-' . $class . ' btn-sm border-light text-white ms-1', false, 'danger', __('form.delete'), [ 'confirm_submit_title' => __('Do you want to delete the shift "%s" from %s to %s?', [ $shift->shiftType->name, $shift->start->format(__('general.datetime')), $shift->end->format(__('H:i')), ]), 'confirm_button_text' => icon('trash') . __('form.delete'), ] ), ], url('/user-shifts', ['delete_shift' => $shift->id])), ]); } $night_shift = ''; if (Shifts::isNightShift($shift->start, $shift->end) && $nightShiftsConfig['enabled'] && $goodie_enabled) { $night_shift = ' '; } $shift_heading = '' . $shift->start->format('H:i') . ' ‐ ' . $shift->end->format('H:i') . ' — ' . htmlspecialchars($shift->shiftType->name) . $night_shift . ''; if ($needed_angeltypes_count > 0) { $shift_heading = '' . $needed_angeltypes_count . ' ' . $shift_heading; } return div('card-header d-flex align-items-center', [ '' . $shift_heading . '', $header_buttons, ]); } }