Dieselbe Liste trägt die öffentliche Seite. Medien bleiben privat, Seiten können mehreren Säulen zugeordnet sein. Co-authored-by: Cursor <cursoragent@cursor.com>
127 lines
3.4 KiB
PHP
127 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use App\Domain\Cms\Page;
|
|
use Illuminate\Support\Str;
|
|
|
|
class PillarService
|
|
{
|
|
public function create(string $name): Pillar
|
|
{
|
|
$name = $this->name($name);
|
|
|
|
if (Pillar::query()->where('name', $name)->exists()) {
|
|
throw new DomainRuleException('Diese fachliche Säule gibt es schon.');
|
|
}
|
|
|
|
return Pillar::query()->create([
|
|
'name' => $name,
|
|
'slug' => $this->uniqueSlug(Str::slug($name, '-', 'de')),
|
|
]);
|
|
}
|
|
|
|
public function present(Pillar $pillar, string $name, string $slug, ?string $summary, bool $published, int $sortOrder): void
|
|
{
|
|
$name = $this->name($name);
|
|
|
|
if (Pillar::query()->where('name', $name)->whereKeyNot($pillar->id)->exists()) {
|
|
throw new DomainRuleException('Diese fachliche Säule gibt es schon.');
|
|
}
|
|
|
|
$summary = trim((string) $summary);
|
|
$pillar->name = $name;
|
|
$pillar->slug = $this->slug($slug, $pillar->id);
|
|
$pillar->public_summary = $summary === '' ? null : $summary;
|
|
$pillar->sort_order = max(0, $sortOrder);
|
|
$pillar->published_at = $published ? ($pillar->published_at ?? now()) : null;
|
|
$pillar->save();
|
|
}
|
|
|
|
public function delete(Pillar $pillar): void
|
|
{
|
|
if ($pillar->elements()->exists() || $pillar->pages()->exists()) {
|
|
throw new DomainRuleException('Eine Säule mit zugeordneten Medien oder Seiten wird nicht gelöscht.');
|
|
}
|
|
|
|
$pillar->delete();
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $pillarIds
|
|
*/
|
|
public function assign(ContentElement $element, array $pillarIds): void
|
|
{
|
|
$element->pillars()->sync($this->ids($pillarIds));
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $pillarIds
|
|
*/
|
|
public function assignPages(Page $page, array $pillarIds): void
|
|
{
|
|
$page->pillars()->sync($this->ids($pillarIds));
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $pillarIds
|
|
* @return list<int>
|
|
*/
|
|
private function ids(array $pillarIds): array
|
|
{
|
|
$ids = array_values(array_unique(array_map(intval(...), $pillarIds)));
|
|
|
|
if (Pillar::query()->whereIn('id', $ids)->count() !== count($ids)) {
|
|
throw new DomainRuleException('Eine fachliche Säule fehlt.');
|
|
}
|
|
|
|
return $ids;
|
|
}
|
|
|
|
private function slug(string $slug, ?int $ignoreId = null): string
|
|
{
|
|
$slug = Str::slug($slug, '-', 'de');
|
|
|
|
if ($slug === '') {
|
|
throw new DomainRuleException('Die öffentliche Adresse der Säule fehlt.');
|
|
}
|
|
|
|
$existing = Pillar::query()->where('slug', $slug);
|
|
|
|
if ($ignoreId !== null) {
|
|
$existing->whereKeyNot($ignoreId);
|
|
}
|
|
|
|
if ($existing->exists()) {
|
|
throw new DomainRuleException('Diese öffentliche Adresse ist schon vergeben.');
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
|
|
private function uniqueSlug(string $base): string
|
|
{
|
|
$base = $base !== '' ? $base : 'saeule';
|
|
$slug = $base;
|
|
$suffix = 2;
|
|
|
|
while (Pillar::query()->where('slug', $slug)->exists()) {
|
|
$slug = $base.'-'.$suffix;
|
|
$suffix++;
|
|
}
|
|
|
|
return $slug;
|
|
}
|
|
|
|
private function name(string $name): string
|
|
{
|
|
$name = trim($name);
|
|
|
|
if ($name === '') {
|
|
throw new DomainRuleException('Der Name der fachlichen Säule fehlt.');
|
|
}
|
|
|
|
return $name;
|
|
}
|
|
}
|