149 lines
5.9 KiB
PHP
149 lines
5.9 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
|
|
$post = [
|
|
'id' => null,
|
|
'title' => '',
|
|
'content' => '',
|
|
'excerpt' => '',
|
|
'image_url' => ''
|
|
];
|
|
$pageTitle = 'Create New Post';
|
|
$action = 'editor.php';
|
|
|
|
// Edit mode
|
|
if (isset($_GET['id'])) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
|
|
$stmt->execute([$_GET['id']]);
|
|
$post = $stmt->fetch();
|
|
if (!$post) {
|
|
// Post not found, redirect or show error
|
|
header("Location: admin.php?error=notfound");
|
|
exit;
|
|
}
|
|
$pageTitle = 'Edit Post';
|
|
$action = 'editor.php?id=' . $_GET['id'];
|
|
} catch (PDOException $e) {
|
|
error_log("DB Error: " . $e->getMessage());
|
|
die("Error fetching post. Check logs.");
|
|
}
|
|
}
|
|
|
|
// Handle form submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$title = $_POST['title'] ?? '';
|
|
$content = $_POST['content'] ?? '';
|
|
$excerpt = $_POST['excerpt'] ?? '';
|
|
$imageUrl = $_POST['image_url'] ?? '';
|
|
$id = $_POST['id'] ?? null;
|
|
|
|
// Basic validation
|
|
if (empty($title) || empty($content)) {
|
|
$error = "Title and Content are required.";
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
if ($id) {
|
|
// Update
|
|
$stmt = $pdo->prepare("UPDATE posts SET title = ?, content = ?, excerpt = ?, image_url = ? WHERE id = ?");
|
|
$stmt->execute([$title, $content, $excerpt, $imageUrl, $id]);
|
|
} else {
|
|
// Insert
|
|
$stmt = $pdo->prepare("INSERT INTO posts (title, content, excerpt, image_url) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$title, $content, $excerpt, $imageUrl]);
|
|
}
|
|
header("Location: admin.php?saved=true");
|
|
exit;
|
|
} catch (PDOException $e) {
|
|
error_log("DB Error: " . $e->getMessage());
|
|
$error = "Error saving post. Check logs for details.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?php echo $pageTitle; ?></title>
|
|
<meta name="robots" content="noindex, nofollow">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="/">My Awesome Blog</a>
|
|
<a href="admin.php" class="nav-link">Admin</a>
|
|
<a href="logout.php" class="nav-link">Logout</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container my-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-8">
|
|
<h1 class="h2 mb-4"><?php echo $pageTitle; ?></h1>
|
|
|
|
<?php if (isset($error)): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<form action="<?php echo $action; ?>" method="POST">
|
|
<input type="hidden" name="id" value="<?php echo htmlspecialchars($post['id'] ?? ''); ?>">
|
|
|
|
<div class="mb-3">
|
|
<label for="title" class="form-label">Title</label>
|
|
<input type="text" class="form-control" id="title" name="title" value="<?php echo htmlspecialchars($post['title'] ?? ''); ?>" required>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="content" class="form-label">Content</label>
|
|
<textarea class="form-control" id="content" name="content" rows="10" required><?php echo htmlspecialchars($post['content'] ?? ''); ?></textarea>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="excerpt" class="form-label">Excerpt</label>
|
|
<textarea class="form-control" id="excerpt" name="excerpt" rows="3"><?php echo htmlspecialchars($post['excerpt'] ?? ''); ?></textarea>
|
|
<div class="form-text">A short summary of the post, shown on the main blog page.</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="image_url" class="form-label">Image URL</label>
|
|
<input type="url" class="form-control" id="image_url" name="image_url" value="<?php echo htmlspecialchars($post['image_url'] ?? 'https://picsum.photos/seed/'.uniqid().'/800/600'); ?>">
|
|
<div class="form-text">URL for the post's main image. A new random placeholder is generated for you.</div>
|
|
</div>
|
|
|
|
<div class="d-flex justify-content-end">
|
|
<a href="admin.php" class="btn btn-secondary me-2">Cancel</a>
|
|
<button type="submit" class="btn btn-primary">Save Post</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="footer mt-auto py-3 bg-white border-top">
|
|
<div class="container text-center">
|
|
<span class="text-muted">© <?php echo date("Y"); ?> My Awesome Blog. All Rights Reserved.</span>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|