item_groups.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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['groupName'] ?? '');
  10. $order = (int)($_POST['sortOrder'] ?? 0);
  11. if ($name !== '') {
  12. $pdo->prepare("INSERT INTO item_groups (groupName, sortOrder) VALUES (?,?)")->execute([$name, $order]);
  13. flash('success', "Group '$name' added.");
  14. } else { flash('error', 'Name required.'); }
  15. }
  16. if ($action === 'edit') {
  17. $id = (int)$_POST['groupID'];
  18. $name = trim($_POST['groupName'] ?? '');
  19. $order = (int)($_POST['sortOrder'] ?? 0);
  20. if ($name !== '') {
  21. $pdo->prepare("UPDATE item_groups SET groupName=?,sortOrder=? WHERE groupID=?")->execute([$name,$order,$id]);
  22. flash('success', 'Group updated.');
  23. } else { flash('error', 'Name required.'); }
  24. }
  25. if ($action === 'delete') {
  26. $id = (int)$_POST['groupID'];
  27. $pdo->prepare("DELETE FROM item_groups WHERE groupID=?")->execute([$id]);
  28. flash('success', 'Group deleted.');
  29. }
  30. header('Location: item_groups.php'); exit;
  31. }
  32. $groups = $pdo->query("SELECT * FROM item_groups ORDER BY sortOrder, groupID")->fetchAll();
  33. admin_head('Item Groups', 'item_groups.php');
  34. show_alerts();
  35. ?>
  36. <h1>Item Groups</h1>
  37. <div class="card">
  38. <h2>Add New Group</h2>
  39. <form method="POST">
  40. <input type="hidden" name="action" value="add">
  41. <div class="form-row">
  42. <div>
  43. <label>Group Name</label>
  44. <input type="text" name="groupName" placeholder="e.g. Clothing" required>
  45. </div>
  46. <div>
  47. <label>Sort Order</label>
  48. <input type="number" name="sortOrder" value="0" min="0">
  49. </div>
  50. </div>
  51. <div style="display:flex;gap:.5rem;">
  52. <button type="submit" class="btn btn-primary">➕ Add Group</button>
  53. </div>
  54. </form>
  55. </div>
  56. <?php foreach ($groups as $g): ?>
  57. <form id="edit-form-<?= $g['groupID'] ?>" method="POST">
  58. <input type="hidden" name="action" value="edit">
  59. <input type="hidden" name="groupID" value="<?= $g['groupID'] ?>">
  60. </form>
  61. <?php endforeach; ?>
  62. <table class="tbl tbl-fixed">
  63. <colgroup>
  64. <col style="width:60px">
  65. <col>
  66. <col style="width:100px">
  67. <col style="width:100px">
  68. <col style="width:200px">
  69. </colgroup>
  70. <thead><tr><th>#</th><th>Name</th><th>Sort</th><th>Items</th><th></th></tr></thead>
  71. <tbody>
  72. <?php foreach ($groups as $g):
  73. $stmt = $pdo->prepare("SELECT COUNT(*) FROM items WHERE groupID=?");
  74. $stmt->execute([$g['groupID']]);
  75. $cnt = $stmt->fetchColumn();
  76. $id = $g['groupID'];
  77. ?>
  78. <tr id="row-<?= $id ?>" data-editing="false">
  79. <td style="color:var(--muted)"><?= $id ?></td>
  80. <td>
  81. <div class="view"><?= htmlspecialchars($g['groupName']) ?></div>
  82. <div class="edit"><input type="text" name="groupName" form="edit-form-<?= $id ?>" value="<?= htmlspecialchars($g['groupName']) ?>" required></div>
  83. </td>
  84. <td>
  85. <div class="view"><?= $g['sortOrder'] ?></div>
  86. <div class="edit"><input type="number" name="sortOrder" form="edit-form-<?= $id ?>" value="<?= (int)$g['sortOrder'] ?>" min="0"></div>
  87. </td>
  88. <td><span class="badge badge-group"><?= $cnt ?></span></td>
  89. <td>
  90. <div class="view actions-buttons">
  91. <button type="button" onclick="startEdit(<?= $id ?>)" class="btn btn-sm btn-teal">Edit</button>
  92. <form method="POST" onsubmit="return confirm('Delete group and ALL its items?')">
  93. <input type="hidden" name="action" value="delete">
  94. <input type="hidden" name="groupID" value="<?= $id ?>">
  95. <button class="btn btn-sm btn-danger">Delete</button>
  96. </form>
  97. </div>
  98. <div class="edit actions-buttons">
  99. <button type="submit" form="edit-form-<?= $id ?>" class="btn btn-sm btn-primary">💾 Save</button>
  100. <button type="button" onclick="cancelEdit(<?= $id ?>)" class="btn btn-sm btn-secondary">Cancel</button>
  101. </div>
  102. </td>
  103. </tr>
  104. <?php endforeach; ?>
  105. </tbody>
  106. </table>
  107. <script>
  108. function startEdit(id) {
  109. var row = document.getElementById('row-' + id);
  110. row.dataset.editing = 'true';
  111. var input = row.querySelector('.edit input[name="groupName"]');
  112. if (input) input.focus();
  113. }
  114. function cancelEdit(id) {
  115. document.getElementById('row-' + id).dataset.editing = 'false';
  116. }
  117. </script>
  118. <?php admin_foot(); ?>