70 lines
3.7 KiB
PHP
70 lines
3.7 KiB
PHP
<?php
|
||
$adminTitle = 'Categories';
|
||
include __DIR__.'/admin_header.php';
|
||
|
||
if (isPost()) {
|
||
csrfCheck();
|
||
$act = p('_act');
|
||
if ($act === 'add') {
|
||
$name = ps('name'); $icon = ps('icon') ?: '🏢'; $sort = (int)p('sort_order',0);
|
||
if ($name) { qrun("INSERT OR IGNORE INTO categories(name,icon,sort_order)VALUES(?,?,?)",[$name,$icon,$sort]); flash('Category added!','success'); }
|
||
}
|
||
if ($act === 'edit') {
|
||
qrun("UPDATE categories SET name=?,icon=?,sort_order=? WHERE id=?",[ps('name'),ps('icon'),(int)p('sort_order'),(int)p('id')]);
|
||
flash('Category updated!','success');
|
||
}
|
||
if ($act === 'delete') {
|
||
$id = (int)p('id');
|
||
$cnt = (int)qval("SELECT COUNT(*) FROM businesses WHERE category_id=?",[$id]);
|
||
if ($cnt > 0) { flash("Cannot delete — $cnt businesses use this category.",'error'); }
|
||
else { qrun("DELETE FROM categories WHERE id=?",[$id]); flash('Category deleted.','success'); }
|
||
}
|
||
go('/admin/categories.php');
|
||
}
|
||
|
||
$editId = iget('edit');
|
||
$editing = $editId ? qone("SELECT * FROM categories WHERE id=?",[$editId]) : null;
|
||
$cats = qall("SELECT c.*,(SELECT COUNT(*) FROM businesses WHERE category_id=c.id) biz_count FROM categories c ORDER BY c.sort_order,c.name");
|
||
?>
|
||
<?php adminTitle('Manage Business Categories') ?>
|
||
|
||
<div class="ibox" style="border-color:var(--gold-d);margin-bottom:2rem;max-width:600px">
|
||
<div class="ibox-title"><?=$editing?'✏️ EDIT CATEGORY':'➕ ADD CATEGORY'?></div>
|
||
<form method="POST"><?=csrfField()?><input type="hidden" name="_act" value="<?=$editing?'edit':'add'?>">
|
||
<?php if($editing): ?><input type="hidden" name="id" value="<?=$editing['id']?>"><?php endif; ?>
|
||
<div class="form-row">
|
||
<div class="fg"><label class="flabel">NAME *</label><input type="text" name="name" class="finput" value="<?=e($editing['name']??'')?>" required placeholder="e.g. Dining & Food"></div>
|
||
<div class="fg"><label class="flabel">EMOJI ICON</label><input type="text" name="icon" class="finput" value="<?=e($editing['icon']??'🏢')?>" placeholder="🏢" style="font-size:1.2rem"></div>
|
||
</div>
|
||
<div class="fg"><label class="flabel">SORT ORDER</label><input type="number" name="sort_order" class="finput" value="<?=$editing['sort_order']??0?>" style="max-width:120px"></div>
|
||
<div class="flex-row">
|
||
<button type="submit" class="btn btn-primary btn-sm"><?=$editing?'Update':'Add Category'?></button>
|
||
<?php if($editing): ?><a href="/admin/categories.php" class="btn btn-secondary btn-sm">Cancel</a><?php endif; ?>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
|
||
<div class="admin-sec-title">ALL CATEGORIES (<?=count($cats)?>)</div>
|
||
<div class="tbl-wrap">
|
||
<table class="dtbl">
|
||
<thead><tr><th>ICON</th><th>NAME</th><th>BUSINESSES</th><th>SORT</th><th>ACTIONS</th></tr></thead>
|
||
<tbody>
|
||
<?php foreach($cats as $c): ?>
|
||
<tr>
|
||
<td style="font-size:1.4rem;text-align:center"><?=$c['icon']?></td>
|
||
<td class="td-name"><?=e($c['name'])?></td>
|
||
<td><span class="badge badge-blue"><?=$c['biz_count']?></span></td>
|
||
<td><?=$c['sort_order']?></td>
|
||
<td><div class="flex-row" style="gap:.3rem">
|
||
<a href="/admin/categories.php?edit=<?=$c['id']?>" class="btn btn-xs btn-outline">✏️ Edit</a>
|
||
<?php if(!$c['biz_count']): ?>
|
||
<form method="POST" style="display:inline"><?=csrfField()?><input type="hidden" name="_act" value="delete"><input type="hidden" name="id" value="<?=$c['id']?>"><button class="btn btn-xs btn-danger" data-confirm="Delete category?">🗑️</button></form>
|
||
<?php else: ?><span style="font-size:.75rem;color:var(--text-d)">In use</span><?php endif; ?>
|
||
</div></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<?php include __DIR__.'/admin_footer.php'; ?>
|