Files
landing-api/app/Filament/Pages/EditorialCalendar.php
Josep Piguillem 8d0f81f9c5 Add an editorial calendar to the panel
A month of publications, scheduled dates, content coming off the site, and
calls opening and closing, each linked to its form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 10:17:46 +02:00

124 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Pages;
use App\Support\EditorialCalendar as Calendar;
use BackedEnum;
use Carbon\CarbonImmutable;
use Filament\Pages\Page as FilamentPage;
use Filament\Support\Icons\Heroicon;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Url;
use UnitEnum;
/**
* A month at a glance: what goes live, what comes off, which calls open and
* close. The dates were always there, one form at a time; this is them side
* by side, which is where a clash or an empty fortnight shows.
*/
class EditorialCalendar extends FilamentPage
{
protected string $view = 'filament.pages.editorial-calendar';
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCalendarDays;
protected static ?int $navigationSort = 5;
/**
* `2026-10`. In the address, so a month can be linked to.
*/
#[Url]
public string $month = '';
public function mount(): void
{
if (! preg_match('/^\d{4}-\d{2}$/', $this->month)) {
$this->month = now()->format('Y-m');
}
}
public static function canAccess(): bool
{
return Auth::user()?->can('panel.access') ?? false;
}
public static function getNavigationGroup(): string|UnitEnum|null
{
return __('panel.groups.content');
}
public static function getNavigationLabel(): string
{
return __('panel.calendar.title');
}
public function getTitle(): string|Htmlable
{
return __('panel.calendar.title');
}
public function previous(): void
{
$this->month = $this->first()->subMonth()->format('Y-m');
}
public function next(): void
{
$this->month = $this->first()->addMonth()->format('Y-m');
}
public function today(): void
{
$this->month = now()->format('Y-m');
}
public function first(): CarbonImmutable
{
return CarbonImmutable::createFromFormat('Y-m-d', $this->month.'-01')->startOfDay();
}
public function label(): string
{
return ucfirst($this->first()->locale(app()->getLocale())->isoFormat('MMMM YYYY'));
}
/**
* Whole weeks, Monday first, covering the month.
*
* @return array<int, array<int, array{date: CarbonImmutable, inMonth: bool, isToday: bool, events: array<int, array<string, mixed>>}>>
*/
public function weeks(): array
{
$start = $this->first()->startOfWeek(CarbonImmutable::MONDAY);
$end = $this->first()->endOfMonth()->endOfWeek(CarbonImmutable::SUNDAY);
$events = Calendar::between($start, $end, Auth::user())->groupBy('date');
$weeks = [];
for ($day = $start; $day->lte($end); $day = $day->addDay()) {
$weeks[intdiv((int) $start->diffInDays($day), 7)][] = [
'date' => $day,
'inMonth' => $day->month === $this->first()->month,
'isToday' => $day->isToday(),
'events' => ($events[$day->toDateString()] ?? collect())->all(),
];
}
return $weeks;
}
/**
* @return array<int, string>
*/
public function weekdays(): array
{
$monday = CarbonImmutable::now()->locale(app()->getLocale())->startOfWeek(CarbonImmutable::MONDAY);
return array_map(fn (int $i) => $monday->addDays($i)->isoFormat('ddd'), range(0, 6));
}
}