activities.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. require_once __DIR__ . '/auth.php';
  3. require_once __DIR__ . '/layout.php';
  4. require_admin();
  5. $pdo = get_pdo();
  6. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  7. $action = $_POST['action'] ?? '';
  8. if ($action === 'add') {
  9. $name = trim($_POST['activityName'] ?? '');
  10. $gid = (int)$_POST['groupID'];
  11. $order = (int)($_POST['sortOrder'] ?? 0);
  12. if ($name !== '' && $gid > 0) {
  13. $pdo->prepare("INSERT INTO activities (groupID,activityName,sortOrder) VALUES (?,?,?)")
  14. ->execute([$gid, $name, $order]);
  15. flash('success', "Activity '$name' added.");
  16. } else {
  17. flash('error', 'Please fill all fields.');
  18. }
  19. }
  20. if ($action === 'edit') {
  21. $id = (int)$_POST['activityID'];
  22. $name = trim($_POST['activityName'] ?? '');
  23. $gid = (int)$_POST['groupID'];
  24. $order = (int)($_POST['sortOrder'] ?? 0);
  25. if ($name !== '') {
  26. $pdo->prepare("UPDATE activities SET activityName=?,groupID=?,sortOrder=? WHERE activityID=?")
  27. ->execute([$name, $gid, $order, $id]);
  28. flash('success', 'Activity updated.');
  29. } else {
  30. flash('error', 'Name cannot be empty.');
  31. }
  32. }
  33. if ($action === 'delete') {
  34. $id = (int)$_POST['activityID'];
  35. $pdo->prepare("DELETE FROM activities WHERE activityID=?")->execute([$id]);
  36. flash('success', 'Activity deleted.');
  37. }
  38. header('Location: activities.php');
  39. exit;
  40. }
  41. $groups = $pdo->query("SELECT * FROM activity_groups ORDER BY sortOrder,groupID")->fetchAll();
  42. $activities = $pdo->query("
  43. SELECT a.*, ag.groupName
  44. FROM activities a
  45. JOIN activity_groups ag ON ag.groupID = a.groupID
  46. ORDER BY ag.sortOrder, a.sortOrder, a.activityID
  47. ")->fetchAll();
  48. admin_head('Activities', 'activities.php');
  49. show_alerts();
  50. ?>
  51. <h1>Activities</h1>
  52. <div class="card">
  53. <h2>Add New Activity</h2>
  54. <form method="POST">
  55. <input type="hidden" name="action" value="add">
  56. <div class="form-row">
  57. <div>
  58. <label>Activity Name</label>
  59. <input type="text" name="activityName" placeholder="e.g. Kayaking" required>
  60. </div>
  61. <div>
  62. <label>Group</label>
  63. <select name="groupID" required>
  64. <option value="">– select –</option>
  65. <?php foreach ($groups as $g): ?>
  66. <option value="<?= $g['groupID'] ?>"><?= htmlspecialchars($g['groupName']) ?></option>
  67. <?php endforeach; ?>
  68. </select>
  69. </div>
  70. </div>
  71. <div>
  72. <label>Sort Order</label>
  73. <input type="number" name="sortOrder" value="0" min="0" style="max-width:120px;">
  74. </div>
  75. <div style="display:flex;gap:.5rem;margin-top:.25rem;">
  76. <button type="submit" class="btn btn-primary">➕ Add</button>
  77. </div>
  78. </form>
  79. </div>
  80. <table class="tbl">
  81. <thead><tr><th>#</th><th>Name</th><th>Group</th><th>Sort</th><th></th></tr></thead>
  82. <tbody>
  83. <?php foreach ($activities as $a):
  84. $id = $a['activityID'];
  85. ?>
  86. <tr id="view-<?= $id ?>">
  87. <td style="color:var(--muted)"><?= $id ?></td>
  88. <td><?= htmlspecialchars($a['activityName']) ?></td>
  89. <td><span class="badge badge-group"><?= htmlspecialchars($a['groupName']) ?></span></td>
  90. <td><?= $a['sortOrder'] ?></td>
  91. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  92. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  93. <form method="POST" onsubmit="return confirm('Delete this activity?')">
  94. <input type="hidden" name="action" value="delete">
  95. <input type="hidden" name="activityID" value="<?= $id ?>">
  96. <button class="btn btn-sm btn-danger">Delete</button>
  97. </form>
  98. </td>
  99. </tr>
  100. <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
  101. <td colspan="5">
  102. <form method="POST" style="display:flex;gap:.75rem;align-items:flex-end;flex-wrap:wrap;padding:.25rem 0;">
  103. <input type="hidden" name="action" value="edit">
  104. <input type="hidden" name="activityID" value="<?= $id ?>">
  105. <div>
  106. <label>Activity Name</label>
  107. <input type="text" name="activityName" value="<?= htmlspecialchars($a['activityName']) ?>" required style="margin-bottom:0;min-width:180px;">
  108. </div>
  109. <div>
  110. <label>Group</label>
  111. <select name="groupID" required style="margin-bottom:0;min-width:140px;">
  112. <?php foreach ($groups as $g): ?>
  113. <option value="<?= $g['groupID'] ?>" <?= $a['groupID'] == $g['groupID'] ? 'selected' : '' ?>>
  114. <?= htmlspecialchars($g['groupName']) ?>
  115. </option>
  116. <?php endforeach; ?>
  117. </select>
  118. </div>
  119. <div>
  120. <label>Sort Order</label>
  121. <input type="number" name="sortOrder" value="<?= (int)$a['sortOrder'] ?>" min="0" style="margin-bottom:0;max-width:90px;">
  122. </div>
  123. <div style="display:flex;gap:.4rem;padding-bottom:1px;">
  124. <button type="submit" class="btn btn-sm btn-primary">💾 Save</button>
  125. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  126. </div>
  127. </form>
  128. </td>
  129. </tr>
  130. <?php endforeach; ?>
  131. </tbody>
  132. </table>
  133. <script>
  134. function startEdit(id) {
  135. document.getElementById('view-' + id).style.display = 'none';
  136. document.getElementById('edit-' + id).style.display = '';
  137. document.querySelector('#edit-' + id + ' input[name="activityName"]').focus();
  138. }
  139. function cancelEdit(id) {
  140. document.getElementById('edit-' + id).style.display = 'none';
  141. document.getElementById('view-' + id).style.display = '';
  142. }
  143. </script>
  144. <?php admin_foot(); ?>