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>
75 lines
2.7 KiB
PHP
75 lines
2.7 KiB
PHP
<?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();
|
|
}
|
|
}
|