164 lines
7.5 KiB
PHP
164 lines
7.5 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
$pdo = db();
|
|
|
|
$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
|
|
$ad = null;
|
|
$message = '';
|
|
$isEdit = false;
|
|
|
|
if ($id) {
|
|
$stmt = $pdo->prepare("SELECT * FROM ads_images WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$ad = $stmt->fetch();
|
|
if ($ad) {
|
|
$isEdit = true;
|
|
} else {
|
|
header("Location: ads.php");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$title = trim($_POST['title']);
|
|
$sort_order = (int)$_POST['sort_order'];
|
|
$is_active = isset($_POST['is_active']) ? 1 : 0;
|
|
$display_layout = $_POST['display_layout'] ?? 'both';
|
|
$image_path = $isEdit ? $ad['image_path'] : null;
|
|
|
|
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
|
|
$uploadDir = __DIR__ . '/../assets/images/ads/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0755, true);
|
|
}
|
|
|
|
$fileInfo = pathinfo($_FILES['image']['name']);
|
|
$fileExt = strtolower($fileInfo['extension']);
|
|
$allowedExts = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
|
|
|
if (in_array($fileExt, $allowedExts)) {
|
|
$fileName = uniqid('ad_') . '.' . $fileExt;
|
|
$targetFile = $uploadDir . $fileName;
|
|
|
|
if (move_uploaded_file($_FILES['image']['tmp_name'], $targetFile)) {
|
|
$image_path = 'assets/images/ads/' . $fileName;
|
|
} else {
|
|
$message = '<div class="alert alert-danger">Failed to upload image.</div>';
|
|
}
|
|
} else {
|
|
$message = '<div class="alert alert-danger">Invalid file type. Allowed: jpg, png, gif, webp.</div>';
|
|
}
|
|
}
|
|
|
|
if (empty($image_path) && !$isEdit) {
|
|
$message = '<div class="alert alert-danger">Image is required for new advertisements.</div>';
|
|
}
|
|
|
|
if (empty($message)) {
|
|
try {
|
|
if ($isEdit) {
|
|
$stmt = $pdo->prepare("UPDATE ads_images SET title = ?, sort_order = ?, is_active = ?, display_layout = ?, image_path = ? WHERE id = ?");
|
|
$stmt->execute([$title, $sort_order, $is_active, $display_layout, $image_path, $id]);
|
|
header("Location: ads.php?success=updated");
|
|
exit;
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO ads_images (title, sort_order, is_active, display_layout, image_path) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$title, $sort_order, $is_active, $display_layout, $image_path]);
|
|
header("Location: ads.php?success=created");
|
|
exit;
|
|
}
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Database error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$isEdit) {
|
|
$ad = [
|
|
'title' => $_POST['title'] ?? '',
|
|
'sort_order' => $_POST['sort_order'] ?? 0,
|
|
'is_active' => 1,
|
|
'display_layout' => 'both',
|
|
'image_path' => ''
|
|
];
|
|
}
|
|
|
|
include 'includes/header.php';
|
|
?>
|
|
|
|
<div class="mb-4">
|
|
<a href="ads.php" class="text-decoration-none text-muted mb-2 d-inline-block"><i class="bi bi-arrow-left"></i> Back to Ads Management</a>
|
|
<h2 class="fw-bold mb-0"><?= $isEdit ? 'Edit Advertisement' : 'Add New Advertisement' ?></h2>
|
|
</div>
|
|
|
|
<?= $message ?>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body">
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<div class="mb-3">
|
|
<label class="form-label">Title / Caption</label>
|
|
<input type="text" name="title" class="form-control" value="<?= htmlspecialchars($ad['title'] ?? '') ?>" placeholder="e.g. Special Offer 50% Off">
|
|
<div class="form-text">This will be shown as a caption on the image.</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Sort Order</label>
|
|
<input type="number" name="sort_order" class="form-control" value="<?= htmlspecialchars($ad['sort_order'] ?? 0) ?>">
|
|
<div class="form-text">Lower numbers appear first in the slider.</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label d-block">Display Layout Preference</label>
|
|
<div class="btn-group w-100" role="group">
|
|
<input type="radio" class="btn-check" name="display_layout" id="layout_both" value="both" <?= ($ad['display_layout'] ?? 'both') === 'both' ? 'checked' : '' ?>>
|
|
<label class="btn btn-outline-primary" for="layout_both">Both Layouts</label>
|
|
|
|
<input type="radio" class="btn-check" name="display_layout" id="layout_split" value="split" <?= ($ad['display_layout'] ?? 'both') === 'split' ? 'checked' : '' ?>>
|
|
<label class="btn btn-outline-primary" for="layout_split">Split View Only</label>
|
|
|
|
<input type="radio" class="btn-check" name="display_layout" id="layout_fullscreen" value="fullscreen" <?= ($ad['display_layout'] ?? 'both') === 'fullscreen' ? 'checked' : '' ?>>
|
|
<label class="btn btn-outline-primary" for="layout_fullscreen">Fullscreen Only</label>
|
|
</div>
|
|
<div class="form-text mt-2">Choose where this advertisement should be visible.</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" name="is_active" id="isActiveSwitch" <?= ($ad['is_active'] ?? 1) ? 'checked' : '' ?>>
|
|
<label class="form-check-label" for="isActiveSwitch">Active (Show in Slider)</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="mb-3">
|
|
<label class="form-label">Advertisement Image <span class="text-danger">*</span></label>
|
|
<?php if (!empty($ad['image_path'])): ?>
|
|
<div class="mb-2">
|
|
<img src="../<?= htmlspecialchars($ad['image_path']) ?>"
|
|
class="img-fluid rounded border shadow-sm"
|
|
style="max-height: 250px; width: 100%; object-fit: cover;"
|
|
alt="Ad Image">
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="mb-2 p-5 bg-light text-center border rounded text-muted">
|
|
<i class="bi bi-images fs-1"></i><br>No Image Selected
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<input type="file" name="image" class="form-control" accept="image/*" <?= !$isEdit ? 'required' : '' ?>>
|
|
<div class="form-text">Recommended size: 1920x1080 (HD) or 16:9 aspect ratio.</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<hr>
|
|
<div class="d-flex justify-content-end gap-2">
|
|
<a href="ads.php" class="btn btn-secondary">Cancel</a>
|
|
<button type="submit" class="btn btn-primary"><?= $isEdit ? 'Save Changes' : 'Upload Image' ?></button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|