Files
laraundfelix/app/Filament/Resources/LicenseArticles/Pages/ViewLicenseArticle.php
T
LarsandCursor f29ea58fd5
CI Feature / test (push) Successful in 54s
Deploy Test / test (push) Successful in 47s
Deploy Test / deploy (push) Successful in 24s
Ergänze die gemeinsame redaktionelle Basis an den Fachobjekten.
Texte, Materialart und Präsentationsmedien bleiben am Medium, Modul, Programm und Lizenzartikel und werden von Verwaltung und Karten aus derselben Quelle gelesen, ohne Fassung oder Lizenz zu verändern.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-19 07:54:36 +01:00

198 lines
7.7 KiB
PHP

<?php
namespace App\Filament\Resources\LicenseArticles\Pages;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\CompositionVersion;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\EditorialDimension;
use App\Domain\Catalog\EditorialDimensionValue;
use App\Domain\Catalog\IdentityDimension;
use App\Domain\Catalog\IdentityDimensionValue;
use App\Domain\Catalog\IdentityProfile;
use App\Domain\Catalog\LicenseArticle;
use App\Filament\Resources\LicenseArticles\LicenseArticleResource;
use App\Filament\Support\CatalogOptions;
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;
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;
/** @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 saveLicenseEditorial(): void
{
$this->guardEditorial();
/** @var LicenseArticle $article */
$article = $this->getRecord();
$releaseId = $article->current_release_id;
$releases = $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());
$ids = array_map(intval(...), $this->dimensionIds);
IdentityDimensionValue::query()
->where('subject_type', $article->getMorphClass())
->where('subject_id', $article->id)
->where('origin', 'direkt')
->whereNotIn('editorial_dimension_value_id', $ids === [] ? [0] : $ids)
->delete();
foreach (EditorialDimensionValue::query()->whereIn('id', $ids)->get() as $value) {
app(IdentityDimension::class)->assign($article, $value, 'direkt');
}
$article->refresh();
$this->fillLicenseEditorial();
$this->deskFeedback = $article->current_release_id === $releaseId && $article->releases()->count() === $releases
? 'Gespeichert. Stand, Preis und Rechte bleiben unverändert.'
: 'Gespeichert. Prüfe den Artikelstand, er hat sich geändert.';
}
/**
* @return list<array{label: string, values: list<array{id: int, label: string}>}>
*/
public function licenseDimensionGroups(): array
{
$groups = [];
foreach (EditorialDimension::query()->with('values')->orderBy('label')->get() 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 [
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();
}),
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();
}
}