views(admin): add category and variant management pages

This commit is contained in:
dualfroz committed 2026-08-27 15:07:30 +02:00
1 parent 124da9db63
commit c200aa2996
3 files changed
+149

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
<?php
$editing = $category !== null && !empty($category['id']);
$action = $editing ? '/admin/categories/' . (int) $category['id'] : '/admin/categories';
$field = static fn (string $key) => $category[$key] ?? '';
?>
<div class="admin__head">
<div>
<p class="small muted"><a href="/admin/categories">Categories</a> / <?= $editing ? 'Edit' : 'New' ?></p>
<h1 style="margin:0"><?= $editing ? e($category['name']) : 'New category' ?></h1>
</div>
</div>
<form method="post" action="<?= e($action) ?>" style="max-width:600px">
<input type="hidden" name="csrfToken" value="<?= e($csrfToken) ?>">
<div class="panel panel--pad">
<div class="field <?= isset($errors['name']) ? 'field--error' : '' ?>">
<label for="name">Name</label>
<input class="input" type="text" id="name" name="name" value="<?= e((string) $field('name')) ?>" required>
<?php if (isset($errors['name'])): ?><span class="field__error"><?= e($errors['name']) ?></span><?php endif; ?>
</div>
<div class="field">
<label for="slug">Slug</label>
<input class="input" type="text" id="slug" name="slug" value="<?= e((string) $field('slug')) ?>" placeholder="auto-generated from name">
</div>
<div class="field">
<label for="description">Description</label>
<textarea class="textarea" id="description" name="description"><?= e((string) $field('description')) ?></textarea>
</div>
</div>
<div class="row mt-lg" style="gap:12px">
<button class="btn btn--primary" type="submit"><?= $editing ? 'Save changes' : 'Create category' ?></button>
<a class="btn" href="/admin/categories">Cancel</a>
</div>
</form>
+39
View File
@@ -0,0 +1,39 @@
<div class="admin__head">
<div>
<h1 style="margin:0">Categories</h1>
<p class="muted" style="margin:0"><?= count($categories) ?> categories.</p>
</div>
<a class="btn btn--primary" href="/admin/categories/create">New category</a>
</div>
<?php if (empty($categories)): ?>
<div class="empty">
<h3>No categories yet</h3>
<p class="muted">Add a category before creating products.</p>
<a class="btn btn--primary mt" href="/admin/categories/create">New category</a>
</div>
<?php else: ?>
<div class="table-wrap">
<table class="table">
<thead><tr><th>Name</th><th>Slug</th><th class="num">Products</th><th></th></tr></thead>
<tbody>
<?php foreach ($categories as $category): ?>
<tr>
<td style="font-weight:600"><?= e($category['name']) ?><div class="small muted"><?= e($category['description']) ?></div></td>
<td class="mono muted">/<?= e($category['slug']) ?></td>
<td class="num"><?= (int) $category['productCount'] ?></td>
<td>
<div class="row" style="gap:6px;justify-content:flex-end">
<a class="btn btn--sm" href="/admin/categories/<?= (int) $category['id'] ?>/edit">Edit</a>
<form method="post" action="/admin/categories/<?= (int) $category['id'] ?>/delete" onsubmit="return confirm('Delete this category?')">
<input type="hidden" name="csrfToken" value="<?= e($csrfToken) ?>">
<button class="btn btn--danger btn--sm" type="submit">Delete</button>
</form>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
+76
View File
@@ -0,0 +1,76 @@
<div class="admin__head">
<div>
<h1 style="margin:0">Variants</h1>
<p class="muted" style="margin:0"><?= e($product['name']) ?></p>
</div>
<a class="btn btn--ghost" href="/admin/products/<?= (int) $product['id'] ?>/edit">Edit product</a>
</div>
<?php if (empty($variants)): ?>
<div class="empty"><p class="muted" style="margin:0">No variants yet. Add one below.</p></div>
<?php else: ?>
<div class="table-wrap">
<table class="table">
<thead><tr><th>SKU</th><th>Options</th><th class="num">Price</th><th class="num">Stock</th><th>Status</th><th></th></tr></thead>
<tbody>
<?php foreach ($variants as $variant): ?>
<tr>
<td class="mono"><?= e($variant['sku']) ?></td>
<td><?= e($variant['label'] !== '' ? $variant['label'] : '-') ?></td>
<td class="num mono"><?= e(formatMoney((int) $variant['priceCents'], $currency)) ?></td>
<td class="num"><span class="badge badge--<?= (int) $variant['stockQuantity'] <= 0 ? 'out' : ((int) $variant['stockQuantity'] <= 5 ? 'low' : 'stock') ?>"><?= (int) $variant['stockQuantity'] ?></span></td>
<td><span class="badge badge--<?= (int) $variant['isActive'] === 1 ? 'stock' : 'out' ?>"><?= (int) $variant['isActive'] === 1 ? 'active' : 'off' ?></span></td>
<td>
<form method="post" action="/admin/variants/<?= (int) $variant['id'] ?>/delete" onsubmit="return confirm('Delete this variant?')">
<input type="hidden" name="csrfToken" value="<?= e($csrfToken) ?>">
<button class="btn btn--danger btn--sm" type="submit">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
<div class="panel panel--pad mt-lg" style="max-width:720px">
<h3 style="margin-top:0">Add a variant</h3>
<?php if (empty($attributes)): ?>
<p class="muted">Create at least one <a href="/admin/attributes">attribute</a> with values first.</p>
<?php else: ?>
<form method="post" action="/admin/products/<?= (int) $product['id'] ?>/variants">
<input type="hidden" name="csrfToken" value="<?= e($csrfToken) ?>">
<div class="row" style="gap:16px">
<label class="field" style="flex:1">
<span class="field__label">SKU (blank = auto)</span>
<input class="input" type="text" name="sku" placeholder="TEE-RED-M">
</label>
<label class="field" style="flex:1">
<span class="field__label">Price</span>
<input class="input" type="number" name="price" step="0.01" min="0" value="<?= number_format((int) $product['priceCents'] / 100, 2, '.', '') ?>" required>
</label>
<label class="field" style="flex:1">
<span class="field__label">Stock</span>
<input class="input" type="number" name="stockQuantity" min="0" value="0" required>
</label>
</div>
<div class="row" style="gap:16px">
<?php foreach ($attributes as $attribute): ?>
<?php if ($attribute['values'] === []) { continue; } ?>
<label class="field" style="flex:1">
<span class="field__label"><?= e($attribute['name']) ?></span>
<select class="select" name="option[<?= (int) $attribute['id'] ?>]">
<option value="">-</option>
<?php foreach ($attribute['values'] as $value): ?>
<option value="<?= (int) $value['id'] ?>"><?= e($value['value']) ?></option>
<?php endforeach; ?>
</select>
</label>
<?php endforeach; ?>
</div>
<div class="mt-lg">
<button class="btn btn--primary" type="submit">Add variant</button>
</div>
</form>
<?php endif; ?>
</div>