| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- // admin.php - View submitted responses
- $form_id = $_GET['id'] ?? '';
- $token = $_GET['token'] ?? '';
- // Basic checks
- $form_file = __DIR__ . '/data/forms/' . preg_replace('/[^a-zA-Z0-9_-]/', '', $form_id) . '.json';
- if (empty($form_id) || empty($token) || !file_exists($form_file)) {
- die('<div style="font-family:sans-serif; text-align:center; padding:50px;"><h2>Access Denied</h2></div>');
- }
- $form_data = json_decode(file_get_contents($form_file), true);
- if ($token !== $form_data['admin_token']) {
- die('<div style="font-family:sans-serif; text-align:center; padding:50px;"><h2>Invalid Token</h2></div>');
- }
- $questions_map = [];
- foreach ($form_data['questions'] as $q) {
- // Keep it short for the table headers
- $questions_map[$q['id']] = mb_strimwidth($q['label'], 0, 30, "...");
- }
- // Read answers
- $answers_dir = __DIR__ . '/data/answers';
- $submissions = [];
- if (is_dir($answers_dir)) {
- $files = glob("{$answers_dir}/{$form_id}_*.json");
- foreach ($files as $f) {
- $data = json_decode(file_get_contents($f), true);
- if ($data) {
- $submissions[] = $data;
- }
- }
- }
- // Sort by submitted_at descending
- usort($submissions, function($a, $b) {
- return strtotime($b['submitted_at']) - strtotime($a['submitted_at']);
- });
- $total_responses = count($submissions);
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Admin - <?= htmlspecialchars($form_data['title']) ?></title>
- <link rel="stylesheet" href="assets/css/style.css">
- </head>
- <body class="admin-page">
- <header class="site-header">
- <div class="container header-inner">
- <div class="brand">
- <span class="brand-title">Responses: <?= htmlspecialchars($form_data['title']) ?></span>
- </div>
- <nav class="site-nav">
- <a href="index.php" class="btn btn-secondary">Admin Home</a>
- </nav>
- </div>
- </header>
- <main class="container">
-
- <div class="admin-stats mt-2 mb-3" style="display:flex; gap:1rem; flex-wrap:wrap;">
- <div class="panel stat-card" style="flex:1; min-width:200px; text-align:center; margin-bottom:0;">
- <div style="font-size:0.9rem; color:var(--brand-muted);">Total Responses</div>
- <div class="stat-value" style="font-size:2rem; font-weight:700; color:var(--brand-accent);"><?= $total_responses ?></div>
- </div>
- <div class="panel stat-card" style="flex:2; min-width:300px; margin-bottom:0; display:flex; flex-direction:column; justify-content:center;">
- <div style="font-size:0.9rem; color:var(--brand-muted);">Public Answering Link</div>
- <div style="margin-top:0.5rem;"><a href="answer.php?id=<?= htmlspecialchars($form_id) ?>" target="_blank">answer.php?id=<?= htmlspecialchars($form_id) ?></a></div>
- </div>
- </div>
- <?php if ($total_responses > 0): ?>
- <div class="table-responsive">
- <table class="responsive-table">
- <thead>
- <tr>
- <th>Date</th>
- <th>Respondent</th>
- <?php foreach ($form_data['questions'] as $q): ?>
- <th><?= htmlspecialchars($questions_map[$q['id']]) ?></th>
- <?php endforeach; ?>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($submissions as $sub): ?>
- <tr>
- <td data-label="Date"><?= date('Y-m-d H:i', strtotime($sub['submitted_at'])) ?></td>
- <td data-label="Respondent">
- <strong><?= htmlspecialchars($sub['respondent_name']) ?></strong><br>
- <span style="font-size:0.8rem; color:var(--brand-muted);"><?= htmlspecialchars($sub['respondent_email']) ?></span>
- </td>
- <?php foreach ($form_data['questions'] as $q): ?>
- <td data-label="<?= htmlspecialchars($questions_map[$q['id']]) ?>">
- <?php
- $val = $sub['answers'][$q['id']] ?? '-';
- $str_val = is_array($val) ? implode(', ', $val) : $val;
- echo nl2br(htmlspecialchars($str_val));
- ?>
- </td>
- <?php endforeach; ?>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- </div>
- <?php else: ?>
- <div class="panel text-center">
- <h3 style="color:var(--brand-muted);">No responses yet.</h3>
- <p>Share the public link to start collecting data.</p>
- </div>
- <?php endif; ?>
- </main>
- </body>
- </html>
|