This commit is contained in:
Flatlogic Bot 2025-10-25 19:05:03 +00:00
parent a2ff9876ac
commit 5cc3f02c65
5 changed files with 242 additions and 94 deletions

155
admin.php
View File

@ -1,122 +1,98 @@
<?php <?php
session_start(); session_start();
require_once 'db/config.php'; // Assuming you have a db connection setup
// Handle logout // Check if user is admin
if (isset($_GET['logout'])) {
session_unset();
session_destroy();
header('Location: login.php');
exit;
}
// If the user is not logged in as admin, redirect to the login page.
if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') { if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') {
// If not admin, redirect to login page
header('Location: login.php'); header('Location: login.php');
exit; exit;
} }
// --- Data Fetching --- // Check for messages
require_once 'db/config.php'; $message = '';
$attendees = []; if (isset($_SESSION['message'])) {
try { $message = $_SESSION['message'];
$pdo = db(); unset($_SESSION['message']);
// Updated to select first_name and new fields
$stmt = $pdo->query('SELECT a.id, w.title AS webinar_title, a.first_name, a.last_name, a.email, a.company, a.how_did_you_hear, a.consented, a.created_at, a.timezone, a.utm_source, a.utm_medium, a.utm_campaign, a.referrer, a.gclid, a.fbclid FROM attendees a JOIN webinars w ON a.webinar_id = w.id ORDER BY a.created_at DESC');
$attendees = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database: " . $e->getMessage());
} }
$pdo = db();
// Pagination settings
$records_per_page = 10;
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $records_per_page;
// Get total number of records
$total_stmt = $pdo->query("SELECT COUNT(*) FROM attendees");
$total_records = $total_stmt->fetchColumn();
$total_pages = ceil($total_records / $records_per_page);
// Get records for the current page
$stmt = $pdo->prepare("SELECT id, first_name, last_name, email, company, utm_source, created_at FROM attendees ORDER BY first_name ASC, last_name ASC LIMIT :limit OFFSET :offset");
$stmt->bindValue(':limit', $records_per_page, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$attendees = $stmt->fetchAll(PDO::FETCH_ASSOC);
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Admin Dashboard</title>
<title>Admin Board</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background-color: #121212;
color: #e0e0e0;
}
.container {
width: 100%;
max-width: 1400px;
padding: 2rem;
}
h1 {
color: #ffd700;
}
.table {
color: #e0e0e0;
}
.table-dark {
--bs-table-bg: #212529;
border-color: #373b3e;
}
.logout-btn {
position: absolute;
top: 1rem;
right: 1rem;
}
</style>
</head> </head>
<body> <body>
<div class="container"> <div class="container mt-5">
<a href="admin.php?logout=1" class="btn btn-secondary logout-btn">Logout</a> <h2>Admin Dashboard</h2>
<h1 class="mb-4 text-center">Webinar Attendees</h1> <p>Welcome, <?php echo htmlspecialchars($_SESSION['user'] ?? 'Admin'); ?>!</p>
<?php if ($message): ?>
<div class="alert alert-info">
<?php echo htmlspecialchars($message); ?>
</div>
<?php endif; ?>
<div class="d-flex justify-content-between align-items-center">
<h3 class="mt-4">Registered Attendees</h3>
<a href="export_csv.php" class="btn btn-success">Download CSV</a>
</div>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-dark table-striped table-hover"> <table class="table table-striped">
<thead> <thead>
<tr> <tr>
<th>ID</th> <th>ID</th>
<th>Webinar</th>
<th>First Name</th> <th>First Name</th>
<th>Last Name</th> <th>Last Name</th>
<th>Email</th> <th>Email</th>
<th>Company</th> <th>Company</th>
<th>How did you hear?</th>
<th>Consented</th>
<th>Registered At</th>
<th>Timezone</th>
<th>UTM Source</th> <th>UTM Source</th>
<th>UTM Medium</th> <th>Registered At</th>
<th>UTM Campaign</th>
<th>Referrer</th>
<th>GCLID</th>
<th>FBCLID</th>
<th>Actions</th> <th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php if (empty($attendees)): ?> <?php if (empty($attendees)): ?>
<tr> <tr>
<td colspan="17" class="text-center">No attendees yet.</td> <td colspan="8" class="text-center">No attendees found.</td>
</tr> </tr>
<?php else: ?> <?php else: ?>
<?php foreach ($attendees as $attendee): ?> <?php foreach ($attendees as $attendee): ?>
<tr> <tr>
<td><?= htmlspecialchars($attendee['id']) ?></td> <td><?php echo htmlspecialchars($attendee['id']); ?></td>
<td><?= htmlspecialchars($attendee['webinar_title']) ?></td> <td><?php echo htmlspecialchars($attendee['first_name']); ?></td>
<td><?= htmlspecialchars($attendee['first_name']) ?></td> <td><?php echo htmlspecialchars($attendee['last_name']); ?></td>
<td><?= htmlspecialchars($attendee['last_name']) ?></td> <td><?php echo htmlspecialchars($attendee['email']); ?></td>
<td><?= htmlspecialchars($attendee['email']) ?></td> <td><?php echo htmlspecialchars($attendee['company']); ?></td>
<td><?= htmlspecialchars($attendee['company']) ?></td> <td><?php echo htmlspecialchars($attendee['utm_source']); ?></td>
<td><?= htmlspecialchars($attendee['how_did_you_hear']) ?></td> <td><?php echo htmlspecialchars($attendee['created_at']); ?></td>
<td><?= $attendee['consented'] ? 'Yes' : 'No' ?></td>
<td><?= htmlspecialchars($attendee['created_at']) ?></td>
<td><?= htmlspecialchars($attendee['timezone']) ?></td>
<td><?= htmlspecialchars($attendee['utm_source']) ?></td>
<td><?= htmlspecialchars($attendee['utm_medium']) ?></td>
<td><?= htmlspecialchars($attendee['utm_campaign']) ?></td>
<td><?= htmlspecialchars($attendee['referrer']) ?></td>
<td><?= htmlspecialchars($attendee['gclid']) ?></td>
<td><?= htmlspecialchars($attendee['fbclid']) ?></td>
<td> <td>
<form action="delete_attendee.php" method="POST" onsubmit="return confirm('Are you sure you want to delete this attendee?');"> <a href="edit_attendee.php?id=<?php echo $attendee['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
<input type="hidden" name="id" value="<?= $attendee['id'] ?>"> <form action="delete_attendee.php" method="POST" style="display: inline-block;">
<button type="submit" class="btn btn-danger btn-sm">Delete</button> <input type="hidden" name="id" value="<?php echo $attendee['id']; ?>">
<button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Delete</button>
</form> </form>
</td> </td>
</tr> </tr>
@ -125,6 +101,23 @@ try {
</tbody> </tbody>
</table> </table>
</div> </div>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<?php if ($page > 1): ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $page - 1; ?>">Previous</a></li>
<?php endif; ?>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php if ($i == $page) echo 'active'; ?>"><a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
<?php if ($page < $total_pages): ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $page + 1; ?>">Next</a></li>
<?php endif; ?>
</ul>
</nav>
</div> </div>
</body> </body>
</html> </html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

View File

@ -1,26 +1,29 @@
<?php <?php
session_start(); session_start();
require_once 'db/config.php';
// If the user is not logged in as admin, do nothing.
if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') { if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') {
header('HTTP/1.1 403 Forbidden'); header('Location: login.php');
exit; exit;
} }
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {
require_once 'db/config.php'; $id = $_POST['id'];
try {
$pdo = db(); $pdo = db();
$stmt = $pdo->prepare('DELETE FROM attendees WHERE id = :id'); $stmt = $pdo->prepare("DELETE FROM attendees WHERE id = ?");
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$stmt->execute(); if ($stmt->execute([$id])) {
} catch (PDOException $e) { if ($stmt->rowCount() > 0) {
// In a real app, you'd log this error. $_SESSION['message'] = "Attendee with ID $id has been deleted successfully.";
// For this example, we'll just stop execution. } else {
die("Database error: " . $e->getMessage()); $_SESSION['message'] = "Error: No attendee found with ID $id. Nothing was deleted.";
} }
} else {
$_SESSION['message'] = "Error: Could not execute the delete statement.";
}
} else {
$_SESSION['message'] = "Error: Invalid request.";
} }
// Redirect back to the admin page
header('Location: admin.php'); header('Location: admin.php');
exit; exit;

