116 lines
4.2 KiB
PHP
116 lines
4.2 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$pageTitle = 'Admin';
|
|
require_once __DIR__ . '/includes/header.php';
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
function slugify($text) {
|
|
$text = preg_replace('~[\pL\d]+~u', '-', $text);
|
|
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
|
|
$text = preg_replace('~[^\-\w]+~', '', $text);
|
|
$text = trim($text, '-');
|
|
$text = preg_replace('~-+~', '-', $text);
|
|
$text = strtolower($text);
|
|
if (empty($text)) {
|
|
return 'n-a';
|
|
}
|
|
return $text;
|
|
}
|
|
|
|
$message = '';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$title = $_POST['title'] ?? '';
|
|
$content = $_POST['content'] ?? '';
|
|
$author = $_POST['author'] ?? '';
|
|
$slug = slugify($title);
|
|
|
|
if ($title && $content && $author) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("INSERT INTO posts (title, content, author, slug) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$title, $content, $author, $slug]);
|
|
$message = '<div class="alert alert-success">Post created successfully!</div>';
|
|
} catch (PDOException $e) {
|
|
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
|
}
|
|
} else {
|
|
$message = '<div class="alert alert-danger">Please fill in all fields.</div>';
|
|
}
|
|
}
|
|
?>
|
|
|
|
<h1>Add New Post <a href="logout.php" class="btn btn-sm btn-danger">Logout</a></h1>
|
|
|
|
<?php echo $message; ?>
|
|
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="title" class="form-label">Title</label>
|
|
<input type="text" class="form-control" id="title" name="title" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="author" class="form-label">Author</label>
|
|
<input type="text" class="form-control" id="author" name="author" 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></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary">Create Post</button>
|
|
</form>
|
|
|
|
<hr class="my-5">
|
|
|
|
<h2 class="mt-5">Manage Posts</h2>
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead class="thead-dark">
|
|
<tr>
|
|
<th scope="col">#</th>
|
|
<th scope="col">Title</th>
|
|
<th scope="col">Author</th>
|
|
<th scope="col">Created At</th>
|
|
<th scope="col">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->query("SELECT id, title, author, created_at FROM posts ORDER BY created_at DESC");
|
|
$posts = $stmt->fetchAll();
|
|
|
|
if ($posts) {
|
|
foreach ($posts as $post) {
|
|
echo "<tr>";
|
|
echo "<th scope=\"row\">" . htmlspecialchars($post['id']) . "</th>";
|
|
echo "<td>" . htmlspecialchars($post['title']) . "</td>";
|
|
echo "<td>" . htmlspecialchars($post['author']) . "</td>";
|
|
echo "<td>" . date("F j, Y, g:i a", strtotime($post['created_at'])) . "</td>";
|
|
echo '<td>
|
|
<a href="post.php?slug=' . htmlspecialchars($post['slug']) . '" class="btn btn-sm btn-info">View</a>
|
|
<a href="edit.php?id=' . htmlspecialchars($post['id']) . '" class="btn btn-sm btn-warning">Edit</a>
|
|
<a href="delete.php?id=' . htmlspecialchars($post['id']) . '" class="btn btn-sm btn-danger" onclick="return confirm(\'Are you sure you want to delete this post?\');">Delete</a>
|
|
</td>';
|
|
echo "</tr>";
|
|
}
|
|
} else {
|
|
echo '<tr><td colspan="5" class="text-center">No posts found.</td></tr>';
|
|
}
|
|
} catch (PDOException $e) {
|
|
echo '<tr><td colspan="5" class="text-center text-danger">Error: ' . $e->getMessage() . '</td></tr>';
|
|
}
|
|
?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|