34950-vm/admin.php
Flatlogic Bot ce9f0dfe67 dkdk
2025-11-05 20:50:07 +00:00

186 lines
7.5 KiB
PHP

<?php
session_start();
require_once 'db/config.php'; // Assuming you have a db connection setup
// Check if user is admin
if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') {
// If not admin, redirect to login page
header('Location: login.php');
exit;
}
// Check for messages
$message = '';
if (isset($_SESSION['message'])) {
$message = $_SESSION['message'];
unset($_SESSION['message']);
}
$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, created_at, how_did_you_hear, company 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);
// Get data for the chart
$chart_stmt = $pdo->query("SELECT DATE(created_at) as registration_day, COUNT(*) as user_count FROM attendees GROUP BY registration_day ORDER BY registration_day");
$chart_data = $chart_stmt->fetchAll(PDO::FETCH_ASSOC);
$chart_labels = json_encode(array_column($chart_data, 'registration_day'));
$chart_values = json_encode(array_column($chart_data, 'user_count'));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<div class="container mt-5">
<h2>Admin Dashboard</h2>
<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="mt-4 p-4 border rounded shadow-sm bg-light">
<div class="d-flex justify-content-between align-items-center mb-3">
<h3 class="h5 mb-0">Daily Registrations</h3>
<button id="downloadChartBtn" class="btn btn-sm btn-outline-primary">Save as Image</button>
</div>
<div style="max-width: 600px; margin: auto;">
<canvas id="registrationsChart"></canvas>
</div>
</div>
<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">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Source</th>
<th>Company</th>
<th>Registered At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($attendees)): ?>
<tr>
<td colspan="8" class="text-center">No attendees found.</td>
</tr>
<?php else: ?>
<?php foreach ($attendees as $attendee): ?>
<tr>
<td><?php echo htmlspecialchars($attendee['id']); ?></td>
<td><?php echo htmlspecialchars($attendee['first_name']); ?></td>
<td><?php echo htmlspecialchars($attendee['last_name']); ?></td>
<td><?php echo htmlspecialchars($attendee['email']); ?></td>
<td><?php echo htmlspecialchars($attendee['how_did_you_hear'] ?? ''); ?></td>
<td><?php echo htmlspecialchars($attendee['company'] ?? ''); ?></td>
<td><?php echo htmlspecialchars($attendee['created_at']); ?></td>
<td>
<a href="edit_attendee.php?id=<?php echo $attendee['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
<form action="delete_attendee.php" method="POST" style="display: inline-block;">
<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>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</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>
<script>
const ctx = document.getElementById('registrationsChart');
const registrationsChart = new Chart(ctx, {
type: 'line',
data: {
labels: <?php echo $chart_labels; ?>,
datasets: [{
label: 'Daily Registrations',
data: <?php echo $chart_values; ?>,
backgroundColor: 'rgba(0, 123, 255, 0.1)',
borderColor: 'rgba(0, 123, 255, 1)',
borderWidth: 2,
tension: 0.4,
fill: true
}]
},
options: {
scales: {
y: {
beginAtZero: true,
ticks: {
stepSize: 1
}
}
},
plugins: {
legend: {
display: false
},
title: {
display: true,
text: 'User Registrations per Day'
}
}
}
});
document.getElementById('downloadChartBtn').addEventListener('click', function() {
const link = document.createElement('a');
link.href = registrationsChart.toBase64Image();
link.download = 'daily-registrations-chart.png';
link.click();
});
</script>
</body>
</html>