mappings.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. require_once __DIR__ . '/auth.php';
  3. require_once __DIR__ . '/layout.php';
  4. require_admin();
  5. $pdo = get_pdo();
  6. // Handle add/remove mapping
  7. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  8. $action = $_POST['action'] ?? '';
  9. $aid = (int)($_POST['activityID'] ?? 0);
  10. $iid = (int)($_POST['itemID'] ?? 0);
  11. if ($action === 'add' && $aid > 0 && $iid > 0) {
  12. try {
  13. $pdo->prepare("INSERT IGNORE INTO activity_item_map (activityID,itemID) VALUES (?,?)")
  14. ->execute([$aid, $iid]);
  15. flash('success', 'Mapping added.');
  16. } catch (Exception $e) {
  17. flash('error', 'Could not add mapping.');
  18. }
  19. }
  20. if ($action === 'remove' && $aid > 0 && $iid > 0) {
  21. $pdo->prepare("DELETE FROM activity_item_map WHERE activityID=? AND itemID=?")
  22. ->execute([$aid, $iid]);
  23. flash('success', 'Mapping removed.');
  24. }
  25. header('Location: mappings.php' . (isset($_GET['aid']) ? '?aid=' . (int)$_GET['aid'] : ''));
  26. exit;
  27. }
  28. // Load all activities and items for selects
  29. $activities = $pdo->query("
  30. SELECT a.activityID, a.activityName, ag.groupName
  31. FROM activities a
  32. JOIN activity_groups ag ON ag.groupID = a.groupID
  33. ORDER BY ag.sortOrder, a.sortOrder, a.activityID
  34. ")->fetchAll();
  35. $allItems = $pdo->query("
  36. SELECT i.itemID, i.itemName, ig.groupName
  37. FROM items i
  38. JOIN item_groups ig ON ig.groupID = i.groupID
  39. ORDER BY ig.sortOrder, i.sortOrder, i.itemID
  40. ")->fetchAll();
  41. // Selected activity filter
  42. $selectedAid = isset($_GET['aid']) ? (int)$_GET['aid'] : 0;
  43. if (!$selectedAid && !empty($activities)) {
  44. $selectedAid = $activities[0]['activityID'];
  45. }
  46. // Get current mappings for selected activity
  47. $mappedItemIds = [];
  48. $mappedItems = [];
  49. if ($selectedAid) {
  50. $stmt = $pdo->prepare("
  51. SELECT i.itemID, i.itemName, ig.groupName
  52. FROM activity_item_map aim
  53. JOIN items i ON i.itemID = aim.itemID
  54. JOIN item_groups ig ON ig.groupID = i.groupID
  55. WHERE aim.activityID = ?
  56. ORDER BY ig.sortOrder, i.sortOrder
  57. ");
  58. $stmt->execute([$selectedAid]);
  59. $mappedItems = $stmt->fetchAll();
  60. $mappedItemIds = array_column($mappedItems, 'itemID');
  61. }
  62. admin_head('Mappings', 'mappings.php');
  63. show_alerts();
  64. ?>
  65. <h1>Activity ↔ Item Mappings</h1>
  66. <div style="display:flex;gap:1.5rem;flex-wrap:wrap;align-items:flex-start;">
  67. <!-- Activity selector -->
  68. <div class="card" style="min-width:220px;flex:0 0 220px;">
  69. <h2>Select Activity</h2>
  70. <?php foreach ($activities as $a): ?>
  71. <a href="?aid=<?= $a['activityID'] ?>"
  72. style="display:block;padding:.45rem .6rem;border-radius:6px;margin-bottom:.25rem;font-size:.85rem;text-decoration:none;
  73. background:<?= $selectedAid==$a['activityID'] ? 'rgba(232,197,71,.1)' : 'transparent' ?>;
  74. color:<?= $selectedAid==$a['activityID'] ? 'var(--accent)' : 'var(--muted)' ?>;
  75. border:1px solid <?= $selectedAid==$a['activityID'] ? 'rgba(232,197,71,.3)' : 'transparent' ?>;">
  76. <?= htmlspecialchars($a['activityName']) ?>
  77. <span style="float:right;font-size:.72rem;color:var(--muted)"><?= htmlspecialchars($a['groupName']) ?></span>
  78. </a>
  79. <?php endforeach; ?>
  80. </div>
  81. <!-- Mapping manager -->
  82. <div style="flex:1;min-width:300px;">
  83. <?php if ($selectedAid):
  84. // Get the activity name
  85. $actName = '';
  86. foreach ($activities as $a) { if ($a['activityID'] == $selectedAid) $actName = $a['activityName']; }
  87. ?>
  88. <div class="card">
  89. <h2 style="margin-bottom:1rem">Items for: <span style="color:var(--accent)"><?= htmlspecialchars($actName) ?></span></h2>
  90. <!-- Add new mapping -->
  91. <form method="POST" style="display:flex;gap:.5rem;align-items:flex-end;margin-bottom:1.25rem;flex-wrap:wrap;">
  92. <input type="hidden" name="action" value="add">
  93. <input type="hidden" name="activityID" value="<?= $selectedAid ?>">
  94. <div style="flex:1;min-width:200px;">
  95. <label>Add Item</label>
  96. <select name="itemID" required style="margin-bottom:0;">
  97. <option value="">– choose item –</option>
  98. <?php
  99. // Group items in optgroups
  100. $optGroups = [];
  101. foreach ($allItems as $it) {
  102. if (in_array($it['itemID'], $mappedItemIds)) continue; // already mapped
  103. $optGroups[$it['groupName']][] = $it;
  104. }
  105. foreach ($optGroups as $grpName => $grpItems):
  106. ?>
  107. <optgroup label="<?= htmlspecialchars($grpName) ?>">
  108. <?php foreach ($grpItems as $it): ?>
  109. <option value="<?= $it['itemID'] ?>"><?= htmlspecialchars($it['itemName']) ?></option>
  110. <?php endforeach; ?>
  111. </optgroup>
  112. <?php endforeach; ?>
  113. </select>
  114. </div>
  115. <button type="submit" class="btn btn-primary">➕ Add</button>
  116. </form>
  117. <!-- Current mapped items -->
  118. <?php if (empty($mappedItems)): ?>
  119. <p style="color:var(--muted);font-size:.85rem">No items mapped yet.</p>
  120. <?php else: ?>
  121. <table class="tbl">
  122. <thead><tr><th>#</th><th>Item</th><th>Group</th><th></th></tr></thead>
  123. <tbody>
  124. <?php foreach ($mappedItems as $it): ?>
  125. <tr>
  126. <td style="color:var(--muted)"><?= $it['itemID'] ?></td>
  127. <td><?= htmlspecialchars($it['itemName']) ?></td>
  128. <td><span class="badge badge-group"><?= htmlspecialchars($it['groupName']) ?></span></td>
  129. <td style="text-align:right">
  130. <form method="POST" onsubmit="return confirm('Remove this mapping?')">
  131. <input type="hidden" name="action" value="remove">
  132. <input type="hidden" name="activityID" value="<?= $selectedAid ?>">
  133. <input type="hidden" name="itemID" value="<?= $it['itemID'] ?>">
  134. <button class="btn btn-sm btn-danger">Remove</button>
  135. </form>
  136. </td>
  137. </tr>
  138. <?php endforeach; ?>
  139. </tbody>
  140. </table>
  141. <?php endif; ?>
  142. </div>
  143. <?php endif; ?>
  144. </div>
  145. </div>
  146. <?php admin_foot(); ?>