Files
laraundfelix/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php
T
LarsandClaude Opus 5.5 e1fd59491c
CI Feature / test (push) Successful in 56s
Baue den Redaktionsmanager auf gemeinsame Bausteine um.
Medium, Modul, Programm und Lizenzartikel nutzen denselben Objektkopf,
dieselben Register (Texte, Präsentationsmedien, Einordnung) und denselben
visuellen Picker. Präsentationsmedien, Abweichungen und Texte bilden einen
Arbeitsstand, den nur „Redaktion speichern“ schreibt; Austausch behält die
Position, Sortierung bleibt in der Rolle. Der Wechsel zur Redaktion und zurück
erhält offene Eingaben des Ausgangseditors.

Entfernt versteckte Mitspeicherungen: Der Didaktikdialog schreibt keine
Redaktionstexte mehr, die Website-Freigabe keine offenen Quellenzeilen,
Entwurf verwerfen setzt keine offenen Redaktionseingaben zurück. Tests, die
alte Orte oder CSS-Zeichenketten prüften, sind auf die neue Bedienung
umgestellt; neue Tests decken Arbeitsstand, Austausch, Abbrechen und
Rückweg ab. Datenmodell, Fassungen und Rechte bleiben unverändert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 22:46:43 +01:00

377 lines
14 KiB
PHP

