activities.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. <?php foreach ($activities as $a): ?>
  81. <form id="edit-form-<?= $a['activityID'] ?>" method="POST">
  82. <input type="hidden" name="action" value="edit">
  83. <input type="hidden" name="activityID" value="<?= $a['activityID'] ?>">
  84. </form>
  85. <?php endforeach; ?>
  86. <table class="tbl">
  87. <thead><tr><th>#</th><th>Name</th><th>Group</th><th>Sort</th><th></th></tr></thead>
  88. <tbody>
  89. <?php foreach ($activities as $a):
  90. $id = $a['activityID'];
  91. ?>
  92. <tr id="view-<?= $id ?>">
  93. <td style="color:var(--muted)"><?= $id ?></td>
  94. <td><?= htmlspecialchars($a['activityName']) ?></td>
  95. <td><span class="badge badge-group"><?= htmlspecialchars($a['groupName']) ?></span></td>
  96. <td><?= $a['sortOrder'] ?></td>
  97. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  98. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  99. <form method="POST" onsubmit="return confirm('Delete this activity?')">
  100. <input type="hidden" name="action" value="delete">
  101. <input type="hidden" name="activityID" value="<?= $id ?>">
  102. <button class="btn btn-sm btn-danger">Delete</button>
  103. </form>
  104. </td>
  105. </tr>
  106. <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
  107. <td style="color:var(--muted)"><?= $id ?></td>
  108. <td><input type="text" name="activityName" form="edit-form-<?= $id ?>" value="<?= htmlspecialchars($a['activityName']) ?>" required style="margin:0;width:100%;"></td>
  109. <td>
  110. <select name="groupID" form="edit-form-<?= $id ?>" required style="margin:0;width:100%;">
  111. <?php foreach ($groups as $g): ?>
  112. <option value="<?= $g['groupID'] ?>" <?= $a['groupID'] == $g['groupID'] ? 'selected' : '' ?>><?= htmlspecialchars($g['groupName']) ?></option>
  113. <?php endforeach; ?>
  114. </select>
  115. </td>
  116. <td><input type="number" name="sortOrder" form="edit-form-<?= $id ?>" value="<?= (int)$a['sortOrder'] ?>" min="0" style="margin:0;width:100%;max-width:80px;"></td>
  117. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  118. <button type="submit" form="edit-form-<?= $id ?>" class="btn btn-sm btn-primary">💾 Save</button>
  119. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  120. </td>
  121. </tr>
  122. <?php endforeach; ?>
  123. </tbody>
  124. </table>
  125. <script>
  126. function startEdit(id) {
  127. document.getElementById('view-' + id).style.display = 'none';
  128. document.getElementById('edit-' + id).style.display = '';
  129. document.querySelector('#edit-' + id + ' input[name="activityName"]').focus();
  130. }
  131. function cancelEdit(id) {
  132. document.getElementById('edit-' + id).style.display = 'none';
  133. document.getElementById('view-' + id).style.display = '';
  134. }
  135. </script>
  136. <?php admin_foot(); ?>