item_groups.php 4.5 KB

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