Files
PhotoGal/web/public/index.php

66 lines
2.0 KiB
PHP

<?php
// public/index.php
require_once '../vendor/autoload.php';
use Hpz\Photogal\Database;
use Hpz\Photogal\Album;
use Hpz\Photogal\Gallery;
$dotenv = Dotenv\Dotenv::createImmutable('../');
$dotenv->load();
if (!isset($_ENV['DB_FILE_PATH'])) {
die('DB_FILE_PATH is not set in the .env file');
}
if (!file_exists('../' . $_ENV['DB_FILE_PATH'])) {
die('Database file does not exist');
}
// Create a new Database connection
$dbFilePath = '../' . $_ENV['DB_FILE_PATH'];
$database = new Database($dbFilePath);
$albumClass = new Album($database);
$galleryClass = new Gallery($database);
$requestUri = trim($_SERVER['REQUEST_URI'], '/');
$requestParts = explode('/', $requestUri);
if ($requestParts[0] === 'gallery' && isset($requestParts[1])) {
// Gallery view
$album = urldecode($requestParts[1]);
$page = isset($requestParts[2]) ? (int)$requestParts[2] : 1;
// Retrieve limit, sort order, and sort direction from cookies or defaults
$limit = $_COOKIE['limit'] ?? 24;
$sortOrder = $_COOKIE['sortOrder'] ?? 'modified_date';
$sortDirection = $_COOKIE['sortDirection'] ?? 'DESC';
// Get images and total count for the selected album
$offset = ($page - 1) * $limit;
$images = $galleryClass->getGalleryImages($album, $limit, $offset, $sortOrder, $sortDirection);
$totalImages = $galleryClass->countImages($album);
$totalPages = ceil($totalImages / $limit);
// Limit the number of pages displayed in the pagination
$paginationRange = $_ENV['PAGINATION_RANGE'] ?? 5;
$startPage = max(1, $page - floor($paginationRange / 2));
$endPage = min($totalPages, $startPage + $paginationRange - 1);
// Render gallery view
include '../src/views/gallery.phtml';
} else {
// Album view
$albums = $albumClass->getAlbums();
$albumPreviews = [];
foreach ($albums as $album) {
$albumPreviews[$album] = $albumClass->getAlbumPreview($album);
}
// Render album view
include '../src/views/albums.phtml';
}
?>