activity_groups.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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['groupName'] ?? '');
  10. $order = (int)($_POST['sortOrder'] ?? 0);
  11. if ($name !== '') {
  12. $pdo->prepare("INSERT INTO activity_groups (groupName, sortOrder) VALUES (?,?)")
  13. ->execute([$name, $order]);
  14. flash('success', "Group '$name' added.");
  15. } else {
  16. flash('error', 'Group name cannot be empty.');
  17. }
  18. }
  19. if ($action === 'edit') {
  20. $id = (int)$_POST['groupID'];
  21. $name = trim($_POST['groupName'] ?? '');
  22. $order = (int)($_POST['sortOrder'] ?? 0);
  23. if ($name !== '') {
  24. $pdo->prepare("UPDATE activity_groups SET groupName=?, sortOrder=? WHERE groupID=?")
  25. ->execute([$name, $order, $id]);
  26. flash('success', 'Group updated.');
  27. } else {
  28. flash('error', 'Group name cannot be empty.');
  29. }
  30. }
  31. if ($action === 'delete') {
  32. $id = (int)$_POST['groupID'];
  33. $pdo->prepare("DELETE FROM activity_groups WHERE groupID=?")->execute([$id]);
  34. flash('success', 'Group deleted.');
  35. }
  36. header('Location: activity_groups.php');
  37. exit;
  38. }
  39. $groups = $pdo->query("SELECT * FROM activity_groups ORDER BY sortOrder, groupID")->fetchAll();
  40. admin_head('Activity Groups', 'activity_groups.php');
  41. show_alerts();
  42. ?>
  43. <h1>Activity Groups</h1>
  44. <div class="card">
  45. <h2>Add New Group</h2>
  46. <form method="POST">
  47. <input type="hidden" name="action" value="add">
  48. <div class="form-row">
  49. <div>
  50. <label>Group Name</label>
  51. <input type="text" name="groupName" placeholder="e.g. Overnight" required>
  52. </div>
  53. <div>
  54. <label>Sort Order</label>
  55. <input type="number" name="sortOrder" value="0" min="0">
  56. </div>
  57. </div>
  58. <div style="display:flex;gap:.5rem;">
  59. <button type="submit" class="btn btn-primary">➕ Add Group</button>
  60. </div>
  61. </form>
  62. </div>
  63. <?php foreach ($groups as $g): ?>
  64. <form id="edit-form-<?= $g['groupID'] ?>" method="POST">
  65. <input type="hidden" name="action" value="edit">
  66. <input type="hidden" name="groupID" value="<?= $g['groupID'] ?>">
  67. </form>
  68. <?php endforeach; ?>
  69. <table class="tbl">
  70. <thead><tr><th>#</th><th>Name</th><th>Sort</th><th>Activities</th><th></th></tr></thead>
  71. <tbody>
  72. <?php foreach ($groups as $g):
  73. $cnt = $pdo->prepare("SELECT COUNT(*) FROM activities WHERE groupID=?");
  74. $cnt->execute([$g['groupID']]);
  75. $actCount = $cnt->fetchColumn();
  76. $id = $g['groupID'];
  77. ?>
  78. <tr id="view-<?= $id ?>">
  79. <td style="color:var(--muted)"><?= $id ?></td>
  80. <td><?= htmlspecialchars($g['groupName']) ?></td>
  81. <td><?= $g['sortOrder'] ?></td>
  82. <td><span class="badge badge-group"><?= $actCount ?></span></td>
  83. <td style="text-align:right;display:flex;gap:.4rem;justify-content:flex-end;">
  84. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  85. <form method="POST" onsubmit="return confirm('Delete this group and ALL its activities?')">
  86. <input type="hidden" name="action" value="delete">
  87. <input type="hidden" name="groupID" value="<?= $id ?>">
  88. <button type="submit" class="btn btn-sm btn-danger">Delete</button>
  89. </form>
  90. </td>
  91. </tr>
  92. <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
  93. <td style="color:var(--muted)"><?= $id ?></td>
  94. <td><input type="text" name="groupName" form="edit-form-<?= $id ?>" value="<?= htmlspecialchars($g['groupName']) ?>" required style="margin:0;width:100%;"></td>
  95. <td><input type="number" name="sortOrder" form="edit-form-<?= $id ?>" value="<?= (int)$g['sortOrder'] ?>" min="0" style="margin:0;width:100%;max-width:80px;"></td>
  96. <td><span class="badge badge-group"><?= $actCount ?></span></td>
  97. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  98. <button type="submit" form="edit-form-<?= $id ?>" class="btn btn-sm btn-primary">💾 Save</button>
  99. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  100. </td>
  101. </tr>
  102. <?php endforeach; ?>
  103. </tbody>
  104. </table>
  105. <script>
  106. function startEdit(id) {
  107. document.getElementById('view-' + id).style.display = 'none';
  108. document.getElementById('edit-' + id).style.display = '';
  109. document.querySelector('#edit-' + id + ' input[name="groupName"]').focus();
  110. }
  111. function cancelEdit(id) {
  112. document.getElementById('edit-' + id).style.display = 'none';
  113. document.getElementById('view-' + id).style.display = '';
  114. }
  115. </script>
  116. <?php admin_foot(); ?>