All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m31s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m9s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m16s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 4m3s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 4m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 4m16s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 4m21s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m26s
- Organigramm: zwei Ebenen offen, Fläche so hoch wie der Inhalt, kleinere Abstände; Fokus-Ansicht in voller Breite mit Seitenspalte und Team-Vorschau - Dokumente: dichtere Zeilen, Unterordner in den Bereichen, Infospalte „Über diesen Ordner“ mit den pflegenden Rollen - Etwas weniger Randabstand im Seitenlayout Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
147 lines
6 KiB
PHP
147 lines
6 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\User;
|
||
use Illuminate\Database\Eloquent\Builder;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Collection;
|
||
use Inertia\Inertia;
|
||
use Inertia\Response;
|
||
|
||
/**
|
||
* Organigramm aus den Vorgesetzten-Angaben (Entra ID oder Admin). Auch bei
|
||
* tausenden Personen schnell: angezeigt werden die oberste Ebene und der Weg
|
||
* zu einer gewählten Person; alles Weitere lädt beim Aufklappen nach.
|
||
*/
|
||
class OrganigramController extends Controller
|
||
{
|
||
public function index(Request $request): Response
|
||
{
|
||
$view = in_array($request->query('ansicht'), ['baum', 'fokus'], true) ? $request->query('ansicht') : 'baum';
|
||
$focus = $request->integer('person') ? User::query()->active()->find($request->integer('person')) : null;
|
||
|
||
// Oberste Ebene: aktive Personen ohne (aktive) Vorgesetzte, die selbst ein Team haben.
|
||
$roots = $this->nodes(User::query()->active()
|
||
->where(fn (Builder $q) => $q->whereNull('manager_id')->orWhereDoesntHave('manager', fn (Builder $m) => $m->where('is_active', true)))
|
||
->whereHas('reports', fn (Builder $r) => $r->where('is_active', true)));
|
||
|
||
// Die Fokus-Ansicht beginnt ohne Auswahl bei der Spitze.
|
||
if ($view === 'fokus' && ! $focus && $roots->isNotEmpty()) {
|
||
$focus = User::query()->find($roots[0]['id']);
|
||
}
|
||
|
||
// Weg von oben zur gewählten Person, samt Team jeder Ebene.
|
||
$path = [];
|
||
$teams = [];
|
||
|
||
if ($focus) {
|
||
$current = $focus;
|
||
|
||
for ($i = 0; $current && $i < 30; $i++) {
|
||
array_unshift($path, $current->id);
|
||
$current = $current->manager_id ? User::query()->active()->find($current->manager_id) : null;
|
||
}
|
||
|
||
foreach ($path as $id) {
|
||
$teams[$id] = $this->nodes(User::query()->active()->where('manager_id', $id));
|
||
}
|
||
|
||
// Wer oben im Weg steht, aber (z. B. ohne Team) nicht in der obersten Ebene ist, dazunehmen.
|
||
if (! $roots->contains('id', $path[0])) {
|
||
$roots = $roots->push(...$this->nodes(User::query()->whereKey($path[0])))->values();
|
||
}
|
||
}
|
||
|
||
// Aufgeklappt: der Weg zur gesuchten Person – sonst die obersten zwei Ebenen,
|
||
// damit das Organigramm gleich etwas zeigt.
|
||
$open = array_values(array_filter($path, fn ($id) => $id !== $focus?->id));
|
||
|
||
if (! $focus && $roots->count() <= 3) {
|
||
foreach ($roots as $root) {
|
||
$teams[$root['id']] = $this->nodes(User::query()->active()->where('manager_id', $root['id']));
|
||
$open[] = $root['id'];
|
||
}
|
||
|
||
$leaders = collect($teams)->flatten(1)->where('reports', '>', 0)->pluck('id')->take(12)->all();
|
||
|
||
if ($leaders !== []) {
|
||
$this->nodes(User::query()->active()->whereIn('manager_id', $leaders))
|
||
->groupBy('manager_id')
|
||
->each(function ($team, $managerId) use (&$teams) {
|
||
$teams[$managerId] = $team->values();
|
||
});
|
||
$open = [...$open, ...$leaders];
|
||
}
|
||
}
|
||
|
||
// Fokus: auch die Teams der direkt Unterstellten – für die Vorschau im Fokus.
|
||
if ($focus && $view === 'fokus') {
|
||
$leads = collect($teams[$focus->id] ?? [])->where('reports', '>', 0)->pluck('id')->take(24)->all();
|
||
|
||
if ($leads !== []) {
|
||
$this->nodes(User::query()->active()->whereIn('manager_id', $leads))->groupBy('manager_id')
|
||
->each(function ($team, $managerId) use (&$teams) {
|
||
$teams[$managerId] = $team->values();
|
||
});
|
||
}
|
||
}
|
||
|
||
return Inertia::render('People/Organigram', [
|
||
'roots' => $roots,
|
||
'path' => $path,
|
||
'open' => array_values(array_unique($open)),
|
||
'teams' => (object) $teams,
|
||
'focus' => $focus?->id,
|
||
'details' => $focus ? [
|
||
'id' => $focus->id,
|
||
'name' => $focus->name,
|
||
'initials' => $focus->initials,
|
||
'job_title' => $focus->job_title,
|
||
'department' => $focus->department,
|
||
'location' => $focus->location,
|
||
'email' => $focus->email,
|
||
'phone' => $focus->phone,
|
||
'mobile' => $focus->mobile,
|
||
'avatar_url' => $focus->avatarUrl(192),
|
||
] : null,
|
||
'view' => $view,
|
||
'stats' => [
|
||
'people' => User::query()->active()->where(fn (Builder $q) => $q->whereNotNull('manager_id')->orWhereHas('reports', fn (Builder $r) => $r->where('is_active', true)))->count(),
|
||
'leaders' => User::query()->active()->whereHas('reports', fn (Builder $r) => $r->where('is_active', true))->count(),
|
||
],
|
||
'unassigned' => User::query()->active()->whereNull('manager_id')->whereDoesntHave('reports')->count(),
|
||
]);
|
||
}
|
||
|
||
public function team(User $user): JsonResponse
|
||
{
|
||
abort_unless($user->is_active, 404);
|
||
|
||
return response()->json(['team' => $this->nodes(User::query()->active()->where('manager_id', $user->id))]);
|
||
}
|
||
|
||
/**
|
||
* @param Builder<User> $query
|
||
* @return Collection<int, array<string, mixed>>
|
||
*/
|
||
private function nodes(Builder $query): Collection
|
||
{
|
||
return $query->withCount(['reports' => fn (Builder $r) => $r->where('is_active', true)])
|
||
->orderBy('last_name')->orderBy('first_name')
|
||
->limit(500)
|
||
->get()
|
||
->map(fn (User $u) => [
|
||
'id' => $u->id,
|
||
'name' => $u->name,
|
||
'initials' => $u->initials,
|
||
'job_title' => $u->job_title,
|
||
'department' => $u->department,
|
||
'location' => $u->location,
|
||
'avatar_url' => $u->avatarUrl(96),
|
||
'reports' => $u->reports_count,
|
||
'manager_id' => $u->manager_id,
|
||
]);
|
||
}
|
||
}
|