228 lines
11 KiB
PHP
228 lines
11 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', t('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[] = t('english_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', t('template_updated'));
|
|
header('Location: ' . url_with_lang('admin_notification_templates.php'));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
render_header(t('edit_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"><?= e(t('edit_template')) ?>: <?= e($template['event_name']) ?></h2>
|
|
<a href="<?= e(url_with_lang('admin_notification_templates.php')) ?>" class="btn btn-outline-secondary"><?= e(t('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"><?= e(t('english_version')) ?></h5>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= e(t('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"><?= e(t('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"><?= e(t('email_body_help')) ?></div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= e(t('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"><?= e(t('arabic_version')) ?></h5>
|
|
<div class="mb-3" dir="rtl">
|
|
<label class="form-label"><?= e(t('email_subject')) ?></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"><?= e(t('email_body')) ?></label>
|
|
<textarea class="form-control" name="email_body_ar" rows="6"><?= e($template['email_body_ar']) ?></textarea>
|
|
<div class="form-text text-muted"><?= e(t('email_body_help')) ?></div>
|
|
</div>
|
|
<div class="mb-3" dir="rtl">
|
|
<label class="form-label"><?= e(t('whatsapp_body')) ?></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"><?= e(t('save_changes')) ?></button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
render_footer();
|
|
exit;
|
|
}
|
|
|
|
// List View
|
|
$page = max(1, (int)($_GET['page'] ?? 1));
|
|
$limit = 20;
|
|
$offset = ($page - 1) * $limit;
|
|
|
|
$total = (int)db()->query("SELECT COUNT(*) FROM notification_templates")->fetchColumn();
|
|
$totalPages = (int)ceil($total / $limit);
|
|
|
|
$stmt = db()->query("SELECT * FROM notification_templates ORDER BY event_name ASC LIMIT $limit OFFSET $offset");
|
|
$templates = $stmt->fetchAll();
|
|
|
|
render_header(t('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"><?= e(t('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"><?= e(t('event_name')) ?></th>
|
|
<th><?= e(t('subject_en')) ?></th>
|
|
<th><?= e(t('subject_ar')) ?></th>
|
|
<th class="text-end pe-4"><?= e(t('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="<?= e(t('edit')) ?>">
|
|
<i class="bi bi-pencil"></i>
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<?php if ($totalPages > 1): ?>
|
|
<div class="px-4 py-3 border-top d-flex justify-content-between align-items-center">
|
|
<span class="text-muted small"><?= e(t('shown')) ?> <?= count($templates) ?> of <?= $total ?> templates</span>
|
|
<ul class="pagination pagination-sm mb-0">
|
|
<li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
|
|
<a class="page-link" href="?page=<?= $page - 1 ?>">Previous</a>
|
|
</li>
|
|
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
|
|
<li class="page-item <?= $i === $page ? 'active' : '' ?>">
|
|
<a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
|
|
</li>
|
|
<?php endfor; ?>
|
|
<li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
|
|
<a class="page-link" href="?page=<?= $page + 1 ?>">Next</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php render_footer(); ?>
|