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>
This commit is contained in:
123
app/Filament/Pages/EditorialCalendar.php
Normal file
123
app/Filament/Pages/EditorialCalendar.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
||||
74
app/Support/EditorialCalendar.php
Normal file
74
app/Support/EditorialCalendar.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Enums\PageStatus;
|
||||
use App\Filament\Resources\Calls\CallResource;
|
||||
use App\Models\Call;
|
||||
use App\Models\Page;
|
||||
use App\Models\Project;
|
||||
use App\Models\Service;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Everything with a date that changes what the site shows: what went or goes
|
||||
* live, what comes off, which calls open and close.
|
||||
*/
|
||||
final class EditorialCalendar
|
||||
{
|
||||
/**
|
||||
* @return Collection<int, array{date: string, type: string, event: string, title: string, url: string, at: CarbonInterface}>
|
||||
*/
|
||||
public static function between(CarbonImmutable $from, CarbonImmutable $to, User $user): Collection
|
||||
{
|
||||
$events = collect();
|
||||
|
||||
$add = function (string $type, Model $record, string $event, ?CarbonInterface $at) use ($events): void {
|
||||
if ($at === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$events->push([
|
||||
'date' => $at->toDateString(),
|
||||
'type' => $type,
|
||||
'event' => $event,
|
||||
'title' => ContentTypes::title($record),
|
||||
'url' => $record instanceof Call ? CallResource::getUrl('edit', ['record' => $record]) : ReviewTarget::url($record),
|
||||
'at' => $at,
|
||||
]);
|
||||
};
|
||||
|
||||
$inRange = fn ($query, string $column) => $query->whereBetween($column, [$from->startOfDay(), $to->endOfDay()]);
|
||||
|
||||
$published = [
|
||||
'pages' => [Page::class, fn ($q) => $q->where('status', PageStatus::Published)],
|
||||
'projects' => [Project::class, fn ($q) => $q],
|
||||
'services' => [Service::class, fn ($q) => $q->where('is_published', true)],
|
||||
];
|
||||
|
||||
foreach ($published as $type => [$model, $live]) {
|
||||
if ($user->cannot('viewAny', $model)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$live($inRange($model::query(), 'published_at'))->get()
|
||||
->each(fn (Model $r) => $add($type, $r, $r->published_at->isFuture() ? 'scheduled' : 'published', $r->published_at));
|
||||
|
||||
$live($inRange($model::query(), 'unpublish_at'))->get()
|
||||
->each(fn (Model $r) => $add($type, $r, 'unpublishes', $r->unpublish_at));
|
||||
}
|
||||
|
||||
if ($user->can('viewAny', Call::class)) {
|
||||
$inRange(Call::query(), 'opens_at')->get()->each(fn (Call $c) => $add('calls', $c, 'opens', $c->opens_at));
|
||||
$inRange(Call::query(), 'closes_at')->get()->each(fn (Call $c) => $add('calls', $c, 'closes', $c->closes_at));
|
||||
}
|
||||
|
||||
return $events->sortBy(fn (array $e) => $e['at']->getTimestamp())->values();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user