items.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. admin_head('Items', 'items.php');
  42. show_alerts();
  43. ?>
  44. <h1>Items</h1>
  45. <div class="card">
  46. <h2>Add New Item</h2>
  47. <form method="POST">
  48. <input type="hidden" name="action" value="add">
  49. <div class="form-row">
  50. <div>
  51. <label>Item Name</label>
  52. <input type="text" name="itemName" placeholder="e.g. Rain jacket" required>
  53. </div>
  54. <div>
  55. <label>Group</label>
  56. <select name="groupID" required>
  57. <option value="">– select –</option>
  58. <?php foreach ($igroups as $g): ?>
  59. <option value="<?= $g['groupID'] ?>"><?= htmlspecialchars($g['groupName']) ?></option>
  60. <?php endforeach; ?>
  61. </select>
  62. </div>
  63. </div>
  64. <div>
  65. <label>Sort Order</label>
  66. <input type="number" name="sortOrder" value="0" min="0" style="max-width:120px;">
  67. </div>
  68. <div style="display:flex;gap:.5rem;margin-top:.25rem;">
  69. <button type="submit" class="btn btn-primary">➕ Add</button>
  70. </div>
  71. </form>
  72. </div>
  73. <?php foreach ($items as $item): ?>
  74. <form id="edit-form-<?= $item['itemID'] ?>" method="POST">
  75. <input type="hidden" name="action" value="edit">
  76. <input type="hidden" name="itemID" value="<?= $item['itemID'] ?>">
  77. </form>
  78. <?php endforeach; ?>
  79. <table class="tbl">
  80. <thead><tr><th>#</th><th>Name</th><th>Group</th><th>Sort</th><th></th></tr></thead>
  81. <tbody>
  82. <?php foreach ($items as $item):
  83. $id = $item['itemID'];
  84. ?>
  85. <tr id="view-<?= $id ?>">
  86. <td style="color:var(--muted)"><?= $id ?></td>
  87. <td><?= htmlspecialchars($item['itemName']) ?></td>
  88. <td><span class="badge badge-group"><?= htmlspecialchars($item['groupName']) ?></span></td>
  89. <td><?= $item['sortOrder'] ?></td>
  90. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  91. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  92. <form method="POST" onsubmit="return confirm('Delete this item?')">
  93. <input type="hidden" name="action" value="delete">
  94. <input type="hidden" name="itemID" value="<?= $id ?>">
  95. <button class="btn btn-sm btn-danger">Delete</button>
  96. </form>
  97. </td>
  98. </tr>
  99. <tr id="edit-<?= $id ?>" style="display:none;background:rgba(79,209,197,.04);">
  100. <td style="color:var(--muted)"><?= $id ?></td>
  101. <td><input type="text" name="itemName" form="edit-form-<?= $id ?>" value="<?= htmlspecialchars($item['itemName']) ?>" required style="margin:0;width:100%;"></td>
  102. <td>
  103. <select name="groupID" form="edit-form-<?= $id ?>" required style="margin:0;width:100%;">
  104. <?php foreach ($igroups as $g): ?>
  105. <option value="<?= $g['groupID'] ?>" <?= $item['groupID'] == $g['groupID'] ? 'selected' : '' ?>><?= htmlspecialchars($g['groupName']) ?></option>
  106. <?php endforeach; ?>
  107. </select>
  108. </td>
  109. <td><input type="number" name="sortOrder" form="edit-form-<?= $id ?>" value="<?= (int)$item['sortOrder'] ?>" min="0" style="margin:0;width:100%;max-width:80px;"></td>
  110. <td style="display:flex;gap:.4rem;justify-content:flex-end;">
  111. <button type="submit" form="edit-form-<?= $id ?>" class="btn btn-sm btn-primary">💾 Save</button>
  112. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  113. </td>
  114. </tr>
  115. <?php endforeach; ?>
  116. </tbody>
  117. </table>
  118. <script>
  119. function startEdit(id) {
  120. document.getElementById('view-' + id).style.display = 'none';
  121. document.getElementById('edit-' + id).style.display = '';
  122. document.querySelector('#edit-' + id + ' input[name="itemName"]').focus();
  123. }
  124. function cancelEdit(id) {
  125. document.getElementById('edit-' + id).style.display = 'none';
  126. document.getElementById('view-' + id).style.display = '';
  127. }
  128. </script>
  129. <?php admin_foot(); ?>