Tour-Ansprechpartner änderbar, Selbst-Austragung gesperrt
This commit is contained in:
parent
77df14237c
commit
322d232012
5 changed files with 82 additions and 5 deletions
|
|
@ -192,11 +192,13 @@ class TourController
|
|||
redirect('/tour');
|
||||
}
|
||||
|
||||
$guides = User::selectableGuides();
|
||||
|
||||
if ($tour->start < today()) {
|
||||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.edit_old', ['tour' => $tour]);
|
||||
view('tour.edit_old', ['tour' => $tour, 'guides' => $guides]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
@ -206,7 +208,7 @@ class TourController
|
|||
$tour->start = german_date($tour->start);
|
||||
$tour->deadline = german_date($tour->deadline);
|
||||
|
||||
view('tour.edit', ['tour' => $tour, 'rooms' => $rooms]);
|
||||
view('tour.edit', ['tour' => $tour, 'rooms' => $rooms, 'guides' => $guides]);
|
||||
}
|
||||
|
||||
public function update(string $id): void
|
||||
|
|
@ -230,6 +232,13 @@ class TourController
|
|||
'user_id' => 'required',
|
||||
]);
|
||||
|
||||
$guide = User::find((int) input('user_id'));
|
||||
|
||||
if ($guide === null || $guide->role === 'GUEST') {
|
||||
flash_set('error', 'Der gewählte Ansprechpartner ist ungültig.');
|
||||
redirect('/tour/' . $tourId . '/edit');
|
||||
}
|
||||
|
||||
$imageError = null;
|
||||
|
||||
if (!empty($_FILES['bild']['name'])) {
|
||||
|
|
@ -247,6 +256,7 @@ class TourController
|
|||
}
|
||||
}
|
||||
|
||||
$tour->user_id = (int) $guide->id;
|
||||
$tour->name = (string) input('name');
|
||||
if (input('beschreibung') !== null) {
|
||||
$tour->beschreibung = (string) input('beschreibung');
|
||||
|
|
@ -292,7 +302,25 @@ class TourController
|
|||
|
||||
public function joinRoom(string $tour_id, string $room_id): void
|
||||
{
|
||||
Booking::joinRoom((int) $tour_id, (int) Auth::id(), (int) $room_id);
|
||||
$tour = Tour::find((int) $tour_id);
|
||||
$user = Auth::user();
|
||||
|
||||
if ($tour === null || $user === null) {
|
||||
redirect('/tour');
|
||||
}
|
||||
|
||||
$roomId = (int) $room_id;
|
||||
|
||||
// Normale Nutzer dürfen sich nur selbst in einen echten, freien Raum
|
||||
// eintragen. room_id 0 (austragen) und Raumwechsel bleiben Admins
|
||||
// bzw. dem Tour-Ansprechpartner vorbehalten.
|
||||
if (Booking::forUser((int) $tour->id, (int) $user->id) !== null
|
||||
|| !TourRoom::isBookable((int) $tour->id, $roomId)
|
||||
|| !Tour::hasSpace((int) $tour->id)) {
|
||||
redirect('/tour/' . $tour_id);
|
||||
}
|
||||
|
||||
Booking::joinRoom((int) $tour->id, (int) $user->id, $roomId);
|
||||
redirect('/tour/' . $tour_id);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,24 @@ class TourRoom extends Model
|
|||
{
|
||||
protected static string $table = 'tour_rooms';
|
||||
|
||||
/** true, wenn der Raum zur Tour gehört und noch einen freien Platz hat. */
|
||||
public static function isBookable(int $tourId, int $roomId): bool
|
||||
{
|
||||
if ($tourId <= 0 || $roomId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$row = db()->fetchOne(
|
||||
'SELECT tr.id, tr.capacity,
|
||||
(SELECT COUNT(*) FROM `bookings` b WHERE b.tour_id = tr.tour_id AND b.room_id = tr.id) AS booked
|
||||
FROM `tour_rooms` tr
|
||||
WHERE tr.tour_id = ? AND tr.id = ?',
|
||||
[$tourId, $roomId]
|
||||
);
|
||||
|
||||
return $row !== null && (int) $row['booked'] < (int) $row['capacity'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Liefert alle Räume einer Tour inkl. gebuchter Benutzer und freier
|
||||
* Plätze. Übernommen aus der ursprünglichen SQL-Abfrage (fetch_rooms).
|
||||
|
|
|
|||
|
|
@ -17,6 +17,17 @@ class User extends Model
|
|||
return $row === null ? null : static::fromRow($row);
|
||||
}
|
||||
|
||||
/** Admins und Mitglieder, die als Tour-Ansprechpartner wählbar sind. */
|
||||
public static function selectableGuides(): array
|
||||
{
|
||||
$rows = db()->fetchAll(
|
||||
'SELECT * FROM `users` WHERE role IN (?, ?) ORDER BY name',
|
||||
['ADMIN', 'MEMBER']
|
||||
);
|
||||
|
||||
return array_map(fn(array $row) => static::fromRow($row), $rows);
|
||||
}
|
||||
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->role === 'ADMIN';
|
||||
|
|
|
|||
|
|
@ -37,7 +37,6 @@
|
|||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="_method" value="PUT">
|
||||
<input type="hidden" name="tour_id" value="<?= e($tour->id) ?>">
|
||||
<input type="hidden" name="user_id" value="<?= e($tour->user_id) ?>">
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-3 col-form-label col-form-label-sm" style="width:180px; text-align:left;font-size:18px;" for="name">Name:</label>
|
||||
|
|
@ -45,6 +44,17 @@
|
|||
value="<?= e(old('name') ?? $tour->name) ?>" required>
|
||||
</div>
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-3" style="width:180px; text-align:left;font-size:18px;" for="user_id">Ansprechpartner:</label>
|
||||
<select class="mt-3 mr-2 form-control input-sm" id="user_id" name="user_id" required>
|
||||
<?php foreach ($guides as $guide): ?>
|
||||
<option value="<?= e($guide->id) ?>"<?= (int) $guide->id === (int) $tour->user_id ? ' selected' : '' ?>>
|
||||
<?= e($guide->name) ?><?= $guide->role === 'ADMIN' ? ' (Admin)' : '' ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-3" style="width:180px; text-align:left;font-size:18px;" for="start">Start:</label>
|
||||
<input class="mt-3 mr-2 form-control input-sm" type="text" id="start" name="start"
|
||||
|
|
|
|||
|
|
@ -9,13 +9,23 @@
|
|||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="_method" value="PUT">
|
||||
<input type="hidden" name="tour_id" value="<?= e($tour->id) ?>">
|
||||
<input type="hidden" name="user_id" value="<?= e($tour->user_id) ?>">
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-3 col-form-label col-form-label-sm" style="width:180px; text-align:left;font-size:18px;" for="name">Name:</label>
|
||||
<input class="mt-3 mr-2 form-control input-sm" type="text" id="name" name="name" value="<?= e(old('name') ?? $tour->name) ?>" required>
|
||||
</div>
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-3" style="width:180px; text-align:left;font-size:18px;" for="user_id">Ansprechpartner:</label>
|
||||
<select class="mt-3 mr-2 form-control input-sm" id="user_id" name="user_id" required>
|
||||
<?php foreach ($guides as $guide): ?>
|
||||
<option value="<?= e($guide->id) ?>"<?= (int) $guide->id === (int) $tour->user_id ? ' selected' : '' ?>>
|
||||
<?= e($guide->name) ?><?= $guide->role === 'ADMIN' ? ' (Admin)' : '' ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group" style="display:flex;">
|
||||
<label class="ml-2 mt-3" style="width:180px; text-align:left;font-size:18px;" for="start">Start:</label>
|
||||
<input class="mt-3 mr-2 form-control input-sm" type="text" id="start" name="start" value="<?= e(old('start') ?? $tour->start) ?>" required>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue