Posts module: posts, drafts and scheduling, categories and tags

This commit is contained in:
Matthijs Iriks committed 2026-12-04 01:33:59 +01:00
1 parent a9a1625241
commit e093614169
18 files changed
+977 -30

No files matched your search

+191
View File
@@ -0,0 +1,191 @@
<?php
namespace IriksIT\Manager\Modules\Posts;
use Closure;
use Illuminate\Support\Facades\DB;
use IriksIT\Manager\Models\Post;
/**
* The categories and tags of the posts, for managing them in one place. A category is the
* `category` of a post and a tag an entry of its `tags`: a new one can simply be typed in a post.
* One that is made by hand, before any post uses it, is a row of `post_labels`; that table only
* remembers names to offer in the post form, a post never points to it.
*
* Renaming to a name that is already in use merges the two. Names are compared as written:
* "Laravel" and "laravel" are two, which is how to merge them.
*/
class PostLabels
{
public const CATEGORY = 'category';
public const TAG = 'tag';
/** @var list<Closure(): iterable<string>> */
private array $suggestions = [];
/** A site offers more categories in the post form, for instance the titles of its services. */
public function suggestCategories(Closure $source): void
{
$this->suggestions[] = $source;
}
/**
* What the post form offers as category: the suggestions of the site, the configured ones,
* and whatever is in use or was made by hand.
*
* @return list<string>
*/
public function categories(): array
{
$suggested = [];
foreach ($this->suggestions as $source) {
array_push($suggested, ...array_values([...$source()]));
}
return array_values(array_unique([...$suggested, ...array_values((array) config('manager.posts.categories', [])), ...$this->names(self::CATEGORY)]));
}
private function table(): string
{
return config('manager.table_prefix').'post_labels';
}
/**
* Every category and tag, the ones made by hand included, with the number of posts (drafts
* included) that use it.
*
* @return array{categories: list<array{name: string, posts: int}>, tags: list<array{name: string, posts: int}>}
*/
public function overview(): array
{
$categories = [];
$tags = [];
foreach (Post::query()->get(['id', 'category', 'tags']) as $post) {
if ($post->category !== null && $post->category !== '') {
$categories[$post->category] = ($categories[$post->category] ?? 0) + 1;
}
foreach (array_unique($this->tagsOf($post)) as $tag) {
$tags[$tag] = ($tags[$tag] ?? 0) + 1;
}
}
foreach (DB::table($this->table())->get(['type', 'name']) as $row) {
if (! is_string($row->name)) {
continue;
}
if ($row->type === self::CATEGORY) {
$categories[$row->name] ??= 0;
} else {
$tags[$row->name] ??= 0;
}
}
return ['categories' => $this->listed($categories), 'tags' => $this->listed($tags)];
}
/**
* The names of all categories or tags, to offer in the post form.
*
* @return list<string>
*/
public function names(string $type): array
{
return array_column($this->overview()[$type === self::CATEGORY ? 'categories' : 'tags'], 'name');
}
/** Makes a category or tag that no post uses yet. False when the name is already there. */
public function create(string $type, string $name): bool
{
if (in_array($name, $this->names($type), true)) {
return false;
}
DB::table($this->table())->insert(['type' => $type, 'name' => $name, 'created_at' => now(), 'updated_at' => now()]);
return true;
}
/** Gives every post with the label the new name. Returns how many posts changed. */
public function rename(string $type, string $from, string $to): int
{
if ($from === $to) {
return 0;
}
// The name that was made by hand moves along, unless the new name is there already.
$made = DB::table($this->table())->where('type', $type);
if ((clone $made)->where('name', $to)->exists()) {
(clone $made)->where('name', $from)->delete();
} else {
(clone $made)->where('name', $from)->update(['name' => $to, 'updated_at' => now()]);
}
return $type === self::CATEGORY
? Post::query()->where('category', $from)->update(['category' => $to])
: $this->changeTags($from, $to);
}
/** Takes the label off every post; the posts stay. Returns how many posts changed. */
public function remove(string $type, string $name): int
{
DB::table($this->table())->where('type', $type)->where('name', $name)->delete();
return $type === self::CATEGORY
? Post::query()->where('category', $name)->update(['category' => null])
: $this->changeTags($name, null);
}
/** Replaces a tag in every post that has it, or takes it out when $to is null. */
private function changeTags(string $from, ?string $to): int
{
return DB::transaction(function () use ($from, $to) {
$changed = 0;
foreach (Post::query()->whereNotNull('tags')->get() as $post) {
$tags = $this->tagsOf($post);
if (! in_array($from, $tags, true)) {
continue;
}
$new = [];
foreach ($tags as $tag) {
$tag = $tag === $from ? $to : $tag;
// A post that had both names keeps the tag once.
if ($tag !== null && ! in_array($tag, $new, true)) {
$new[] = $tag;
}
}
$post->update(['tags' => $new]);
$changed++;
}
return $changed;
});
}
/** @return list<string> */
private function tagsOf(Post $post): array
{
return array_values(array_filter(is_array($post->tags) ? $post->tags : [], fn (mixed $tag) => is_string($tag) && $tag !== ''));
}
/**
* @param array<array-key, int> $counts
* @return list<array{name: string, posts: int}>
*/
private function listed(array $counts): array
{
$names = array_map(fn (int|string $name) => (string) $name, array_keys($counts));
natcasesort($names);
return array_values(array_map(fn (string $name) => ['name' => $name, 'posts' => $counts[$name]], $names));
}
}