diff --git a/assets/js/main.js b/assets/js/main.js index 00dac9e..e074845 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -1,14 +1,52 @@ document.addEventListener('DOMContentLoaded', function () { - // Initialize Bootstrap components - var toastEl = document.getElementById('sosToast'); - var toast = new bootstrap.Toast(toastEl); + const sosButton = document.getElementById('sosButton'); + const sosToastEl = document.getElementById('sosToast'); + const sosToast = new bootstrap.Toast(sosToastEl); + const toastBody = sosToastEl.querySelector('.toast-body'); + const toastHeader = sosToastEl.querySelector('.toast-header .me-auto'); - // SOS Button functionality - var sosButton = document.getElementById('sosButton'); if (sosButton) { sosButton.addEventListener('click', function () { - // Show the toast notification - toast.show(); + // Disable button to prevent multiple clicks + sosButton.disabled = true; + sosButton.querySelector('.sos-text').textContent = 'Sending...'; + + fetch('sos.php', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + } + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + toastHeader.classList.remove('text-danger'); + toastHeader.classList.add('text-success'); + toastHeader.innerHTML = ' SOS Sent'; + toastBody.textContent = data.message; + } else { + toastHeader.classList.remove('text-success'); + toastHeader.classList.add('text-danger'); + toastHeader.innerHTML = ' Error'; + toastBody.textContent = data.message; + } + sosToast.show(); + }) + .catch(error => { + toastHeader.classList.remove('text-success'); + toastHeader.classList.add('text-danger'); + toastHeader.innerHTML = ' Network Error'; + toastBody.textContent = 'Could not connect to the server. Please check your connection.'; + sosToast.show(); + console.error('SOS fetch error:', error); + }) + .finally(() => { + // Re-enable the button after a delay + setTimeout(() => { + sosButton.disabled = false; + sosButton.querySelector('.sos-text').textContent = 'SOS'; + }, 5000); // 5-second cooldown + }); }); } -}); +}); \ No newline at end of file diff --git a/db/migrate.php b/db/migrate.php index f89fea6..0108e48 100644 --- a/db/migrate.php +++ b/db/migrate.php @@ -1,12 +1,41 @@ exec($sql); - echo "Migration successful: users table created.\n"; + + // 1. Create migrations table if it doesn't exist + $pdo->exec("CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) PRIMARY KEY, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); + + // 2. Get all migrations that have been run + $ran_migrations_stmt = $pdo->query("SELECT migration FROM migrations"); + $ran_migrations = $ran_migrations_stmt->fetchAll(PDO::FETCH_COLUMN); + + // 3. Get all migration files + $migration_files = glob(__DIR__ . '/migrations/*.sql'); + sort($migration_files); + + $migrations_to_run = array_filter($migration_files, function($file) use ($ran_migrations) { + return !in_array(basename($file), $ran_migrations); + }); + + if (empty($migrations_to_run)) { + echo "No new migrations to run.\n"; + } else { + foreach ($migrations_to_run as $file) { + echo "Running migration: " . basename($file) . "...\n"; + $sql = file_get_contents($file); + $pdo->exec($sql); + + // 4. Record the migration + $stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)"); + $stmt->execute([basename($file)]); + echo "Migration successful: " . basename($file) . "\n"; + } + } + } catch (PDOException $e) { die("Migration failed: " . $e->getMessage() . "\n"); -} - +} \ No newline at end of file diff --git a/db/migrations/002_create_trips_table.sql b/db/migrations/002_create_trips_table.sql new file mode 100644 index 0000000..6496935 --- /dev/null +++ b/db/migrations/002_create_trips_table.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS trips ( + id INT AUTO_INCREMENT PRIMARY KEY, + user_id INT(11) UNSIGNED NOT NULL, + destination VARCHAR(255) NOT NULL, + trip_time DATETIME NOT NULL, + status VARCHAR(50) DEFAULT 'planned', + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE +); diff --git a/index.php b/index.php index 1676ed7..3651669 100644 --- a/index.php +++ b/index.php @@ -1,3 +1,4 @@ +
@@ -18,7 +19,7 @@ - + @@ -34,7 +35,13 @@You have no planned trips yet.
+ +| Destination | +Date & Time | +Status | +Booked On | +
|---|---|---|---|
| + | + | + | + |
An SOS alert was triggered by:
" + . "Please take appropriate action immediately.
"; +$textBody = "SOS Alert from " . $userName . " (" . $userEmail . ") at " . date('Y-m-d H:i:s') . " UTC."; + +// Send the email. If no recipient is set in .env, it will use the default. +$result = MailService::sendMail(null, $subject, $htmlBody, $textBody); + +if (!empty($result['success'])) { + $response['success'] = true; + $response['message'] = 'SOS alert sent successfully. Help is on the way.'; +} else { + // In a real app, you would log this error in more detail. + $response['message'] = 'Failed to send SOS alert. Please try again or contact support.'; + // For debugging, you might want to include the error, but not in production. + // $response['error'] = $result['error']; +} + +echo json_encode($response); diff --git a/trip-setup.php b/trip-setup.php index eed919f..7781319 100644 --- a/trip-setup.php +++ b/trip-setup.php @@ -1,3 +1,42 @@ +prepare("INSERT INTO trips (user_id, destination, trip_time) VALUES (:user_id, :destination, :trip_time)"); + $stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT); + $stmt->bindParam(':destination', $destination, PDO::PARAM_STR); + $stmt->bindParam(':trip_time', $trip_time, PDO::PARAM_STR); + + if ($stmt->execute()) { + header('Location: my-trips.php?status=success'); + exit(); + } else { + $error_message = 'Failed to save the trip. Please try again.'; + } + } catch (PDOException $e) { + $error_message = 'Database error: ' . $e->getMessage(); + } + } +} +?> @@ -18,7 +57,7 @@ - + @@ -34,7 +73,13 @@Enter your destination and travel time to find verified travel companions.
- @@ -86,4 +135,4 @@ - \ No newline at end of file +