Tour-Chat für Teilnehmer implementieren

This commit is contained in:
torsten 2026-11-10 09:04:13 +01:00
parent 569d939907
commit a4cc8fd857
18 changed files with 1390 additions and 11 deletions

View file

@ -10,6 +10,7 @@ use App\Models\Booking;
use App\Models\PushSubscription;
use App\Models\Tour;
use App\Models\TourRoom;
use App\Models\TourMessage;
use App\Models\User;
class TourController
@ -219,6 +220,7 @@ class TourController
}
$this->notifyEvent($tour, false);
$this->postChatSystem((int) $tour->id, 'Die Tour wurde angelegt. Hier könnt ihr euch austauschen.');
redirect('/tour');
}
@ -258,9 +260,16 @@ class TourController
redirect('/tour');
}
$current = Auth::user();
$guide = User::find((int) $tour->tour_guide_id);
$canManage = $this->canManageTour($tour);
$tourImages = $this->tourImages((int) $tour->id);
$canChat = $current !== null
&& TourMessage::isAvailable()
&& Booking::canAccessTour((int) $tour->id, $current);
$chatUnread = $canChat
? TourMessage::unreadCount((int) $tour->id, (int) $current->id)
: 0;
$users = [];
foreach (Booking::forTour((int) $tour->id) as $booking) {
@ -281,6 +290,8 @@ class TourController
'guide' => $guide,
'tourImages' => $tourImages,
'canManage' => $canManage,
'canChat' => $canChat,
'chatUnread' => $chatUnread,
]);
return;
@ -298,6 +309,8 @@ class TourController
'tourImages' => $tourImages,
'rooms' => $rooms,
'canManage' => $canManage,
'canChat' => $canChat,
'chatUnread' => $chatUnread,
]);
}
@ -404,6 +417,9 @@ class TourController
if ($tour->is_cancelled && !$wasCancelled) {
$this->notifyEvent($tour, true);
$this->postChatSystem($tourId, 'Die Tour wurde abgesagt.');
} elseif (!$tour->is_cancelled && $wasCancelled) {
$this->postChatSystem($tourId, 'Die Tour wurde wieder aktiviert.');
}
redirect('/tour/' . $tourId . '/edit');
@ -429,6 +445,7 @@ class TourController
// Abhängige Daten zuerst entfernen.
db()->execute('DELETE FROM `bookings` WHERE tour_id = ?', [$tourId]);
db()->execute('DELETE FROM `tour_rooms` WHERE tour_id = ?', [$tourId]);
TourMessage::deleteForTour($tourId);
db()->execute('DELETE FROM `tours` WHERE id = ?', [$tourId]);
// Galerie und Tourbilder von der Festplatte entfernen.
@ -681,6 +698,7 @@ class TourController
}
Booking::joinRoom((int) $tour->id, (int) $user->id, $roomId);
$this->postChatSystem((int) $tour->id, $user->name . ' hat die Tour gebucht.');
redirect('/tour/' . $tour_id);
}
@ -693,9 +711,35 @@ class TourController
}
$userId = (int) input('user_id', 0);
$roomId = (int) $room_id;
if ($userId > 0) {
Booking::joinRoom((int) $tour->id, $userId, (int) $room_id);
if ($roomId > 0 && TourRoom::findForTour($roomId, (int) $tour->id) === null) {
redirect('/tour/' . $tour_id . '/edit');
}
Booking::joinRoom((int) $tour->id, $userId, $roomId);
$participant = User::find($userId);
if ($participant !== null) {
if ($roomId > 0) {
$room = TourRoom::findForTour($roomId, (int) $tour->id);
$roomName = $room !== null ? (string) $room['name'] : '';
$roomLabel = $roomName !== ''
? 'dem ' . $roomName . ' (ID ' . $roomId . ')'
: 'einem Zimmer';
$this->postChatSystem(
(int) $tour->id,
$participant->name . ' wurde ' . $roomLabel . ' zugeordnet.'
);
} else {
$this->postChatSystem(
(int) $tour->id,
'Die Zimmerzuordnung von ' . $participant->name . ' wurde entfernt.'
);
}
}
}
redirect('/tour/' . $tour_id . '/edit');
@ -717,7 +761,13 @@ class TourController
redirect('/tour/' . $tour_id);
}
$participant = User::find((int) $user_id);
Booking::deleteForUser((int) $tour_id, (int) $user_id);
if ($participant !== null) {
$this->postChatSystem((int) $tour_id, $participant->name . ' wurde aus der Tour entfernt.');
}
redirect('/tour/' . $tour_id);
}
@ -730,6 +780,10 @@ class TourController
}
Tour::createRoom((int) $tour->id, (int) $capacity);
$this->postChatSystem(
(int) $tour->id,
(int) $capacity === 1 ? 'Ein Einzelzimmer wurde erstellt.' : 'Ein Doppelzimmer wurde erstellt.'
);
redirect('/tour/' . $tour_id . '/edit');
}
@ -741,18 +795,37 @@ class TourController
redirect('/tour');
}
$room = TourRoom::findForTour((int) $room_id, (int) $tour->id);
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]);
$roomName = $room !== null ? (string) $room['name'] : '';
$this->postChatSystem(
(int) $tour->id,
$roomName !== ''
? 'Das ' . $roomName . ' (ID ' . (int) $room_id . ') wurde gelöscht.'
: 'Ein Zimmer wurde gelöscht.'
);
redirect('/tour/' . $tour_id . '/edit');
}
private function canManageTour(?Tour $tour): bool
{
$user = Auth::user();
return $tour !== null && Tour::canManage((int) $tour->id, Auth::user());
}
return $user !== null
&& $tour !== null
&& ($user->isAdmin() || (int) $user->id === (int) $tour->tour_guide_id);
/** Schreibt eine Systemnachricht in den Tour-Chat, ohne Buchungen zu blockieren. */
private function postChatSystem(int $tourId, string $body): void
{
try {
TourMessage::postSystem($tourId, $body);
} catch (\Throwable $e) {
app_log('error', 'Tour-Chat Systemnachricht fehlgeschlagen', [
'tour_id' => $tourId,
'error' => $e->getMessage(),
]);
}
}
}