Veröffentliche fachliche Säulen als Themenbereiche der Website.
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>
This commit is contained in:
1 parent
af85a94cd5
commit
8cd382c357
21 files changed
+379
-13
No files matched your search
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Domain\Cms\Page;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PillarService
|
||||
{
|
||||
public function create(string $name): Pillar
|
||||
@@ -12,10 +15,13 @@ public function create(string $name): Pillar
|
||||
throw new DomainRuleException('Diese fachliche Säule gibt es schon.');
|
||||
}
|
||||
|
||||
return Pillar::query()->create(['name' => $name]);
|
||||
return Pillar::query()->create([
|
||||
'name' => $name,
|
||||
'slug' => $this->uniqueSlug(Str::slug($name, '-', 'de')),
|
||||
]);
|
||||
}
|
||||
|
||||
public function rename(Pillar $pillar, string $name): void
|
||||
public function present(Pillar $pillar, string $name, string $slug, ?string $summary, bool $published, int $sortOrder): void
|
||||
{
|
||||
$name = $this->name($name);
|
||||
|
||||
@@ -23,14 +29,19 @@ public function rename(Pillar $pillar, string $name): void
|
||||
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()) {
|
||||
throw new DomainRuleException('Eine Säule mit zugeordneten Medien wird nicht gelöscht.');
|
||||
if ($pillar->elements()->exists() || $pillar->pages()->exists()) {
|
||||
throw new DomainRuleException('Eine Säule mit zugeordneten Medien oder Seiten wird nicht gelöscht.');
|
||||
}
|
||||
|
||||
$pillar->delete();
|
||||
@@ -40,6 +51,23 @@ public function delete(Pillar $pillar): void
|
||||
* @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)));
|
||||
|
||||
@@ -47,7 +75,42 @@ public function assign(ContentElement $element, array $pillarIds): void
|
||||
throw new DomainRuleException('Eine fachliche Säule fehlt.');
|
||||
}
|
||||
|
||||
$element->pillars()->sync($ids);
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user