diff --git a/app/Http/Controllers/VacationCalendarController.php b/app/Http/Controllers/VacationCalendarController.php index 88c8b156..f3dbafba 100644 --- a/app/Http/Controllers/VacationCalendarController.php +++ b/app/Http/Controllers/VacationCalendarController.php @@ -4,14 +4,17 @@ namespace Toby\Http\Controllers; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Cache; use Inertia\Response; use Toby\Domain\CalendarGenerator; use Toby\Enums\Month; use Toby\Helpers\YearPeriodRetriever; use Toby\Http\Resources\SimpleUserResource; use Toby\Models\User; +use Toby\Models\YearPeriod; class VacationCalendarController extends Controller { @@ -20,12 +23,25 @@ public function index( YearPeriodRetriever $yearPeriodRetriever, CalendarGenerator $calendarGenerator, ?string $month = null, - ): Response { + ?int $year = null, + ): Response|RedirectResponse { + if ($year !== null) { + return $this->changeYearPeriod($request, $month, $year); + } + $month = Month::fromNameOrCurrent((string)$month); $currentUser = $request->user(); $withTrashedUsers = $currentUser->canSeeInactiveUsers(); $yearPeriod = $yearPeriodRetriever->selected(); + $previousYearPeriod = YearPeriod::query() + ->where("year", "<", $yearPeriod->year) + ->orderBy("year", "desc") + ->first(); + $nextYearPeriod = YearPeriod::query() + ->where("year", ">", $yearPeriod->year) + ->orderBy("year") + ->first(); $carbonMonth = Carbon::create($yearPeriod->year, $month->toCarbonNumber()); $users = User::query() @@ -42,9 +58,21 @@ public function index( return inertia("Calendar", [ "calendar" => $calendar, "current" => Month::current(), - "selected" => $month->value, + "selectedMonth" => $month->value, "users" => SimpleUserResource::collection($users), "withBlockedUsers" => $withTrashedUsers, + "previousYearPeriod" => $previousYearPeriod, + "nextYearPeriod" => $nextYearPeriod, ]); } + + private function changeYearPeriod(Request $request, string $month, int $year): RedirectResponse + { + $yearPeriod = YearPeriod::query()->where("year", $year)->firstOrFail(); + $request->session()->put(YearPeriodRetriever::SESSION_KEY, $yearPeriod->id); + Cache::forget("selected_year_period"); + + return redirect()->route("calendar", ["month" => $month]) + ->with("info", __("Year period changed.")); + } } diff --git a/lang/pl.json b/lang/pl.json index 5e28ac3d..e17f4a6d 100644 --- a/lang/pl.json +++ b/lang/pl.json @@ -41,6 +41,7 @@ "Holiday updated.": "Dzień wolny zaktualizowany.", "Holiday deleted.": "Dzień wolny usunięty.", "Selected year period changed.": "Wybrany rok zmieniony.", + "Year period changed.": "Zmieniono rok.", "Vacation limits updated.": "Limity urlopów zaktualizowane.", "Request created.": "Wniosek utworzony.", "Request accepted.": "Wniosek zaakceptowany.", diff --git a/resources/js/Pages/Calendar.vue b/resources/js/Pages/Calendar.vue index 5a9076b9..b25d07ff 100644 --- a/resources/js/Pages/Calendar.vue +++ b/resources/js/Pages/Calendar.vue @@ -1,5 +1,5 @@