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|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|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 $data */ private function json(array $data, int $status = 200): JsonResponse { return response()->json($data, $status)->header('Cache-Control', 'no-store, private'); } }