diff --git a/public/comets.php b/public/comets.php index ca1820e..9799f10 100644 --- a/public/comets.php +++ b/public/comets.php @@ -203,6 +203,69 @@ $pdo = new PDO( ); $userId = isset($_SESSION['user_id']) ? (int) $_SESSION['user_id'] : 0; + +if ($_SERVER['REQUEST_METHOD'] === 'POST' && $userId > 0) { + $action = $_POST['action'] ?? ''; + $cometId = (int) ($_POST['comet_id'] ?? 0); + + if ($action === 'add_comet' && $cometId > 0) { + $stmtCheck = $pdo->prepare(" + SELECT `id` + FROM `comets_mpc` + WHERE `id` = :id + LIMIT 1 + "); + $stmtCheck->execute([':id' => $cometId]); + + if ($stmtCheck->fetch()) { + $stmtInsert = $pdo->prepare(" + INSERT INTO `app_user_comets` ( + `user_id`, + `comet_id`, + `is_favorite`, + `created_at` + ) VALUES ( + :user_id, + :comet_id, + 0, + NOW() + ) + ON DUPLICATE KEY UPDATE + `comet_id` = VALUES(`comet_id`) + "); + $stmtInsert->execute([ + ':user_id' => $userId, + ':comet_id' => $cometId, + ]); + } + } + + if ($action === 'remove_comet' && $cometId > 0) { + $stmtCount = $pdo->prepare(" + SELECT COUNT(*) + FROM `app_user_comets` + WHERE `user_id` = :user_id + "); + $stmtCount->execute([':user_id' => $userId]); + $cometCount = (int) $stmtCount->fetchColumn(); + + if ($cometCount > 1) { + $stmtDelete = $pdo->prepare(" + DELETE FROM `app_user_comets` + WHERE `user_id` = :user_id + AND `comet_id` = :comet_id + "); + $stmtDelete->execute([ + ':user_id' => $userId, + ':comet_id' => $cometId, + ]); + } + } + + header('Location: comets.php'); + exit; +} + $today = new DateTimeImmutable('today', new DateTimeZone('UTC')); $windowStart = $today->modify('-180 days'); $windowEnd = $today->modify('+365 days'); @@ -632,6 +695,9 @@ arsort($orbitTypeCounts);