<?php
namespace App\Filament\Resources\LicenseArticles\Pages;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\CompositionVersion;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\EditorialContentKind;
use App\Domain\Catalog\EditorialDimension;
use App\Domain\Catalog\EditorialProfileReader;
use App\Domain\Catalog\IdentityDimension;
use App\Domain\Catalog\IdentityDimensionValue;
use App\Domain\Catalog\IdentityProfile;
use App\Domain\Catalog\LicenseArticle;
use App\Filament\Resources\ContentElements\ContentElementResource;
use App\Filament\Resources\LicenseArticles\LicenseArticleResource;
use App\Filament\Support\CatalogOptions;
use App\Filament\Support\EditorialCatalog;
use App\Filament\Support\ManagesEditorialSurface;
use App\Filament\Support\ReportsDomainRules;
use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
class ViewLicenseArticle extends ViewRecord
{
use ManagesEditorialSurface;
use ReportsDomainRules;
protected static string $resource = LicenseArticleResource::class;
public string $workingTitle = '';
public string $displayTitle = '';
public string $teaser = '';
public string $summary = '';
public string $descriptionText = '';
public string $searchTerms = '';
public int|string|null $kindId = null;
public ?int $releaseBeforeSave = null;
public int $releaseCountBeforeSave = 0;
/** @var list<int|string> */
public array $dimensionIds = [];
public function mount(int|string $record): void
{
parent::mount($record);
$this->fillLicenseEditorial();
}
public function canEditEditorial(): bool
{
return LicenseArticleResource::keeper();
}
public function canViewEditorial(): bool
{
return LicenseArticleResource::keeper();
}
public function getTitle(): string|Htmlable
{
return $this->redaktionTitel();
}
public function saveLicenseEditorial(): void
{
$this->saveRedaktion();
}
protected function saveRedaktionTexte(): bool
{
$this->guardEditorial();
/** @var LicenseArticle $article */
$article = $this->getRecord();
$this->releaseBeforeSave = $article->current_release_id;
$this->releaseCountBeforeSave = $article->releases()->count();
app(IdentityProfile::class)->fill($article, [
'working_title' => $this->workingTitle,
'display_title' => $this->displayTitle,
'teaser' => $this->teaser,
'summary' => $this->summary,
'description' => $this->descriptionText,
'search_terms' => $this->searchTerms,
'editorial_content_kind_id' => filled($this->kindId) ? (int) $this->kindId : null,
], auth()->user());
app(IdentityDimension::class)->replaceOffered($article, array_map(intval(...), $this->dimensionIds));
$article->refresh();
$this->fillLicenseEditorial();
return true;
}
protected function redaktionSavedMessage(): string
{
/** @var LicenseArticle $article */
$article = $this->getRecord();
return $article->current_release_id === $this->releaseBeforeSave && $article->releases()->count() === $this->releaseCountBeforeSave
? 'Gespeichert. Stand, Preis und Rechte bleiben unverändert.'
: 'Gespeichert. Prüfe den Artikelstand, er hat sich geändert.';
}
protected function resetRedaktionTexte(): void
{
$this->fillLicenseEditorial();
}
public function einordnungUnsaved(): bool
{
/** @var LicenseArticle $article */
$article = $this->getRecord()->fresh() ?? $this->getRecord();
$kind = filled($this->kindId) ? (int) $this->kindId : null;
$storedKind = $article->editorial_content_kind_id !== null ? (int) $article->editorial_content_kind_id : null;
if ($kind !== $storedKind) {
return true;
}
$stored = IdentityDimensionValue::query()
->where('subject_type', $article->getMorphClass())
->where('subject_id', $article->getKey())
->where('origin', 'direkt')
->pluck('editorial_dimension_value_id')
->map(fn ($id): int => (int) $id)
->all();
$current = array_map(intval(...), $this->dimensionIds);
sort($stored);
sort($current);
return $stored !== $current;
}
/**
* @return array{art: string, fuer: string, liste: string, zurueck: string, typ: ?string, umfang: string}
*/
public function redaktionKopf(): array
{
return [
'art' => 'Lizenzartikel',
'fuer' => 'den Lizenzartikel',
'liste' => 'Lizenzartikel',
'zurueck' => 'Zurück zum Lizenzartikel',
'typ' => null,
'umfang' => '„Redaktion speichern“ sichert die eigenen Texte, Präsentationsmedien und Einordnung dieses Lizenzartikels. Leere Angaben übernehmen weiter die fachliche Quelle. Stand, Preis und Rechte bleiben unverändert.',
];
}
public function redaktionTitel(): string
{
foreach ([$this->displayTitle, $this->workingTitle] as $title) {
if (trim($title) !== '') {
return trim($title);
}
}
$profile = $this->editorialProfile('artikel');
return (string) ($profile->value('display_title') ?? $this->getRecord()->code);
}
/**
* @return list<array<string, mixed>>
*/
public function redaktionFelder(): array
{
/** @var LicenseArticle $article */
$article = $this->getRecord();
$source = app(EditorialProfileReader::class)->sourceOf($article);
$sourceTitle = $source !== null ? $this->sourceTitle($source) : null;
$stored = fn (string $column): string => (string) ($article->getAttribute($column) ?? '');
$quelle = fn (string $column): ?array => $sourceTitle === null ? null : [
'titel' => $sourceTitle,
'wert' => (string) ($source->getAttribute($column) ?? ''),
];
return [
['id' => 'arbeitstitel', 'label' => 'Interner Arbeitstitel', 'model' => 'workingTitle', 'kind' => 'text', 'stored' => $stored('working_title'), 'hilfe' => 'Nur in der Verwaltung sichtbar.'],
['id' => 'anzeigename', 'label' => 'Name', 'model' => 'displayTitle', 'kind' => 'text', 'stored' => $stored('display_title'), 'quelle' => $quelle('display_title')],
['id' => 'teaser', 'label' => 'Teaser', 'model' => 'teaser', 'kind' => 'textarea', 'rows' => 2, 'stored' => $stored('teaser'), 'quelle' => $quelle('teaser')],
['id' => 'kurz', 'label' => 'Kurzbeschreibung', 'model' => 'summary', 'kind' => 'textarea', 'rows' => 4, 'stored' => $stored('summary'), 'quelle' => $quelle('summary')],
['id' => 'beschreibung', 'label' => 'Beschreibung', 'model' => 'descriptionText', 'kind' => 'textarea', 'rows' => 6, 'stored' => $stored('description'), 'quelle' => $quelle('description')],
['id' => 'suche', 'label' => 'Suchbegriffe', 'model' => 'searchTerms', 'kind' => 'text', 'gruppe' => 'weitere', 'stored' => $stored('search_terms'), 'quelle' => $quelle('search_terms')],
];
}
/**
* @return array{titel: string, art: string, fuer: string, url: ?string}|null
*/
public function redaktionQuelle(): ?array
{
/** @var LicenseArticle $article */
$article = $this->getRecord();
$source = app(EditorialProfileReader::class)->sourceOf($article);
if ($source === null) {
return null;
}
$url = match (true) {
$source instanceof Composition => EditorialCatalog::editUrl($source),
$source instanceof ContentElement => ContentElementResource::getUrl('view', ['record' => $source]),
default => null,
};
return [
'titel' => $this->sourceTitle($source),
'art' => $source instanceof ContentElement ? 'Medium' : ($source instanceof Composition && $source->kind === CompositionKind::Kurs ? 'Programm' : 'Modul'),
'fuer' => 'diesen Lizenzartikel',
'url' => $url,
];
}
public function redaktionOhneQuelle(): string
{
return 'Keine eindeutige fachliche Quelle. Es wird nichts aus einem Modul, Programm oder Medium übernommen.';
}
/**
* @return array{model: string, options: array<int|string, string>, hilfe: string}
*/
public function einordnungArt(): array
{
return [
'model' => 'kindId',
'options' => EditorialContentKind::selectOptions(),
'hilfe' => 'Die Einordnung des Artikels ändert weder die Quelle noch den Preis.',
];
}
/**
* @return list<array{legend: string, model: string, values: list<array{id: int, label: string}>}>
*/
public function einordnungGruppen(): array
{
return array_map(fn (array $group): array => [
'legend' => $group['label'],
'model' => 'dimensionIds',
'values' => $group['values'],
], $this->licenseDimensionGroups());
}
private function sourceTitle(Model $source): string
{
$title = $source->getAttribute('display_title') ?: ($source->getAttribute('working_title') ?? $source->getAttribute('designation'));
return (string) $title;
}
/**
* @return list<array{label: string, values: list<array{id: int, label: string}>}>
*/
public function licenseDimensionGroups(): array
{
$groups = [];
foreach (EditorialDimension::forSubject($this->getRecord()) as $dimension) {
$values = [];
foreach ($dimension->values as $value) {
$values[] = ['id' => $value->id, 'label' => $value->label];
}
if ($values !== []) {
$groups[] = ['label' => $dimension->label, 'values' => $values];
}
}
return $groups;
}
private function fillLicenseEditorial(): void
{
/** @var LicenseArticle $article */
$article = $this->getRecord();
$this->workingTitle = (string) ($article->working_title ?? '');
$this->displayTitle = (string) ($article->display_title ?? '');
$this->teaser = (string) ($article->teaser ?? '');
$this->summary = (string) ($article->summary ?? '');
$this->descriptionText = (string) ($article->description ?? '');
$this->searchTerms = (string) ($article->search_terms ?? '');
$this->kindId = $article->editorial_content_kind_id;
$this->dimensionIds = IdentityDimensionValue::query()
->where('subject_type', $article->getMorphClass())
->where('subject_id', $article->id)
->where('origin', 'direkt')
->pluck('editorial_dimension_value_id')
->all();
}
protected function getHeaderActions(): array
{
return [];
}
public function releaseAction(): Action
{
return Action::make('release')
->label('Stand veröffentlichen')
->schema([
Select::make('root')->label('Wurzel')->options(fn (): array => CatalogOptions::readyTargets())->required(),
Textarea::make('classification')->label('Einordnung')->required()
->helperText('Warum dies derselbe Lizenzartikel bleibt und kein neues Produkt ist.'),
TextInput::make('change_note')->label('Änderungshinweis'),
])
->action(function (array $data): void {
$this->report(function () use ($data): string {
/** @var LicenseArticle $article */
$article = $this->getRecord();
[$type, $id] = explode(':', $data['root'], 2);
$root = $type === 'composition'
? CompositionVersion::query()->findOrFail($id)
: ContentElementVersion::query()->findOrFail($id);
$release = app(ArticleService::class)->publishRelease(
$article,
$root,
$data['classification'],
$data['change_note'] ?? null,
);
$report = $release->impact_report ?? [];
$offers = $report['offers'] ?? [];
return 'Stand '.$release->number.' ist veröffentlicht. Inhalte: '.($report['contents'] ?? 0)
.', Inhaber: '.($report['holders'] ?? 0)
.', Angebote: '.(implode(', ', $offers) !== '' ? implode(', ', $offers) : 'keins').'.';
}, 'Der Artikel-Stand ist veröffentlicht.');
$this->refreshRecord();
});
}
public function withdrawAction(): Action
{
return Action::make('withdraw')
->label('Stand zurückziehen')
->schema([
Textarea::make('reason')->label('Begründung')->required(),
])
->action(function (array $data): void {
$this->report(function () use ($data): string {
$result = app(ArticleService::class)->withdraw($this->getRecord(), $data['reason']);
$current = $result['fallback_number'] === null
? 'Es gibt keinen früheren Stand.'
: 'Aktuell ist wieder Stand '.$result['fallback_number'].'.';
$holders = $result['holders'] === [] ? 'niemand' : implode(', ', $result['holders']);
return 'Der Stand ist zurückgezogen. '.$current.' Betroffen: '.$holders.'.';
}, 'Der Stand ist zurückgezogen.');
$this->refreshRecord();
});
}
private function refreshRecord(): void
{
$this->getRecord()->unsetRelations();
$this->getRecord()->refresh();
}
}