admin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // admin.php - View submitted responses
  3. $form_id = $_GET['id'] ?? '';
  4. $token = $_GET['token'] ?? '';
  5. // Basic checks
  6. $form_file = __DIR__ . '/data/forms/' . preg_replace('/[^a-zA-Z0-9_-]/', '', $form_id) . '.json';
  7. if (empty($form_id) || empty($token) || !file_exists($form_file)) {
  8. die('<div style="font-family:sans-serif; text-align:center; padding:50px;"><h2>Access Denied</h2></div>');
  9. }
  10. $form_data = json_decode(file_get_contents($form_file), true);
  11. if ($token !== $form_data['admin_token']) {
  12. die('<div style="font-family:sans-serif; text-align:center; padding:50px;"><h2>Invalid Token</h2></div>');
  13. }
  14. $questions_map = [];
  15. foreach ($form_data['questions'] as $q) {
  16. // Keep it short for the table headers
  17. $questions_map[$q['id']] = mb_strimwidth($q['label'], 0, 30, "...");
  18. }
  19. // Read answers
  20. $answers_dir = __DIR__ . '/data/answers';
  21. $submissions = [];
  22. if (is_dir($answers_dir)) {
  23. $files = glob("{$answers_dir}/{$form_id}_*.json");
  24. foreach ($files as $f) {
  25. $data = json_decode(file_get_contents($f), true);
  26. if ($data) {
  27. $submissions[] = $data;
  28. }
  29. }
  30. }
  31. // Sort by submitted_at descending
  32. usort($submissions, function($a, $b) {
  33. return strtotime($b['submitted_at']) - strtotime($a['submitted_at']);
  34. });
  35. $total_responses = count($submissions);
  36. ?>
  37. <!DOCTYPE html>
  38. <html lang="en">
  39. <head>
  40. <meta charset="UTF-8">
  41. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  42. <title>Admin - <?= htmlspecialchars($form_data['title']) ?></title>
  43. <link rel="stylesheet" href="assets/css/style.css">
  44. </head>
  45. <body class="admin-page">
  46. <header class="site-header">
  47. <div class="container header-inner">
  48. <div class="brand">
  49. <span class="brand-title">Responses: <?= htmlspecialchars($form_data['title']) ?></span>
  50. </div>
  51. <nav class="site-nav">
  52. <a href="index.php" class="btn btn-secondary">Admin Home</a>
  53. </nav>
  54. </div>
  55. </header>
  56. <main class="container">
  57. <div class="admin-stats mt-2 mb-3" style="display:flex; gap:1rem; flex-wrap:wrap;">
  58. <div class="panel stat-card" style="flex:1; min-width:200px; text-align:center; margin-bottom:0;">
  59. <div style="font-size:0.9rem; color:var(--brand-muted);">Total Responses</div>
  60. <div class="stat-value" style="font-size:2rem; font-weight:700; color:var(--brand-accent);"><?= $total_responses ?></div>
  61. </div>
  62. <div class="panel stat-card" style="flex:2; min-width:300px; margin-bottom:0; display:flex; flex-direction:column; justify-content:center;">
  63. <div style="font-size:0.9rem; color:var(--brand-muted);">Public Answering Link</div>
  64. <div style="margin-top:0.5rem;"><a href="answer.php?id=<?= htmlspecialchars($form_id) ?>" target="_blank">answer.php?id=<?= htmlspecialchars($form_id) ?></a></div>
  65. </div>
  66. </div>
  67. <?php if ($total_responses > 0): ?>
  68. <div class="table-responsive">
  69. <table class="responsive-table">
  70. <thead>
  71. <tr>
  72. <th>Date</th>
  73. <th>Respondent</th>
  74. <?php foreach ($form_data['questions'] as $q): ?>
  75. <th><?= htmlspecialchars($questions_map[$q['id']]) ?></th>
  76. <?php endforeach; ?>
  77. </tr>
  78. </thead>
  79. <tbody>
  80. <?php foreach ($submissions as $sub): ?>
  81. <tr>
  82. <td data-label="Date"><?= date('Y-m-d H:i', strtotime($sub['submitted_at'])) ?></td>
  83. <td data-label="Respondent">
  84. <strong><?= htmlspecialchars($sub['respondent_name']) ?></strong><br>
  85. <span style="font-size:0.8rem; color:var(--brand-muted);"><?= htmlspecialchars($sub['respondent_email']) ?></span>
  86. </td>
  87. <?php foreach ($form_data['questions'] as $q): ?>
  88. <td data-label="<?= htmlspecialchars($questions_map[$q['id']]) ?>">
  89. <?php
  90. $val = $sub['answers'][$q['id']] ?? '-';
  91. $str_val = is_array($val) ? implode(', ', $val) : $val;
  92. echo nl2br(htmlspecialchars($str_val));
  93. ?>
  94. </td>
  95. <?php endforeach; ?>
  96. </tr>
  97. <?php endforeach; ?>
  98. </tbody>
  99. </table>
  100. </div>
  101. <?php else: ?>
  102. <div class="panel text-center">
  103. <h3 style="color:var(--brand-muted);">No responses yet.</h3>
  104. <p>Share the public link to start collecting data.</p>
  105. </div>
  106. <?php endif; ?>
  107. </main>
  108. </body>
  109. </html>