editable($request, $type, $id, $easy); $data = $request->validate(['html' => ['nullable', 'string', 'max:200000'], 'ai_assisted' => ['boolean']]); $saved = $easy->save($type, $id, $data['html'] ?? null, $request->user(), (bool) ($data['ai_assisted'] ?? false)); Audit::log($saved ? 'easy_language.saved' : 'easy_language.removed', ['type' => $type, 'id' => $id]); return back()->with('status', $saved ? 'Leichte Sprache gespeichert.' : 'Leichte Sprache entfernt.'); } public function suggest(Request $request, string $type, int $id, EasyLanguage $easy, AiAssistant $ai): JsonResponse { $subject = $this->editable($request, $type, $id, $easy); try { $html = app(RichText::class)->toSafeHtml($ai->easyLanguage($subject['title'], ($subject['text'])())); } catch (RuntimeException $e) { return response()->json(['message' => $e->getMessage()], 422); } Audit::log('ai.easy_language', ['type' => $type, 'id' => $id, 'model' => $ai->model()]); return response()->json(['html' => $html]); } /** * @return array{can_view: bool, can_edit: bool, title: string, text: callable(): string} */ private function editable(Request $request, string $type, int $id, EasyLanguage $easy): array { $subject = $easy->resolve($type, $id, $request->user()); abort_unless($subject !== null && $subject['can_edit'], 403); return $subject; } }