register('news', fn (int $id, User $user) => [ * 'can_view' => …, 'can_edit' => …, 'title' => …, 'text' => fn () => '…', * ]); * * „text“ liefert den Ausgangstext für den KI-Vorschlag – nur, was alle Lesenden sehen. */ class EasyLanguage { /** @var array */ private array $types = []; public function register(string $type, callable $resolve): void { $this->types[$type] = $resolve; } /** * @return array{can_view: bool, can_edit: bool, title: string, text: callable(): string}|null */ public function resolve(string $type, int $id, User $user): ?array { return isset($this->types[$type]) ? ($this->types[$type])($id, $user) : null; } public function html(string $type, int $id): ?string { return DB::table('easy_language_texts')->where('type', $type)->where('subject_id', $id)->value('html'); } public function save(string $type, int $id, ?string $html, User $user, bool $aiAssisted = false): ?string { $clean = trim(app(RichText::class)->toSafeHtml($html)); if (trim(strip_tags($clean)) === '') { DB::table('easy_language_texts')->where('type', $type)->where('subject_id', $id)->delete(); return null; } $values = ['html' => $clean, 'ai_assisted' => $aiAssisted, 'updated_by' => $user->id, 'updated_at' => now()]; $query = DB::table('easy_language_texts')->where('type', $type)->where('subject_id', $id); $query->exists() ? $query->update($values) : DB::table('easy_language_texts')->insert([...$values, 'type' => $type, 'subject_id' => $id, 'created_at' => now()]); return $clean; } public function forget(string $type, int $id): void { DB::table('easy_language_texts')->where('type', $type)->where('subject_id', $id)->delete(); } /** * Für die Anzeige: vorhandene Fassung und ob die Person sie bearbeiten darf. * * @return array|null */ public function props(string $type, int $id, User $user, bool $canEdit): ?array { $html = $this->html($type, $id); if ($html === null && ! $canEdit) { return null; } return [ 'type' => $type, 'id' => $id, 'html' => $html, 'can_edit' => $canEdit, 'ai' => $canEdit && app(AiAssistant::class)->available(), 'ai_external' => app(AiAssistant::class)->isExternal(), ]; } }