39038-vm/admin_notification_templates.php
2026-03-14 13:13:28 +00:00

201 lines
9.0 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/layout.php'; require_role('admin');
ensure_schema();
// Access Control
if (($_SESSION['user_role'] ?? '') !== 'admin') {
header('Location: ' . url_with_lang('login.php'));
exit;
}
// Ensure table exists (idempotent)
try {
db()->exec("
CREATE TABLE IF NOT EXISTS notification_templates (
id INT AUTO_INCREMENT PRIMARY KEY,
event_name VARCHAR(50) NOT NULL UNIQUE,
email_subject_en VARCHAR(255),
email_body_en TEXT,
email_subject_ar VARCHAR(255),
email_body_ar TEXT,
whatsapp_body_en TEXT,
whatsapp_body_ar TEXT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
");
} catch (Throwable $e) {
// Ignore if table exists or permission issue, subsequent queries will fail if critical
}
$action = $_GET['action'] ?? 'list';
$id = (int)($_GET['id'] ?? 0);
$errors = [];
$flash = get_flash();
if ($action === 'edit' && $id > 0) {
// Handle Edit
$stmt = db()->prepare("SELECT * FROM notification_templates WHERE id = ?");
$stmt->execute([$id]);
$template = $stmt->fetch();
if (!$template) {
set_flash('error', 'Template not found.');
header('Location: ' . url_with_lang('admin_notification_templates.php'));
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token();
$email_subject_en = trim($_POST['email_subject_en'] ?? '');
$email_body_en = trim($_POST['email_body_en'] ?? '');
$email_subject_ar = trim($_POST['email_subject_ar'] ?? '');
$email_body_ar = trim($_POST['email_body_ar'] ?? '');
$whatsapp_body_en = trim($_POST['whatsapp_body_en'] ?? '');
$whatsapp_body_ar = trim($_POST['whatsapp_body_ar'] ?? '');
if ($email_subject_en === '' || $email_body_en === '') {
$errors[] = 'English subject and body are required.';
}
if (!$errors) {
$stmt = db()->prepare("
UPDATE notification_templates SET
email_subject_en = ?, email_body_en = ?,
email_subject_ar = ?, email_body_ar = ?,
whatsapp_body_en = ?, whatsapp_body_ar = ?
WHERE id = ?
");
$stmt->execute([
$email_subject_en, $email_body_en,
$email_subject_ar, $email_body_ar,
$whatsapp_body_en, $whatsapp_body_ar,
$id
]);
set_flash('success', 'Template updated successfully.');
header('Location: ' . url_with_lang('admin_notification_templates.php'));
exit;
}
}
render_header('Edit Notification Template', 'admin', true);
?>
<div class="row g-0">
<div class="col-md-2 bg-white border-end min-vh-100">
<?= render_admin_sidebar('notification_templates') ?>
</div>
<div class="col-md-10">
<div class="p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="h4 mb-0">Edit Template: <?= e($template['event_name']) ?></h2>
<a href="<?= e(url_with_lang('admin_notification_templates.php')) ?>" class="btn btn-outline-secondary">Back to List</a>
</div>
<?php if ($errors): ?>
<div class="alert alert-danger"><?= e(implode('<br>', $errors)) ?></div>
<?php endif; ?>
<div class="card border-0 shadow-sm">
<div class="card-body">
<form method="post"> <?= csrf_field() ?>
<div class="row g-4">
<div class="col-md-6">
<h5 class="mb-3 border-bottom pb-2">English</h5>
<div class="mb-3">
<label class="form-label">Email Subject</label>
<input type="text" class="form-control" name="email_subject_en" value="<?= e($template['email_subject_en']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Email Body</label>
<textarea class="form-control" name="email_body_en" rows="6" required><?= e($template['email_body_en']) ?></textarea>
<div class="form-text text-muted">Use placeholders like {shipment_id}, {user_name}, {offer_price}.</div>
</div>
<div class="mb-3">
<label class="form-label">WhatsApp Body</label>
<textarea class="form-control" name="whatsapp_body_en" rows="4"><?= e($template['whatsapp_body_en']) ?></textarea>
</div>
</div>
<div class="col-md-6">
<h5 class="mb-3 border-bottom pb-2">Arabic</h5>
<div class="mb-3" dir="rtl">
<label class="form-label">موضوع البريد الإلكتروني</label>
<input type="text" class="form-control" name="email_subject_ar" value="<?= e($template['email_subject_ar']) ?>">
</div>
<div class="mb-3" dir="rtl">
<label class="form-label">نص البريد الإلكتروني</label>
<textarea class="form-control" name="email_body_ar" rows="6"><?= e($template['email_body_ar']) ?></textarea>
</div>
<div class="mb-3" dir="rtl">
<label class="form-label">نص الواتساب</label>
<textarea class="form-control" name="whatsapp_body_ar" rows="4"><?= e($template['whatsapp_body_ar']) ?></textarea>
</div>
</div>
</div>
<hr>
<div class="text-end">
<button type="submit" class="btn btn-primary px-4">Save Changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
render_footer();
exit;
}
// List View
$stmt = db()->query("SELECT * FROM notification_templates ORDER BY event_name ASC");
$templates = $stmt->fetchAll();
render_header('Notification Templates', 'admin', true);
?>
<div class="row g-0">
<div class="col-md-2 bg-white border-end min-vh-100">
<?= render_admin_sidebar('notification_templates') ?>
</div>
<div class="col-md-10">
<div class="p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="h4 mb-0">Notification Templates</h2>
</div>
<?php if ($flash): ?>
<div class="alert alert-<?= $flash['type'] === 'error' ? 'danger' : 'success' ?>"><?= e($flash['message']) ?></div>
<?php endif; ?>
<div class="card border-0 shadow-sm">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light">
<tr>
<th class="ps-4">Event Name</th>
<th>Subject (EN)</th>
<th>Subject (AR)</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($templates as $t): ?>
<tr>
<td class="ps-4 fw-medium"><?= e($t['event_name']) ?></td>
<td><?= e($t['email_subject_en']) ?></td>
<td><?= e($t['email_subject_ar']) ?></td>
<td class="text-end pe-4">
<a href="<?= e(url_with_lang('admin_notification_templates.php', ['action' => 'edit', 'id' => $t['id']])) ?>" class="btn btn-sm p-1 border-0 bg-transparent text-primary" title="Edit">
<i class="bi bi-pencil"></i>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php render_footer(); ?>