WebOrbiton
v2.0.0.1

Publisium

99 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. require_once __DIR__ . '/includes/featured-banner.php';
  13. ​
  14. UserAuth::boot();
  15. ​
  16. $db = Database::site();
  17. $settings = SiteFront::settings();
  18. ​
  19. $source = (string) ($_GET['source'] ?? '');
  20. $page = Pagination::currentPage();
  21. ​
  22. if ($source === 'category') {
  23. $slug = trim((string) ($_GET['slug'] ?? ''));
  24. ​
  25. $categoryStatement = $db->prepare('SELECT id FROM categories WHERE slug = :slug LIMIT 1');
  26. $categoryStatement->execute(['slug' => $slug]);
  27. $category = $categoryStatement->fetch();
  28. ​
  29. if (!$category) {
  30. http_response_code(404);
  31. exit;
  32. }
  33. ​
  34. $perPage = max(1, (int) $settings['articles_per_page_category']);
  35. $offset = Pagination::offset($page, $perPage);
  36. ​
  37. $totalStatement = $db->prepare("SELECT COUNT(*) AS c FROM articles a WHERE a.status = 'published' AND a.category_id = :category_id" . Languages::articleFilterSql('a'));
  38. $totalStatement->execute(['category_id' => $category['id']]);
  39. $totalArticles = (int) $totalStatement->fetch()['c'];
  40. if (($settings['article_loading_mode'] ?? 'scroll') === 'static') {
  41. $perPage = max(1, $totalArticles);
  42. $page = 1;
  43. $offset = 0;
  44. }
  45. ​
  46. $articlesStatement = $db->prepare(
  47. "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"
  48. );
  49. $articlesStatement->bindValue('category_id', $category['id'], PDO::PARAM_INT);
  50. $articlesStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
  51. $articlesStatement->bindValue('offset', $offset, PDO::PARAM_INT);
  52. $articlesStatement->execute();
  53. $articles = Languages::articles($articlesStatement->fetchAll());
  54. ​
  55. $baseUrl = 'category.php';
  56. $extraParams = ['slug' => $slug];
  57. } elseif ($source === 'home') {
  58. $excludedIds = featuredExcludedIds($settings, Languages::onlyTranslated(featuredBannerArticles($db, $settings)));
  59. $excludeSql = (empty($excludedIds) ? '' : ' AND a.id NOT IN (' . implode(',', $excludedIds) . ')') . Languages::articleFilterSql('a');
  60. ​
  61. $perPage = max(1, (int) $settings['articles_per_page_home']);
  62. $offset = Pagination::offset($page, $perPage);
  63. ​
  64. $totalArticles = (int) $db->query("SELECT COUNT(*) AS c FROM articles a WHERE a.status = 'published'" . $excludeSql)->fetch()['c'];
  65. if (($settings['article_loading_mode'] ?? 'scroll') === 'static') {
  66. $perPage = max(1, $totalArticles);
  67. $page = 1;
  68. $offset = 0;
  69. }
  70. ​
  71. $articlesStatement = $db->prepare(
  72. "SELECT a.*, c.name AS category_name, c.slug AS category_slug FROM articles a LEFT JOIN categories c ON c.id = a.category_id WHERE a.status = 'published'" . $excludeSql . " ORDER BY a.published_at DESC LIMIT :limit OFFSET :offset"
  73. );
  74. $articlesStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
  75. $articlesStatement->bindValue('offset', $offset, PDO::PARAM_INT);
  76. $articlesStatement->execute();
  77. $articles = Languages::articles($articlesStatement->fetchAll());
  78. ​
  79. $baseUrl = SiteFront::homeUrl();
  80. $extraParams = [];
  81. } else {
  82. http_response_code(400);
  83. exit;
  84. }
  85. ​
  86. $totalPages = Pagination::totalPages($totalArticles, $perPage);
  87. ​
  88. header('Content-Type: text/html; charset=utf-8');
  89. ​
  90. if (Languages::current() !== '') {
  91. ob_start([Languages::class, 'absolutizeUrls']);
  92. }
  93. ​
  94. foreach ($articles as $article) {
  95. echo renderArticleCard($article);
  96. }
  97. ​
  98. echo Pagination::render($settings['pagination_style'], $baseUrl, $page, $totalPages, $extraParams, 'article-list');
  99. ​