2026-10-09 09:07:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Core\Auth;
|
|
|
|
|
use App\Core\Image;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
|
|
class UserController
|
|
|
|
|
{
|
|
|
|
|
public function index(): void
|
|
|
|
|
{
|
2026-10-23 13:14:31 +02:00
|
|
|
$users = User::all();
|
|
|
|
|
|
|
|
|
|
// Nach Nachname sortieren (bei Gleichstand nach vollem Namen).
|
|
|
|
|
usort($users, function (User $a, User $b): int {
|
|
|
|
|
$cmp = strnatcasecmp(last_name($a->name), last_name($b->name));
|
|
|
|
|
|
|
|
|
|
return $cmp !== 0 ? $cmp : strnatcasecmp($a->name, $b->name);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$perPage = 20;
|
|
|
|
|
$total = count($users);
|
|
|
|
|
$lastPage = max(1, (int) ceil($total / $perPage));
|
|
|
|
|
$page = max(1, (int) input('page', 1));
|
|
|
|
|
|
|
|
|
|
// Paginator nur für Desktop-Browser und wenn es mehr als eine Seite gibt.
|
|
|
|
|
$paginate = is_desktop() && $total > $perPage;
|
|
|
|
|
|
|
|
|
|
if ($paginate) {
|
|
|
|
|
$page = min($page, $lastPage);
|
|
|
|
|
$users = array_slice($users, ($page - 1) * $perPage, $perPage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view('users.index', [
|
|
|
|
|
'users' => $users,
|
|
|
|
|
'paginator' => [
|
|
|
|
|
'enabled' => $paginate,
|
|
|
|
|
'page' => $page,
|
|
|
|
|
'lastPage' => $lastPage,
|
|
|
|
|
'total' => $total,
|
|
|
|
|
'perPage' => $perPage,
|
|
|
|
|
],
|
|
|
|
|
]);
|
2026-10-09 09:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create(): void
|
|
|
|
|
{
|
|
|
|
|
// Im Original ohne Funktion.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(): void
|
|
|
|
|
{
|
|
|
|
|
// Im Original ohne Funktion.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show(string $id): void
|
|
|
|
|
{
|
|
|
|
|
$user = User::find((int) $id);
|
|
|
|
|
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
http_response_code(404);
|
|
|
|
|
exit('Benutzer nicht gefunden.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view('users.show', ['user' => $user]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(string $id): void
|
|
|
|
|
{
|
|
|
|
|
$user = User::find((int) $id);
|
|
|
|
|
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
redirect('/member');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$current = Auth::user();
|
|
|
|
|
if (!$current->isAdmin() && (int) $current->id !== (int) $user->id) {
|
|
|
|
|
redirect('/member');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view('users.edit', ['user' => $user]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(string $id): void
|
|
|
|
|
{
|
|
|
|
|
$userId = (int) input('id', $id);
|
|
|
|
|
$user = User::find($userId);
|
|
|
|
|
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
redirect('/member');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$current = Auth::user();
|
|
|
|
|
|
2026-10-23 13:14:31 +02:00
|
|
|
if (!$current->isAdmin() && (int) $current->id !== (int) $userId) {
|
|
|
|
|
redirect('/member');
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-08 14:32:36 +01:00
|
|
|
// Bild wird in Image::store() inhaltlich geprüft (Format/Größe);
|
|
|
|
|
// die dortige Fehlermeldung wird als Flash-Meldung angezeigt.
|
2026-10-09 09:07:18 +02:00
|
|
|
validate([
|
|
|
|
|
'name' => 'required|min:3',
|
|
|
|
|
'email' => 'required|email',
|
|
|
|
|
'mobile' => 'required',
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-08 17:44:13 +01:00
|
|
|
$name = User::normalizeName((string) input('name'));
|
|
|
|
|
|
|
|
|
|
if ($name !== User::normalizeName((string) $user->name) && User::byName($name, $userId) !== null) {
|
|
|
|
|
flash_set('error', 'Dieser Name ist bereits vergeben. Bitte einen eindeutigen Namen angeben.');
|
|
|
|
|
redirect('/member/' . $userId . '/edit');
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-08 14:32:36 +01:00
|
|
|
$imageError = null;
|
|
|
|
|
|
2026-10-09 09:07:18 +02:00
|
|
|
if (!empty($_FILES['bild']['name'])) {
|
2026-11-08 14:32:36 +01:00
|
|
|
try {
|
|
|
|
|
Image::store($_FILES['bild'], 'user', (string) $userId);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
if (!$e instanceof \RuntimeException) {
|
|
|
|
|
error_log('Benutzerbild-Upload: ' . $e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$imageError = $e instanceof \RuntimeException
|
|
|
|
|
? $e->getMessage()
|
|
|
|
|
: 'Das Bild konnte nicht verarbeitet werden.';
|
|
|
|
|
}
|
2026-10-09 09:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-11-08 17:44:13 +01:00
|
|
|
$user->name = $name;
|
2026-10-09 09:07:18 +02:00
|
|
|
$user->email = (string) input('email');
|
|
|
|
|
$user->mobile = (string) input('mobile');
|
|
|
|
|
|
|
|
|
|
// Rolle nur durch Admins änderbar
|
|
|
|
|
if ($current->isAdmin()) {
|
|
|
|
|
$user->role = (string) input('role', $user->role);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user->updated_at = now();
|
|
|
|
|
$user->save();
|
|
|
|
|
|
2026-11-08 14:32:36 +01:00
|
|
|
if ($imageError !== null) {
|
|
|
|
|
flash_set('error', $imageError);
|
|
|
|
|
redirect('/member/' . $userId . '/edit');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 09:07:18 +02:00
|
|
|
redirect('/member');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy(string $user_id): void
|
|
|
|
|
{
|
|
|
|
|
$user = User::find((int) $user_id);
|
|
|
|
|
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
redirect('/member');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Zugehörige Buchungen mitlöschen, um verwaiste Einträge zu vermeiden.
|
|
|
|
|
db()->execute('DELETE FROM `bookings` WHERE user_id = ?', [(int) $user_id]);
|
|
|
|
|
db()->execute('DELETE FROM `users` WHERE id = ?', [(int) $user_id]);
|
|
|
|
|
|
|
|
|
|
redirect('/member');
|
|
|
|
|
}
|
|
|
|
|
}
|