Initial commit
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
define('BASE_PATH', realpath(__DIR__ . '/../'));
|
||||
|
||||
require_once BASE_PATH . '/src/db.php';
|
||||
require_once BASE_PATH . '/src/PostManager.php';
|
||||
require_once BASE_PATH . '/src/FileManager.php';
|
||||
|
||||
$action = $_GET['action'] ?? 'list';
|
||||
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;
|
||||
|
||||
$postManager = new PostManager($db);
|
||||
$fileManager = new FileManager($db, __DIR__ . '/assets/uploads');
|
||||
|
||||
|
||||
// Gérer les accès
|
||||
// les fonctions create, delete, edit doit être autorisée aux personnes dont les roles leur permette
|
||||
|
||||
|
||||
|
||||
// Afficher la bonne page
|
||||
switch ($action) {
|
||||
case 'create':
|
||||
$title = $_POST['title'] ?? '';
|
||||
$content = $_POST['content'] ?? '';
|
||||
$published_at = $_POST['published_at'] ?? date('Y-m-d H:i:s');
|
||||
$published_at = str_replace('T', ' ', $published_at); // conversion HTML -> SQL
|
||||
$errors = [];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (trim($title) === '') {
|
||||
$errors[] = 'Le titre est obligatoire.';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$postId = $postManager->create($title, $content, $published_at);
|
||||
|
||||
if (!empty($_FILES['files']['name'][0])) {
|
||||
foreach ($_FILES['files']['tmp_name'] as $i => $tmpName) {
|
||||
if ($_FILES['files']['error'][$i] === UPLOAD_ERR_OK) {
|
||||
$file = [
|
||||
'name' => $_FILES['files']['name'][$i],
|
||||
'type' => $_FILES['files']['type'][$i],
|
||||
'tmp_name' => $_FILES['files']['tmp_name'][$i],
|
||||
'error' => $_FILES['files']['error'][$i],
|
||||
'size' => $_FILES['files']['size'][$i],
|
||||
];
|
||||
$fileManager->upload($postId, $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
header("Location: route.php");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$formAction = 'route.php?action=create';
|
||||
$action = 'create';
|
||||
include BASE_PATH . '/templates/post_form.php';
|
||||
break;
|
||||
|
||||
case 'view':
|
||||
if (!$id) {
|
||||
echo "ID manquant.";
|
||||
exit;
|
||||
}
|
||||
|
||||
$post = $postManager->get($id);
|
||||
if (!$post) {
|
||||
echo "Post introuvable.";
|
||||
exit;
|
||||
}
|
||||
|
||||
include __DIR__ . '/../templates/post_view.php';
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
if ($id) {
|
||||
$postManager->delete($id);
|
||||
}
|
||||
header("Location: route.php");
|
||||
exit;
|
||||
|
||||
case 'edit':
|
||||
if (!$id) {
|
||||
echo "ID manquant.";
|
||||
exit;
|
||||
}
|
||||
|
||||
$post = $postManager->get($id);
|
||||
if (!$post) {
|
||||
echo "Post introuvable.";
|
||||
exit;
|
||||
}
|
||||
|
||||
$title = $_POST['title'] ?? $post['title'];
|
||||
$content = $_POST['content'] ?? $post['content'];
|
||||
$published_at = $_POST['published_at'] ?? date('Y-m-d\TH:i', strtotime($post['created_at']));
|
||||
$published = isset($_POST['published']) ? true : $post['is_published'];
|
||||
$errors = [];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (trim($title) === '') {
|
||||
$errors[] = 'Le titre est obligatoire.';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$published_at_sql = str_replace('T', ' ', $_POST['published_at']);
|
||||
$postManager->update($id, $title, $content, $published_at_sql, $published);
|
||||
|
||||
if (!empty($_FILES['files']['name'][0])) {
|
||||
foreach ($_FILES['files']['tmp_name'] as $i => $tmpName) {
|
||||
if ($_FILES['files']['error'][$i] === UPLOAD_ERR_OK) {
|
||||
$file = [
|
||||
'name' => $_FILES['files']['name'][$i],
|
||||
'type' => $_FILES['files']['type'][$i],
|
||||
'tmp_name' => $_FILES['files']['tmp_name'][$i],
|
||||
'error' => $_FILES['files']['error'][$i],
|
||||
'size' => $_FILES['files']['size'][$i],
|
||||
];
|
||||
$fileManager->upload($id, $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
header("Location: route.php?action=view&id=$id");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$formAction = "route.php?action=edit&id=$id";
|
||||
$action = 'edit';
|
||||
include BASE_PATH . '/templates/post_form.php';
|
||||
break;
|
||||
|
||||
case 'list':
|
||||
default:
|
||||
$posts = $postManager->getAll();
|
||||
include BASE_PATH . '/templates/post_list.php';
|
||||
break;
|
||||
}
|
||||
Reference in New Issue
Block a user