115
edit_attendee.php Normal file
View File

@ -0,0 +1,115 @@
<?php
session_start();
require_once 'db/config.php';
// If the user is not logged in as admin, redirect to the login page.
if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') {
header('Location: login.php');
exit;
}
$attendee = null;
$message = '';
if (!isset($_GET['id']) && $_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: admin.php');
exit;
}
$id = $_GET['id'] ?? $_POST['id'];
try {
$pdo = db();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Update logic
$fields = ['first_name', 'last_name', 'email', 'company', 'how_did_you_hear', 'consented'];
$sql = 'UPDATE attendees SET ';
$params = [];
foreach ($fields as $field) {
if (isset($_POST[$field])) {
$sql .= "$field = ?, ";
$params[] = $_POST[$field];
}
}
$sql = rtrim($sql, ', ') . ' WHERE id = ?';
$params[] = $_POST['id'];
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
header('Location: admin.php');
exit;
}
// Fetch logic
$stmt = $pdo->prepare('SELECT * FROM attendees WHERE id = ?');
$stmt->execute([$id]);
$attendee = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$attendee) {
header('Location: admin.php');
exit;
}
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Attendee</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background-color: #121212;
color: #e0e0e0;
}
.container {
max-width: 600px;
padding-top: 2rem;
}
h1 {
color: #ffd700;
}
</style>
</head>
<body>
<div class="container">
<h1 class="mb-4">Edit Attendee #<?= htmlspecialchars($attendee['id']) ?></h1>
<form method="POST">
<input type="hidden" name="id" value="<?= htmlspecialchars($attendee['id']) ?>">
<div class="mb-3">
<label for="first_name" class="form-label">First Name</label>
<input type="text" class="form-control" id="first_name" name="first_name" value="<?= htmlspecialchars($attendee['first_name']) ?>">
</div>
<div class="mb-3">
<label for="last_name" class="form-label">Last Name</label>
<input type="text" class="form-control" id="last_name" name="last_name" value="<?= htmlspecialchars($attendee['last_name']) ?>">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" value="<?= htmlspecialchars($attendee['email']) ?>">
</div>
<div class="mb-3">
<label for="company" class="form-label">Company</label>
<input type="text" class="form-control" id="company" name="company" value="<?= htmlspecialchars($attendee['company']) ?>">
</div>
<div class="mb-3">
<label for="how_did_you_hear" class="form-label">How did you hear?</label>
<input type="text" class="form-control" id="how_did_you_hear" name="how_did_you_hear" value="<?= htmlspecialchars($attendee['how_did_you_hear']) ?>">
</div>
<div class="mb-3 form-check">
<input type="hidden" name="consented" value="0">
<input type="checkbox" class="form-check-input" id="consented" name="consented" value="1" <?= $attendee['consented'] ? 'checked' : '' ?>>
<label class="form-check-label" for="consented">Consented</label>
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
<a href="admin.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
</body>
</html>

37
export_csv.php Normal file
View File

@ -0,0 +1,37 @@
<?php
session_start();
require_once 'db/config.php';
// Check if user is admin
if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') {
http_response_code(403);
echo "Forbidden";
exit;
}
$pdo = db();
$stmt = $pdo->query("SELECT first_name, last_name, email FROM attendees ORDER BY created_at DESC");
$attendees = $stmt->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=attendees.csv');
$output = fopen('php://output', 'w');
// Add BOM to fix UTF-8 in Excel
fputs($output, "\xEF\xBB\xBF");
// Add header row
fputcsv($output, ['First Name', 'Last Name', 'Email']);
// Add data rows
foreach ($attendees as $attendee) {
fputcsv($output, [
$attendee['first_name'],
$attendee['last_name'],
$attendee['email']
]);
}
fclose($output);
exit;