72 lines
2.1 KiB
PHP
72 lines
2.1 KiB
PHP
<?php
|
|
require_once BASE_PATH . '/src/db.php';
|
|
require_once BASE_PATH . '/src/PostManager.php';
|
|
|
|
$postManager = new PostManager($db);
|
|
|
|
$errors = [];
|
|
$title = '';
|
|
$content = '';
|
|
$published = false;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$title = trim($_POST['title'] ?? '');
|
|
$content = trim($_POST['content'] ?? '');
|
|
$published = isset($_POST['published']);
|
|
|
|
if ($title === '') {
|
|
$errors[] = 'Le titre est obligatoire.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
$postId = $postManager->create($title, $content, $published);
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Nouveau post</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
</head>
|
|
<body class="bg-light">
|
|
<div class="container py-4">
|
|
<h1 class="mb-4">Ajouter un nouveau post</h1>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<ul class="mb-0">
|
|
<?php foreach ($errors as $error): ?>
|
|
<li><?= htmlspecialchars($error) ?></li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="title" class="form-label">Titre</label>
|
|
<input type="text" class="form-control" id="title" name="title" value="<?= htmlspecialchars($title) ?>" required>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="content" class="form-label">Contenu</label>
|
|
<textarea class="form-control" id="content" name="content" rows="6"><?= htmlspecialchars($content) ?></textarea>
|
|
</div>
|
|
|
|
<div class="form-check mb-3">
|
|
<input class="form-check-input" type="checkbox" id="published" name="published" <?= $published ? 'checked' : '' ?>>
|
|
<label class="form-check-label" for="published">Publier immédiatement</label>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary">Enregistrer</button>
|
|
<a href="index.php" class="btn btn-secondary">Annuler</a>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|