WebOrbiton
v1.0.0.6

Publisium

92 lines · 3.8 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/article-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('not_found_title', 'Not found') . ' - ' . $settings['site_name'];
  30. $showBanner = false;
  31. require __DIR__ . '/includes/front-header.php';
  32. echo '<p>' . htmlspecialchars(Language::get('category_not_found', 'We couldn’t find this category.')) . '</p>';
  33. require __DIR__ . '/includes/front-footer.php';
  34. exit;
  35. }
  36. ​
  37. $currentPage = Pagination::currentPage();
  38. $totalStatement = $db->prepare("SELECT COUNT(*) AS c FROM articles a WHERE a.status = 'published' AND a.category_id = :category_id" . Languages::articleFilterSql('a'));
  39. $totalStatement->execute(['category_id' => $category['id']]);
  40. $totalArticles = (int) $totalStatement->fetch()['c'];
  41. $isStaticArticleLoading = ($settings['article_loading_mode'] ?? 'scroll') === 'static';
  42. $perPage = $isStaticArticleLoading
  43. ? max(1, $totalArticles)
  44. : max(1, (int) $settings['articles_per_page_category']);
  45. $currentPage = $isStaticArticleLoading ? 1 : $currentPage;
  46. $offset = Pagination::offset($currentPage, $perPage);
  47. $totalPages = Pagination::totalPages($totalArticles, $perPage);
  48. ​
  49. $articlesStatement = $db->prepare(
  50. "SELECT a.* FROM articles a WHERE a.status = 'published' AND a.category_id = :category_id" . Languages::articleFilterSql('a') . " ORDER BY a.published_at DESC LIMIT :limit OFFSET :offset"
  51. );
  52. $articlesStatement->bindValue('category_id', $category['id'], PDO::PARAM_INT);
  53. $articlesStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
  54. $articlesStatement->bindValue('offset', $offset, PDO::PARAM_INT);
  55. $articlesStatement->execute();
  56. $articles = Languages::articles($articlesStatement->fetchAll());
  57. ​
  58. $pageTitle = $category['name'] . ' - ' . $settings['site_name'];
  59. $pageDescription = $category['description'] ?? '';
  60. $showBanner = false;
  61. $pageType = 'category';
  62. ​
  63. require __DIR__ . '/includes/front-header.php';
  64. ​
  65. ?>
  66. <h1 class="category-title"><?= htmlspecialchars($category['name']) ?></h1>
  67. <?php if (!empty($category['description'])): ?>
  68. <p class="category-description"><?= htmlspecialchars($category['description']) ?></p>
  69. <?php endif; ?>
  70. ​
  71. <?= SiteFront::renderAds('category-top') ?>
  72. ​
  73. <div class="article-grid" id="article-list" data-pagination-source="category" data-category-slug="<?= htmlspecialchars($slug, ENT_QUOTES) ?>">
  74. <?php $inFeedAds = SiteFront::activeAds('in-feed'); ?>
  75. <?php foreach ($articles as $inFeedIndex => $article): ?>
  76. <?= renderArticleCard($article) ?>
  77. <?php if ($inFeedIndex === 2 && !empty($inFeedAds)): ?>
  78. <?php foreach ($inFeedAds as $inFeedAd): ?>
  79. <?= SiteFront::renderAd($inFeedAd) ?>
  80. <?php endforeach; ?>
  81. <?php endif; ?>
  82. <?php endforeach; ?>
  83. <?php if (empty($articles)): ?>
  84. <p><?= htmlspecialchars(Language::get('category_no_articles', 'No articles in this category yet.')) ?></p>
  85. <?php endif; ?>
  86. </div>
  87. ​
  88. <div id="pagination-container">
  89. <?= $isStaticArticleLoading ? '' : Pagination::render($settings['pagination_style'], 'category.php', $currentPage, $totalPages, ['slug' => $slug], 'article-list') ?>
  90. </div>
  91. ​
  92. <?php require __DIR__ . '/includes/front-footer.php'; ?>