34695-vm/profile.php
Flatlogic Bot 90597ae75f draftv1
2025-10-05 16:43:57 +00:00

153 lines
7.2 KiB
PHP

<?php
require_once 'auth.php';
require_login();
$user_id = $_SESSION['user_id'];
$user = null;
// Fetch user data to populate the form
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT email, receive_weekly_summary FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// In a real app, log this error.
$error_message = "Error fetching user data.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Profile - LTD Tracker</title>
<meta name="description" content="Manage your user profile.">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg sticky-top">
<div class="container">
<a class="navbar-brand" href="index.php">LTD Tracker</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><span class="nav-link">Role: <?php echo htmlspecialchars($_SESSION['role_name']); ?></span></li>
<?php if (is_admin()): ?>
<li class="nav-item"><a href="admin.php" class="nav-link">Admin</a></li>
<?php endif; ?>
<li class="nav-item"><a href="index.php" class="nav-link">Deals</a></li>
<li class="nav-item"><a href="profile.php" class="nav-link active">Profile</a></li>
<li class="nav-item"><a href="logout.php" class="btn btn-outline-secondary ms-2">Logout</a></li>
<li class="nav-item d-flex align-items-center ms-3">
<div class="theme-switcher" id="theme-switcher">
<i id="theme-icon" data-feather="moon"></i>
</div>
</li>
</ul>
</div>
</div>
</nav>
<main class="container mt-5">
<section id="profile" class="section">
<h1 class="text-center mb-5">My Profile</h1>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-body">
<form id="profileForm">
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($user['email'] ?? ''); ?>" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">New Password (optional)</label>
<input type="password" class="form-control" id="password" name="password" aria-describedby="passwordHelp">
<div id="passwordHelp" class="form-text">Leave blank if you don't want to change it.</div>
</div>
<div class="mb-3">
<label for="password_confirm" class="form-label">Confirm New Password</label>
<input type="password" class="form-control" id="password_confirm" name="password_confirm">
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" value="1" id="receive_weekly_summary" name="receive_weekly_summary" <?php echo ($user['receive_weekly_summary'] ?? true) ? 'checked' : ''; ?>>
<label class="form-check-label" for="receive_weekly_summary">
Receive weekly summary email
</label>
</div>
<button type="submit" class="btn btn-primary">Update Profile</button>
</form>
<div id="profile-message" class="mt-3"></div>
</div>
</div>
</div>
</div>
</section>
</main>
<footer class="py-4 bg-dark text-white text-center mt-5">
<div class="container">
<p class="mb-0">&copy; <?php echo date('Y'); ?> LTD Tracker. All rights reserved.</p>
<a href="/privacy.php" class="text-white">Privacy Policy</a>
</div>
</footer>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
<script>
feather.replace();
document.getElementById('profileForm').addEventListener('submit', async (e) => {
e.preventDefault();
const form = e.target;
const formData = new FormData(form);
const data = Object.fromEntries(formData.entries());
data.receive_weekly_summary = document.getElementById('receive_weekly_summary').checked ? 1 : 0;
const messageDiv = document.getElementById('profile-message');
messageDiv.textContent = '';
messageDiv.classList.remove('alert', 'alert-success', 'alert-danger');
if (data.password && data.password !== data.password_confirm) {
messageDiv.textContent = 'Passwords do not match.';
messageDiv.classList.add('alert', 'alert-danger');
return;
}
try {
const response = await fetch('api.php?action=update_profile', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const result = await response.json();
if (result.success) {
messageDiv.textContent = 'Profile updated successfully!';
messageDiv.classList.add('alert', 'alert-success');
form.reset();
// Optionally, refresh user data on page if needed
} else {
messageDiv.textContent = 'Error: ' + result.error;
messageDiv.classList.add('alert', 'alert-danger');
}
} catch (error) {
messageDiv.textContent = 'An unexpected error occurred.';
messageDiv.classList.add('alert', 'alert-danger');
}
});
</script>
</body>
</html>