items.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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['itemName'] ?? '');
  10. $gid = (int)$_POST['groupID'];
  11. $order = (int)($_POST['sortOrder'] ?? 0);
  12. if ($name !== '' && $gid > 0) {
  13. $pdo->prepare("INSERT INTO items (groupID,itemName,sortOrder) VALUES (?,?,?)")->execute([$gid,$name,$order]);
  14. flash('success', "Item '$name' added.");
  15. } else { flash('error', 'Please fill all fields.'); }
  16. }
  17. if ($action === 'edit') {
  18. $id = (int)$_POST['itemID'];
  19. $name = trim($_POST['itemName'] ?? '');
  20. $gid = (int)$_POST['groupID'];
  21. $order = (int)($_POST['sortOrder'] ?? 0);
  22. if ($name !== '') {
  23. $pdo->prepare("UPDATE items SET itemName=?,groupID=?,sortOrder=? WHERE itemID=?")->execute([$name,$gid,$order,$id]);
  24. flash('success', 'Item updated.');
  25. } else { flash('error', 'Name required.'); }
  26. }
  27. if ($action === 'delete') {
  28. $id = (int)$_POST['itemID'];
  29. $pdo->prepare("DELETE FROM items WHERE itemID=?")->execute([$id]);
  30. flash('success', 'Item deleted.');
  31. }
  32. header('Location: items.php'); exit;
  33. }
  34. $igroups = $pdo->query("SELECT * FROM item_groups ORDER BY sortOrder,groupID")->fetchAll();
  35. $items = $pdo->query("
  36. SELECT i.*, 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. $editing = null;
  42. if (isset($_GET['edit'])) {
  43. $stmt = $pdo->prepare("SELECT * FROM items WHERE itemID=?");
  44. $stmt->execute([(int)$_GET['edit']]);
  45. $editing = $stmt->fetch();
  46. }
  47. admin_head('Items', 'items.php');
  48. show_alerts();
  49. ?>
  50. <h1>Items</h1>
  51. <div class="card">
  52. <h2><?= $editing ? 'Edit Item' : 'Add New Item' ?></h2>
  53. <form method="POST">
  54. <input type="hidden" name="action" value="<?= $editing ? 'edit' : 'add' ?>">
  55. <?php if ($editing): ?>
  56. <input type="hidden" name="itemID" value="<?= $editing['itemID'] ?>">
  57. <?php endif; ?>
  58. <div class="form-row">
  59. <div>
  60. <label>Item Name</label>
  61. <input type="text" name="itemName"
  62. value="<?= htmlspecialchars($editing['itemName'] ?? '') ?>"
  63. placeholder="e.g. Rain jacket" required>
  64. </div>
  65. <div>
  66. <label>Group</label>
  67. <select name="groupID" required>
  68. <option value="">– select –</option>
  69. <?php foreach ($igroups as $g): ?>
  70. <option value="<?= $g['groupID'] ?>"
  71. <?= ($editing && $editing['groupID'] == $g['groupID']) ? 'selected' : '' ?>>
  72. <?= htmlspecialchars($g['groupName']) ?>
  73. </option>
  74. <?php endforeach; ?>
  75. </select>
  76. </div>
  77. </div>
  78. <div>
  79. <label>Sort Order</label>
  80. <input type="number" name="sortOrder" value="<?= (int)($editing['sortOrder'] ?? 0) ?>" min="0" style="max-width:120px;">
  81. </div>
  82. <div style="display:flex;gap:.5rem;margin-top:.25rem;">
  83. <button type="submit" class="btn btn-primary"><?= $editing ? '💾 Save' : '➕ Add' ?></button>
  84. <?php if ($editing): ?><a href="items.php" class="btn btn-secondary">Cancel</a><?php endif; ?>
  85. </div>
  86. </form>
  87. </div>
  88. <table class="tbl">
  89. <thead><tr><th>#</th><th>Name</th><th>Group</th><th>Sort</th><th></th></tr></thead>
  90. <tbody>
  91. <?php foreach ($items as $item): ?>
  92. <tr>
  93. <td style="color:var(--muted)"><?= $item['itemID'] ?></td>
  94. <td><?= htmlspecialchars($item['itemName']) ?></td>
  95. <td><span class="badge badge-group"><?= htmlspecialchars($item['groupName']) ?></span></td>
  96. <td><?= $item['sortOrder'] ?></td>
  97. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  98. <a href="?edit=<?= $item['itemID'] ?>" class="btn btn-sm btn-teal">Edit</a>
  99. <form method="POST" onsubmit="return confirm('Delete this item?')">
  100. <input type="hidden" name="action" value="delete">
  101. <input type="hidden" name="itemID" value="<?= $item['itemID'] ?>">
  102. <button class="btn btn-sm btn-danger">Delete</button>
  103. </form>
  104. </td>
  105. </tr>
  106. <?php endforeach; ?>
  107. </tbody>
  108. </table>
  109. <?php admin_foot(); ?>