281 lines
11 KiB
PHP
281 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Compositions\Pages;
|
|
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\CompositionDisplayRelease;
|
|
use App\Domain\Catalog\CompositionKind;
|
|
use App\Domain\Catalog\CompositionVersion;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentService;
|
|
use App\Domain\Catalog\ContentVersionStatus;
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Catalog\EditorialContentKind;
|
|
use App\Filament\Resources\Combinations\CombinationResource;
|
|
use App\Filament\Resources\Courses\CourseResource;
|
|
use App\Filament\Resources\Exercises\ExerciseResource;
|
|
use App\Filament\Support\CatalogOptions;
|
|
use App\Filament\Support\CatalogReading;
|
|
use App\Models\User;
|
|
use Filament\Actions\Action;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Forms\Components\Placeholder;
|
|
use Filament\Forms\Components\Repeater;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Resources\Pages\EditRecord;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Support\Exceptions\Halt;
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\HtmlString;
|
|
|
|
abstract class EditComposition extends EditRecord
|
|
{
|
|
protected ?string $savedTitle = null;
|
|
|
|
public function getSubheading(): string|Htmlable|null
|
|
{
|
|
return 'Bausteine hinzufügen, gegen eine neuere Medienfassung tauschen oder aus der Liste nehmen. Speichern legt dafür eine neue Fassung an. Die bisherige Fassung bleibt und wird nicht nachträglich verändert.';
|
|
}
|
|
|
|
public function form(Schema $schema): Schema
|
|
{
|
|
return $schema->columns(1)->components([
|
|
TextInput::make('designation')->label('Bezeichnung')->required(),
|
|
Select::make('kind')->label('Art')->options(CompositionKind::options())->required()
|
|
->helperText('Übung, Kurs und fachliche Kombination sind verschiedene Zusammenstellungen. Die Art lässt sich hier ändern.'),
|
|
Select::make('editorial_content_kind_id')
|
|
->label('Inhaltsart')
|
|
->options(fn (): array => EditorialContentKind::selectOptions())
|
|
->nullable()
|
|
->helperText('Optional für Bibliothek und Website. Ändert weder die technische Art noch eine Fassung.'),
|
|
Placeholder::make('history')->label('Aktuelle Fassung')->content(function (): HtmlString {
|
|
/** @var Composition $composition */
|
|
$composition = $this->getRecord();
|
|
$lines = app(CatalogReading::class)->compositionMembers($composition);
|
|
|
|
return new HtmlString(collect($lines)->map(fn (string $line): string => e($line))->implode('<br>'));
|
|
}),
|
|
Placeholder::make('pillars')->label('Säulen der Medien')->content(function (): HtmlString {
|
|
/** @var Composition $composition */
|
|
$composition = $this->getRecord();
|
|
$lines = app(CatalogReading::class)->memberPillars($composition);
|
|
|
|
return new HtmlString(collect($lines)->map(fn (string $line): string => e($line))->implode('<br>'));
|
|
}),
|
|
Repeater::make('members')
|
|
->label('Bausteine der neuen Fassung')
|
|
->schema([
|
|
TextInput::make('role')->label('Rolle')->placeholder('Film, Merkblatt, Übung')->required(),
|
|
Select::make('target')->label('Fassung')->required()->searchable()
|
|
->options(fn (): array => CatalogOptions::assignmentTargets($this->getRecord()->getKey())),
|
|
])
|
|
->minItems(1)
|
|
->reorderable()
|
|
->addActionLabel('Baustein hinzufügen')
|
|
->helperText('Eine Zeile entfernen nimmt den Baustein nur aus der neuen Fassung. Eine neuere Medienfassung gilt erst, wenn du sie hier auswählst.'),
|
|
TextInput::make('note')->label('Notiz zur neuen Fassung'),
|
|
]);
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('displayRelease')
|
|
->label('Zur Anzeige freigeben')
|
|
->requiresConfirmation()
|
|
->visible(function (): bool {
|
|
$version = $this->currentReadyVersion();
|
|
|
|
return $version !== null && ! app(CompositionDisplayRelease::class)->isReleased($version);
|
|
})
|
|
->action(function (): void {
|
|
$version = $this->currentReadyVersion();
|
|
$actor = Filament::auth()->user();
|
|
abort_unless($version !== null && $actor instanceof User, 403);
|
|
app(CompositionDisplayRelease::class)->release($version, $actor);
|
|
Notification::make()->title('Die aktuelle Fassung ist zur Anzeige freigegeben.')->success()->send();
|
|
}),
|
|
Action::make('displayRevoke')
|
|
->label('Anzeigefreigabe zurücknehmen')
|
|
->color('danger')
|
|
->requiresConfirmation()
|
|
->visible(function (): bool {
|
|
$version = $this->currentReadyVersion();
|
|
|
|
return $version !== null && app(CompositionDisplayRelease::class)->isReleased($version);
|
|
})
|
|
->action(function (): void {
|
|
$version = $this->currentReadyVersion();
|
|
$actor = Filament::auth()->user();
|
|
abort_unless($version !== null && $actor instanceof User, 403);
|
|
app(CompositionDisplayRelease::class)->revoke($version, $actor);
|
|
Notification::make()->title('Die Anzeigefreigabe dieser Fassung ist zurückgenommen.')->success()->send();
|
|
}),
|
|
];
|
|
}
|
|
|
|
protected function getSaveFormAction(): Action
|
|
{
|
|
return parent::getSaveFormAction()->label('Neue Fassung veröffentlichen');
|
|
}
|
|
|
|
protected function getSavedNotificationTitle(): ?string
|
|
{
|
|
return $this->savedTitle ?? 'Die neue Fassung ist veröffentlicht.';
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function mutateFormDataBeforeFill(array $data): array
|
|
{
|
|
/** @var Composition $composition */
|
|
$composition = $this->getRecord();
|
|
$version = $composition->versions()
|
|
->where('status', ContentVersionStatus::Bereit)
|
|
->orderByDesc('number')
|
|
->with('members')
|
|
->first();
|
|
$members = [];
|
|
|
|
foreach ($version?->members ?? [] as $member) {
|
|
$target = $member->content_element_version_id
|
|
? 'element:'.$member->content_element_version_id
|
|
: 'composition:'.$member->nested_composition_version_id;
|
|
$members[] = ['role' => $member->role, 'target' => $target];
|
|
}
|
|
|
|
$kind = $composition->kind;
|
|
|
|
return [
|
|
'designation' => $composition->designation,
|
|
'kind' => $kind instanceof CompositionKind ? $kind->value : $kind,
|
|
'editorial_content_kind_id' => $composition->editorial_content_kind_id,
|
|
'members' => $members,
|
|
'note' => null,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
protected function handleRecordUpdate(Model $record, array $data): Model
|
|
{
|
|
/** @var Composition $record */
|
|
try {
|
|
$kind = CompositionKind::from($data['kind']);
|
|
$record->designation = trim((string) $data['designation']);
|
|
$record->kind = $kind;
|
|
$record->editorial_content_kind_id = filled($data['editorial_content_kind_id'] ?? null)
|
|
? (int) $data['editorial_content_kind_id']
|
|
: null;
|
|
$record->save();
|
|
|
|
$members = [];
|
|
|
|
foreach ($data['members'] as $member) {
|
|
if (! is_string($member['target'] ?? null) || ! str_contains($member['target'], ':')) {
|
|
throw new DomainRuleException('Jeder Baustein braucht eine Fassung.');
|
|
}
|
|
|
|
[$type, $id] = explode(':', $member['target'], 2);
|
|
$target = $type === 'composition'
|
|
? CompositionVersion::query()->findOrFail($id)
|
|
: ContentElementVersion::query()->findOrFail($id);
|
|
$members[] = ['role' => $member['role'], 'target' => $target];
|
|
}
|
|
|
|
$note = is_string($data['note'] ?? null) ? trim($data['note']) : '';
|
|
|
|
if ($note !== '' || ! $this->sameMembers($record, $members)) {
|
|
$actor = Filament::auth()->user();
|
|
app(ContentService::class)->publishComposition(
|
|
$record,
|
|
$members,
|
|
$actor instanceof User ? $actor : null,
|
|
$note !== '' ? $note : null,
|
|
);
|
|
$this->savedTitle = 'Die neue Fassung ist veröffentlicht. Die bisherige Fassung bleibt erhalten.';
|
|
} else {
|
|
$this->savedTitle = 'Bezeichnung und Art sind gespeichert. Die Bausteine sind unverändert, deshalb gibt es keine neue Fassung.';
|
|
}
|
|
} catch (DomainRuleException $exception) {
|
|
Notification::make()->title($exception->getMessage())->danger()->send();
|
|
|
|
throw new Halt;
|
|
}
|
|
|
|
return $record;
|
|
}
|
|
|
|
protected function afterSave(): void
|
|
{
|
|
$this->getRecord()->unsetRelations();
|
|
$this->getRecord()->refresh();
|
|
$this->fillForm();
|
|
}
|
|
|
|
protected function getRedirectUrl(): ?string
|
|
{
|
|
/** @var Composition $record */
|
|
$record = $this->getRecord();
|
|
$kind = $record->kind instanceof CompositionKind ? $record->kind : CompositionKind::from((string) $record->kind);
|
|
$resource = match ($kind) {
|
|
CompositionKind::Kurs => CourseResource::class,
|
|
CompositionKind::Kombination => CombinationResource::class,
|
|
CompositionKind::Uebung => ExerciseResource::class,
|
|
};
|
|
|
|
if ($resource !== static::getResource()) {
|
|
return $resource::getUrl('edit', ['record' => $record]);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param list<array{role: string, target: ContentElementVersion|CompositionVersion}> $members
|
|
*/
|
|
private function currentReadyVersion(): ?CompositionVersion
|
|
{
|
|
/** @var Composition $composition */
|
|
$composition = $this->getRecord();
|
|
|
|
return $composition->versions()
|
|
->where('status', ContentVersionStatus::Bereit)
|
|
->orderByDesc('number')
|
|
->first();
|
|
}
|
|
|
|
private function sameMembers(Composition $composition, array $members): bool
|
|
{
|
|
$version = $composition->versions()
|
|
->where('status', ContentVersionStatus::Bereit)
|
|
->orderByDesc('number')
|
|
->with('members')
|
|
->first();
|
|
|
|
if ($version === null) {
|
|
return false;
|
|
}
|
|
|
|
$current = $version->members->map(function ($member): string {
|
|
$target = $member->content_element_version_id
|
|
? 'element:'.$member->content_element_version_id
|
|
: 'composition:'.$member->nested_composition_version_id;
|
|
|
|
return $member->role.'|'.$target;
|
|
})->all();
|
|
$next = array_values(array_map(
|
|
fn (array $member): string => $member['role'].'|'.($member['target'] instanceof ContentElementVersion ? 'element:' : 'composition:').$member['target']->id,
|
|
$members,
|
|
));
|
|
|
|
return $current === $next;
|
|
}
|
|
}
|