| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- require_once __DIR__ . '/auth.php';
- require_once __DIR__ . '/layout.php';
- require_admin();
- $pdo = get_pdo();
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $action = $_POST['action'] ?? '';
- if ($action === 'add') {
- $name = trim($_POST['itemName'] ?? '');
- $gid = (int)$_POST['groupID'];
- $order = (int)($_POST['sortOrder'] ?? 0);
- if ($name !== '' && $gid > 0) {
- $pdo->prepare("INSERT INTO items (groupID,itemName,sortOrder) VALUES (?,?,?)")->execute([$gid,$name,$order]);
- flash('success', "Item '$name' added.");
- } else { flash('error', 'Please fill all fields.'); }
- }
- if ($action === 'edit') {
- $id = (int)$_POST['itemID'];
- $name = trim($_POST['itemName'] ?? '');
- $gid = (int)$_POST['groupID'];
- $order = (int)($_POST['sortOrder'] ?? 0);
- if ($name !== '') {
- $pdo->prepare("UPDATE items SET itemName=?,groupID=?,sortOrder=? WHERE itemID=?")->execute([$name,$gid,$order,$id]);
- flash('success', 'Item updated.');
- } else { flash('error', 'Name required.'); }
- }
- if ($action === 'delete') {
- $id = (int)$_POST['itemID'];
- $pdo->prepare("DELETE FROM items WHERE itemID=?")->execute([$id]);
- flash('success', 'Item deleted.');
- }
- header('Location: items.php'); exit;
- }
- $igroups = $pdo->query("SELECT * FROM item_groups ORDER BY sortOrder,groupID")->fetchAll();
- $items = $pdo->query("
- SELECT i.*, ig.groupName
- FROM items i
- JOIN item_groups ig ON ig.groupID = i.groupID
- ORDER BY ig.sortOrder, i.sortOrder, i.itemID
- ")->fetchAll();
- $editing = null;
- if (isset($_GET['edit'])) {
- $stmt = $pdo->prepare("SELECT * FROM items WHERE itemID=?");
- $stmt->execute([(int)$_GET['edit']]);
- $editing = $stmt->fetch();
- }
- admin_head('Items', 'items.php');
- show_alerts();
- ?>
- <h1>Items</h1>
- <div class="card">
- <h2><?= $editing ? 'Edit Item' : 'Add New Item' ?></h2>
- <form method="POST">
- <input type="hidden" name="action" value="<?= $editing ? 'edit' : 'add' ?>">
- <?php if ($editing): ?>
- <input type="hidden" name="itemID" value="<?= $editing['itemID'] ?>">
- <?php endif; ?>
- <div class="form-row">
- <div>
- <label>Item Name</label>
- <input type="text" name="itemName"
- value="<?= htmlspecialchars($editing['itemName'] ?? '') ?>"
- placeholder="e.g. Rain jacket" required>
- </div>
- <div>
- <label>Group</label>
- <select name="groupID" required>
- <option value="">– select –</option>
- <?php foreach ($igroups as $g): ?>
- <option value="<?= $g['groupID'] ?>"
- <?= ($editing && $editing['groupID'] == $g['groupID']) ? 'selected' : '' ?>>
- <?= htmlspecialchars($g['groupName']) ?>
- </option>
- <?php endforeach; ?>
- </select>
- </div>
- </div>
- <div>
- <label>Sort Order</label>
- <input type="number" name="sortOrder" value="<?= (int)($editing['sortOrder'] ?? 0) ?>" min="0" style="max-width:120px;">
- </div>
- <div style="display:flex;gap:.5rem;margin-top:.25rem;">
- <button type="submit" class="btn btn-primary"><?= $editing ? '💾 Save' : '➕ Add' ?></button>
- <?php if ($editing): ?><a href="items.php" class="btn btn-secondary">Cancel</a><?php endif; ?>
- </div>
- </form>
- </div>
- <table class="tbl">
- <thead><tr><th>#</th><th>Name</th><th>Group</th><th>Sort</th><th></th></tr></thead>
- <tbody>
- <?php foreach ($items as $item): ?>
- <tr>
- <td style="color:var(--muted)"><?= $item['itemID'] ?></td>
- <td><?= htmlspecialchars($item['itemName']) ?></td>
- <td><span class="badge badge-group"><?= htmlspecialchars($item['groupName']) ?></span></td>
- <td><?= $item['sortOrder'] ?></td>
- <td style="display:flex;gap:.4rem;justify-content:flex-end;">
- <a href="?edit=<?= $item['itemID'] ?>" class="btn btn-sm btn-teal">Edit</a>
- <form method="POST" onsubmit="return confirm('Delete this item?')">
- <input type="hidden" name="action" value="delete">
- <input type="hidden" name="itemID" value="<?= $item['itemID'] ?>">
- <button class="btn btn-sm btn-danger">Delete</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php admin_foot(); ?>
|