Loheide-eu-mitarbeiterplatt.../app/Accessibility/EasyLanguage.php
Friederich Loheide 268742983a
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m17s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m28s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 6m32s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 6m48s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 8m51s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 8m52s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 7m31s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 52s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 7m51s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m22s
KI-Assistent: OpenAI und Ollama als Anbieter (0.35.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 13:57:37 +00:00

89 lines
2.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Accessibility;
use App\Ai\AiAssistant;
use App\Models\User;
use App\Support\RichText;
use Illuminate\Support\Facades\DB;
/**
* Leichte Sprache als zweite Fassung von Inhalten. Plugins melden ihre Inhaltsarten an:
*
* $easy->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<string, callable(int, User): (array{can_view: bool, can_edit: bool, title: string, text: callable(): string}|null)> */
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<string, mixed>|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(),
];
}
}