...
This commit is contained in:
parent
1db373a03d
commit
9bcbd3e194
118 changed files with 1116 additions and 155 deletions
104
motorradig/src/Controllers/AdminController.php
Normal file
104
motorradig/src/Controllers/AdminController.php
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
use App\Models\Page;
|
||||
|
||||
class AdminController
|
||||
{
|
||||
public function uploadTourplan(): void
|
||||
{
|
||||
view('admin.upload_tourplan');
|
||||
}
|
||||
|
||||
public function storeTourplan(): void
|
||||
{
|
||||
validate(['tourplan' => 'required|mimes:pdf']);
|
||||
|
||||
$dir = storage_path('img_storage/pdf');
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
move_uploaded_file($_FILES['tourplan']['tmp_name'], $dir . '/Tourenplan.pdf');
|
||||
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
public function uploadMitgliedsantrag(): void
|
||||
{
|
||||
view('admin.upload_mitgliedsantrag');
|
||||
}
|
||||
|
||||
public function storeMitgliedsantrag(): void
|
||||
{
|
||||
validate(['mitgliedsantrag' => 'required|mimes:pdf']);
|
||||
|
||||
$dir = public_path('img/pdf');
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
move_uploaded_file($_FILES['mitgliedsantrag']['tmp_name'], $dir . '/Mitgliedsantrag.pdf');
|
||||
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
public function editHome(): void
|
||||
{
|
||||
$page = Page::byName('home');
|
||||
|
||||
view('admin.edit_home', ['content' => $page->content ?? '']);
|
||||
}
|
||||
|
||||
public function storeHome(): void
|
||||
{
|
||||
validate(['pagecontent' => 'required']);
|
||||
|
||||
$page = Page::byName('home');
|
||||
|
||||
if ($page !== null) {
|
||||
$page->content = (string) input('pagecontent');
|
||||
$page->updated_at = now();
|
||||
$page->save();
|
||||
}
|
||||
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
public function editActivities(): void
|
||||
{
|
||||
$page = Page::byName('activities');
|
||||
$content = str_replace('<br/><br/>', "\n\n", $page->content ?? '');
|
||||
|
||||
view('admin.edit_activities', ['content' => $content]);
|
||||
}
|
||||
|
||||
public function storeActivities(): void
|
||||
{
|
||||
validate(['pagecontent' => 'required']);
|
||||
|
||||
$page = Page::byName('activities');
|
||||
|
||||
if ($page !== null) {
|
||||
$page->content = str_replace("\n\n", '<br/><br/>', (string) input('pagecontent'));
|
||||
$page->updated_at = now();
|
||||
$page->save();
|
||||
}
|
||||
|
||||
redirect('/activities');
|
||||
}
|
||||
|
||||
public function deleteGuests(): void
|
||||
{
|
||||
if (Auth::user()->isAdmin()) {
|
||||
db()->execute("DELETE FROM `bookings` WHERE user_id IN (SELECT id FROM `users` WHERE role = 'GUEST')");
|
||||
db()->execute("DELETE FROM `users` WHERE role = 'GUEST'");
|
||||
}
|
||||
|
||||
redirect('/');
|
||||
}
|
||||
}
|
||||
92
motorradig/src/Controllers/AuthController.php
Normal file
92
motorradig/src/Controllers/AuthController.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
use App\Models\User;
|
||||
|
||||
class AuthController
|
||||
{
|
||||
public function showLoginForm(): void
|
||||
{
|
||||
view('auth.login');
|
||||
}
|
||||
|
||||
public function login(): void
|
||||
{
|
||||
validate([
|
||||
'email' => 'required|email',
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$email = (string) input('email');
|
||||
$password = (string) input('password');
|
||||
|
||||
if (!Auth::attempt($email, $password)) {
|
||||
$_SESSION['_errors']['email'][] = 'Anmeldedaten sind ungültig.';
|
||||
$_SESSION['_old'] = $_POST;
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
public function logout(): void
|
||||
{
|
||||
Auth::logout();
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
public function showRegisterForm(): void
|
||||
{
|
||||
view('auth.register');
|
||||
}
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
validate([
|
||||
'name' => 'required|min:3',
|
||||
'email' => 'required|email',
|
||||
'mobile' => 'required',
|
||||
'password' => 'required|min:8',
|
||||
]);
|
||||
|
||||
$name = (string) input('name');
|
||||
|
||||
// Einfacher Spam-Schutz: mehr als drei Großbuchstaben im Namen ablehnen.
|
||||
if (preg_match_all('/[A-Z]/', $name) > 3) {
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
$password = (string) input('password');
|
||||
$confirmation = (string) input('password_confirmation');
|
||||
|
||||
if ($password !== $confirmation) {
|
||||
$_SESSION['_errors']['password'][] = 'Die Passwörter stimmen nicht überein.';
|
||||
$_SESSION['_old'] = $_POST;
|
||||
redirect('/register');
|
||||
}
|
||||
|
||||
if (User::byEmail((string) input('email')) !== null) {
|
||||
$_SESSION['_errors']['email'][] = 'Diese E-Mail-Adresse ist bereits vergeben.';
|
||||
$_SESSION['_old'] = $_POST;
|
||||
redirect('/register');
|
||||
}
|
||||
|
||||
$user = User::create([
|
||||
'name' => $name,
|
||||
'email' => (string) input('email'),
|
||||
'mobile' => (string) input('mobile'),
|
||||
'role' => 'GUEST',
|
||||
'password' => password_hash($password, PASSWORD_BCRYPT),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
redirect('/');
|
||||
}
|
||||
}
|
||||
158
motorradig/src/Controllers/GalleryController.php
Normal file
158
motorradig/src/Controllers/GalleryController.php
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
use App\Core\Image;
|
||||
use App\Models\Booking;
|
||||
|
||||
class GalleryController
|
||||
{
|
||||
public function show(string $tour_id): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user === null) {
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
$hasTour = Booking::forUser((int) $tour_id, (int) $user->id) !== null;
|
||||
|
||||
if (!$user->isAdmin() && !$hasTour) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$imagelist = $this->imageList('tour-' . $tour_id);
|
||||
|
||||
$tourName = db()->fetchValue('SELECT name FROM tours WHERE id = ?', [(int) $tour_id]) ?: '';
|
||||
|
||||
view('tour.gallery.show', [
|
||||
'tour_id' => $tour_id,
|
||||
'tour_name' => $tourName,
|
||||
'imagelist' => $imagelist,
|
||||
]);
|
||||
}
|
||||
|
||||
public function showDeletedImages(string $tour_id): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user === null || !$user->isAdmin()) {
|
||||
redirect('/tour/index_old');
|
||||
}
|
||||
|
||||
$imagelist = $this->imageList('tour-' . $tour_id . '-DELETED');
|
||||
|
||||
$tourName = db()->fetchValue('SELECT name FROM tours WHERE id = ?', [(int) $tour_id]) ?: '';
|
||||
|
||||
view('tour.gallery.show_deleted', [
|
||||
'tour_id' => $tour_id,
|
||||
'tour_name' => $tourName,
|
||||
'imagelist' => $imagelist,
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(string $tour_id): void
|
||||
{
|
||||
validate(['files' => 'required']);
|
||||
|
||||
$files = $_FILES['files'] ?? [];
|
||||
$tmpNames = $files['tmp_name'] ?? [];
|
||||
|
||||
foreach ($tmpNames as $i => $tmpName) {
|
||||
if (empty($tmpName) || !is_uploaded_file($tmpName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $files['name'][$i] ?? '';
|
||||
$upload = [
|
||||
'tmp_name' => $tmpName,
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
Image::store($upload, 'tour_' . $tour_id, $name);
|
||||
}
|
||||
|
||||
redirect('/tour/gallery/' . $tour_id);
|
||||
}
|
||||
|
||||
public function deleteImage(string $tour_id, string $image_name): void
|
||||
{
|
||||
$base = $this->safeBaseName($image_name);
|
||||
|
||||
$sourceDir = storage_path('img_storage/tour-' . $tour_id);
|
||||
$targetDir = storage_path('img_storage/tour-' . $tour_id . '-DELETED');
|
||||
|
||||
$this->ensureDir($targetDir);
|
||||
|
||||
foreach ([$base . '.jpg', $base . '_gross.jpg'] as $file) {
|
||||
if (is_file("$sourceDir/$file")) {
|
||||
rename("$sourceDir/$file", "$targetDir/$file");
|
||||
}
|
||||
}
|
||||
|
||||
redirect('/tour/gallery/' . $tour_id);
|
||||
}
|
||||
|
||||
public function restoreImage(string $tour_id, string $image_name): void
|
||||
{
|
||||
$base = $this->safeBaseName($image_name);
|
||||
|
||||
$sourceDir = storage_path('img_storage/tour-' . $tour_id . '-DELETED');
|
||||
$targetDir = storage_path('img_storage/tour-' . $tour_id);
|
||||
|
||||
$this->ensureDir($targetDir);
|
||||
|
||||
foreach ([$base . '.jpg', $base . '_gross.jpg'] as $file) {
|
||||
if (is_file("$sourceDir/$file")) {
|
||||
rename("$sourceDir/$file", "$targetDir/$file");
|
||||
}
|
||||
}
|
||||
|
||||
redirect('/tour/gallery/' . $tour_id . '/show_deleted');
|
||||
}
|
||||
|
||||
public function nameImage(string $tour_id, string $image_name): void
|
||||
{
|
||||
// Anzeigenamen wurden früher in der Tabelle gallery_items gespeichert.
|
||||
// Diese Tabelle ist entfallen; das Umbenennen ist daher ohne Funktion.
|
||||
redirect('/tour/gallery/' . $tour_id);
|
||||
}
|
||||
|
||||
/** Liefert [dateiname, anzeigename]-Paare für ein Galerie-Verzeichnis. */
|
||||
private function imageList(string $category): array
|
||||
{
|
||||
$dir = storage_path('img_storage/' . $category);
|
||||
$files = is_dir($dir) ? glob($dir . '/*_gross.jpg') : [];
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$fileName = basename($file);
|
||||
$base = substr($fileName, 0, -10); // '_gross.jpg' entfernen
|
||||
$list[] = [$base, $base . '.jpg'];
|
||||
}
|
||||
|
||||
sort($list);
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
private function safeBaseName(string $imageName): string
|
||||
{
|
||||
$name = basename($imageName);
|
||||
$name = preg_replace('/\.(jpg|jpeg|png|gif)$/i', '', $name) ?? $name;
|
||||
$name = preg_replace('/[^a-zA-Z0-9._-]/', '', $name) ?? '';
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
private function ensureDir(string $dir): void
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
142
motorradig/src/Controllers/HomeController.php
Normal file
142
motorradig/src/Controllers/HomeController.php
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
use App\Models\MembershipApplication;
|
||||
use App\Models\Page;
|
||||
|
||||
class HomeController
|
||||
{
|
||||
public function pageHome(): void
|
||||
{
|
||||
$page = Page::byName('home');
|
||||
$content = $page->content ?? '';
|
||||
|
||||
// Führende/abschließende Anführungszeichen entfernen (wie im Original)
|
||||
$content = rtrim(ltrim($content, '"'), '"');
|
||||
|
||||
view('home', ['content' => $content]);
|
||||
}
|
||||
|
||||
public function pageActivities(): void
|
||||
{
|
||||
$page = Page::byName('activities');
|
||||
|
||||
view('activities', ['content' => $page->content ?? '']);
|
||||
}
|
||||
|
||||
public function becomeMember(): void
|
||||
{
|
||||
view('become_member');
|
||||
}
|
||||
|
||||
public function meetings(): void
|
||||
{
|
||||
view('meetings');
|
||||
}
|
||||
|
||||
public function mitgliedsantrag(): void
|
||||
{
|
||||
view('mitgliedsantrag');
|
||||
}
|
||||
|
||||
public function storeMitgliedsantrag(): void
|
||||
{
|
||||
validate([
|
||||
'nachname' => 'required',
|
||||
'vorname' => 'required',
|
||||
'strasse' => 'required',
|
||||
'plz_ort' => 'required',
|
||||
'geburtsdatum' => 'required',
|
||||
'aufnahme_ab' => 'required',
|
||||
'kontoinhaber' => 'required',
|
||||
'iban' => 'required',
|
||||
]);
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
$nachname = (string) input('nachname');
|
||||
$vorname = (string) input('vorname');
|
||||
$strasse = (string) input('strasse');
|
||||
$plzOrt = (string) input('plz_ort');
|
||||
$telefon = (string) input('telefon');
|
||||
$email = (string) input('email');
|
||||
$geburtsdatum = (string) input('geburtsdatum');
|
||||
$motorrad = (string) input('motorrad');
|
||||
$aufnahmeAb = (string) input('aufnahme_ab');
|
||||
$ortDatum1 = (string) input('ort_datum_1');
|
||||
$unterschrift1 = (string) input('unterschrift_1');
|
||||
$mandatsref = (string) input('mandatsreferenz');
|
||||
$glaeubigerId = (string) input('glaeubiger_id');
|
||||
$kontoinhaber = (string) input('kontoinhaber');
|
||||
$kreditinstitut = (string) input('kreditinstitut');
|
||||
$bic = (string) input('bic');
|
||||
$iban = (string) input('iban');
|
||||
$ortDatum2 = (string) input('ort_datum_2');
|
||||
$unterschrift2 = (string) input('unterschrift_2');
|
||||
|
||||
MembershipApplication::create([
|
||||
'user_id' => $user?->id,
|
||||
'nachname' => $nachname,
|
||||
'vorname' => $vorname,
|
||||
'strasse' => $strasse,
|
||||
'plz_ort' => $plzOrt,
|
||||
'telefon' => $telefon,
|
||||
'email' => $email,
|
||||
'geburtsdatum' => $geburtsdatum,
|
||||
'motorrad' => $motorrad,
|
||||
'aufnahme_ab' => $aufnahmeAb,
|
||||
'ort_datum_1' => $ortDatum1,
|
||||
'unterschrift_1' => $unterschrift1,
|
||||
'mandatsreferenz' => $mandatsref,
|
||||
'glaeubiger_id' => $glaeubigerId,
|
||||
'kontoinhaber' => $kontoinhaber,
|
||||
'kreditinstitut' => $kreditinstitut,
|
||||
'bic' => $bic,
|
||||
'iban' => $iban,
|
||||
'ort_datum_2' => $ortDatum2,
|
||||
'unterschrift_2' => $unterschrift2,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$body = "Neuer Mitgliedsantrag über die Website\n"
|
||||
. "========================================\n\n"
|
||||
. "Persönliche Angaben\n"
|
||||
. " Nachname: " . $nachname . "\n"
|
||||
. " Vorname: " . $vorname . "\n"
|
||||
. " Straße, Nr.: " . $strasse . "\n"
|
||||
. " PLZ, Ort: " . $plzOrt . "\n"
|
||||
. " Telefon: " . $telefon . "\n"
|
||||
. " E-Mail: " . $email . "\n"
|
||||
. " Geburtsdatum: " . $geburtsdatum . "\n"
|
||||
. " Motorrad: " . $motorrad . "\n"
|
||||
. " Aufnahme ab: " . $aufnahmeAb . "\n"
|
||||
. " Ort / Datum: " . $ortDatum1 . "\n"
|
||||
. " Unterschrift: " . $unterschrift1 . "\n\n"
|
||||
. "SEPA-Lastschriftmandat\n"
|
||||
. " Mandatsreferenz: " . $mandatsref . "\n"
|
||||
. " Gläubiger-ID: " . $glaeubigerId . "\n"
|
||||
. " Kontoinhaber: " . $kontoinhaber . "\n"
|
||||
. " Kreditinstitut: " . $kreditinstitut . "\n"
|
||||
. " BIC: " . $bic . "\n"
|
||||
. " IBAN: " . $iban . "\n"
|
||||
. " Ort / Datum: " . $ortDatum2 . "\n"
|
||||
. " Unterschrift: " . $unterschrift2 . "\n";
|
||||
|
||||
send_mail(
|
||||
(string) config('app.membership_notify_email'),
|
||||
'Neuer Mitgliedsantrag: ' . $vorname . ' ' . $nachname,
|
||||
$body,
|
||||
'Motorrad-IG <motorradig@aol.com>',
|
||||
$email
|
||||
);
|
||||
|
||||
$_SESSION['_flash']['status'] = 'Vielen Dank! Dein Mitgliedsantrag wurde gespeichert.';
|
||||
|
||||
redirect('/mitgliedsantrag');
|
||||
}
|
||||
}
|
||||
68
motorradig/src/Controllers/ImageController.php
Normal file
68
motorradig/src/Controllers/ImageController.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
|
||||
class ImageController
|
||||
{
|
||||
public function getImage(string $category, string $imageName): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user !== null && ($user->isAdmin() || $user->isMember())) {
|
||||
$file = $this->resolve(storage_path('img_storage'), $category, $imageName);
|
||||
|
||||
if ($file !== null) {
|
||||
$this->serve($file);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$placeholder = public_path('img/placeholder/icon_unknown.png');
|
||||
$this->serve($placeholder);
|
||||
}
|
||||
|
||||
/** Löst Kategorie + Dateiname zu einem sicheren Pfad auf. */
|
||||
private function resolve(string $base, string $category, string $imageName): ?string
|
||||
{
|
||||
$category = basename($category);
|
||||
$imageName = basename($imageName);
|
||||
|
||||
$file = realpath($base . '/' . $category . '/' . $imageName);
|
||||
|
||||
if ($file === false || !str_starts_with($file, realpath($base))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
private function serve(string $file): void
|
||||
{
|
||||
$mime = $this->mimeType($file);
|
||||
|
||||
header('Content-Type: ' . $mime);
|
||||
header('Content-Length: ' . (string) filesize($file));
|
||||
readfile($file);
|
||||
exit;
|
||||
}
|
||||
|
||||
private function mimeType(string $file): string
|
||||
{
|
||||
$map = [
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'webp' => 'image/webp',
|
||||
'pdf' => 'application/pdf',
|
||||
];
|
||||
|
||||
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
||||
|
||||
return $map[$ext] ?? 'application/octet-stream';
|
||||
}
|
||||
}
|
||||
289
motorradig/src/Controllers/TourController.php
Normal file
289
motorradig/src/Controllers/TourController.php
Normal file
|
|
@ -0,0 +1,289 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
use App\Core\Image;
|
||||
use App\Models\Booking;
|
||||
use App\Models\Tour;
|
||||
use App\Models\TourRoom;
|
||||
use App\Models\User;
|
||||
|
||||
class TourController
|
||||
{
|
||||
public function index(): void
|
||||
{
|
||||
$tours = Tour::upcoming();
|
||||
$participantCount = [];
|
||||
|
||||
foreach ($tours as $tour) {
|
||||
$participantCount[$tour->id] = Booking::countForTour((int) $tour->id);
|
||||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
}
|
||||
|
||||
view('tour.index', ['tours' => $tours, 'participantCount' => $participantCount]);
|
||||
}
|
||||
|
||||
public function indexOld(): void
|
||||
{
|
||||
$tours = Tour::past();
|
||||
$participantCount = [];
|
||||
$imagecountPerTour = [];
|
||||
|
||||
foreach ($tours as $tour) {
|
||||
$participantCount[$tour->id] = Booking::countForTour((int) $tour->id);
|
||||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
$files = glob(storage_path('img_storage/tour-' . $tour->id) . '/*') ?: [];
|
||||
$imagecountPerTour[$tour->id] = (int) (count($files) / 2);
|
||||
}
|
||||
|
||||
view('tour.index_old', [
|
||||
'tours' => $tours,
|
||||
'participantCount' => $participantCount,
|
||||
'imagecountPerTour' => $imagecountPerTour,
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(): void
|
||||
{
|
||||
$tour = new Tour();
|
||||
$tour->name = 'neue Tour';
|
||||
$tour->beschreibung = 'Tour Beschreibung';
|
||||
$tour->user_id = Auth::id();
|
||||
|
||||
view('tour.create', ['tour' => $tour]);
|
||||
}
|
||||
|
||||
public function store(): void
|
||||
{
|
||||
validate([
|
||||
'name' => 'required|min:3',
|
||||
'beschreibung' => 'required',
|
||||
'start' => 'required',
|
||||
'deadline' => 'required',
|
||||
'user_id' => 'required',
|
||||
'bild' => 'mimes:jpg,jpeg,bmp,png,gif',
|
||||
]);
|
||||
|
||||
$tour = Tour::create([
|
||||
'name' => (string) input('name'),
|
||||
'user_id' => (int) input('user_id'),
|
||||
'beschreibung' => (string) input('beschreibung'),
|
||||
'start' => db_date(input('start')),
|
||||
'deadline' => db_date(input('deadline')),
|
||||
'max_participants' => (int) (input('max_participants') ?: 5),
|
||||
'is_cancelled' => (bool) input('is_cancelled', false),
|
||||
'bericht' => null,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
public function show(string $id): void
|
||||
{
|
||||
$tour = Tour::find((int) $id);
|
||||
|
||||
if ($tour === null) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$creator = User::find((int) $tour->user_id);
|
||||
$canManage = $this->canManageBookings($tour);
|
||||
|
||||
$users = [];
|
||||
foreach (Booking::forTour((int) $tour->id) as $booking) {
|
||||
$user = User::find((int) $booking->user_id);
|
||||
if ($user !== null) {
|
||||
$users[] = $user;
|
||||
}
|
||||
}
|
||||
|
||||
// Vergangene Tour
|
||||
if ($tour->start < today()) {
|
||||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.show_old', [
|
||||
'tour' => $tour,
|
||||
'users' => $users,
|
||||
'creator' => $creator,
|
||||
'canManage' => $canManage,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$rooms = array_map(fn(array $row) => (object) $row, TourRoom::fetchRooms((int) $tour->id));
|
||||
|
||||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.show', [
|
||||
'tour' => $tour,
|
||||
'users' => $users,
|
||||
'creator' => $creator,
|
||||
'rooms' => $rooms,
|
||||
'canManage' => $canManage,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(string $id): void
|
||||
{
|
||||
$current = Auth::user();
|
||||
|
||||
if (!$current->isAdmin()) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$tour = Tour::find((int) $id);
|
||||
|
||||
if ($tour === null) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
if ($tour->start < today()) {
|
||||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.edit_old', ['tour' => $tour]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$rooms = array_map(fn(array $row) => (object) $row, TourRoom::fetchRooms((int) $tour->id));
|
||||
|
||||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.edit', ['tour' => $tour, 'rooms' => $rooms]);
|
||||
}
|
||||
|
||||
public function update(string $id): void
|
||||
{
|
||||
$tourId = (int) input('tour_id', $id);
|
||||
$tour = Tour::find($tourId);
|
||||
|
||||
if ($tour === null) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
if (!Auth::user()->isAdmin()) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
validate([
|
||||
'tour_id' => 'required',
|
||||
'name' => 'required|min:3',
|
||||
'user_id' => 'required',
|
||||
'bild' => 'mimes:jpg,jpeg,bmp,png,gif',
|
||||
]);
|
||||
|
||||
if (!empty($_FILES['bild']['name'])) {
|
||||
Image::store($_FILES['bild'], 'tour_' . $tourId, (string) input('name'));
|
||||
}
|
||||
|
||||
$tour->name = (string) input('name');
|
||||
if (input('beschreibung') !== null) {
|
||||
$tour->beschreibung = (string) input('beschreibung');
|
||||
}
|
||||
if (input('start') !== null) {
|
||||
$tour->start = db_date(input('start'));
|
||||
}
|
||||
if (input('deadline') !== null) {
|
||||
$tour->deadline = db_date(input('deadline'));
|
||||
}
|
||||
if (input('max_participants') !== null) {
|
||||
$tour->max_participants = (int) input('max_participants');
|
||||
}
|
||||
if (input('is_cancelled')) {
|
||||
$tour->is_cancelled = true;
|
||||
} else {
|
||||
$tour->is_cancelled = false;
|
||||
}
|
||||
if (input('bericht') !== null) {
|
||||
$tour->bericht = (string) input('bericht');
|
||||
}
|
||||
|
||||
$tour->updated_at = now();
|
||||
$tour->save();
|
||||
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
public function destroy(string $id): void
|
||||
{
|
||||
// Im Original ohne Funktion.
|
||||
}
|
||||
|
||||
public function uploadGallery(string $tour_id): void
|
||||
{
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
public function joinRoom(string $tour_id, string $room_id): void
|
||||
{
|
||||
Booking::joinRoom((int) $tour_id, (int) Auth::id(), (int) $room_id);
|
||||
redirect('/tour/' . $tour_id);
|
||||
}
|
||||
|
||||
public function joinRoom4user(string $tour_id, string $room_id): void
|
||||
{
|
||||
$userId = (int) input('user_id', 0);
|
||||
|
||||
if ($userId > 0) {
|
||||
Booking::joinRoom((int) $tour_id, $userId, (int) $room_id);
|
||||
}
|
||||
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
}
|
||||
|
||||
public function cancelBooking(string $tour_id): void
|
||||
{
|
||||
if (!$this->canManageBookings(Tour::find((int) $tour_id))) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
Booking::deleteForUser((int) $tour_id, (int) Auth::id());
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
public function cancelBookingAdmin(string $tour_id, string $user_id): void
|
||||
{
|
||||
if (!$this->canManageBookings(Tour::find((int) $tour_id))) {
|
||||
redirect('/tour/' . $tour_id);
|
||||
}
|
||||
|
||||
Booking::deleteForUser((int) $tour_id, (int) $user_id);
|
||||
redirect('/tour/' . $tour_id);
|
||||
}
|
||||
|
||||
public function createRoom(string $tour_id, string $capacity): void
|
||||
{
|
||||
Tour::createRoom((int) $tour_id, (int) $capacity);
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
}
|
||||
|
||||
public function deleteRoom(string $tour_id, string $room_id): void
|
||||
{
|
||||
db()->execute('UPDATE `bookings` SET room_id = NULL WHERE room_id = ?', [(int) $room_id]);
|
||||
db()->execute('DELETE FROM `tour_rooms` WHERE tour_id = ? AND id = ?', [(int) $tour_id, (int) $room_id]);
|
||||
|
||||
redirect('/tour/' . $tour_id . '/edit');
|
||||
}
|
||||
|
||||
private function canManageBookings(?Tour $tour): bool
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
return $user !== null
|
||||
&& $tour !== null
|
||||
&& ($user->isAdmin() || (int) $user->id === (int) $tour->user_id);
|
||||
}
|
||||
}
|
||||
142
motorradig/src/Controllers/UserController.php
Normal file
142
motorradig/src/Controllers/UserController.php
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
<?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
|
||||
{
|
||||
$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,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (!$current->isAdmin() && (int) $current->id !== (int) $userId) {
|
||||
redirect('/member');
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue