250 lines
14 KiB
PHP
250 lines
14 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/includes/app.php';
|
|
require_once __DIR__ . '/includes/layout.php'; require_role('admin');
|
|
|
|
if (empty($_SESSION['user_id']) || $_SESSION['user_role'] !== 'admin') {
|
|
header('Location: ' . url_with_lang('login.php'));
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Handle form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') { validate_csrf_token();
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
if ($action === 'create' || $action === 'edit') {
|
|
$id = $_POST['id'] ?? null;
|
|
$title = $_POST['title'] ?? '';
|
|
$title_ar = $_POST['title_ar'] ?? '';
|
|
$subtitle = $_POST['subtitle'] ?? '';
|
|
$subtitle_ar = $_POST['subtitle_ar'] ?? '';
|
|
$content = $_POST['content'] ?? '';
|
|
$content_ar = $_POST['content_ar'] ?? '';
|
|
$layout = $_POST['layout'] ?? 'text_left';
|
|
$button_text = $_POST['button_text'] ?? '';
|
|
$button_text_ar = $_POST['button_text_ar'] ?? '';
|
|
$button_link = $_POST['button_link'] ?? '';
|
|
$section_order = (int)($_POST['section_order'] ?? 0);
|
|
$is_active = isset($_POST['is_active']) ? 1 : 0;
|
|
|
|
$image_path = $_POST['current_image'] ?? '';
|
|
|
|
if (!empty($_FILES['image']['name'])) {
|
|
$uploadDir = __DIR__ . '/uploads/pages/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0777, true);
|
|
}
|
|
$ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
|
|
$fileName = uniqid('img_') . '.' . $ext;
|
|
$dest = $uploadDir . $fileName;
|
|
if (move_uploaded_file($_FILES['image']['tmp_name'], $dest)) {
|
|
$image_path = 'uploads/pages/' . $fileName;
|
|
}
|
|
}
|
|
|
|
if ($action === 'create') {
|
|
$stmt = $pdo->prepare("INSERT INTO landing_sections (title, title_ar, subtitle, subtitle_ar, content, content_ar, image_path, layout, button_text, button_text_ar, button_link, section_order, is_active, section_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'custom')");
|
|
$stmt->execute([$title, $title_ar, $subtitle, $subtitle_ar, $content, $content_ar, $image_path, $layout, $button_text, $button_text_ar, $button_link, $section_order, $is_active]);
|
|
set_flash('success', 'Section created successfully.');
|
|
} else {
|
|
$stmt = $pdo->prepare("UPDATE landing_sections SET title=?, title_ar=?, subtitle=?, subtitle_ar=?, content=?, content_ar=?, image_path=?, layout=?, button_text=?, button_text_ar=?, button_link=?, section_order=?, is_active=? WHERE id=?");
|
|
$stmt->execute([$title, $title_ar, $subtitle, $subtitle_ar, $content, $content_ar, $image_path, $layout, $button_text, $button_text_ar, $button_link, $section_order, $is_active, $id]);
|
|
set_flash('success', 'Section updated successfully.');
|
|
}
|
|
header('Location: ' . url_with_lang('admin_landing_pages.php'));
|
|
exit;
|
|
} elseif ($action === 'delete') {
|
|
$id = $_POST['id'] ?? null;
|
|
if ($id) {
|
|
$stmt = $pdo->prepare("SELECT section_type FROM landing_sections WHERE id=?");
|
|
$stmt->execute([$id]);
|
|
$sec = $stmt->fetch();
|
|
if ($sec && $sec['section_type'] !== 'custom') {
|
|
set_flash('danger', 'Built-in sections cannot be deleted, but you can hide them by unchecking Active.');
|
|
} else {
|
|
$stmt = $pdo->prepare("DELETE FROM landing_sections WHERE id=?");
|
|
$stmt->execute([$id]);
|
|
set_flash('success', 'Section deleted successfully.');
|
|
}
|
|
}
|
|
header('Location: ' . url_with_lang('admin_landing_pages.php'));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$stmt = $pdo->query("SELECT * FROM landing_sections ORDER BY section_order ASC, id ASC");
|
|
$sections = $stmt->fetchAll();
|
|
|
|
$editId = $_GET['edit'] ?? null;
|
|
$editSection = null;
|
|
if ($editId) {
|
|
$stmt = $pdo->prepare("SELECT * FROM landing_sections WHERE id = ?");
|
|
$stmt->execute([$editId]);
|
|
$editSection = $stmt->fetch();
|
|
}
|
|
|
|
render_header(t('app_name') . ' - Landing Pages', 'admin', true);
|
|
?>
|
|
|
|
<div class="row g-0">
|
|
<div class="col-md-2 bg-white border-end min-vh-100">
|
|
<?php render_admin_sidebar('landing_pages'); ?>
|
|
</div>
|
|
<div class="col-md-10 p-4">
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h2 class="fw-bold mb-0">Landing Page Customization</h2>
|
|
<a href="<?= e(url_with_lang('admin_dashboard.php')) ?>" class="btn btn-outline-secondary">Back to Dashboard</a>
|
|
</div>
|
|
|
|
<?php if ($flash = get_flash()): ?>
|
|
<div class="alert alert-<?= e($flash['type'] === 'success' ? 'success' : 'danger') ?> alert-dismissible fade show">
|
|
<?= e($flash['message']) ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="row g-0">
|
|
<div class="col-md-5">
|
|
<div class="panel p-4 shadow-sm border-0 rounded-4 bg-white">
|
|
<h4 class="mb-4"><?= $editSection ? 'Edit Section' : 'Add New Section' ?></h4>
|
|
<form action="<?= e(url_with_lang('admin_landing_pages.php')) ?>" method="POST" enctype="multipart/form-data">
|
|
<input type="hidden" name="action" value="<?= $editSection ? 'edit' : 'create' ?>">
|
|
<?php if ($editSection): ?>
|
|
<input type="hidden" name="id" value="<?= e($editSection['id']) ?>">
|
|
<input type="hidden" name="current_image" value="<?= e($editSection['image_path']) ?>">
|
|
<?php endif; ?>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Title (English) <span class="text-danger">*</span></label>
|
|
<input type="text" name="title" class="form-control" value="<?= e($editSection['title'] ?? '') ?>" required>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Title (Arabic)</label>
|
|
<input type="text" name="title_ar" class="form-control" dir="rtl" value="<?= e($editSection['title_ar'] ?? '') ?>">
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Subtitle (English) (Optional)</label>
|
|
<textarea name="subtitle" class="form-control" rows="2"><?= e($editSection['subtitle'] ?? '') ?></textarea>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Subtitle (Arabic) (Optional)</label>
|
|
<textarea name="subtitle_ar" class="form-control" rows="2" dir="rtl"><?= e($editSection['subtitle_ar'] ?? '') ?></textarea>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Content (English) (HTML allowed)</label>
|
|
<textarea name="content" class="form-control" rows="5"><?= e($editSection['content'] ?? '') ?></textarea>
|
|
<small class="text-muted">Not applicable for most built-in sections.</small>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Content (Arabic) (HTML allowed)</label>
|
|
<textarea name="content_ar" class="form-control" rows="5" dir="rtl"><?= e($editSection['content_ar'] ?? '') ?></textarea>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Layout Type</label>
|
|
<select name="layout" class="form-select" <?= ($editSection['section_type'] ?? 'custom') !== 'custom' ? 'disabled' : '' ?>>
|
|
<option value="text_left" <?= ($editSection['layout'] ?? '') === 'text_left' ? 'selected' : '' ?>>Text Left, Image Right</option>
|
|
<option value="text_right" <?= ($editSection['layout'] ?? '') === 'text_right' ? 'selected' : '' ?>>Image Left, Text Right</option>
|
|
<option value="center" <?= ($editSection['layout'] ?? '') === 'center' ? 'selected' : '' ?>>Center (No Image)</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Order</label>
|
|
<input type="number" name="section_order" class="form-control" value="<?= e($editSection['section_order'] ?? 0) ?>">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Button Text (English)</label>
|
|
<input type="text" name="button_text" class="form-control" value="<?= e($editSection['button_text'] ?? '') ?>">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Button Text (Arabic)</label>
|
|
<input type="text" name="button_text_ar" class="form-control" dir="rtl" value="<?= e($editSection['button_text_ar'] ?? '') ?>">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Button Link (e.g. login.php)</label>
|
|
<input type="text" name="button_link" class="form-control" value="<?= e($editSection['button_link'] ?? '') ?>">
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Upload Picture</label>
|
|
<input type="file" name="image" class="form-control" accept="image/*">
|
|
<?php if (!empty($editSection['image_path'])): ?>
|
|
<div class="mt-2">
|
|
<img src="<?= e($editSection['image_path']) ?>" alt="Current Image" style="max-height: 80px; border-radius: 4px;">
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="mb-4 form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" name="is_active" id="isActive" <?= (!isset($editSection) || $editSection['is_active']) ? 'checked' : '' ?>>
|
|
<label class="form-check-label" for="isActive">Active</label>
|
|
</div>
|
|
|
|
<div class="d-flex gap-2">
|
|
<button type="submit" class="btn btn-primary"><?= $editSection ? 'Update Section' : 'Create Section' ?></button>
|
|
<?php if ($editSection): ?>
|
|
<a href="<?= e(url_with_lang('admin_landing_pages.php')) ?>" class="btn btn-outline-secondary">Cancel</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-7">
|
|
<div class="panel p-4 shadow-sm border-0 rounded-4 bg-white">
|
|
<h4 class="mb-4">Current Sections</h4>
|
|
<?php if (!$sections): ?>
|
|
<p class="text-muted">No custom sections added yet.</p>
|
|
<?php else: ?>
|
|
<div class="list-group">
|
|
<?php foreach ($sections as $sec): ?>
|
|
<div class="list-group-item list-group-item-action d-flex justify-content-between align-items-center p-3">
|
|
<div>
|
|
<h6 class="mb-1 fw-bold"><?= e($sec['title']) ?> <span class="badge bg-<?= $sec['is_active'] ? 'success' : 'secondary' ?> ms-2"><?= $sec['is_active'] ? 'Active' : 'Draft' ?></span></h6>
|
|
<small class="text-muted">Order: <?= e($sec['section_order']) ?> | Type: <?= e(ucfirst($sec['section_type'])) ?> <?= $sec['section_type']==='custom' ? '| Layout: '.e($sec['layout']) : '' ?></small>
|
|
<?php if (!empty($sec['title_ar'])): ?>
|
|
<div class="mt-1 small text-muted">AR: <?= e($sec['title_ar']) ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="d-flex gap-2">
|
|
<a href="<?= e(url_with_lang('admin_landing_pages.php', ['edit' => $sec['id']])) ?>" class="btn btn-sm p-1 border-0 bg-transparent text-primary" title="Edit">
|
|
<i class="bi bi-pencil"></i>
|
|
</a>
|
|
<?php if ($sec['section_type'] === 'custom'): ?>
|
|
<form action="<?= e(url_with_lang('admin_landing_pages.php')) ?>" method="POST" onsubmit="return confirm('Are you sure you want to delete this section?');" style="display:inline;">
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="id" value="<?= e($sec['id']) ?>">
|
|
<button type="submit" class="btn btn-sm p-1 border-0 bg-transparent text-danger" title="Delete">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<?php render_footer(); ?>
|