WebOrbiton
v1.0.0.1

StocketBase

89 lines · 3.7 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/includes/config.php';
  6. require_once __DIR__ . '/includes/database.php';
  7. require_once __DIR__ . '/includes/user-auth.php';
  8. require_once __DIR__ . '/includes/language.php';
  9. require_once __DIR__ . '/includes/site-front.php';
  10. require_once __DIR__ . '/includes/pagination.php';
  11. require_once __DIR__ . '/includes/product-card.php';
  12. ​
  13. UserAuth::boot();
  14. ​
  15. $db = Database::site();
  16. $settings = SiteFront::settings();
  17. ​
  18. $slug = trim((string) ($_GET['slug'] ?? ''));
  19. ​
  20. $categoryStatement = $db->prepare('SELECT * FROM categories WHERE slug = :slug LIMIT 1');
  21. $categoryStatement->execute(['slug' => $slug]);
  22. $category = $categoryStatement->fetch();
  23. if ($category) {
  24. $category = Languages::categories([$category])[0];
  25. }
  26. ​
  27. if (!$category) {
  28. http_response_code(404);
  29. $pageTitle = Language::get('page_title_not_found', 'Not found') . ' - ' . $settings['site_name'];
  30. require __DIR__ . '/includes/front-header.php';
  31. echo '<p>' . htmlspecialchars(Language::get('category_not_found', 'Category not found.')) . '</p>';
  32. require __DIR__ . '/includes/front-footer.php';
  33. exit;
  34. }
  35. ​
  36. $currentPage = Pagination::currentPage();
  37. $totalStatement = $db->prepare("SELECT COUNT(*) AS c FROM products p WHERE p.status = 'published' AND p.category_id = :category_id");
  38. $totalStatement->execute(['category_id' => $category['id']]);
  39. $totalProducts = (int) $totalStatement->fetch()['c'];
  40. $isStaticProductLoading = ($settings['product_loading_mode'] ?? 'scroll') === 'static';
  41. $perPage = $isStaticProductLoading
  42. ? max(1, $totalProducts)
  43. : max(1, (int) $settings['products_per_page_category']);
  44. $currentPage = $isStaticProductLoading ? 1 : $currentPage;
  45. $offset = Pagination::offset($currentPage, $perPage);
  46. $totalPages = Pagination::totalPages($totalProducts, $perPage);
  47. ​
  48. $productsStatement = $db->prepare(
  49. "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"
  50. );
  51. $productsStatement->bindValue('category_id', $category['id'], PDO::PARAM_INT);
  52. $productsStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
  53. $productsStatement->bindValue('offset', $offset, PDO::PARAM_INT);
  54. $productsStatement->execute();
  55. $products = $productsStatement->fetchAll();
  56. ​
  57. $pageTitle = $category['name'] . ' - ' . $settings['site_name'];
  58. $pageDescription = $category['description'] ?? '';
  59. $pageType = 'category';
  60. ​
  61. require __DIR__ . '/includes/front-header.php';
  62. ​
  63. ?>
  64. <h1 class="category-title"><?= htmlspecialchars($category['name']) ?></h1>
  65. <?php if (!empty($category['description'])): ?>
  66. <p class="category-description"><?= htmlspecialchars($category['description']) ?></p>
  67. <?php endif; ?>
  68. ​
  69. <div class="article-grid" id="article-list" data-pagination-source="category" data-category-slug="<?= htmlspecialchars($slug, ENT_QUOTES) ?>">
  70. <?php $catalogTopAds = SiteFront::activeAds('catalog-top'); ?>
  71. <?php foreach ($products as $inFeedIndex => $product): ?>
  72. <?= renderProductCard($product) ?>
  73. <?php if ($inFeedIndex === 2 && !empty($catalogTopAds)): ?>
  74. <?php foreach ($catalogTopAds as $catalogTopAd): ?>
  75. <?= SiteFront::renderAd($catalogTopAd) ?>
  76. <?php endforeach; ?>
  77. <?php endif; ?>
  78. <?php endforeach; ?>
  79. <?php if (empty($products)): ?>
  80. <p><?= htmlspecialchars(Language::get('category_no_products', 'No products in this category yet.')) ?></p>
  81. <?php endif; ?>
  82. </div>
  83. ​
  84. <div id="pagination-container">
  85. <?= $isStaticProductLoading ? '' : Pagination::render($settings['pagination_style'], 'category.php', $currentPage, $totalPages, ['slug' => $slug], 'article-list') ?>
  86. </div>
  87. ​
  88. <?php require __DIR__ . '/includes/front-footer.php'; ?>
  89. ​