add routing, move code to classes; misc changes

This commit is contained in:
2024-08-27 20:59:58 -05:00
parent 5438f9e358
commit 86827a7b10
13 changed files with 434 additions and 168 deletions

View File

@@ -4,10 +4,12 @@
require_once '../vendor/autoload.php';
use Hpz\Photogal\Database;
use Hpz\Photogal\Image;
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');
}
@@ -18,48 +20,46 @@ if (!file_exists('../' . $_ENV['DB_FILE_PATH'])) {
// Create a new Database connection
$dbFilePath = '../' . $_ENV['DB_FILE_PATH'];
$database = new Database($dbFilePath);
$image = new Image($database);
// Get the list of albums
$albumsQuery = "SELECT DISTINCT album FROM images";
$albumsResult = $database->getConnection()->query($albumsQuery);
$albums = [];
while ($row = $albumsResult->fetchArray(SQLITE3_ASSOC)) {
$albums[] = $row['album'];
}
$albumClass = new Album($database);
$galleryClass = new Gallery($database);
// Fetch the first image from each album
$albumPreviews = [];
foreach ($albums as $album) {
$albumPreviews[$album] = $image->getImages($album, 1, 0)[0] ?? null;
$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';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Photo Gallery - Select Album</title>
<link href="/css/styles.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
<div class="container mx-auto p-4">
<h1 class="text-3xl font-bold mb-4">Select an Album</h1>
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
<?php foreach ($albumPreviews as $album => $image): ?>
<?php if ($image): ?>
<div class="bg-white shadow-md rounded-lg overflow-hidden">
<a href="gallery.php?album=<?= urlencode($album) ?>">
<img src="images/<?= $album ?>/thumbs/<?= $image['title'] ?>.webp" alt="<?= htmlspecialchars($album) ?>" class="w-full h-48 object-cover">
<div class="p-4">
<h2 class="text-xl font-semibold"><?= htmlspecialchars($album) ?></h2>
</div>
</a>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</body>
</html>