39038-vm/admin_landing_pages.php
2026-03-08 03:22:33 +00:00

219 lines
11 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/app.php';
require_once __DIR__ . '/includes/layout.php';
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') {
$action = $_POST['action'] ?? '';
if ($action === 'create' || $action === 'edit') {
$id = $_POST['id'] ?? null;
$title = $_POST['title'] ?? '';
$subtitle = $_POST['subtitle'] ?? '';
$content = $_POST['content'] ?? '';
$layout = $_POST['layout'] ?? 'text_left';
$button_text = $_POST['button_text'] ?? '';
$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, subtitle, content, image_path, layout, button_text, button_link, section_order, is_active, section_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 'custom')");
$stmt->execute([$title, $subtitle, $content, $image_path, $layout, $button_text, $button_link, $section_order, $is_active]);
set_flash('success', 'Section created successfully.');
} else {
$stmt = $pdo->prepare("UPDATE landing_sections SET title=?, subtitle=?, content=?, image_path=?, layout=?, button_text=?, button_link=?, section_order=?, is_active=? WHERE id=?");
$stmt->execute([$title, $subtitle, $content, $image_path, $layout, $button_text, $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');
?>
<div class="row g-4">
<div class="col-lg-3">
<?php render_admin_sidebar('landing_pages'); ?>
</div>
<div class="col-lg-9">
<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-4">
<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 <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">Subtitle (Optional)</label>
<textarea name="subtitle" class="form-control" rows="2"><?= e($editSection['subtitle'] ?? '') ?></textarea>
</div>
<div class="mb-3">
<label class="form-label">Content (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="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</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 Link (e.g. login.php)</label>
<input type="text" name="button_link" class="form-control" value="<?= e($editSection['button_link'] ?? '') ?>">
</div>
</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>
</div>
<div class="d-flex gap-2">
<a href="<?= e(url_with_lang('admin_landing_pages.php', ['edit' => $sec['id']])) ?>" class="btn btn-sm btn-outline-primary">Edit</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 btn-outline-danger">Delete</button>
</form>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php render_footer(); ?>