IT-Service: Baumstruktur, HTML-Antworten, Fehlermeldungen, geschlossene Tickets, Meine IT im Profilkasten
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m31s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m57s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m46s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m9s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m13s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m30s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m27s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m7s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 20:30:24 +00:00
parent ab35a67406
commit 50ee85c06b
119 changed files with 611 additions and 463 deletions

View file

@ -96,7 +96,7 @@ class ItServiceController extends Controller
'category_id' => $categories ? ['required', 'integer', Rule::in($categories)] : ['nullable', 'integer'],
'location_id' => $locations ? ['required', 'integer', Rule::in($locations)] : ['nullable', 'integer'],
'asset_id' => ['nullable', 'integer'],
], [], ['subject' => 'Betreff', 'description' => 'Beschreibung', 'category_id' => 'Kategorie', 'location_id' => 'Standort']);
], [], ['subject' => 'Betreff', 'description' => 'Nachricht', 'category_id' => 'Kategorie', 'location_id' => 'Standort']);
$id = $this->orFail(fn () => $this->it->createTicket($request->user(), $data), 'subject');
@ -119,6 +119,13 @@ class ItServiceController extends Controller
public function reply(Request $request, int $id): RedirectResponse
{
$data = $request->validate(['message' => ['required', 'string', 'max:20000']], [], ['message' => 'Antwort']);
$ticket = $this->orFail(fn () => $this->it->ticket($request->user(), $id), 'message');
if (! $ticket['open']) {
throw ValidationException::withMessages(['message' => 'Dieses Ticket ist abgeschlossen – bitte ein neues Ticket erstellen.']);
}
$this->orFail(fn () => $this->it->reply($request->user(), $id, $data['message']), 'message');
return back()->with('status', 'Antwort gesendet.');

View file

@ -3,6 +3,7 @@
namespace Inhaus\Plugins\ItService;
use App\Models\User;
use App\Support\RichText;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Arr;
use Inhaus\Plugins\ItService\OpenNit\Client;
@ -15,6 +16,8 @@ use Inhaus\Plugins\ItService\OpenNit\Settings;
*/
class ItService
{
public const CLOSED_STATUSES = ['closed', 'resolved', 'solved', 'done', 'geschlossen', 'erledigt', 'geloest', 'gelöst', 'cancelled', 'canceled'];
public const OPEN_STATUSES = ['open', 'new', 'pending', 'in_progress', 'waiting', 'reopened', 'offen', 'neu', 'in_bearbeitung', 'wartend'];
public function __construct(private readonly Client $client, private readonly Settings $settings) {}
@ -65,12 +68,12 @@ class ItService
return [
...self::ticketSummary($ticket),
'description' => self::text($ticket, ['description', 'body', 'message', 'text']),
'description' => self::html(self::text($ticket, ['description', 'body', 'message', 'text'])),
'category' => self::text($ticket, ['category', 'category_name']),
'location' => self::text($ticket, ['location', 'location_name']),
'asset' => self::text($ticket, ['asset', 'asset_name']),
'replies' => array_map(fn ($reply) => [
'message' => is_array($reply) ? (string) (self::text($reply, ['message', 'body', 'text', 'content']) ?? '') : (string) $reply,
'message' => self::html(is_array($reply) ? self::text($reply, ['message', 'body', 'text', 'content']) : (string) $reply),
'author' => is_array($reply) ? self::text($reply, ['author', 'from_name', 'name', 'user', 'author_name']) : null,
'from_staff' => is_array($reply) && (bool) ($reply['is_staff'] ?? $reply['internal_user'] ?? $reply['from_staff'] ?? (isset($reply['from_email']) && mb_strtolower((string) $reply['from_email']) !== mb_strtolower($user->email))),
'created_at' => is_array($reply) ? self::text($reply, ['created_at', 'date', 'created']) : null,
@ -220,7 +223,7 @@ class ItService
public static function isOpen(string $status): bool
{
return ! in_array(mb_strtolower($status), ['closed', 'resolved', 'done', 'geschlossen', 'erledigt', 'geloest', 'gelöst', 'cancelled'], true);
return ! in_array(mb_strtolower($status), self::CLOSED_STATUSES, true);
}
public static function ticketStatus(string $status): string
@ -230,9 +233,9 @@ class ItService
'in_progress', 'in_bearbeitung', 'assigned' => 'In Bearbeitung',
'pending', 'waiting', 'wartend', 'on_hold' => 'Wartet auf Rückmeldung',
'reopened' => 'Wieder geöffnet',
'resolved', 'geloest', 'gelöst', 'done', 'erledigt' => 'Gelöst',
'resolved', 'solved', 'geloest', 'gelöst', 'done', 'erledigt' => 'Gelöst',
'closed', 'geschlossen' => 'Geschlossen',
'cancelled' => 'Storniert',
'cancelled', 'canceled' => 'Storniert',
default => $status !== '' ? ucfirst(str_replace('_', ' ', $status)) : 'Offen',
};
}
@ -270,26 +273,57 @@ class ItService
*/
private static function tree(array $items): array
{
$byId = [];
$children = [];
foreach ($items as $item) {
if (is_array($item) && isset($item['id'])) {
$children[$item['parent_id'] ?? 0][] = $item;
$byId[$item['id']] = $item;
}
}
foreach ($byId as $item) {
// Eltern, die in der Liste fehlen, machen den Eintrag zur obersten Ebene
$parent = $item['parent_id'] ?? null;
$children[$parent !== null && isset($byId[$parent]) ? $parent : 0][] = $item;
}
$result = [];
$walk = function ($parent, int $depth) use (&$walk, &$children, &$result) {
$walk = function ($parent, int $depth, array $path) use (&$walk, &$children, &$result) {
foreach ($children[$parent] ?? [] as $item) {
$result[] = ['value' => (int) $item['id'], 'label' => str_repeat('– ', $depth).($item['name'] ?? '')];
$walk($item['id'], $depth + 1);
$name = trim((string) ($item['name'] ?? ''));
// Einrückung mit geschützten Leerzeichen – Auswahllisten verschlucken normale
$result[] = [
'value' => (int) $item['id'],
'label' => ($depth > 0 ? str_repeat("\u{00A0}\u{00A0}\u{00A0}\u{00A0}", $depth - 1)."\u{00A0}\u{00A0}└\u{00A0}" : '').$name,
'path' => implode(' › ', [...$path, $name]),
'depth' => $depth,
];
$walk($item['id'], $depth + 1, [...$path, $name]);
}
};
$walk(0, 0);
$walk(0, 0, []);
return $result;
}
/**
* Texte aus OpenNIT können HTML enthalten (<br>, <p> …): bereinigt ausgeben,
* reinen Text mit Zeilenumbrüchen übernehmen.
*/
private static function html(?string $text): ?string
{
if ($text === null || trim($text) === '') {
return null;
}
if ($text === strip_tags($text)) {
return nl2br(e($text), false);
}
return app(RichText::class)->sanitize($text);
}
/**
* @param array<string, mixed> $data
* @param list<string> $keys

View file

@ -3,7 +3,10 @@
namespace Inhaus\Plugins\ItService;
use App\Dashboard\Widgets;
use App\Models\User;
use App\Plugins\Hooks;
use App\Plugins\Plugin;
use App\Plugins\PluginAccess;
use App\Support\Abilities;
use App\Support\Navigation;
use Inhaus\Plugins\ItService\OpenNit\Settings;
@ -20,6 +23,11 @@ class ItServicePlugin extends Plugin
$this->app->make(Navigation::class)->add('it-service', 'IT-Service', '/it-service', active: 'it-service.*', sort: 75);
// Knopf „Meine IT“ im Profilkasten der Startseite
Hooks::filter('greeting.actions', fn (array $actions, User $user) => app(Settings::class)->configured() && app(PluginAccess::class)->allows('it-service', $user)
? [...$actions, ['label' => 'Meine IT', 'href' => '/it-service', 'icon' => 'monitor-smartphone']]
: $actions);
// Die Kachel lädt ihre Zahlen selbst nach – die Startseite wartet nicht auf OpenNIT.
$this->app->make(Widgets::class)->add('it-service.summary', 'it-service::MyIt', 'aside', sort: 25,
props: fn () => app(Settings::class)->configured() ? [] : null,

View file

@ -16,17 +16,44 @@ class OpenNitException extends RuntimeException
public static function fromStatus(int $status, array $body): self
{
$detail = is_string($body['message'] ?? null) ? $body['message'] : (is_string($body['error'] ?? null) ? $body['error'] : null);
$detail = self::detail($body);
$message = match ($status) {
400 => 'Die Angaben sind unvollständig oder ungültig.'.($detail ? " ({$detail})" : ''),
401 => 'Die Verbindung zum IT-Service ist nicht richtig eingerichtet (Anmeldung abgelehnt). Bitte die Administration informieren.',
403 => 'Der IT-Service erlaubt diesen Zugriff nicht.',
404 => 'Nicht gefunden – vielleicht gehört der Eintrag zu einer anderen Person.',
409 => $detail ?: 'Das Gerät ist in diesem Zeitraum nicht verfügbar.',
$message = match (true) {
$status === 401 => 'Die Verbindung zum IT-Service ist nicht richtig eingerichtet (Anmeldung abgelehnt). Bitte die Administration informieren.',
$status === 403 => $detail ?: 'Der IT-Service erlaubt diesen Zugriff nicht.',
$status === 404 => $detail ?: 'Nicht gefunden – vielleicht gehört der Eintrag zu einer anderen Person.',
$status === 409 => $detail ?: 'Das Gerät ist in diesem Zeitraum nicht verfügbar.',
// Übrige Eingabefehler (400, 422 …): die Meldung von OpenNIT zeigen, z. B. „Zeitraum zu lang“
$status >= 400 && $status < 500 => $detail ?: 'Die Angaben sind unvollständig oder ungültig.',
// Auch bei Serverfehlern eine kurze, lesbare Meldung von OpenNIT zeigen
$detail !== null && mb_strlen($detail) <= 200 && ! str_contains($detail, 'Exception') => $detail,
default => 'Der IT-Service ist gerade nicht erreichbar. Bitte später erneut versuchen.',
};
return new self($message, $status, $body);
}
/**
* Fehlertext aus der Antwort: message, error oder die erste Feldmeldung.
*
* @param array<string, mixed> $body
*/
private static function detail(array $body): ?string
{
foreach (['message', 'error', 'detail'] as $key) {
if (is_string($body[$key] ?? null) && trim($body[$key]) !== '') {
return trim($body[$key]);
}
}
foreach ((array) ($body['errors'] ?? []) as $messages) {
$first = is_array($messages) ? reset($messages) : $messages;
if (is_string($first) && $first !== '') {
return $first;
}
}
return null;
}
}