Loheide-eu-mitarbeiterplatt.../app/Accessibility/EasyLanguage.php
Friederich Loheide 856a5c30af
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 9m24s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 9m15s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 9m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 9m21s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m5s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m10s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m8s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m23s
Vorlesen und Leichte Sprache mit KI-Vorschlag (0.34.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 13:24:56 +00:00

88 lines
2.8 KiB
PHP
Raw 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(),
];
}
}