Meteorschauer neu
This commit is contained in:
+221
-25
@@ -109,6 +109,20 @@ function setExclusiveFavorite(PDO $pdo, string $tableName, string $idColumn, int
|
||||
}
|
||||
}
|
||||
|
||||
function countUserRows(PDO $pdo, string $tableName, int $userId): int
|
||||
{
|
||||
$stmtCount = $pdo->prepare("
|
||||
SELECT COUNT(*)
|
||||
FROM `{$tableName}`
|
||||
WHERE `user_id` = :user_id
|
||||
");
|
||||
$stmtCount->execute([
|
||||
':user_id' => $userId,
|
||||
]);
|
||||
|
||||
return (int) $stmtCount->fetchColumn();
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$action = $_POST['action'] ?? '';
|
||||
|
||||
@@ -168,17 +182,89 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$stmtDelete = $pdo->prepare("
|
||||
DELETE FROM `app_user_satellites`
|
||||
$stmtSatellite = $pdo->prepare("
|
||||
SELECT `satellite_id`, `is_favorite`
|
||||
FROM `app_user_satellites`
|
||||
WHERE `user_id` = :user_id
|
||||
AND `satellite_id` = :satellite_id
|
||||
LIMIT 1
|
||||
");
|
||||
$stmtDelete->execute([
|
||||
$stmtSatellite->execute([
|
||||
':user_id' => $userId,
|
||||
':satellite_id' => $satelliteId,
|
||||
]);
|
||||
$satelliteRow = $stmtSatellite->fetch();
|
||||
|
||||
$successMessage = 'Der Satellit wurde entfernt.';
|
||||
if (!$satelliteRow) {
|
||||
$errors[] = 'Der Satellit ist nicht in deiner Liste vorhanden.';
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$satelliteCount = countUserRows($pdo, 'app_user_satellites', $userId);
|
||||
if ($satelliteCount <= 1) {
|
||||
$errors[] = 'Der letzte Satellit kann nicht geloescht werden.';
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
try {
|
||||
$stmtDelete = $pdo->prepare("
|
||||
DELETE FROM `app_user_satellites`
|
||||
WHERE `user_id` = :user_id
|
||||
AND `satellite_id` = :satellite_id
|
||||
");
|
||||
$stmtDelete->execute([
|
||||
':user_id' => $userId,
|
||||
':satellite_id' => $satelliteId,
|
||||
]);
|
||||
|
||||
if ((int) ($satelliteRow['is_favorite'] ?? 0) === 1) {
|
||||
$stmtNextFavorite = $pdo->prepare("
|
||||
SELECT `satellite_id`
|
||||
FROM `app_user_satellites`
|
||||
WHERE `user_id` = :user_id
|
||||
ORDER BY `created_at` ASC, `satellite_id` ASC
|
||||
LIMIT 1
|
||||
");
|
||||
$stmtNextFavorite->execute([
|
||||
':user_id' => $userId,
|
||||
]);
|
||||
$nextSatelliteId = $stmtNextFavorite->fetchColumn();
|
||||
|
||||
if ($nextSatelliteId !== false) {
|
||||
$stmtResetFavorites = $pdo->prepare("
|
||||
UPDATE `app_user_satellites`
|
||||
SET `is_favorite` = 0
|
||||
WHERE `user_id` = :user_id
|
||||
");
|
||||
$stmtResetFavorites->execute([
|
||||
':user_id' => $userId,
|
||||
]);
|
||||
|
||||
$stmtSetFavorite = $pdo->prepare("
|
||||
UPDATE `app_user_satellites`
|
||||
SET `is_favorite` = 1
|
||||
WHERE `user_id` = :user_id
|
||||
AND `satellite_id` = :satellite_id
|
||||
");
|
||||
$stmtSetFavorite->execute([
|
||||
':user_id' => $userId,
|
||||
':satellite_id' => (int) $nextSatelliteId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
$successMessage = 'Der Satellit wurde entfernt.';
|
||||
} catch (Throwable $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
$errors[] = 'Der Satellit konnte nicht entfernt werden.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,6 +275,32 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$errors[] = 'Ungueltiger Satellit.';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$stmtCurrentSatellite = $pdo->prepare("
|
||||
SELECT `is_favorite`
|
||||
FROM `app_user_satellites`
|
||||
WHERE `user_id` = :user_id
|
||||
AND `satellite_id` = :satellite_id
|
||||
LIMIT 1
|
||||
");
|
||||
$stmtCurrentSatellite->execute([
|
||||
':user_id' => $userId,
|
||||
':satellite_id' => $satelliteId,
|
||||
]);
|
||||
$currentSatelliteFavorite = $stmtCurrentSatellite->fetchColumn();
|
||||
|
||||
if ($currentSatelliteFavorite === false) {
|
||||
$errors[] = 'Der Satellit ist nicht in deiner Liste vorhanden.';
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$satelliteCount = countUserRows($pdo, 'app_user_satellites', $userId);
|
||||
if ($satelliteCount <= 1 && (int) $currentSatelliteFavorite === 1) {
|
||||
$errors[] = 'Beim letzten Satelliten kann der Favorit nicht entfernt werden.';
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
if (!setExclusiveFavorite($pdo, 'app_user_satellites', 'satellite_id', $userId, $satelliteId)) {
|
||||
$errors[] = 'Der Satellit ist nicht in deiner Liste vorhanden.';
|
||||
@@ -450,17 +562,89 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$stmtDelete = $pdo->prepare("
|
||||
DELETE FROM `app_user_locations`
|
||||
$stmtLocation = $pdo->prepare("
|
||||
SELECT `id`, `is_default`
|
||||
FROM `app_user_locations`
|
||||
WHERE `id` = :id
|
||||
AND `user_id` = :user_id
|
||||
LIMIT 1
|
||||
");
|
||||
$stmtDelete->execute([
|
||||
$stmtLocation->execute([
|
||||
':id' => $locationId,
|
||||
':user_id' => $userId,
|
||||
]);
|
||||
$locationRow = $stmtLocation->fetch();
|
||||
|
||||
$successMessage = 'Der Standort wurde geloescht.';
|
||||
if (!$locationRow) {
|
||||
$errors[] = 'Der Standort wurde nicht gefunden.';
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$locationCount = countUserRows($pdo, 'app_user_locations', $userId);
|
||||
if ($locationCount <= 1) {
|
||||
$errors[] = 'Der letzte Standort kann nicht geloescht werden.';
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
try {
|
||||
$stmtDelete = $pdo->prepare("
|
||||
DELETE FROM `app_user_locations`
|
||||
WHERE `id` = :id
|
||||
AND `user_id` = :user_id
|
||||
");
|
||||
$stmtDelete->execute([
|
||||
':id' => $locationId,
|
||||
':user_id' => $userId,
|
||||
]);
|
||||
|
||||
if ((int) ($locationRow['is_default'] ?? 0) === 1) {
|
||||
$stmtNextDefault = $pdo->prepare("
|
||||
SELECT `id`
|
||||
FROM `app_user_locations`
|
||||
WHERE `user_id` = :user_id
|
||||
ORDER BY `name` ASC, `id` ASC
|
||||
LIMIT 1
|
||||
");
|
||||
$stmtNextDefault->execute([
|
||||
':user_id' => $userId,
|
||||
]);
|
||||
$nextLocationId = $stmtNextDefault->fetchColumn();
|
||||
|
||||
if ($nextLocationId !== false) {
|
||||
$stmtResetDefaults = $pdo->prepare("
|
||||
UPDATE `app_user_locations`
|
||||
SET `is_default` = 0
|
||||
WHERE `user_id` = :user_id
|
||||
");
|
||||
$stmtResetDefaults->execute([
|
||||
':user_id' => $userId,
|
||||
]);
|
||||
|
||||
$stmtSetDefault = $pdo->prepare("
|
||||
UPDATE `app_user_locations`
|
||||
SET `is_default` = 1
|
||||
WHERE `user_id` = :user_id
|
||||
AND `id` = :id
|
||||
");
|
||||
$stmtSetDefault->execute([
|
||||
':user_id' => $userId,
|
||||
':id' => (int) $nextLocationId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
$successMessage = 'Der Standort wurde geloescht.';
|
||||
} catch (Throwable $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
$errors[] = 'Der Standort konnte nicht geloescht werden.';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -697,11 +881,15 @@ $loggedIn = true;
|
||||
<button type="submit" class="btn btn-sm btn-secondary">Als Standard</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
<form method="post" action="" class="favorites-loading-form" data-loading-message="Standort wird entfernt..." onsubmit="return confirm('Standort wirklich loeschen?');">
|
||||
<input type="hidden" name="action" value="delete_location">
|
||||
<input type="hidden" name="location_id" value="<?= (int) $location['id'] ?>">
|
||||
<button type="submit" class="btn btn-sm btn-danger">Loeschen</button>
|
||||
</form>
|
||||
<?php if (count($locations) > 1): ?>
|
||||
<form method="post" action="" class="favorites-loading-form" data-loading-message="Standort wird entfernt..." onsubmit="return confirm('Standort wirklich loeschen?');">
|
||||
<input type="hidden" name="action" value="delete_location">
|
||||
<input type="hidden" name="location_id" value="<?= (int) $location['id'] ?>">
|
||||
<button type="submit" class="btn btn-sm btn-danger">Loeschen</button>
|
||||
</form>
|
||||
<?php else: ?>
|
||||
<span class="hint">Letzter Standort</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
@@ -893,18 +1081,26 @@ $loggedIn = true;
|
||||
<td><?= (int) $satellite['is_favorite'] === 1 ? '*' : '-' ?></td>
|
||||
<td><?= h($satellite['created_at']) ?></td>
|
||||
<td class="action-cell">
|
||||
<form method="post" action="" class="favorites-loading-form" data-loading-message="Satelliten-Favorit wird aktualisiert...">
|
||||
<input type="hidden" name="action" value="toggle_satellite_favorite">
|
||||
<input type="hidden" name="satellite_id" value="<?= (int) $satellite['id'] ?>">
|
||||
<button type="submit" class="btn btn-sm btn-secondary">
|
||||
<?= (int) $satellite['is_favorite'] === 1 ? 'Favorit entfernen' : 'Als Favorit' ?>
|
||||
</button>
|
||||
</form>
|
||||
<form method="post" action="" class="favorites-loading-form" data-loading-message="Satellit wird entfernt..." onsubmit="return confirm('Satellit wirklich entfernen?');">
|
||||
<input type="hidden" name="action" value="remove_satellite">
|
||||
<input type="hidden" name="satellite_id" value="<?= (int) $satellite['id'] ?>">
|
||||
<button type="submit" class="btn btn-sm btn-danger">Entfernen</button>
|
||||
</form>
|
||||
<?php if (count($trackedSatellites) <= 1 && (int) $satellite['is_favorite'] === 1): ?>
|
||||
<span class="hint">Favorit bleibt gesetzt</span>
|
||||
<?php else: ?>
|
||||
<form method="post" action="" class="favorites-loading-form" data-loading-message="Satelliten-Favorit wird aktualisiert...">
|
||||
<input type="hidden" name="action" value="toggle_satellite_favorite">
|
||||
<input type="hidden" name="satellite_id" value="<?= (int) $satellite['id'] ?>">
|
||||
<button type="submit" class="btn btn-sm btn-secondary">
|
||||
<?= (int) $satellite['is_favorite'] === 1 ? 'Favorit entfernen' : 'Als Favorit' ?>
|
||||
</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
<?php if (count($trackedSatellites) > 1): ?>
|
||||
<form method="post" action="" class="favorites-loading-form" data-loading-message="Satellit wird entfernt..." onsubmit="return confirm('Satellit wirklich entfernen?');">
|
||||
<input type="hidden" name="action" value="remove_satellite">
|
||||
<input type="hidden" name="satellite_id" value="<?= (int) $satellite['id'] ?>">
|
||||
<button type="submit" class="btn btn-sm btn-danger">Entfernen</button>
|
||||
</form>
|
||||
<?php else: ?>
|
||||
<span class="hint">Letzter Satellit</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
|
||||
Reference in New Issue
Block a user