- initial
This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
$adminTitle = 'Businesses';
|
||||
include __DIR__.'/admin_header.php';
|
||||
|
||||
// Handle POST actions
|
||||
if (isPost()) {
|
||||
csrfCheck();
|
||||
$act = p('_act');
|
||||
|
||||
if ($act === 'add' || $act === 'edit') {
|
||||
$id = (int)p('id');
|
||||
$name = ps('name');
|
||||
if (!$name) { flash('Business name is required.','error'); go('/admin/businesses.php'.($id?"?edit=$id":'')); }
|
||||
|
||||
$baseSlug = ps('slug') ?: makeSlug($name);
|
||||
|
||||
// Find a unique slug (excluding self on edit)
|
||||
$slug = $baseSlug; $i = 0;
|
||||
while (true) {
|
||||
$candidate = $baseSlug . ($i ? "-$i" : '');
|
||||
$clash = $act === 'edit'
|
||||
? qval("SELECT id FROM businesses WHERE slug=? AND id!=?", [$candidate, $id])
|
||||
: qval("SELECT id FROM businesses WHERE slug=?", [$candidate]);
|
||||
if (!$clash) { $slug = $candidate; break; }
|
||||
$i++;
|
||||
}
|
||||
|
||||
// Build hours JSON
|
||||
$days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
|
||||
$hrs = [];
|
||||
foreach ($days as $d) { $v = ps("hours_$d"); if ($v !== '') $hrs[$d] = $v; }
|
||||
|
||||
$fields = [
|
||||
'name' => $name,
|
||||
'slug' => $slug,
|
||||
'category_id' => (int)p('category_id'),
|
||||
'subcategory' => ps('subcategory'),
|
||||
'description' => ps('description'),
|
||||
'address' => ps('address'),
|
||||
'phone' => ps('phone'),
|
||||
'email' => ps('email'),
|
||||
'website' => ps('website'),
|
||||
'hours' => json_encode($hrs),
|
||||
'lat' => (float)p('lat', 39.4364),
|
||||
'lng' => (float)p('lng', -78.9762),
|
||||
'is_active' => (int)p('is_active', 1),
|
||||
'is_featured' => (int)p('is_featured', 0),
|
||||
];
|
||||
|
||||
if ($act === 'add') {
|
||||
qrun("INSERT INTO businesses(name,slug,category_id,subcategory,description,address,phone,email,website,hours,lat,lng,is_active,is_featured)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)", array_values($fields));
|
||||
flash('Business added!','success');
|
||||
go('/admin/businesses.php');
|
||||
} else {
|
||||
qrun("UPDATE businesses SET name=?,slug=?,category_id=?,subcategory=?,description=?,address=?,phone=?,email=?,website=?,hours=?,lat=?,lng=?,is_active=?,is_featured=? WHERE id=?", array_merge(array_values($fields), [$id]));
|
||||
flash('Business updated!','success');
|
||||
go('/admin/businesses.php?edit='.$id);
|
||||
}
|
||||
}
|
||||
|
||||
if ($act === 'delete') {
|
||||
$id = (int)p('id');
|
||||
qrun("DELETE FROM businesses WHERE id=?", [$id]);
|
||||
flash('Business deleted.','success');
|
||||
go('/admin/businesses.php');
|
||||
}
|
||||
|
||||
if ($act === 'toggle') {
|
||||
$id = (int)p('id');
|
||||
qrun("UPDATE businesses SET is_active = 1 - is_active WHERE id=?", [$id]);
|
||||
go('/admin/businesses.php');
|
||||
}
|
||||
}
|
||||
|
||||
$editId = iget('edit');
|
||||
$editing = $editId ? qone("SELECT * FROM businesses WHERE id=?", [$editId]) : null;
|
||||
$cats = qall("SELECT * FROM categories ORDER BY sort_order");
|
||||
$days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
|
||||
$h = $editing ? parseHours($editing['hours']) : [];
|
||||
|
||||
$q = gs('q');
|
||||
$page = max(1, iget('page'));
|
||||
$perPage = 25;
|
||||
$where = $q ? "WHERE b.name LIKE ? OR b.address LIKE ? OR b.subcategory LIKE ?" : "";
|
||||
$params = $q ? ["%$q%", "%$q%", "%$q%"] : [];
|
||||
$total = (int)qval("SELECT COUNT(*) FROM businesses b $where", $params);
|
||||
$pages = max(1, (int)ceil($total / $perPage));
|
||||
$page = min($page, $pages);
|
||||
$offset = ($page - 1) * $perPage;
|
||||
$bizs = qall("SELECT b.*,c.name cat FROM businesses b LEFT JOIN categories c ON c.id=b.category_id $where ORDER BY b.name LIMIT $perPage OFFSET $offset", $params);
|
||||
?>
|
||||
<?php adminTitle($editing ? 'Edit Business: '.e($editing['name']) : 'Manage Businesses') ?>
|
||||
|
||||
<!-- Add/Edit Form -->
|
||||
<div class="ibox" style="border-color:var(--gold-d);margin-bottom:2rem">
|
||||
<div class="ibox-title"><?= $editing ? '✏️ EDIT: '.strtoupper(e($editing['name'])) : '➕ ADD NEW BUSINESS' ?></div>
|
||||
<form method="POST" action="/admin/businesses.php<?= $editing ? '?edit='.$editing['id'] : '' ?>">
|
||||
<?=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">BUSINESS NAME *</label>
|
||||
<input type="text" name="name" class="finput" value="<?=e($editing['name']??'')?>" required>
|
||||
</div>
|
||||
<div class="fg">
|
||||
<label class="flabel">SLUG (auto-generated if blank)</label>
|
||||
<input type="text" name="slug" class="finput" value="<?=e($editing['slug']??'')?>" placeholder="auto-generated from name">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="fg">
|
||||
<label class="flabel">CATEGORY</label>
|
||||
<select name="category_id" class="fselect">
|
||||
<?php foreach ($cats as $c): ?>
|
||||
<option value="<?=$c['id']?>"<?= (int)($editing['category_id']??0) === (int)$c['id'] ? ' selected' : '' ?>><?=e($c['icon'].' '.$c['name'])?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="fg">
|
||||
<label class="flabel">SUBCATEGORY</label>
|
||||
<input type="text" name="subcategory" class="finput" value="<?=e($editing['subcategory']??'')?>" placeholder="e.g. Italian Restaurant">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="fg">
|
||||
<label class="flabel">DESCRIPTION</label>
|
||||
<textarea name="description" class="ftextarea" style="min-height:90px"><?=e($editing['description']??'')?></textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="fg">
|
||||
<label class="flabel">ADDRESS</label>
|
||||
<input type="text" name="address" class="finput" value="<?=e($editing['address']??'')?>">
|
||||
</div>
|
||||
<div class="fg">
|
||||
<label class="flabel">PHONE</label>
|
||||
<input type="text" name="phone" class="finput" value="<?=e($editing['phone']??'')?>">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="fg">
|
||||
<label class="flabel">EMAIL</label>
|
||||
<input type="email" name="email" class="finput" value="<?=e($editing['email']??'')?>">
|
||||
</div>
|
||||
<div class="fg">
|
||||
<label class="flabel">WEBSITE (no https://)</label>
|
||||
<input type="text" name="website" class="finput" value="<?=e($editing['website']??'')?>" placeholder="example.com">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="fg">
|
||||
<label class="flabel">LATITUDE</label>
|
||||
<input type="number" name="lat" class="finput" value="<?=e($editing['lat']??39.4364)?>" step="0.0001">
|
||||
</div>
|
||||
<div class="fg">
|
||||
<label class="flabel">LONGITUDE</label>
|
||||
<input type="number" name="lng" class="finput" value="<?=e($editing['lng']??-78.9762)?>" step="0.0001">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="margin-bottom:1rem">
|
||||
<label class="flabel" style="margin-bottom:.6rem">HOURS OF OPERATION</label>
|
||||
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:.5rem">
|
||||
<?php foreach ($days as $d): ?>
|
||||
<div style="display:flex;gap:.4rem;align-items:center">
|
||||
<span style="font-family:'Oswald',sans-serif;font-size:.72rem;color:var(--gold);width:28px;flex-shrink:0"><?=$d?></span>
|
||||
<input type="text" name="hours_<?=$d?>" class="finput" style="font-size:.8rem;padding:.4rem .6rem"
|
||||
value="<?=e($h[$d]??'')?>" placeholder="e.g. 9am-5pm">
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-row" style="margin-bottom:.75rem">
|
||||
<div class="fg">
|
||||
<label class="flabel">STATUS</label>
|
||||
<select name="is_active" class="fselect">
|
||||
<option value="1"<?= ($editing['is_active'] ?? 1) == 1 ? ' selected' : '' ?>>Active</option>
|
||||
<option value="0"<?= ($editing['is_active'] ?? 1) == 0 ? ' selected' : '' ?>>Inactive (hidden)</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="fg">
|
||||
<label class="flabel">FEATURED</label>
|
||||
<select name="is_featured" class="fselect">
|
||||
<option value="0"<?= ($editing['is_featured'] ?? 0) == 0 ? ' selected' : '' ?>>Not Featured</option>
|
||||
<option value="1"<?= ($editing['is_featured'] ?? 0) == 1 ? ' selected' : '' ?>>Featured</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex-row">
|
||||
<button type="submit" class="btn btn-primary"><?= $editing ? 'Update Business' : 'Add Business' ?></button>
|
||||
<?php if ($editing): ?>
|
||||
<a href="/admin/businesses.php" class="btn btn-secondary">Cancel / Add New</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- Businesses list -->
|
||||
<div class="admin-sec-title">ALL BUSINESSES (<?=$total?>)</div>
|
||||
<form method="GET" action="/admin/businesses.php" style="display:flex;gap:.5rem;margin-bottom:1rem">
|
||||
<input type="text" name="q" class="finput" style="max-width:300px" value="<?=e($q)?>" placeholder="Search businesses...">
|
||||
<button type="submit" class="btn btn-secondary btn-sm">Search</button>
|
||||
<?php if ($q): ?><a href="/admin/businesses.php" class="btn btn-outline btn-sm">Clear</a><?php endif; ?>
|
||||
</form>
|
||||
|
||||
<div class="tbl-wrap">
|
||||
<table class="dtbl">
|
||||
<thead>
|
||||
<tr><th>NAME</th><th>CATEGORY</th><th>PHONE</th><th>STATUS</th><th>FEATURED</th><th>ACTIONS</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($bizs as $b): ?>
|
||||
<tr<?= ($editing && $editing['id'] == $b['id']) ? ' style="background:rgba(201,168,76,.06)"' : '' ?>>
|
||||
<td><a href="/business.php?slug=<?=e($b['slug'])?>" class="td-name" target="_blank"><?=e($b['name'])?></a></td>
|
||||
<td><span style="font-size:.78rem;color:var(--text-d)"><?=e($b['cat']??'--')?></span></td>
|
||||
<td style="font-size:.8rem"><?=e($b['phone'])?></td>
|
||||
<td><?= $b['is_active'] ? '<span class="badge badge-green">Active</span>' : '<span class="badge badge-gray">Inactive</span>' ?></td>
|
||||
<td><?= $b['is_featured'] ? '<span class="badge badge-gold">Featured</span>' : '--' ?></td>
|
||||
<td>
|
||||
<div class="flex-row" style="gap:.3rem">
|
||||
<a href="/admin/businesses.php?edit=<?=$b['id']?>" class="btn btn-xs <?= ($editing && $editing['id']==$b['id']) ? 'btn-primary' : 'btn-outline' ?>">Edit</a>
|
||||
<form method="POST" action="/admin/businesses.php" style="display:inline">
|
||||
<?=csrfField()?>
|
||||
<input type="hidden" name="_act" value="toggle">
|
||||
<input type="hidden" name="id" value="<?=$b['id']?>">
|
||||
<button class="btn btn-xs btn-secondary"><?= $b['is_active'] ? 'Deactivate' : 'Activate' ?></button>
|
||||
</form>
|
||||
<form method="POST" action="/admin/businesses.php" style="display:inline">
|
||||
<?=csrfField()?>
|
||||
<input type="hidden" name="_act" value="delete">
|
||||
<input type="hidden" name="id" value="<?=$b['id']?>">
|
||||
<button class="btn btn-xs btn-danger" data-confirm="Permanently delete '<?=e($b['name'])?>' and all its data?">Delete</button>
|
||||
</form>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<?php if ($pages > 1): ?>
|
||||
<div style="display:flex;gap:.5rem;margin-top:1rem;flex-wrap:wrap">
|
||||
<?php for ($i = 1; $i <= $pages; $i++): ?>
|
||||
<a href="?<?=http_build_query(array_filter(['q'=>$q,'page'=>$i]))?>" class="btn btn-sm<?= $i===$page ? ' btn-primary' : ' btn-outline'?>"><?=$i?></a>
|
||||
<?php endfor; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php include __DIR__.'/admin_footer.php'; ?>
|
||||
Reference in New Issue
Block a user