Das Widget Anfrage wählt statt eines Anfrage-Einstiegs mehrere Anfragegründe, jeder zeigt auf ein veröffentlichtes Formular
(optional mit vorgewähltem Anliegen). Der Besucher erhält genau das Formular seines Grundes: /anfrage/formular/{vorlage}
mit Grund in der Adresse, dasselbe Token, dieselbe Auswertung und Annahme. Ein Dialog (dialog-Element) und die Grundauswahl
als Karten oder Auswahlfeld kommen aus resources/js/inquiry-widget.js. Seiten mit einem Einstieg bleiben darstellbar.
Bereiche lassen sich wie Gruppen anzeigen oder ausblenden.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
134 lines
5.1 KiB
PHP
134 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Domain\Communication\FormEvaluator;
|
|
use App\Domain\Communication\FormPayload;
|
|
use App\Domain\Communication\FormReason;
|
|
use App\Domain\Communication\FormToken;
|
|
use App\Domain\Communication\InquiryEntry;
|
|
use App\Domain\Communication\InquiryService;
|
|
use App\Domain\Communication\InquiryTemplate;
|
|
use App\Domain\Communication\SubmissionException;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* Öffentliche Schnittstelle der Anfrage-Einstiege. Die Seite des CMS enthält nur die Kennung des Einstiegs; Formular,
|
|
* Bedingungen und Annahme kommen von hier, damit eine zentrale Veröffentlichung sofort auf allen Seiten wirkt und keine
|
|
* Formularstände in gespeicherten Seiten veralten.
|
|
*/
|
|
class InquiryFormController extends Controller
|
|
{
|
|
public function __construct(private readonly InquiryService $service, private readonly FormEvaluator $evaluator) {}
|
|
|
|
public function definition(InquiryEntry $entry): JsonResponse
|
|
{
|
|
$delivered = $this->service->deliver($entry);
|
|
|
|
if ($delivered === null) {
|
|
return $this->json(['message' => 'Dieses Anfrageformular ist zurzeit nicht verfügbar.'], 404);
|
|
}
|
|
|
|
return $this->json([
|
|
...FormPayload::definition($delivered['form'], $this->evaluator),
|
|
'token' => FormToken::issue($entry->id, $delivered['version']->id),
|
|
'csrf' => csrf_token(),
|
|
]);
|
|
}
|
|
|
|
public function state(Request $request, InquiryEntry $entry): JsonResponse
|
|
{
|
|
$delivered = $this->service->deliver($entry);
|
|
|
|
if ($delivered === null) {
|
|
return $this->json(['message' => 'Dieses Anfrageformular ist zurzeit nicht verfügbar.'], 404);
|
|
}
|
|
|
|
$values = $request->input('values');
|
|
|
|
if (! is_array($values) || strlen((string) json_encode($values)) > 40000) {
|
|
return $this->json(['message' => 'Die Eingaben sind ungültig.'], 422);
|
|
}
|
|
|
|
return $this->json(FormPayload::state($delivered['form'], $values, $this->evaluator));
|
|
}
|
|
|
|
public function submit(Request $request, InquiryEntry $entry): JsonResponse
|
|
{
|
|
try {
|
|
$result = $this->service->submit($entry, [
|
|
'token' => $request->input('token'),
|
|
'values' => $request->input('values'),
|
|
'website' => $request->input('website'),
|
|
'source' => $request->input('source'),
|
|
]);
|
|
} catch (SubmissionException $e) {
|
|
return $this->json(['message' => $e->getMessage(), 'fields' => (object) $e->fields], $e->getCode() ?: 422);
|
|
}
|
|
|
|
return $this->json(['reference' => $result['reference']], 201);
|
|
}
|
|
|
|
/**
|
|
* Widget „Anfrage“ (ADR-61): das veröffentlichte Formular einer Vorlage für den gewählten Anfragegrund. Der Grund
|
|
* (Anliegen, Unteranliegen, Formularführung) steht in der Adresse, damit Zustand und Absendung ohne weitere
|
|
* Angaben dasselbe Formular meinen.
|
|
*/
|
|
public function formDefinition(Request $request, InquiryTemplate $template): JsonResponse
|
|
{
|
|
$delivered = $this->service->deliverForm($template, FormReason::clean($request->query()));
|
|
|
|
if ($delivered === null) {
|
|
return $this->json(['message' => 'Dieses Anfrageformular ist zurzeit nicht verfügbar.'], 404);
|
|
}
|
|
|
|
return $this->json([
|
|
...FormPayload::definition($delivered['form'], $this->evaluator),
|
|
'token' => FormToken::issue($template->id, $delivered['version']->id, 'form'),
|
|
'csrf' => csrf_token(),
|
|
]);
|
|
}
|
|
|
|
public function formState(Request $request, InquiryTemplate $template): JsonResponse
|
|
{
|
|
$delivered = $this->service->deliverForm($template, FormReason::clean($request->query()));
|
|
|
|
if ($delivered === null) {
|
|
return $this->json(['message' => 'Dieses Anfrageformular ist zurzeit nicht verfügbar.'], 404);
|
|
}
|
|
|
|
$values = $request->input('values');
|
|
|
|
if (! is_array($values) || strlen((string) json_encode($values)) > 40000) {
|
|
return $this->json(['message' => 'Die Eingaben sind ungültig.'], 422);
|
|
}
|
|
|
|
return $this->json(FormPayload::state($delivered['form'], $values, $this->evaluator));
|
|
}
|
|
|
|
public function formSubmit(Request $request, InquiryTemplate $template): JsonResponse
|
|
{
|
|
try {
|
|
$result = $this->service->submitForm($template, FormReason::clean($request->query()), [
|
|
'token' => $request->input('token'),
|
|
'values' => $request->input('values'),
|
|
'website' => $request->input('website'),
|
|
'source' => $request->input('source'),
|
|
]);
|
|
} catch (SubmissionException $e) {
|
|
return $this->json(['message' => $e->getMessage(), 'fields' => (object) $e->fields], $e->getCode() ?: 422);
|
|
}
|
|
|
|
return $this->json(['reference' => $result['reference']], 201);
|
|
}
|
|
|
|
/**
|
|
* @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');
|
|
}
|
|
}
|