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:
2026-10-01 10:17:46 +02:00
parent 773d3901c6
commit 8d0f81f9c5
8 changed files with 414 additions and 0 deletions

View File

@@ -1165,6 +1165,14 @@ abre en cuanto se crea. Antes la copia salía sin slug —un registro sin
dirección, que la salud del sitio marcaba— y conservaba el estado: duplicar una
página publicada daba otra «publicada» sin URL.
**Calendario editorial** (Contenido → Calendario editorial) — un mes de un
vistazo: lo que se publicó o se publicará (programado), lo que sale de la web,
y las convocatorias que abren y cierran, cada cosa enlazada a su formulario. Las
fechas ya estaban, de una en una en cada ficha; juntas es donde se ve que tres
cosas salen el mismo lunes y nada en quince días. El mes va en la URL
(`?month=2026-10`) para poder mandarlo, y en el móvil pasa a una lista con solo
los días que tienen algo. Cada persona ve los tipos que puede listar.
**Próximos vencimientos** — en el dashboard, las convocatorias que cierran y
el contenido que sale de la web solo (`unpublish_at`) en las próximas dos
semanas, a quien puede editarlos. Y `content:expiring`, cada día a las 8, avisa

View 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));
}
}

View 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();
}
}

View File

@@ -144,6 +144,27 @@ return [
'select_all' => 'Selecciona-ho tot',
],
'calendar' => [
'title' => 'Calendari editorial',
'legend' => 'Llegenda',
'previous' => 'Mes anterior',
'next' => 'Mes següent',
'today' => 'Avui',
'events' => [
'published' => 'Publicat',
'scheduled' => 'Programat',
'unpublishes' => 'Surt del web',
'opens' => 'Obre',
'closes' => 'Tanca',
],
'types' => [
'pages' => 'Pàgina',
'projects' => 'Projecte',
'services' => 'Servei',
'calls' => 'Convocatòria',
],
],
'expiring' => [
'heading' => 'Pròxims venciments',
'description' => 'Convocatòries que tanquen i contingut que surt del web en els pròxims :days dies.',

View File

@@ -144,6 +144,27 @@ return [
'select_all' => 'Select all',
],
'calendar' => [
'title' => 'Editorial calendar',
'legend' => 'Legend',
'previous' => 'Previous month',
'next' => 'Next month',
'today' => 'Today',
'events' => [
'published' => 'Published',
'scheduled' => 'Scheduled',
'unpublishes' => 'Comes off the site',
'opens' => 'Opens',
'closes' => 'Closes',
],
'types' => [
'pages' => 'Page',
'projects' => 'Project',
'services' => 'Service',
'calls' => 'Call',
],
],
'expiring' => [
'heading' => 'Coming up',
'description' => 'Calls closing and content coming off the site in the next :days days.',

View File

@@ -144,6 +144,27 @@ return [
'select_all' => 'Seleccionar todos',
],
'calendar' => [
'title' => 'Calendario editorial',
'legend' => 'Leyenda',
'previous' => 'Mes anterior',
'next' => 'Mes siguiente',
'today' => 'Hoy',
'events' => [
'published' => 'Publicado',
'scheduled' => 'Programado',
'unpublishes' => 'Sale de la web',
'opens' => 'Abre',
'closes' => 'Cierra',
],
'types' => [
'pages' => 'Página',
'projects' => 'Proyecto',
'services' => 'Servicio',
'calls' => 'Convocatoria',
],
],
'expiring' => [
'heading' => 'Próximos vencimientos',
'description' => 'Convocatorias que cierran y contenido que sale de la web en los próximos :days días.',

View File

@@ -0,0 +1,85 @@
<x-filament-panels::page>
<style>
.ec-toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .75rem; }
.ec-toolbar h2 { font-size: 1.125rem; font-weight: 600; }
.ec-legend { display: flex; flex-wrap: wrap; gap: .5rem; }
.ec-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border-top: 1px solid var(--ec-line); border-left: 1px solid var(--ec-line); }
.ec-grid { --ec-line: color-mix(in oklab, currentColor 12%, transparent); }
.ec-head { padding: .375rem .5rem; font-size: .75rem; font-weight: 600; text-transform: uppercase; opacity: .7; border-right: 1px solid var(--ec-line); border-bottom: 1px solid var(--ec-line); }
.ec-day { min-height: 7rem; padding: .375rem; border-right: 1px solid var(--ec-line); border-bottom: 1px solid var(--ec-line); display: flex; flex-direction: column; gap: .25rem; }
.ec-day.is-out { opacity: .45; }
.ec-day .ec-num { font-size: .75rem; font-weight: 600; }
.ec-day.is-today .ec-num { display: inline-grid; place-items: center; width: 1.5rem; height: 1.5rem; border-radius: 999px; background: var(--primary-600, #d97706); color: #fff; }
.ec-event { display: block; font-size: .75rem; line-height: 1.3; padding: .125rem .375rem; border-radius: .375rem; border-left: 3px solid var(--ec-tone); background: color-mix(in oklab, var(--ec-tone) 12%, transparent); overflow-wrap: anywhere; }
.ec-event:hover { background: color-mix(in oklab, var(--ec-tone) 22%, transparent); }
.ec-published { --ec-tone: #16a34a; }
.ec-scheduled { --ec-tone: #2563eb; }
.ec-unpublishes { --ec-tone: #9ca3af; }
.ec-opens { --ec-tone: #d97706; }
.ec-closes { --ec-tone: #dc2626; }
.ec-weekday-inline { display: none; }
/* On a phone a seven-column grid is unreadable: one day per row,
and only the days that have something. */
@media (max-width: 768px) {
.ec-grid { grid-template-columns: 1fr; }
.ec-head { display: none; }
.ec-day { min-height: 0; }
.ec-day.is-empty, .ec-day.is-out { display: none; }
.ec-weekday-inline { display: inline; }
}
</style>
<div class="ec-toolbar">
<h2>{{ $this->label() }}</h2>
<div class="ec-legend" aria-label="{{ __('panel.calendar.legend') }}">
@foreach (['published', 'scheduled', 'unpublishes', 'opens', 'closes'] as $event)
<span class="ec-event ec-{{ $event }}">{{ __('panel.calendar.events.'.$event) }}</span>
@endforeach
</div>
<div style="display: flex; gap: .5rem;">
<x-filament::button color="gray" icon="heroicon-m-chevron-left" wire:click="previous" :label-sr-only="true">
{{ __('panel.calendar.previous') }}
</x-filament::button>
<x-filament::button color="gray" wire:click="today">
{{ __('panel.calendar.today') }}
</x-filament::button>
<x-filament::button color="gray" icon="heroicon-m-chevron-right" wire:click="next" :label-sr-only="true">
{{ __('panel.calendar.next') }}
</x-filament::button>
</div>
</div>
<x-filament::section>
<div class="ec-grid" role="grid" aria-label="{{ $this->label() }}">
@foreach ($this->weekdays() as $weekday)
<div class="ec-head" role="columnheader">{{ $weekday }}</div>
@endforeach
@foreach ($this->weeks() as $week)
@foreach ($week as $day)
<div @class([
'ec-day',
'is-out' => ! $day['inMonth'],
'is-today' => $day['isToday'],
'is-empty' => $day['events'] === [],
]) role="gridcell">
<span class="ec-num">
<span class="ec-weekday-inline">{{ $day['date']->locale(app()->getLocale())->isoFormat('dddd') }}</span>
{{ $day['date']->day }}
</span>
@foreach ($day['events'] as $event)
<a href="{{ $event['url'] }}" class="ec-event ec-{{ $event['event'] }}">
<span class="sr-only">{{ __('panel.calendar.events.'.$event['event']) }}:</span>
{{ __('panel.calendar.types.'.$event['type']) }} · {{ $event['title'] }}
</a>
@endforeach
</div>
@endforeach
@endforeach
</div>
</x-filament::section>
</x-filament-panels::page>

View File

@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Panel;
use App\Enums\CallStatus;
use App\Filament\Pages\EditorialCalendar;
use App\Models\Call;
use App\Models\Page;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use PHPUnit\Framework\Attributes\Test;
use Tests\FakesPireon;
use Tests\TestCase;
class EditorialCalendarTest extends TestCase
{
use FakesPireon, RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed(RolePermissionSeeder::class);
$this->travelTo('2026-10-15 10:00:00');
}
#[Test]
public function the_month_shows_what_goes_live_comes_off_opens_and_closes(): void
{
$this->actingAs($this->userWithRoles('editor'));
Page::factory()->published()->create(['title' => ['es' => 'Ya publicada'], 'published_at' => '2026-10-02 09:00']);
Page::factory()->published()->create(['title' => ['es' => 'Programada'], 'published_at' => '2026-10-20 09:00']);
Page::factory()->draft()->create(['title' => ['es' => 'Borrador con fecha'], 'published_at' => '2026-10-21 09:00']);
Call::factory()->create(['title' => ['es' => 'Backend'], 'status' => CallStatus::Open, 'closes_at' => '2026-10-30']);
Page::factory()->published()->create(['title' => ['es' => 'Otro mes'], 'published_at' => '2026-12-01 09:00']);
Livewire::withQueryParams(['month' => '2026-10'])
->test(EditorialCalendar::class)
->assertSee('Ya publicada')
->assertSee('Programada')
->assertSee('Backend')
->assertDontSee('Borrador con fecha')
->assertDontSee('Otro mes')
->call('next')
->assertSet('month', '2026-11')
->call('today')
->assertSet('month', '2026-10');
}
#[Test]
public function the_page_opens(): void
{
$this->actingAs($this->userWithRoles('writer'));
$this->get(EditorialCalendar::getUrl())->assertOk();
}
}