> */ 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 */ 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, tags: list} */ 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 */ 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 */ 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 $counts * @return list */ 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)); } }