activity_groups.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. <table class="tbl">
  64. <thead><tr><th>#</th><th>Name</th><th>Sort</th><th>Activities</th><th></th></tr></thead>
  65. <tbody>
  66. <?php foreach ($groups as $g):
  67. $cnt = $pdo->prepare("SELECT COUNT(*) FROM activities WHERE groupID=?");
  68. $cnt->execute([$g['groupID']]);
  69. $actCount = $cnt->fetchColumn();
  70. $id = $g['groupID'];
  71. ?>
  72. <tr id="view-<?= $id ?>">
  73. <td style="color:var(--muted)"><?= $id ?></td>
  74. <td><?= htmlspecialchars($g['groupName']) ?></td>
  75. <td><?= $g['sortOrder'] ?></td>
  76. <td><span class="badge badge-group"><?= $actCount ?></span></td>
  77. <td style="text-align:right;display:flex;gap:.4rem;justify-content:flex-end;">
  78. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  79. <form method="POST" onsubmit="return confirm('Delete this group and ALL its activities?')">
  80. <input type="hidden" name="action" value="delete">
  81. <input type="hidden" name="groupID" value="<?= $id ?>">
  82. <button type="submit" class="btn btn-sm btn-danger">Delete</button>
  83. </form>
  84. </td>
  85. </tr>
  86. <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
  87. <td colspan="5">
  88. <form method="POST" style="display:flex;gap:.75rem;align-items:flex-end;flex-wrap:wrap;padding:.25rem 0;">
  89. <input type="hidden" name="action" value="edit">
  90. <input type="hidden" name="groupID" value="<?= $id ?>">
  91. <div>
  92. <label>Group Name</label>
  93. <input type="text" name="groupName" value="<?= htmlspecialchars($g['groupName']) ?>" required style="margin-bottom:0;min-width:180px;">
  94. </div>
  95. <div>
  96. <label>Sort Order</label>
  97. <input type="number" name="sortOrder" value="<?= (int)$g['sortOrder'] ?>" min="0" style="margin-bottom:0;max-width:100px;">
  98. </div>
  99. <div style="display:flex;gap:.4rem;padding-bottom:1px;">
  100. <button type="submit" class="btn btn-sm btn-primary">💾 Save</button>
  101. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  102. </div>
  103. </form>
  104. </td>
  105. </tr>
  106. <?php endforeach; ?>
  107. </tbody>
  108. </table>
  109. <script>
  110. function startEdit(id) {
  111. document.getElementById('view-' + id).style.display = 'none';
  112. document.getElementById('edit-' + id).style.display = '';
  113. document.querySelector('#edit-' + id + ' input[name="groupName"]').focus();
  114. }
  115. function cancelEdit(id) {
  116. document.getElementById('edit-' + id).style.display = 'none';
  117. document.getElementById('view-' + id).style.display = '';
  118. }
  119. </script>
  120. <?php admin_foot(); ?>