Loheide-eu-mitarbeiterplatt.../app/Support/QuickLinks.php
Friederich Loheide a618927a52
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m7s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m30s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 8m6s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 8m21s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 17m13s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 17m12s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m52s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 9m29s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 47s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m23s
Schnellzugriffe mit Logo, Kategorien und Sichtbar für (0.51.0)
Untertitel, Logo, Kategorie, Rollen und Anheften je Eintrag; Knopf
„Anwendungen“ in der Kopfzeile mit allen sichtbaren Einträgen.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-29 18:44:53 +00:00

93 lines
3.9 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\Support;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
/**
* Schnellzugriffe (App-Launcher): Anwendungen und Seiten, die viele täglich brauchen –
* z. B. Dienstplan, Dokumentation, Archiv. Gepflegt im Admin-Bereich.
*
* Je Eintrag: Bezeichnung, Untertitel, Adresse, Symbol oder Logo, Kategorie, „Sichtbar für“
* (Rollen; leer = alle) und „oben anheften“. Auf der Startseite stehen die angehefteten
* (ohne angeheftete: alle), der Rest steckt hinter „Alle Anwendungen“ in der Kopfzeile.
*/
class QuickLinks
{
public function __construct(private readonly Settings $settings) {}
/**
* Alle Einträge, wie gespeichert (für die Verwaltung).
*
* @return list<array{label: string, subtitle: string, url: string, icon: string, logo: string|null, category: string, roles: list<int>, pinned: bool, new_tab: bool}>
*/
public function all(): array
{
$links = $this->settings->get('portal.quick_links');
if (! is_array($links)) {
return $this->defaults();
}
return array_values(array_map(fn (array $link) => [
'label' => (string) ($link['label'] ?? ''),
'subtitle' => (string) ($link['subtitle'] ?? ''),
'url' => preg_match('#^(https?://|/)#i', (string) ($link['url'] ?? '')) ? (string) $link['url'] : '#',
'icon' => Icons::has($link['icon'] ?? null) ? $link['icon'] : 'link',
'logo' => filled($link['logo'] ?? null) ? (string) $link['logo'] : null,
'category' => trim((string) ($link['category'] ?? '')),
'roles' => array_values(array_map('intval', array_filter((array) ($link['roles'] ?? []), 'is_numeric'))),
'pinned' => (bool) ($link['pinned'] ?? false),
'new_tab' => (bool) ($link['new_tab'] ?? false),
], array_filter($links, 'is_array')));
}
/**
* Was eine Person sehen darf – fertig für die Oberfläche (Logo als Adresse).
*
* @return list<array{label: string, subtitle: string, url: string, icon: string, logo_url: string|null, category: string, pinned: bool, new_tab: bool}>
*/
public function forUser(?User $user): array
{
$roleIds = $user ? $user->roles()->pluck('roles.id')->map(fn ($id) => (int) $id)->all() : [];
return array_values(array_map(fn (array $link) => [
'label' => $link['label'],
'subtitle' => $link['subtitle'],
'url' => $link['url'],
'icon' => $link['icon'],
'logo_url' => $link['logo'] ? Storage::disk('uploads')->url($link['logo']) : null,
'category' => $link['category'],
'pinned' => $link['pinned'],
'new_tab' => $link['new_tab'],
], array_filter(
$this->all(),
fn (array $link) => $link['roles'] === [] || array_intersect($link['roles'], $roleIds) !== [],
)));
}
/**
* Für die Startseite: angeheftete Einträge – oder alle, wenn keiner angeheftet ist.
*
* @param list<array<string, mixed>> $links aus forUser()
* @return list<array<string, mixed>>
*/
public static function pinned(array $links): array
{
$pinned = array_values(array_filter($links, fn (array $link) => $link['pinned']));
return $pinned !== [] ? $pinned : $links;
}
/**
* @return list<array{label: string, subtitle: string, url: string, icon: string, logo: string|null, category: string, roles: list<int>, pinned: bool, new_tab: bool}>
*/
public function defaults(): array
{
return [
['label' => 'Personen', 'subtitle' => '', 'url' => '/personen', 'icon' => 'users', 'logo' => null, 'category' => '', 'roles' => [], 'pinned' => false, 'new_tab' => false],
['label' => 'Mein Profil', 'subtitle' => '', 'url' => '/profil/bearbeiten', 'icon' => 'file-text', 'logo' => null, 'category' => '', 'roles' => [], 'pinned' => false, 'new_tab' => false],
];
}
}