Loheide-eu-mitarbeiterplatt.../app/Filament/Components/AudienceSection.php
Friederich Loheide 1877a0922f
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m45s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m25s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m21s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m49s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m59s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m21s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m30s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Rollen ausschließen: bei Inhalten (Zielgruppen) und für ganze Module
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:30:11 +00:00

58 lines
2.6 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\Filament\Components;
use App\Models\Role;
use App\Models\User;
use App\Support\Permissions\Audiences;
use Filament\Forms\Components\Select;
use Filament\Schemas\Components\Section;
use Illuminate\Database\Eloquent\Model;
/**
* Formularbereich „Sichtbarkeit“ für Objekte mit Zielgruppen.
*/
class AudienceSection
{
public static function make(string $type, string $description = 'Leer lassen = für alle sichtbar. Sonst sehen es nur Personen, die mindestens eine Angabe erfüllen.'): Section
{
$field = fn (string $rule, string $label, callable $options) => Select::make("audience_{$rule}")
->label($label)
->multiple()
->searchable()
->options($options)
->afterStateHydrated(fn (Select $component, ?Model $record) => $component->state($record ? Audiences::get($type, $record->getKey())[$rule] : []))
->saveRelationshipsUsing(function (Model $record, Select $component) use ($type) {
// Alle Felder zusammen speichern (einmal pro Formular reicht, aber schadet nicht).
$state = $component->getContainer()->getRawState();
Audiences::sync($type, $record->getKey(), [
'role' => $state['audience_role'] ?? [],
'department' => $state['audience_department'] ?? [],
'location' => $state['audience_location'] ?? [],
'exclude_role' => $state['audience_exclude_role'] ?? [],
]);
})
->dehydrated(false);
$roles = fn () => Role::query()->orderBy('name')->pluck('name', 'id')->mapWithKeys(fn ($name, $id) => [(string) $id => $name])->all();
return Section::make('Sichtbarkeit')
->description($description)
->collapsible()
->schema([
$field('role', 'Rollen', $roles),
$field('department', 'Abteilungen', fn () => self::distinct('department')),
$field('location', 'Standorte', fn () => self::distinct('location')),
$field('exclude_role', 'Ausgeschlossene Rollen', $roles)
->helperText('Personen mit einer dieser Rollen sehen es nie – auch wenn eine Angabe oben passt. Beispiel: Klientinnen und Klienten von einer internen Seite ausschließen.'),
]);
}
/**
* @return array<string, string>
*/
private static function distinct(string $column): array
{
return User::query()->whereNotNull($column)->distinct()->orderBy($column)->pluck($column, $column)->all();
}
}