| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- require_once __DIR__ . '/auth.php';
- require_once __DIR__ . '/layout.php';
- require_admin();
- $pdo = get_pdo();
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $action = $_POST['action'] ?? '';
- if ($action === 'add') {
- $name = trim($_POST['activityName'] ?? '');
- $gid = (int)$_POST['groupID'];
- $order = (int)($_POST['sortOrder'] ?? 0);
- if ($name !== '' && $gid > 0) {
- $pdo->prepare("INSERT INTO activities (groupID,activityName,sortOrder) VALUES (?,?,?)")
- ->execute([$gid, $name, $order]);
- flash('success', "Activity '$name' added.");
- } else {
- flash('error', 'Please fill all fields.');
- }
- }
- if ($action === 'edit') {
- $id = (int)$_POST['activityID'];
- $name = trim($_POST['activityName'] ?? '');
- $gid = (int)$_POST['groupID'];
- $order = (int)($_POST['sortOrder'] ?? 0);
- if ($name !== '') {
- $pdo->prepare("UPDATE activities SET activityName=?,groupID=?,sortOrder=? WHERE activityID=?")
- ->execute([$name, $gid, $order, $id]);
- flash('success', 'Activity updated.');
- } else {
- flash('error', 'Name cannot be empty.');
- }
- }
- if ($action === 'delete') {
- $id = (int)$_POST['activityID'];
- $pdo->prepare("DELETE FROM activities WHERE activityID=?")->execute([$id]);
- flash('success', 'Activity deleted.');
- }
- header('Location: activities.php');
- exit;
- }
- $groups = $pdo->query("SELECT * FROM activity_groups ORDER BY sortOrder,groupID")->fetchAll();
- $activities = $pdo->query("
- SELECT a.*, ag.groupName
- FROM activities a
- JOIN activity_groups ag ON ag.groupID = a.groupID
- ORDER BY ag.sortOrder, a.sortOrder, a.activityID
- ")->fetchAll();
- admin_head('Activities', 'activities.php');
- show_alerts();
- ?>
- <h1>Activities</h1>
- <div class="card">
- <h2>Add New Activity</h2>
- <form method="POST">
- <input type="hidden" name="action" value="add">
- <div class="form-row">
- <div>
- <label>Activity Name</label>
- <input type="text" name="activityName" placeholder="e.g. Kayaking" required>
- </div>
- <div>
- <label>Group</label>
- <select name="groupID" required>
- <option value="">– select –</option>
- <?php foreach ($groups as $g): ?>
- <option value="<?= $g['groupID'] ?>"><?= htmlspecialchars($g['groupName']) ?></option>
- <?php endforeach; ?>
- </select>
- </div>
- </div>
- <div>
- <label>Sort Order</label>
- <input type="number" name="sortOrder" value="0" min="0" style="max-width:120px;">
- </div>
- <div style="display:flex;gap:.5rem;margin-top:.25rem;">
- <button type="submit" class="btn btn-primary">➕ Add</button>
- </div>
- </form>
- </div>
- <?php foreach ($activities as $a): ?>
- <form id="edit-form-<?= $a['activityID'] ?>" method="POST">
- <input type="hidden" name="action" value="edit">
- <input type="hidden" name="activityID" value="<?= $a['activityID'] ?>">
- </form>
- <?php endforeach; ?>
- <table class="tbl">
- <thead><tr><th>#</th><th>Name</th><th>Group</th><th>Sort</th><th></th></tr></thead>
- <tbody>
- <?php foreach ($activities as $a):
- $id = $a['activityID'];
- ?>
- <tr id="view-<?= $id ?>">
- <td style="color:var(--muted)"><?= $id ?></td>
- <td><?= htmlspecialchars($a['activityName']) ?></td>
- <td><span class="badge badge-group"><?= htmlspecialchars($a['groupName']) ?></span></td>
- <td><?= $a['sortOrder'] ?></td>
- <td style="display:flex;gap:.4rem;justify-content:flex-end;">
- <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
- <form method="POST" onsubmit="return confirm('Delete this activity?')">
- <input type="hidden" name="action" value="delete">
- <input type="hidden" name="activityID" value="<?= $id ?>">
- <button class="btn btn-sm btn-danger">Delete</button>
- </form>
- </td>
- </tr>
- <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
- <td style="color:var(--muted)"><?= $id ?></td>
- <td><input type="text" name="activityName" form="edit-form-<?= $id ?>" value="<?= htmlspecialchars($a['activityName']) ?>" required style="margin:0;width:100%;"></td>
- <td>
- <select name="groupID" form="edit-form-<?= $id ?>" required style="margin:0;width:100%;">
- <?php foreach ($groups as $g): ?>
- <option value="<?= $g['groupID'] ?>" <?= $a['groupID'] == $g['groupID'] ? 'selected' : '' ?>><?= htmlspecialchars($g['groupName']) ?></option>
- <?php endforeach; ?>
- </select>
- </td>
- <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>
- <td style="display:flex;gap:.4rem;justify-content:flex-end;">
- <button type="submit" form="edit-form-<?= $id ?>" class="btn btn-sm btn-primary">💾 Save</button>
- <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <script>
- function startEdit(id) {
- document.getElementById('view-' + id).style.display = 'none';
- document.getElementById('edit-' + id).style.display = '';
- document.querySelector('#edit-' + id + ' input[name="activityName"]').focus();
- }
- function cancelEdit(id) {
- document.getElementById('edit-' + id).style.display = 'none';
- document.getElementById('view-' + id).style.display = '';
- }
- </script>
- <?php admin_foot(); ?>
|