Files
KantSites/src/Controller/FormAdminController.php
T
kantarciandClaude Opus 5 c8a8fa7460 Rebuild DEEG as a fifth installation, add stepped forms and a plugin system, and clear the review backlog
Forms gained steps, conditions, image choices, number fields with units and a
summary step; the DEEG price finder uses all of it. Plugins are manifest plus
Twig: blocks, settings, containers, routes, presets and theme packs, switched
per installation. The review backlog is done: archive pages render through the
normal page pipeline, forms and post lists work inside areas and sections,
repeated sections get their own ids, breakpoints and colours come from the
palette, backup and update work on MySQL and lock each other out.

The task worker no longer shortens a process that has no time limit:
set_time_limit() sets the limit instead of extending it, which turned an
unlimited CLI run into eighty seconds and killed the full test suite far away
from the scheduler.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-16 07:32:35 +02:00

103 lines
5.0 KiB
PHP

<?php
namespace App\Controller;
use App\Content\ButtonDesign;
use App\Content\FormRepository;
use App\Content\MediaLibrary;
use App\Content\Theme;
use App\Domain\InvalidForm;
use App\Domain\StaleWrite;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
final class FormAdminController extends AbstractController
{
public function __construct(private FormRepository $forms, private Theme $theme, private MediaLibrary $media)
{
}
#[Route('/%kant.admin_path%/forms', name: 'admin_forms', methods: ['GET'])]
public function index(Request $request): Response
{
// Empfaenger, Felder und die Einsendungen selbst: nichts davon gehoert in die Hand einer
// Rolle, die nicht einmal eine Seite veroeffentlichen darf.
$this->denyAccessUnlessGranted('ROLE_PUBLISHER');
$theme = $this->theme->load();
return $this->render('admin/forms.html.twig', [
'forms' => $this->forms->all(),
'types' => FormRepository::TYPES,
'colors' => FormRepository::COLORS,
// Die Bilder fuer eine Bildauswahl: die Mediathek als Liste, damit je Option gewaehlt statt getippt wird.
'images' => array_map(static fn (array $item): array => ['id' => $item['id'], 'title' => $item['title'], 'src' => $item['variants'][0]['src'] ?? ''], $this->media->page(1, false, 96)['items']),
'submissions' => $this->forms->submissions($request->query->getInt('seite', 1)),
// Die Vorschau zeichnet den Absendeknopf mit dem Standardbutton der Website und
// kennt die Palette, damit eine Farbe gewaehlt statt getippt wird.
'schema' => ['colors' => $theme['colors'], 'fonts' => $theme['fonts'], 'buttons' => $theme['buttons'] ?? ButtonDesign::defaults($theme['base']), 'base' => $theme['base']],
]);
}
#[Route('/%kant.admin_path%/forms/{slug}', name: 'admin_form_save', requirements: ['slug' => '[a-z0-9-]{1,80}|neu'], methods: ['POST'])]
public function save(Request $request, string $slug): Response
{
$this->denyAccessUnlessGranted('ROLE_PUBLISHER');
if (!$this->isCsrfTokenValid('forms', $request->request->getString('_csrf_token'))) {
return $this->back('Deine Sitzung ist abgelaufen. Bitte melde dich erneut an.');
}
$fields = [];
foreach ($request->request->all('fields') as $row) {
if (!is_array($row) || '' === trim((string) ($row['label'] ?? ''))) {
continue;
}
$fields[] = [
'name' => (string) ($row['name'] ?? ''),
'type' => (string) ($row['type'] ?? 'text'),
'label' => (string) $row['label'],
'required' => '' !== (string) ($row['required'] ?? ''),
'help' => (string) ($row['help'] ?? ''),
'options' => array_map(trim(...), explode(',', (string) ($row['options'] ?? ''))),
'images' => (string) ($row['images'] ?? ''),
'min' => (string) ($row['min'] ?? ''),
'max' => (string) ($row['max'] ?? ''),
'unit' => (string) ($row['unit'] ?? ''),
// Die Bedingung: technischer Name eines früheren Auswahlfeldes und die Werte, bei
// denen dieses Feld gilt, als Kommaliste.
'showIfField' => (string) ($row['showIfField'] ?? ''),
'showIfValues' => (string) ($row['showIfValues'] ?? ''),
];
}
try {
$this->forms->save(
'neu' === $slug ? $request->request->getString('slug') : $slug,
$request->request->getString('name'),
$fields,
[
'recipient' => $request->request->getString('recipient'),
'sender' => $request->request->getString('sender'),
'subject' => $request->request->getString('subject'),
'success' => $request->request->getString('success'),
'replyToField' => $request->request->getString('replyToField'),
'buttonLabel' => $request->request->getString('buttonLabel'),
'summary' => '' !== $request->request->getString('summary'),
...array_map(fn (string $field): string => $request->request->getString($field), array_combine(array_keys(FormRepository::COLORS), array_keys(FormRepository::COLORS))),
],
$request->request->getInt('expectedVersion', -1),
);
} catch (InvalidForm|StaleWrite $e) {
return $this->back($e->getMessage());
}
return $this->back('Das Formular wurde gespeichert.', 'success');
}
private function back(string $message, string $type = 'error'): Response
{
$this->addFlash($type, $message);
return $this->redirectToRoute('admin_forms');
}
}