aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/Controllers/GalleryController.php
blob: 2edcd172475414639e40da7dfdb0af55bb538d44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

declare(strict_types=1);
// Gallery: public paginated feed of all posts, newest first.

namespace App\Controllers;

use App\Models\Post;

class GalleryController
{
    private Post $post;
    private const POSTS_PER_PAGE = 5;

    public function __construct()
    {
        $this->post = new Post();
    }

    public function index(): void
    {
        $page = max(1, (int) ($_GET['page'] ?? 1));
        $offset = ($page - 1) * self::POSTS_PER_PAGE;

        $posts = $this->post->findAllPaginated(self::POSTS_PER_PAGE, $offset);
        $totalPosts = $this->post->countAll();
        $totalPages = max(1, (int) ceil($totalPosts / self::POSTS_PER_PAGE));

        $content = __DIR__ . '/../Views/gallery/index.php';
        include __DIR__ . '/../Views/layouts/main.php';
    }
}