From ec77d2f77b96488b1bc170ced2abab12b3c19416 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Sat, 21 Mar 2026 22:46:34 +0100 Subject: Add user's own posts grid and post deletion to editor page --- src/app/Controllers/EditorController.php | 37 +++++++++++++++++++++++++- src/app/Views/editor/index.php | 15 +++++++++++ src/config/routes.php | 1 + src/public/css/style.css | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/app/Controllers/EditorController.php b/src/app/Controllers/EditorController.php index 7cfb9b9..c7dd9fc 100644 --- a/src/app/Controllers/EditorController.php +++ b/src/app/Controllers/EditorController.php @@ -31,6 +31,8 @@ class EditorController // Map filesystem paths to URL paths the browser can load $overlays = array_map(static fn($path) => '/assets/overlays/' . basename($path), $overlayFiles); + $userPosts = $this->post->findByUserId($_SESSION['user_id']); + $content = __DIR__ . '/../Views/editor/index.php'; include __DIR__ . '/../Views/layouts/main.php'; } @@ -102,7 +104,40 @@ class EditorController $relativePath = 'uploads/posts/' . basename($outputPath); $this->post->create($_SESSION['user_id'], $relativePath); - echo json_encode(['success' => true, 'redirect' => '/gallery']); + echo json_encode(['success' => true, 'redirect' => '/editor']); + } + + public function destroy(string $id): void + { + if (!isset($_SESSION['user_id'])) { + header('Location: /login'); + return; + } + + if (!Csrf::validate($_POST['csrf_token'] ?? '')) { + Flash::set('error', 'Invalid CSRF token.'); + header('Location: /editor'); + return; + } + + $post = $this->post->findById((int) $id); + + // Only the post owner can delete it + if (!$post || $post['user_id'] !== $_SESSION['user_id']) { + Flash::set('error', 'Post not found.'); + header('Location: /editor'); + return; + } + + // Delete the image file from disk + $filePath = \dirname(__DIR__, 2) . '/' . $post['image_path']; + if (file_exists($filePath)) { + unlink($filePath); + } + + $this->post->delete((int) $id); + Flash::set('success', 'Post deleted.'); + header('Location: /editor'); } /** diff --git a/src/app/Views/editor/index.php b/src/app/Views/editor/index.php index 624002a..7f2c258 100644 --- a/src/app/Views/editor/index.php +++ b/src/app/Views/editor/index.php @@ -46,4 +46,19 @@ + + +

My posts

+
+ +
+ My post +
+ + +
+
+ +
+ diff --git a/src/config/routes.php b/src/config/routes.php index c8bb52f..aa7e34e 100644 --- a/src/config/routes.php +++ b/src/config/routes.php @@ -31,3 +31,4 @@ $router->get('/gallery', 'GalleryController', 'index'); // Editor $router->get('/editor', 'EditorController', 'show'); $router->post('/editor', 'EditorController', 'store'); +$router->post('/editor/delete/{id}', 'EditorController', 'destroy'); diff --git a/src/public/css/style.css b/src/public/css/style.css index e152519..2119a40 100644 --- a/src/public/css/style.css +++ b/src/public/css/style.css @@ -407,6 +407,51 @@ footer { margin-top: 0.5rem; } +.my-posts-title { + margin-top: 2rem; + margin-bottom: 1rem; + font-size: 1.2rem; +} + +.my-posts-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); + gap: 1rem; +} + +.my-post { + background: #fff; + border: 1px solid #dbdbdb; + border-radius: 8px; + overflow: hidden; +} + +.my-post img { + width: 100%; + aspect-ratio: 1; + object-fit: cover; + display: block; +} + +.delete-form { + padding: 0.5rem; +} + +.btn-delete { + width: 100%; + padding: 0.4rem; + background: #fff; + color: #b71c1c; + border: 1px solid #b71c1c; + border-radius: 4px; + font-size: 0.85rem; + cursor: pointer; +} + +.btn-delete:hover { + background: #fdecea; +} + @media (max-width: 600px) { header nav { flex-direction: column; -- cgit v1.2.3