| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- require_once __DIR__ . '/auth.php';
- require_once __DIR__ . '/layout.php';
- require_admin();
- $pdo = get_pdo();
- // Handle add/remove mapping
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $action = $_POST['action'] ?? '';
- $aid = (int)($_POST['activityID'] ?? 0);
- $iid = (int)($_POST['itemID'] ?? 0);
- if ($action === 'add' && $aid > 0 && $iid > 0) {
- $qty = max(1, (int)($_POST['quantity'] ?? 1));
- try {
- $pdo->prepare("INSERT IGNORE INTO activity_item_map (activityID,itemID,quantity) VALUES (?,?,?)")
- ->execute([$aid, $iid, $qty]);
- flash('success', 'Mapping added.');
- } catch (Exception $e) {
- flash('error', 'Could not add mapping.');
- }
- }
- if ($action === 'update' && $aid > 0 && $iid > 0) {
- $qty = max(1, (int)($_POST['quantity'] ?? 1));
- $pdo->prepare("UPDATE activity_item_map SET quantity=? WHERE activityID=? AND itemID=?")
- ->execute([$qty, $aid, $iid]);
- flash('success', 'Quantity updated.');
- }
- if ($action === 'remove' && $aid > 0 && $iid > 0) {
- $pdo->prepare("DELETE FROM activity_item_map WHERE activityID=? AND itemID=?")
- ->execute([$aid, $iid]);
- flash('success', 'Mapping removed.');
- }
- header('Location: mappings.php' . (isset($_GET['aid']) ? '?aid=' . (int)$_GET['aid'] : ''));
- exit;
- }
- // Load all activities and items for selects
- $activities = $pdo->query("
- SELECT a.activityID, a.activityName, ag.groupName
- FROM activities a
- JOIN activity_groups ag ON ag.groupID = a.groupID
- ORDER BY ag.sortOrder, a.sortOrder, a.activityID
- ")->fetchAll();
- $allItems = $pdo->query("
- SELECT i.itemID, i.itemName, ig.groupName
- FROM items i
- JOIN item_groups ig ON ig.groupID = i.groupID
- ORDER BY ig.sortOrder, i.sortOrder, i.itemID
- ")->fetchAll();
- // Selected activity filter
- $selectedAid = isset($_GET['aid']) ? (int)$_GET['aid'] : 0;
- if (!$selectedAid && !empty($activities)) {
- $selectedAid = $activities[0]['activityID'];
- }
- // Get current mappings for selected activity
- $mappedItemIds = [];
- $mappedItems = [];
- if ($selectedAid) {
- $stmt = $pdo->prepare("
- SELECT i.itemID, i.itemName, ig.groupName, aim.quantity
- FROM activity_item_map aim
- JOIN items i ON i.itemID = aim.itemID
- JOIN item_groups ig ON ig.groupID = i.groupID
- WHERE aim.activityID = ?
- ORDER BY ig.sortOrder, i.sortOrder
- ");
- $stmt->execute([$selectedAid]);
- $mappedItems = $stmt->fetchAll();
- $mappedItemIds = array_column($mappedItems, 'itemID');
- }
- admin_head('Mappings', 'mappings.php');
- show_alerts();
- ?>
- <h1>Activity ↔ Item Mappings</h1>
- <div style="display:flex;gap:1.5rem;flex-wrap:wrap;align-items:flex-start;">
- <!-- Activity selector -->
- <div class="card" style="min-width:220px;flex:0 0 220px;">
- <h2>Select Activity</h2>
- <?php foreach ($activities as $a): ?>
- <a href="?aid=<?= $a['activityID'] ?>"
- style="display:block;padding:.45rem .6rem;border-radius:6px;margin-bottom:.25rem;font-size:.85rem;text-decoration:none;
- background:<?= $selectedAid==$a['activityID'] ? 'rgba(232,197,71,.1)' : 'transparent' ?>;
- color:<?= $selectedAid==$a['activityID'] ? 'var(--accent)' : 'var(--muted)' ?>;
- border:1px solid <?= $selectedAid==$a['activityID'] ? 'rgba(232,197,71,.3)' : 'transparent' ?>;">
- <?= htmlspecialchars($a['activityName']) ?>
- <span style="float:right;font-size:.72rem;color:var(--muted)"><?= htmlspecialchars($a['groupName']) ?></span>
- </a>
- <?php endforeach; ?>
- </div>
- <!-- Mapping manager -->
- <div style="flex:1;min-width:300px;">
- <?php if ($selectedAid):
- // Get the activity name
- $actName = '';
- foreach ($activities as $a) { if ($a['activityID'] == $selectedAid) $actName = $a['activityName']; }
- ?>
- <div class="card">
- <h2 style="margin-bottom:1rem">Items for: <span style="color:var(--accent)"><?= htmlspecialchars($actName) ?></span></h2>
- <!-- Add new mapping -->
- <form method="POST" style="display:flex;gap:.5rem;align-items:flex-end;margin-bottom:1.25rem;flex-wrap:wrap;">
- <input type="hidden" name="action" value="add">
- <input type="hidden" name="activityID" value="<?= $selectedAid ?>">
- <div style="flex:1;min-width:200px;">
- <label>Add Item</label>
- <select name="itemID" required style="margin-bottom:0;">
- <option value="">– choose item –</option>
- <?php
- // Group items in optgroups
- $optGroups = [];
- foreach ($allItems as $it) {
- if (in_array($it['itemID'], $mappedItemIds)) continue; // already mapped
- $optGroups[$it['groupName']][] = $it;
- }
- foreach ($optGroups as $grpName => $grpItems):
- ?>
- <optgroup label="<?= htmlspecialchars($grpName) ?>">
- <?php foreach ($grpItems as $it): ?>
- <option value="<?= $it['itemID'] ?>"><?= htmlspecialchars($it['itemName']) ?></option>
- <?php endforeach; ?>
- </optgroup>
- <?php endforeach; ?>
- </select>
- </div>
- <div style="width:90px;">
- <label>Qty</label>
- <input type="number" name="quantity" value="1" min="1" style="margin-bottom:0;width:100%;">
- </div>
- <button type="submit" class="btn btn-primary">➕ Add</button>
- </form>
- <!-- Current mapped items -->
- <?php if (empty($mappedItems)): ?>
- <p style="color:var(--muted);font-size:.85rem">No items mapped yet.</p>
- <?php else: ?>
- <table class="tbl">
- <thead><tr><th>#</th><th>Item</th><th>Group</th><th>Qty</th><th></th></tr></thead>
- <tbody>
- <?php foreach ($mappedItems as $it): ?>
- <tr>
- <td style="color:var(--muted)"><?= $it['itemID'] ?></td>
- <td><?= htmlspecialchars($it['itemName']) ?></td>
- <td><span class="badge badge-group"><?= htmlspecialchars($it['groupName']) ?></span></td>
- <td>
- <form method="POST" style="display:flex;gap:.3rem;align-items:center;">
- <input type="hidden" name="action" value="update">
- <input type="hidden" name="activityID" value="<?= $selectedAid ?>">
- <input type="hidden" name="itemID" value="<?= $it['itemID'] ?>">
- <input type="number" name="quantity" value="<?= (int)$it['quantity'] ?>" min="1" style="margin:0;width:70px;">
- <button class="btn btn-sm btn-primary">💾</button>
- </form>
- </td>
- <td style="text-align:right">
- <form method="POST" onsubmit="return confirm('Remove this mapping?')">
- <input type="hidden" name="action" value="remove">
- <input type="hidden" name="activityID" value="<?= $selectedAid ?>">
- <input type="hidden" name="itemID" value="<?= $it['itemID'] ?>">
- <button class="btn btn-sm btn-danger">Remove</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php endif; ?>
- </div>
- <?php endif; ?>
- </div>
- </div>
- <?php admin_foot(); ?>
|