index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Form\FormSchema;
  5. use App\Security\Csrf;
  6. require __DIR__ . '/src/autoload.php';
  7. Bootstrap::init();
  8. $schema = new FormSchema();
  9. $steps = $schema->getSteps();
  10. $csrf = Csrf::token();
  11. $app = Bootstrap::config('app');
  12. $disclaimerConfigRaw = $app['disclaimer'] ?? [];
  13. if (is_string($disclaimerConfigRaw)) {
  14. $disclaimerConfig = ['text' => $disclaimerConfigRaw];
  15. } elseif (is_array($disclaimerConfigRaw)) {
  16. $disclaimerConfig = $disclaimerConfigRaw;
  17. } else {
  18. $disclaimerConfig = [];
  19. }
  20. $disclaimerTitle = (string) ($disclaimerConfig['title'] ?? 'Hinweis');
  21. $disclaimerText = (string) ($disclaimerConfig['text'] ?? '');
  22. $disclaimerAcceptLabel = (string) ($disclaimerConfig['accept_label'] ?? 'Hinweis gelesen, weiter');
  23. $addressDisclaimerConfigRaw = $app['address_disclaimer'] ?? ($app['address_disclaimer_text'] ?? '');
  24. if (is_string($addressDisclaimerConfigRaw)) {
  25. $addressDisclaimerText = $addressDisclaimerConfigRaw;
  26. } elseif (is_array($addressDisclaimerConfigRaw)) {
  27. $addressDisclaimerText = (string) ($addressDisclaimerConfigRaw['text'] ?? '');
  28. } else {
  29. $addressDisclaimerText = '';
  30. }
  31. $baseUrl = Bootstrap::baseUrl();
  32. /** @param array<string, mixed> $field */
  33. function renderField(array $field, string $addressDisclaimerText): void
  34. {
  35. $keyRaw = (string) ($field['key'] ?? '');
  36. $key = htmlspecialchars($keyRaw);
  37. $label = htmlspecialchars((string) $field['label']);
  38. $type = (string) ($field['type'] ?? 'text');
  39. $requiredAlways = (bool) ($field['required'] ?? false);
  40. $requiredConditional = isset($field['required_if']) && is_array($field['required_if']);
  41. $required = $requiredAlways ? 'required' : '';
  42. $requiredLabel = '';
  43. if ($requiredAlways) {
  44. $requiredLabel = ' <span class="required-mark required-mark-field" aria-hidden="true">* Pflichtfeld</span>';
  45. } elseif ($requiredConditional) {
  46. $requiredLabel = ' <span class="required-mark required-mark-field" aria-hidden="true">* Pflichtfeld</span>';
  47. }
  48. $fieldClass = 'field';
  49. if ($requiredAlways || $requiredConditional) {
  50. $fieldClass .= ' mandatory-field';
  51. }
  52. if ($requiredAlways) {
  53. $fieldClass .= ' mandatory-field-hard';
  54. }
  55. echo '<div class="' . $fieldClass . '" data-field="' . $key . '">';
  56. if ($type === 'checkbox') {
  57. echo '<label class="checkbox-label"><input type="checkbox" name="form_data[' . $key . ']" value="1" ' . $required . '> ' . $label . $requiredLabel . '</label>';
  58. } else {
  59. $labelFor = $type === 'table' ? htmlspecialchars($keyRaw . '__r0__c0') : $key;
  60. echo '<label for="' . $labelFor . '">' . $label . $requiredLabel . '</label>';
  61. if ($type === 'textarea') {
  62. echo '<textarea id="' . $key . '" name="form_data[' . $key . ']" ' . $required . '></textarea>';
  63. } elseif ($type === 'select') {
  64. echo '<select id="' . $key . '" name="form_data[' . $key . ']" ' . $required . '>';
  65. echo '<option value="">Bitte wählen</option>';
  66. foreach (($field['options'] ?? []) as $option) {
  67. if (!is_array($option)) {
  68. continue;
  69. }
  70. $value = htmlspecialchars((string) ($option['value'] ?? ''));
  71. $optLabel = htmlspecialchars((string) ($option['label'] ?? ''));
  72. echo '<option value="' . $value . '">' . $optLabel . '</option>';
  73. }
  74. echo '</select>';
  75. } elseif ($type === 'table') {
  76. $rows = (int) ($field['rows'] ?? 4);
  77. if ($rows < 1) {
  78. $rows = 1;
  79. } elseif ($rows > 50) {
  80. $rows = 50;
  81. }
  82. $columns = [];
  83. if (isset($field['columns']) && is_array($field['columns'])) {
  84. foreach ($field['columns'] as $index => $column) {
  85. if (!is_array($column)) {
  86. continue;
  87. }
  88. $columnLabelRaw = trim((string) ($column['label'] ?? ''));
  89. if ($columnLabelRaw === '') {
  90. $columnLabelRaw = 'Spalte ' . ($index + 1);
  91. }
  92. $columnTypeRaw = strtolower(trim((string) ($column['type'] ?? 'text')));
  93. $columnType = in_array($columnTypeRaw, ['text', 'date', 'number', 'email', 'tel'], true) ? $columnTypeRaw : 'text';
  94. $columns[] = [
  95. 'label' => $columnLabelRaw,
  96. 'type' => $columnType,
  97. 'placeholder' => (string) ($column['placeholder'] ?? ''),
  98. ];
  99. }
  100. }
  101. if (empty($columns)) {
  102. $columns = [
  103. ['label' => 'Spalte 1', 'type' => 'text', 'placeholder' => ''],
  104. ['label' => 'Spalte 2', 'type' => 'text', 'placeholder' => ''],
  105. ['label' => 'Spalte 3', 'type' => 'text', 'placeholder' => ''],
  106. ];
  107. }
  108. echo '<input id="' . $key . '" type="hidden" name="form_data[' . $key . ']">';
  109. echo '<div class="table-input-wrapper">';
  110. echo '<div class="table-responsive">';
  111. echo '<table class="form-table-input" data-table-field="1" data-table-key="' . $key . '" data-table-rows="' . (string) $rows . '">';
  112. echo '<thead><tr>';
  113. foreach ($columns as $column) {
  114. echo '<th>' . htmlspecialchars($column['label']) . '</th>';
  115. }
  116. echo '</tr></thead>';
  117. echo '<tbody>';
  118. for ($row = 0; $row < $rows; $row++) {
  119. echo '<tr>';
  120. foreach ($columns as $columnIndex => $column) {
  121. $cellId = $keyRaw . '__r' . $row . '__c' . $columnIndex;
  122. $cellIdEscaped = htmlspecialchars($cellId);
  123. $placeholder = trim((string) ($column['placeholder'] ?? ''));
  124. $placeholderEscaped = htmlspecialchars($placeholder);
  125. $ariaLabel = htmlspecialchars($column['label'] . ' Zeile ' . ($row + 1));
  126. echo '<td>';
  127. echo '<input id="' . $cellIdEscaped . '" class="table-cell-input" type="' . htmlspecialchars((string) $column['type']) . '" data-table-cell="1" data-table-key="' . $key . '" data-row-index="' . (string) $row . '" data-col-index="' . (string) $columnIndex . '" aria-label="' . $ariaLabel . '" autocomplete="off"';
  128. if ($placeholder !== '') {
  129. echo ' placeholder="' . $placeholderEscaped . '"';
  130. }
  131. echo '>';
  132. echo '</td>';
  133. }
  134. echo '</tr>';
  135. }
  136. echo '</tbody>';
  137. echo '</table>';
  138. echo '</div>';
  139. echo '</div>';
  140. } elseif ($type === 'file') {
  141. $accept = htmlspecialchars((string) ($field['accept'] ?? ''));
  142. $description = trim((string) ($field['description'] ?? ''));
  143. $fileInputId = $key . '_file';
  144. $cameraInputId = $key . '_camera';
  145. echo '<div class="upload-control" data-upload-key="' . $key . '">';
  146. echo '<div class="upload-actions">';
  147. echo '<label class="upload-action-btn" for="' . $fileInputId . '">Datei auswählen</label>';
  148. echo '<label class="upload-action-btn upload-action-btn-camera" for="' . $cameraInputId . '">Foto aufnehmen</label>';
  149. echo '</div>';
  150. if ($description !== '') {
  151. echo '<small class="hint">' . nl2br(htmlspecialchars($description)) . '</small>';
  152. }
  153. echo '<input id="' . $fileInputId . '" class="upload-native-input" type="file" name="' . $key . '" accept="' . $accept . '">';
  154. echo '<input id="' . $cameraInputId . '" class="upload-native-input" type="file" name="' . $key . '__camera" accept="image/*" capture="environment">';
  155. echo '<p class="upload-selected" data-upload-selected="' . $key . '">Keine Datei gewählt</p>';
  156. echo '</div>';
  157. echo '<div class="upload-list" data-upload-list="' . $key . '"></div>';
  158. } else {
  159. $inputType = htmlspecialchars($type);
  160. echo '<input id="' . $key . '" type="' . $inputType . '" name="form_data[' . $key . ']" ' . $required . '>';
  161. }
  162. }
  163. $subtext = trim((string) ($field['subtext'] ?? ''));
  164. if ($subtext !== '') {
  165. echo '<small class="hint">' . nl2br(htmlspecialchars($subtext)) . '</small>';
  166. }
  167. if ($keyRaw === 'strasse' && trim($addressDisclaimerText) !== '') {
  168. echo '<div class="address-disclaimer">' . nl2br(htmlspecialchars($addressDisclaimerText)) . '</div>';
  169. }
  170. if (isset($field['required_if']) && is_array($field['required_if'])) {
  171. $depField = htmlspecialchars((string) ($field['required_if']['field'] ?? ''));
  172. $depValue = htmlspecialchars((string) ($field['required_if']['equals'] ?? ''));
  173. }
  174. echo '<div class="error" data-error-for="' . $key . '"></div>';
  175. echo '</div>';
  176. }
  177. ?><!doctype html>
  178. <html lang="de">
  179. <head>
  180. <meta charset="utf-8">
  181. <meta name="viewport" content="width=device-width, initial-scale=1">
  182. <title><?= htmlspecialchars((string) $app['project_name']) ?></title>
  183. <link rel="stylesheet" href="<?= htmlspecialchars(Bootstrap::url('assets/css/tokens.css')) ?>">
  184. <link rel="stylesheet" href="<?= htmlspecialchars(Bootstrap::url('assets/css/base.css')) ?>">
  185. </head>
  186. <body>
  187. <header class="site-header">
  188. <div class="container header-inner">
  189. <a class="brand" href="<?= htmlspecialchars(Bootstrap::url('index.php')) ?>">
  190. <img class="brand-logo" src="<?= htmlspecialchars(Bootstrap::url('assets/images/feuerwehr-logo-invers.webp')) ?>" alt="Feuerwehr Logo">
  191. <div>
  192. <div class="brand-title"><?= htmlspecialchars((string) $app['project_name']) ?></div>
  193. <div class="brand-subtitle">Feuerwehr Freising</div>
  194. </div>
  195. </a>
  196. </div>
  197. </header>
  198. <main class="container">
  199. <h1>Digitaler Mitgliedsantrag Feuerwehrverein</h1>
  200. <section id="disclaimerSection" class="card">
  201. <h2><?= htmlspecialchars($disclaimerTitle) ?></h2>
  202. <p class="disclaimer-text"><?= nl2br(htmlspecialchars($disclaimerText)) ?></p>
  203. <div class="field disclaimer-ack-field">
  204. <label class="checkbox-label">
  205. <input id="disclaimerReadCheckbox" type="checkbox" value="1">
  206. Ich habe den Hinweis gelesen und verstanden.
  207. </label>
  208. <div id="disclaimerReadError" class="error"></div>
  209. </div>
  210. <button id="acceptDisclaimerBtn" type="button" class="btn" disabled><?= htmlspecialchars($disclaimerAcceptLabel) ?></button>
  211. </section>
  212. <section id="wizardSection" class="card hidden">
  213. <h2>Mitgliedsantrag</h2>
  214. <p class="required-legend"><span class="required-mark" aria-hidden="true">*</span> Pflichtfeld</p>
  215. <div id="progress" class="progress"></div>
  216. <form id="applicationForm" enctype="multipart/form-data" novalidate>
  217. <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf) ?>">
  218. <input type="hidden" id="applicationEmail" name="email" value="">
  219. <input type="hidden" id="applicationWebsite" name="website" value="">
  220. <?php foreach ($steps as $index => $step): ?>
  221. <section class="step hidden" data-step="<?= $index + 1 ?>">
  222. <h3>Schritt <?= $index + 1 ?>: <?= htmlspecialchars((string) ($step['title'] ?? '')) ?></h3>
  223. <p><?= htmlspecialchars((string) ($step['description'] ?? '')) ?></p>
  224. <?php foreach (($step['fields'] ?? []) as $field): ?>
  225. <?php if (is_array($field)) { renderField($field, $addressDisclaimerText); } ?>
  226. <?php endforeach; ?>
  227. </section>
  228. <?php endforeach; ?>
  229. <section id="summarySection" class="step-summary hidden">
  230. <h3>Zusammenfassung</h3>
  231. <p>Bitte prüfen Sie alle Angaben vor dem verbindlichen Absenden.</p>
  232. <div id="summaryMissingNotice" class="summary-missing-note hidden" role="status" aria-live="polite"></div>
  233. <div id="summaryContent" class="summary-content"></div>
  234. </section>
  235. <div class="wizard-actions">
  236. <button type="button" id="prevBtn" class="btn btn-secondary">Zurück</button>
  237. <button type="button" id="nextBtn" class="btn">Weiter</button>
  238. <button type="button" id="submitBtn" class="btn hidden">
  239. <span data-submit-label>Verbindlich absenden</span>
  240. <span id="submitSpinner" class="btn-spinner hidden" aria-hidden="true"></span>
  241. </button>
  242. </div>
  243. <p id="feedbackMessage" class="status-text" role="status" aria-live="polite"></p>
  244. </form>
  245. </section>
  246. <section id="startSection" class="card hidden">
  247. <h2>Status</h2>
  248. <p id="startIntroText">Bitte E-Mail eingeben. Bestehende Entwürfe werden automatisch geladen.</p>
  249. <form id="startForm" novalidate>
  250. <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf) ?>">
  251. <div class="hp-field" aria-hidden="true">
  252. <label for="website">Website</label>
  253. <input id="website" type="text" name="website" autocomplete="off" tabindex="-1">
  254. </div>
  255. <div class="field" id="startEmailField">
  256. <label for="startEmail">E-Mail <span class="required-mark required-mark-field-start" aria-hidden="true">* Pflichtfeld</span></label>
  257. <input id="startEmail" type="email" name="email" required inputmode="email" autocomplete="email">
  258. <div id="startEmailError" class="error"></div>
  259. </div>
  260. <div class="inline-actions" id="startActions">
  261. <button id="startSubmitBtn" type="submit" class="btn">Formular laden</button>
  262. </div>
  263. <div id="compactStatusBox" class="compact-status hidden">
  264. <p><strong>E-Mail:</strong> <span id="statusEmailValue">-</span></p>
  265. <p><strong>Speicherstatus:</strong> <span id="draftStatusValue">Noch nicht gespeichert</span></p>
  266. <button id="resetDataBtn" type="button" class="btn btn-small">Gespeicherte Daten löschen und neu starten</button>
  267. </div>
  268. <p id="startFeedbackMessage" class="status-text" role="status" aria-live="polite"></p>
  269. </form>
  270. </section>
  271. </main>
  272. <script>
  273. window.APP_BOOT = {
  274. steps: <?= json_encode($steps, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>,
  275. csrf: <?= json_encode($csrf, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>,
  276. contactEmail: <?= json_encode((string) ($app['contact_email'] ?? ''), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>,
  277. baseUrl: <?= json_encode($baseUrl, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>
  278. };
  279. </script>
  280. <script src="<?= htmlspecialchars(Bootstrap::url('assets/js/form.js')) ?>"></script>
  281. </body>
  282. </html>