forked from eatvio/eatvio-infra
73 lines
2.1 KiB
PHP
Executable File
73 lines
2.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Diary;
|
|
use Carbon\Carbon;
|
|
use Carbon\CarbonPeriod;
|
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|
|
|
class CalendarController extends Controller
|
|
{
|
|
public int $weekStartsAt;
|
|
|
|
public int $weekEndsAt;
|
|
|
|
public Carbon $startsAt;
|
|
|
|
public Carbon $endsAt;
|
|
|
|
public Carbon $gridStartsAt;
|
|
|
|
public Carbon $gridEndsAt;
|
|
|
|
public function __construct(?int $initialYear = null, ?int $initialMonth = null, ?int $weekStartsAt = null)
|
|
{
|
|
$this->weekStartsAt = $weekStartsAt ?? Carbon::MONDAY;
|
|
$this->weekEndsAt = $this->weekStartsAt === Carbon::MONDAY
|
|
? Carbon::SUNDAY
|
|
: collect([0, 1, 2, 3, 4, 5, 6])->get($this->weekStartsAt + 6 - 7);
|
|
|
|
$initialYear = $initialYear ?? Carbon::today()->year;
|
|
$initialMonth = $initialMonth ?? Carbon::today()->month;
|
|
|
|
$this->startsAt = Carbon::createFromDate($initialYear, $initialMonth, 1)->startOfDay();
|
|
$this->endsAt = $this->startsAt->clone()->endOfMonth()->startOfDay();
|
|
$this->calculateGridStartsEnds();
|
|
}
|
|
|
|
public function calculateGridStartsEnds(): void
|
|
{
|
|
$this->gridStartsAt = $this->startsAt->clone()->startOfWeek($this->weekStartsAt);
|
|
$this->gridEndsAt = $this->endsAt->clone()->endOfWeek($this->weekEndsAt);
|
|
}
|
|
|
|
public function index(): View|ViewFactory
|
|
{
|
|
$curUser = Auth::user();
|
|
$fullGridPeriod = CarbonPeriod::create(
|
|
$this->gridStartsAt,
|
|
'1 day',
|
|
$this->gridEndsAt
|
|
);
|
|
|
|
$filledDays = Diary::where('user_id', $curUser->id)
|
|
->whereMonth('date', (string) now()->month)
|
|
->get()
|
|
->keyBy('date');
|
|
|
|
return view(
|
|
'calendar.index',
|
|
[
|
|
'fullGridPeriod' => $fullGridPeriod,
|
|
'currentLocale' => LaravelLocalization::getCurrentLocale(),
|
|
'filledDays' => $filledDays,
|
|
'curUser' => $curUser,
|
|
]
|
|
);
|
|
}
|
|
}
|