๐ services.php
๐ Path: //home/skhata/pimsnepal.com.np/admin/services.php
๐ Size: 6.02 KB
๐ Perm: 0644
๐ MIME: text/x-php
<?php
require_once __DIR__ . '/../includes/functions.php';
requireAdmin();
require __DIR__ . '/includes/ui.php';
$pageTitle = 'Services';
function slugify(string $text): string
{
$text = strtolower(trim($text));
$text = preg_replace('/[^a-z0-9]+/', '-', $text);
return trim($text, '-');
}
/** Convert simple line-based textarea input "Q: ... \n A: ..." pairs into JSON, or accept raw JSON. */
function parseFeaturesInput(string $raw): string
{
$lines = array_filter(array_map('trim', explode("\n", $raw)));
return json_encode(array_values($lines));
}
function parseFaqInput(string $raw): string
{
// Each FAQ separated by a blank line; first line = question, rest = answer.
$blocks = preg_split('/\n\s*\n/', trim($raw));
$faqs = [];
foreach ($blocks as $block) {
$lines = array_values(array_filter(array_map('trim', explode("\n", $block))));
if (count($lines) >= 2) {
$faqs[] = ['q' => $lines[0], 'a' => implode(' ', array_slice($lines, 1))];
}
}
return json_encode($faqs);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrfVerify($_POST['csrf_token'] ?? null)) {
$action = $_POST['action'] ?? '';
if ($action === 'save') {
$id = (int) ($_POST['id'] ?? 0);
$slug = trim($_POST['slug'] ?? '') ?: slugify($_POST['title'] ?? '');
$fields = [
$slug,
trim($_POST['icon'] ?? ''),
trim($_POST['title'] ?? ''),
trim($_POST['short_desc'] ?? ''),
trim($_POST['description'] ?? ''),
trim($_POST['banner_gradient'] ?? ''),
parseFeaturesInput($_POST['features'] ?? ''),
parseFaqInput($_POST['faqs'] ?? ''),
(int) ($_POST['sort_order'] ?? 0),
isset($_POST['is_active']) ? 1 : 0,
];
if ($id > 0) {
db()->prepare('UPDATE services SET slug=?,icon=?,title=?,short_desc=?,description=?,banner_gradient=?,features=?,faqs=?,sort_order=?,is_active=? WHERE id=?')->execute([...$fields, $id]);
} else {
db()->prepare('INSERT INTO services (slug,icon,title,short_desc,description,banner_gradient,features,faqs,sort_order,is_active) VALUES (?,?,?,?,?,?,?,?,?,?)')->execute($fields);
}
flash('success', 'Service saved.');
} elseif ($action === 'delete') {
db()->prepare('DELETE FROM services WHERE id = ?')->execute([(int) ($_POST['id'] ?? 0)]);
flash('success', 'Service deleted.');
}
redirect('services.php');
}
require __DIR__ . '/includes/admin-header.php';
adminFlashBox();
$services = db()->query('SELECT * FROM services ORDER BY sort_order ASC, id ASC')->fetchAll();
$editId = (int) ($_GET['edit'] ?? 0);
$editing = null;
foreach ($services as $s) { if ((int) $s['id'] === $editId) $editing = $s; }
$featuresText = $editing ? implode("\n", jdecode($editing['features'])) : '';
$faqsText = '';
if ($editing) {
foreach (jdecode($editing['faqs']) as $f) {
$faqsText .= ($f['q'] ?? '') . "\n" . ($f['a'] ?? '') . "\n\n";
}
}
?>
<div class="grid lg:grid-cols-3 gap-6">
<div class="lg:col-span-2 space-y-3">
<?php foreach ($services as $s): ?>
<div class="bg-white rounded-2xl border border-gray-100 p-5 flex items-center gap-4">
<div class="w-10 h-10 rounded-lg flex items-center justify-center text-white shrink-0" style="background:<?= e($s['banner_gradient'] ?: '#0D6EFD') ?>"><i class="bi <?= e($s['icon'] ?: 'bi-briefcase-fill') ?>"></i></div>
<div class="flex-1 min-w-0">
<p class="font-semibold text-sm truncate"><?= e($s['title']) ?></p>
<p class="text-xs text-gray-500 truncate">/<?= e($s['slug']) ?> ยท order <?= (int) $s['sort_order'] ?> ยท <?= $s['is_active'] ? 'active' : 'hidden' ?></p>
</div>
<a href="?edit=<?= (int) $s['id'] ?>" class="text-primary font-semibold text-sm">Edit</a>
<form method="post" onsubmit="return confirm('Delete this service?');">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?= (int) $s['id'] ?>" />
<button class="text-red-600 font-semibold text-sm">Delete</button>
</form>
</div>
<?php endforeach; ?>
</div>
<div class="bg-white rounded-2xl border border-gray-100 p-6 h-fit">
<h2 class="font-heading font-semibold mb-4"><?= $editing ? 'Edit Service' : 'Add Service' ?></h2>
<form method="post">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="action" value="save" />
<input type="hidden" name="id" value="<?= (int) ($editing['id'] ?? 0) ?>" />
<?php inputField('Title', 'title', $editing['title'] ?? '', 'text', true); ?>
<?php inputField('Slug (leave blank to auto-generate)', 'slug', $editing['slug'] ?? ''); ?>
<?php inputField('Bootstrap Icon class (e.g. bi-cart-check)', 'icon', $editing['icon'] ?? ''); ?>
<?php inputField('Banner gradient CSS (e.g. linear-gradient(120deg,#0D6EFD,#1E3A8A))', 'banner_gradient', $editing['banner_gradient'] ?? ''); ?>
<?php textareaField('Short Description', 'short_desc', $editing['short_desc'] ?? '', 2, true); ?>
<?php textareaField('Full Description', 'description', $editing['description'] ?? '', 4, true); ?>
<?php textareaField('Key Features (one per line)', 'features', $featuresText, 5); ?>
<?php textareaField('FAQs (question line, then answer line, blank line between each FAQ)', 'faqs', $faqsText, 6); ?>
<?php numberField('Sort Order', 'sort_order', $editing['sort_order'] ?? 0); ?>
<?php checkboxField('Active', 'is_active', $editing ? !empty($editing['is_active']) : true); ?>
<button class="btn-primary-brand bg-primary text-white px-5 py-2.5 rounded-lg font-semibold w-full"><?= $editing ? 'Update' : 'Add Service' ?></button>
<?php if ($editing): ?><a href="services.php" class="block text-center text-sm text-gray-500 mt-3">Cancel edit</a><?php endif; ?>
</form>
</div>
</div>
<?php require __DIR__ . '/includes/admin-footer.php'; ?>