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>
79 lines
2.8 KiB
PHP
79 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Domain\Communication\FormEvaluator;
|
|
use App\Domain\Communication\FormPayload;
|
|
use App\Domain\Communication\FormToken;
|
|
use App\Domain\Communication\InquiryEntry;
|
|
use App\Domain\Communication\InquiryService;
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* @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');
|
|
}
|
|
}
|