'', 'showForm' => false, 'editingId' => null, 'editLocale' => 'de', 'slug' => '', 'articleTitle' => '', 'subtitle' => '', 'image' => '', 'category' => '', 'date_label' => '', 'read_time' => '', 'authorName' => '', 'authorBio' => '', 'authorAvatar' => '', 'intro' => '', 'sections' => [], 'is_published' => true, 'order' => 0, ]); on(['media-selected' => function ($mediaId, $url, $field) { $media = $mediaId ? \FluxCms\Core\Models\CmsMedia::find($mediaId) : null; if ($field === 'article_image' && $media) { $this->image = $media->filename; } if ($field === 'author_avatar' && $media) { $this->authorAvatar = $media->filename; } }]); $articles = computed( fn () => CmsArticle::query() ->when($this->search, fn ($q, $s) => $q->where('slug', 'like', "%{$s}%") ->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(title, '$.de')) LIKE ?", ["%{$s}%"])) ->ordered() ->get(), ); $openCreate = function () { $this->editingId = null; $this->reset(['slug', 'articleTitle', 'subtitle', 'image', 'category', 'date_label', 'read_time', 'authorName', 'authorBio', 'authorAvatar', 'intro', 'sections']); $this->is_published = true; $this->order = 0; $this->sections = [['title' => '', 'content' => '']]; $this->showForm = true; }; $openEdit = function (int $id) { $article = CmsArticle::find($id); if (! $article) { return; } $this->editingId = $id; $this->slug = $article->slug; $this->image = $article->image ?? ''; $this->category = $article->category ?? ''; $this->date_label = $article->date_label ?? ''; $this->read_time = $article->read_time ?? ''; $this->is_published = $article->is_published; $this->order = $article->order ?? 0; $author = $article->author ?? []; $this->authorName = $author['name'] ?? ''; $this->authorBio = $author['bio'] ?? ''; $this->authorAvatar = $author['avatar'] ?? ''; $this->loadLocaleFields($article, $this->editLocale); $this->showForm = true; }; $loadLocaleFields = function (CmsArticle $article, string $locale) { $this->articleTitle = $article->getTranslation('title', $locale) ?? ''; $this->subtitle = $article->getTranslation('subtitle', $locale) ?? ''; $content = $article->getTranslation('content', $locale); $this->intro = $content['intro'] ?? ''; $this->sections = $content['sections'] ?? [['title' => '', 'content' => '']]; }; $switchLocale = function (string $locale) { $this->editLocale = $locale; if ($this->editingId) { $article = CmsArticle::find($this->editingId); if ($article) { $this->loadLocaleFields($article, $locale); } } }; $addSection = function () { $this->sections[] = ['title' => '', 'content' => '']; }; $removeSection = function (int $index) { unset($this->sections[$index]); $this->sections = array_values($this->sections); }; $save = function () { $validated = validator([ 'slug' => $this->slug, 'articleTitle' => $this->articleTitle, 'subtitle' => $this->subtitle, 'image' => $this->image, 'category' => $this->category, 'date_label' => $this->date_label, 'read_time' => $this->read_time, 'authorName' => $this->authorName, 'intro' => $this->intro, 'is_published' => $this->is_published, 'order' => $this->order, ], [ 'slug' => 'required|string|max:255', 'articleTitle' => 'required|string|max:500', 'subtitle' => 'nullable|string|max:1000', 'image' => 'nullable|string|max:500', 'category' => 'nullable|string|max:255', 'date_label' => 'nullable|string|max:100', 'read_time' => 'nullable|string|max:50', 'authorName' => 'nullable|string|max:255', 'intro' => 'nullable|string', 'is_published' => 'boolean', 'order' => 'integer|min:0', ])->validate(); $article = $this->editingId ? CmsArticle::findOrFail($this->editingId) : CmsArticle::query()->make(); $article->slug = $validated['slug']; $article->setTranslation('title', $this->editLocale, $validated['articleTitle']); $article->setTranslation('subtitle', $this->editLocale, $validated['subtitle'] ?? ''); $contentData = [ 'intro' => $validated['intro'] ?? '', 'sections' => collect($this->sections) ->filter(fn ($s) => ! empty($s['title']) || ! empty($s['content'])) ->values() ->toArray(), ]; $article->setTranslation('content', $this->editLocale, $contentData); $article->image = $validated['image'] ?? null; $article->category = $validated['category'] ?? null; $article->date_label = $validated['date_label'] ?? null; $article->read_time = $validated['read_time'] ?? null; $article->author = [ 'name' => $this->authorName, 'bio' => $this->authorBio, 'avatar' => $this->authorAvatar, ]; $article->is_published = $validated['is_published']; $article->order = $validated['order']; $article->save(); $this->showForm = false; $this->editingId = null; Flux::toast(variant: 'success', heading: 'Gespeichert', text: 'Artikel wurde erfolgreich gespeichert.'); }; $togglePublished = function (int $id) { $article = CmsArticle::findOrFail($id); $article->update(['is_published' => ! $article->is_published]); Flux::toast(heading: 'Status geändert', text: $article->is_published ? 'Veröffentlicht' : 'Entwurf'); }; $deleteArticle = function (int $id) { $article = CmsArticle::findOrFail($id); $article->delete(); Flux::toast(variant: 'success', heading: 'Gelöscht', text: 'Artikel wurde entfernt.'); }; $cancelForm = function () { $this->showForm = false; $this->editingId = null; }; ?>
{{ $image ?: 'Kein Bild' }}
{{ $article->author['name'] ?? '–' }} · {{ $article->date_label ?? '–' }} · {{ $article->read_time ?? '–' }}