115 lines
5.6 KiB
PHP
115 lines
5.6 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/header.php';
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$favorite_locations = [];
|
|
$subscriptions = [];
|
|
if (isset($_SESSION['user_id'])) {
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Fetch favorite locations
|
|
$stmt = $pdo->prepare("SELECT * FROM favorite_locations WHERE user_id = ? ORDER BY city_name ASC");
|
|
$stmt->execute([$_SESSION['user_id']]);
|
|
$favorite_locations = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Fetch all subscriptions for the user
|
|
$stmt = $pdo->prepare("SELECT * FROM weather_subscriptions WHERE user_id = ?");
|
|
$stmt->execute([$_SESSION['user_id']]);
|
|
$subscriptions = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
// Log error or handle it gracefully
|
|
error_log("DB Error: " . $e->getMessage());
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<div class="row">
|
|
<!-- Main Content Column -->
|
|
<div class="col-lg-8">
|
|
<div class="card app-card mb-4">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Search for a City</h5>
|
|
<form id="city-search-form" class="mb-4">
|
|
<div class="input-group">
|
|
<input type="text" id="city-input" class="form-control" placeholder="Enter city name...">
|
|
<button class="btn btn-primary" type="submit">Search</button>
|
|
</div>
|
|
</form>
|
|
<div id="search-history" class="mt-3"></div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="weather-display" class="card app-card">
|
|
<!-- Weather data will be loaded here by JavaScript -->
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Sidebar Column -->
|
|
<div class="col-lg-4">
|
|
<?php if (isset($_SESSION['user_id'])): ?>
|
|
<div class="card app-card mb-4">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Favorite Locations</h5>
|
|
<?php if (!empty($favorite_locations)): ?>
|
|
<ul class="list-group list-group-flush" id="favorite-locations-list">
|
|
<?php
|
|
$subscription_map = [];
|
|
foreach ($subscriptions as $sub) {
|
|
$subscription_map[$sub['location_id']] = $sub;
|
|
}
|
|
|
|
foreach ($favorite_locations as $location):
|
|
$subscription = $subscription_map[$location['id']] ?? null;
|
|
?>
|
|
<li class="list-group-item" data-location-id="<?php echo $location['id']; ?>">
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
<a href="#" class="city-link fw-bold" data-city="<?php echo htmlspecialchars($location['city_name']); ?>">
|
|
<?php echo htmlspecialchars($location['city_name']); ?>
|
|
</a>
|
|
<button class="btn btn-sm btn-outline-danger delete-location-btn" data-id="<?php echo $location['id']; ?>">
|
|
<i class="fas fa-trash-alt"></i>
|
|
</button>
|
|
</div>
|
|
<div class="subscription-controls mt-2">
|
|
<?php if ($subscription && $subscription['is_active']): ?>
|
|
<small>Subscribed for <?php echo htmlspecialchars(date("g:i A", strtotime($subscription['delivery_time']))); ?></small>
|
|
<button class="btn btn-sm btn-warning unsubscribe-btn" data-id="<?php echo $subscription['id']; ?>">Unsubscribe</button>
|
|
<?php else: ?>
|
|
<div class="input-group input-group-sm">
|
|
<input type="time" class="form-control alert-time-input" value="08:00">
|
|
<button class="btn btn-sm btn-info subscribe-btn">Subscribe</button>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<?php else: ?>
|
|
<p class="text-muted">You have no favorite locations yet.</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card app-card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Historical Weather</h5>
|
|
<div class="input-group mb-3">
|
|
<input type="date" id="history-date" class="form-control">
|
|
<button id="get-history-btn" class="btn btn-secondary">Get History</button>
|
|
</div>
|
|
<div id="history-display"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const isUserLoggedIn = <?php echo json_encode(isset($_SESSION['user_id'])); ?>;
|
|
</script>
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|