v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/includes/config.php';
- require_once __DIR__ . '/includes/database.php';
- require_once __DIR__ . '/includes/user-auth.php';
- require_once __DIR__ . '/includes/language.php';
- require_once __DIR__ . '/includes/site-front.php';
- require_once __DIR__ . '/includes/pagination.php';
- require_once __DIR__ . '/includes/product-card.php';
-
- UserAuth::boot();
-
- $db = Database::site();
- $settings = SiteFront::settings();
-
- $slug = trim((string) ($_GET['slug'] ?? ''));
-
- $categoryStatement = $db->prepare('SELECT * FROM categories WHERE slug = :slug LIMIT 1');
- $categoryStatement->execute(['slug' => $slug]);
- $category = $categoryStatement->fetch();
- if ($category) {
- $category = Languages::categories([$category])[0];
- }
-
- if (!$category) {
- http_response_code(404);
- $pageTitle = 'Not found - ' . $settings['site_name'];
- require __DIR__ . '/includes/front-header.php';
- echo '<p>' . htmlspecialchars(Language::get('category_not_found', 'Category not found.')) . '</p>';
- require __DIR__ . '/includes/front-footer.php';
- exit;
- }
-
- $currentPage = Pagination::currentPage();
- $totalStatement = $db->prepare("SELECT COUNT(*) AS c FROM products p WHERE p.status = 'published' AND p.category_id = :category_id");
- $totalStatement->execute(['category_id' => $category['id']]);
- $totalProducts = (int) $totalStatement->fetch()['c'];
- $isStaticProductLoading = ($settings['product_loading_mode'] ?? 'scroll') === 'static';
- $perPage = $isStaticProductLoading
- ? max(1, $totalProducts)
- : max(1, (int) $settings['products_per_page_category']);
- $currentPage = $isStaticProductLoading ? 1 : $currentPage;
- $offset = Pagination::offset($currentPage, $perPage);
- $totalPages = Pagination::totalPages($totalProducts, $perPage);
-
- $productsStatement = $db->prepare(
- "SELECT p.* FROM products p WHERE p.status = 'published' AND p.category_id = :category_id ORDER BY p.published_at DESC LIMIT :limit OFFSET :offset"
- );
- $productsStatement->bindValue('category_id', $category['id'], PDO::PARAM_INT);
- $productsStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
- $productsStatement->bindValue('offset', $offset, PDO::PARAM_INT);
- $productsStatement->execute();
- $products = $productsStatement->fetchAll();
-
- $pageTitle = $category['name'] . ' - ' . $settings['site_name'];
- $pageDescription = $category['description'] ?? '';
- $pageType = 'category';
-
- require __DIR__ . '/includes/front-header.php';
-
- ?>
- <h1 class="category-title"><?= htmlspecialchars($category['name']) ?></h1>
- <?php if (!empty($category['description'])): ?>
- <p class="category-description"><?= htmlspecialchars($category['description']) ?></p>
- <?php endif; ?>
-
- <div class="article-grid" id="article-list" data-pagination-source="category" data-category-slug="<?= htmlspecialchars($slug, ENT_QUOTES) ?>">
- <?php $catalogTopAds = SiteFront::activeAds('catalog-top'); ?>
- <?php foreach ($products as $inFeedIndex => $product): ?>
- <?= renderProductCard($product) ?>
- <?php if ($inFeedIndex === 2 && !empty($catalogTopAds)): ?>
- <?php foreach ($catalogTopAds as $catalogTopAd): ?>
- <?= SiteFront::renderAd($catalogTopAd) ?>
- <?php endforeach; ?>
- <?php endif; ?>
- <?php endforeach; ?>
- <?php if (empty($products)): ?>
- <p><?= htmlspecialchars(Language::get('category_no_products', 'No products in this category yet.')) ?></p>
- <?php endif; ?>
- </div>
-
- <div id="pagination-container">
- <?= $isStaticProductLoading ? '' : Pagination::render($settings['pagination_style'], 'category.php', $currentPage, $totalPages, ['slug' => $slug], 'article-list') ?>
- </div>
-
- <?php require __DIR__ . '/includes/front-footer.php'; ?>
-