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