Files
laraundfelix/app/Http/Controllers/InquiryPreviewController.php
T
LarsandClaude Sonnet 5.5 c6a8167601 Baue die Verwaltung Kommunikation, die öffentliche Anfrageschnittstelle und das CMS-Widget Anfrage
Anliegen, Feldkatalog, Feldgruppen, Anfragevorlagen mit Fassungen, Anfrage-Einstiege, Posteingang mit
Status und Notizen, Einstellungen mit Empfängern. Das Widget speichert nur die Kennung eines veröffentlichten
Einstiegs; Formular, Bedingungen und Annahme kommen zur Laufzeit vom Server (Kompaktformular und
mehrstufiger Dialog aus derselben Beschreibung).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 17:41:55 +01:00

142 lines
5.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Domain\Communication\CommunicationAccess;
use App\Domain\Communication\FormEvaluator;
use App\Domain\Communication\FormFactory;
use App\Domain\Communication\FormPayload;
use App\Domain\Communication\InquiryEntry;
use App\Domain\Communication\InquiryTemplate;
use App\Domain\Communication\TemplateCompiler;
use App\Domain\Communication\TemplateDraft;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
/**
* Vorschau für die Verwaltung: das Formular, wie es ein Besucher sieht, aus dem gespeicherten Entwurf einer Vorlage oder
* eines Einstiegs. Dieselbe Darstellung und dieselbe Auswertung wie öffentlich; gesendet wird nichts.
*/
class InquiryPreviewController extends Controller
{
public function __construct(
private readonly TemplateCompiler $compiler,
private readonly FormFactory $factory,
private readonly FormEvaluator $evaluator,
) {}
public function template(Request $request, InquiryTemplate $template): View
{
$this->authorizeStaff();
$mode = $request->query('modus') === 'wizard' ? 'wizard' : 'compact';
$result = $this->compiler->compile(TemplateDraft::clean((array) $template->draft));
return view('communication.preview', [
'title' => 'Vorschau: Vorlage „'.$template->name.'“',
'note' => 'Entwurf der Vorlage mit allen angebotenen Anliegen. Es wird nichts gesendet.',
'errors' => $result['errors'],
'mode' => $mode,
'switchUrl' => route('communication.preview.template', ['template' => $template, 'modus' => $mode === 'wizard' ? 'compact' : 'wizard']),
'definitionUrl' => route('communication.preview.template.definition', ['template' => $template, 'modus' => $mode]),
'stateUrl' => route('communication.preview.template.state', ['template' => $template, 'modus' => $mode]),
]);
}
public function templateDefinition(Request $request, InquiryTemplate $template): JsonResponse
{
$this->authorizeStaff();
$form = $this->templateForm($request, $template);
return $form === null ? $this->unavailable() : $this->json([...FormPayload::definition($form, $this->evaluator), 'token' => 'vorschau', 'csrf' => csrf_token()]);
}
public function templateState(Request $request, InquiryTemplate $template): JsonResponse
{
$this->authorizeStaff();
$form = $this->templateForm($request, $template);
return $form === null ? $this->unavailable() : $this->json(FormPayload::state($form, (array) $request->input('values', []), $this->evaluator));
}
public function entry(InquiryEntry $entry): View
{
$this->authorizeStaff();
$problem = $this->entryForm($entry) === null ? 'Die Anfragevorlage dieses Einstiegs hat noch keine gültige Fassung.' : null;
return view('communication.preview', [
'title' => 'Vorschau: Einstieg „'.$entry->name.'“',
'note' => 'Gespeicherter Entwurf des Einstiegs mit der gültigen Fassung der Vorlage. Es wird nichts gesendet.',
'errors' => $problem === null ? [] : [$problem],
'mode' => null,
'switchUrl' => null,
'definitionUrl' => route('communication.preview.entry.definition', $entry),
'stateUrl' => route('communication.preview.entry.state', $entry),
]);
}
public function entryDefinition(InquiryEntry $entry): JsonResponse
{
$this->authorizeStaff();
$form = $this->entryForm($entry);
return $form === null ? $this->unavailable() : $this->json([...FormPayload::definition($form, $this->evaluator), 'token' => 'vorschau', 'csrf' => csrf_token()]);
}
public function entryState(Request $request, InquiryEntry $entry): JsonResponse
{
$this->authorizeStaff();
$form = $this->entryForm($entry);
return $form === null ? $this->unavailable() : $this->json(FormPayload::state($form, (array) $request->input('values', []), $this->evaluator));
}
/**
* @return array<string, mixed>|null
*/
private function templateForm(Request $request, InquiryTemplate $template): ?array
{
$result = $this->compiler->compile(TemplateDraft::clean((array) $template->draft));
if ($result['definition'] === null) {
return null;
}
return $this->factory->effective($result['definition'], ['mode' => $request->query('modus') === 'wizard' ? 'wizard' : 'compact']);
}
/**
* @return array<string, mixed>|null
*/
private function entryForm(InquiryEntry $entry): ?array
{
$version = $entry->template->publishedVersion;
return $version === null ? null : $this->factory->effective((array) $version->definition, (array) $entry->config);
}
private function authorizeStaff(): void
{
abort_unless(CommunicationAccess::configure(auth('staff')->user()), 403);
}
private function unavailable(): JsonResponse
{
return $this->json(['message' => 'Für diese Vorschau fehlt eine gültige Konfiguration.'], 404);
}
/**
* @param array<string, mixed> $data
*/
private function json(array $data, int $status = 200): JsonResponse
{
return response()->json($data, $status)->header('Cache-Control', 'no-store, private');
}
}