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-10-09 09:07:18 +02:00
|
|
|
validate([
|
|
|
|
|
'name' => 'required|min:3',
|
|
|
|
|
'email' => 'required|email',
|
|
|
|
|
'mobile' => 'required',
|
|
|
|
|
'bild' => 'mimes:jpg,jpeg,bmp,png,gif',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!empty($_FILES['bild']['name'])) {
|
|
|
|
|
Image::store($_FILES['bild'], 'user', (string) $userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user->name = (string) input('name');
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
}
|