getRecord(); } public function editorialProfile(?string $audience = null): EditorialProfile { return app(EditorialProfileReader::class)->read($this->editorialSubject(), $audience); } /** * @return list */ public function editorialGaps(): array { $profile = $this->editorialProfile(); $gaps = []; foreach ([ 'display_title' => 'Anzeigename', 'teaser' => 'Teaser', 'summary' => 'Kurzbeschreibung', 'description' => 'Ausführliche Beschreibung', ] as $field => $label) { if ($profile->value($field) === null) { $gaps[] = $label; } } return $gaps; } /** * @return list> */ public function presentationRows(): array { return app(PresentationMedia::class)->rows($this->editorialSubject()); } /** * @return array */ public function presentationRoleOptions(): array { return PresentationRole::selectOptions(); } /** * @return array{items: list>, more: bool} */ public function presentationChoices(): array { return app(EditorialCatalog::class)->presentationBrowse($this->presentationQuery); } public function assignPickedPresentation(): void { $this->guardEditorial(); $row = app(EditorialCatalog::class)->rowForTarget($this->presentationPick, 0, allowDraft: true); if ($row === null || ! isset($row['element_id'])) { $this->deskFeedback = 'Bitte ein Medium aus dem Bestand wählen.'; return; } $this->rememberAssignment((int) $row['element_id'], isset($row['target']) ? (string) $row['target'] : null); $this->presentationPick = ''; $this->presentationInspect = ''; } public function openRedaktion(): void { $this->guardEditorial(); $this->redaktionOpen = true; $this->redaktionTab = 'texte'; } public function closeRedaktion(): void { $this->redaktionOpen = false; $this->presentationInspect = ''; $this->replacingAssignment = null; } public function cancelPresentationPick(): void { $this->presentationPick = ''; $this->presentationInspect = ''; $this->replacingAssignment = null; $this->deskFeedback = 'Die vorhandene Zuordnung bleibt unverändert.'; } public function beginExchange(int $assignmentId): void { $this->guardEditorial(); foreach ($this->presentationRows() as $row) { if ((int) $row['id'] !== $assignmentId) { continue; } $this->presentationRole = (string) $row['role']; $this->replacingAssignment = $assignmentId; $this->presentationPick = ''; $this->presentationInspect = ''; $this->redaktionTab = 'medien'; $this->deskFeedback = 'Wähle das neue Medium und übernimm es. Abbrechen lässt die bisherige Zuordnung stehen.'; if (method_exists($this, 'openPresentationPicker')) { $this->openPresentationPicker(); } return; } $this->deskFeedback = 'Diese Zuordnung gibt es nicht mehr.'; } public function placePresentation(int $assignmentId, int $beforeId): void { $this->guardEditorial(); $rows = $this->presentationRows(); $moving = null; foreach ($rows as $row) { if ((int) $row['id'] === $assignmentId) { $moving = $row; } } if ($moving === null || ! $moving['ordered']) { $this->deskFeedback = 'Diese Rolle hat eine feste einzelne Zuordnung und wird nicht umsortiert.'; return; } $ids = []; foreach ($rows as $row) { if ($row['role'] === $moving['role']) { $ids[] = (int) $row['id']; } } $from = array_search($assignmentId, $ids, true); $to = array_search($beforeId, $ids, true); if ($from === false || $to === false || $from === $to) { return; } $direction = $to < $from ? -1 : 1; for ($step = 0; $step < abs($to - $from); $step++) { app(PresentationMedia::class)->move($this->editorialSubject(), $assignmentId, $direction, $this->deskActor()); } $this->deskFeedback = 'Die Reihenfolge gilt nur in dieser Rolle.'; } public function detachPresentation(int $assignmentId): void { $this->guardEditorial(); $this->deskFeedback = $this->reportPresentation( fn (): string => app(PresentationMedia::class)->detach($this->editorialSubject(), $assignmentId, $this->deskActor()), ); } public function movePresentation(int $assignmentId, int $direction): void { $this->guardEditorial(); $this->deskFeedback = $this->reportPresentation(function () use ($assignmentId, $direction): string { app(PresentationMedia::class)->move($this->editorialSubject(), $assignmentId, $direction, $this->deskActor()); return 'Die Reihenfolge ist gespeichert.'; }); } public function releaseAssigned(int $elementId): void { abort_unless(ContentElementResource::editor(), 403); $element = ContentElement::query()->find($elementId); if (! $element instanceof ContentElement) { $this->deskFeedback = 'Das Medium fehlt.'; return; } $this->deskFeedback = app(WebsiteRelease::class)->release($element, $this->deskActor()); } public function withdrawAssigned(int $elementId): void { abort_unless(ContentElementResource::editor(), 403); $element = ContentElement::query()->find($elementId); if (! $element instanceof ContentElement) { $this->deskFeedback = 'Das Medium fehlt.'; return; } $this->deskFeedback = app(WebsiteRelease::class)->withdraw($element, $this->deskActor()); } /** * @return list */ public function variantRows(): array { $subject = $this->editorialSubject(); return EditorialTextVariant::query() ->where('subject_type', $subject->getMorphClass()) ->where('subject_id', $subject->getKey()) ->orderBy('audience') ->orderBy('field') ->get() ->all(); } public function saveVariant(): void { $this->guardEditorial(); $this->deskFeedback = $this->reportPresentation( fn (): string => app(EditorialProfileReader::class)->putVariant( $this->editorialSubject(), $this->variantField, $this->variantAudience, $this->variantBody, $this->deskActor(), ), ); $this->variantBody = ''; } public function forgetVariant(int $variantId): void { $this->guardEditorial(); $variant = EditorialTextVariant::query()->find($variantId); if (! $variant instanceof EditorialTextVariant) { $this->deskFeedback = 'Diese Abweichung gibt es nicht mehr.'; return; } $this->reportPresentation(function () use ($variant): string { app(EditorialProfileReader::class)->forgetVariant($this->editorialSubject(), $variant, $this->deskActor()); return 'Die abweichende Formulierung ist entfernt.'; }); $this->deskFeedback = 'Die abweichende Formulierung ist entfernt. Es gilt wieder die zentrale Angabe.'; } public function variantFieldLabel(string $field): string { return match ($field) { 'display_title' => 'Anzeigename', 'teaser' => 'Teaser', 'summary' => 'Kurzbeschreibung', 'description' => 'Ausführliche Beschreibung', default => $field, }; } public function canReleasePresentation(): bool { return ContentElementResource::editor(); } public function canEditEditorial(): bool { return ContentElementResource::editor(); } protected function rememberAssignment(int $elementId, ?string $target): void { $sourceId = null; if ($this->bindToVersion && in_array($this->presentationRole, PresentationRole::BOUND_ROLES, true)) { $subject = $this->editorialSubject(); $sourceId = $subject instanceof ContentElement ? $subject->versions()->where('status', ContentVersionStatus::Bereit)->orderByDesc('number')->value('id') : $this->versionIdFromTarget($target); } $this->deskFeedback = $this->reportPresentation( fn (): string => app(PresentationMedia::class)->assign( $this->editorialSubject(), $this->presentationRole, $elementId, $sourceId !== null ? (int) $sourceId : null, $this->deskActor(), ), ); $this->finishExchange(); } protected function finishExchange(): void { $previous = $this->replacingAssignment; if ($previous === null || ! str_contains($this->deskFeedback, 'zugeordnet')) { return; } $this->replacingAssignment = null; $still = PresentationAssignment::query()->find($previous); if (! $still instanceof PresentationAssignment) { $this->deskFeedback = 'Die Zuordnung ist ausgetauscht. '.$this->deskFeedback; return; } if (str_contains($this->deskFeedback, 'bereits')) { return; } $removed = $this->reportPresentation( fn (): string => app(PresentationMedia::class)->detach($this->editorialSubject(), $previous, $this->deskActor()), ); $this->deskFeedback = str_contains($removed, 'entfernt') ? 'Die bisherige Zuordnung ist ersetzt. '.$this->deskFeedback : $removed; } protected function versionIdFromTarget(?string $target): ?int { if (is_string($target) && preg_match('/^element:(\d+)$/', $target, $match) === 1) { return (int) $match[1]; } return null; } protected function guardEditorial(): void { abort_unless($this->canEditEditorial(), 403); } protected function deskActor(): ?User { $user = auth()->user(); return $user instanceof User ? $user : null; } /** * @param callable(): string $action */ protected function reportPresentation(callable $action): string { try { return $action(); } catch (DomainRuleException $exception) { return $exception->getMessage(); } } }