WebOrbiton
v1.0.0.1

StocketBase

135 lines · 4.9 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. require_once __DIR__ . '/includes/product-search.php';
  13. require_once __DIR__ . '/includes/tags.php';
  14. ​
  15. UserAuth::boot();
  16. ​
  17. $db = Database::site();
  18. $settings = SiteFront::settings();
  19. ​
  20. $source = (string) ($_GET['source'] ?? '');
  21. $page = Pagination::currentPage();
  22. ​
  23. if ($source === 'category') {
  24. $slug = trim((string) ($_GET['slug'] ?? ''));
  25. ​
  26. $categoryStatement = $db->prepare('SELECT id FROM categories WHERE slug = :slug LIMIT 1');
  27. $categoryStatement->execute(['slug' => $slug]);
  28. $category = $categoryStatement->fetch();
  29. ​
  30. if (!$category) {
  31. http_response_code(404);
  32. exit;
  33. }
  34. ​
  35. $perPage = max(1, (int) $settings['products_per_page_category']);
  36. $offset = Pagination::offset($page, $perPage);
  37. ​
  38. $totalStatement = $db->prepare("SELECT COUNT(*) AS c FROM products p WHERE p.status = 'published' AND p.category_id = :category_id");
  39. $totalStatement->execute(['category_id' => $category['id']]);
  40. $totalProducts = (int) $totalStatement->fetch()['c'];
  41. if (($settings['product_loading_mode'] ?? 'scroll') === 'static') {
  42. $perPage = max(1, $totalProducts);
  43. $page = 1;
  44. $offset = 0;
  45. }
  46. ​
  47. $productsStatement = $db->prepare(
  48. "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"
  49. );
  50. $productsStatement->bindValue('category_id', $category['id'], PDO::PARAM_INT);
  51. $productsStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
  52. $productsStatement->bindValue('offset', $offset, PDO::PARAM_INT);
  53. $productsStatement->execute();
  54. $products = $productsStatement->fetchAll();
  55. ​
  56. $baseUrl = 'category.php';
  57. $extraParams = ['slug' => $slug];
  58. } elseif ($source === 'home') {
  59. $excludedIds = [];
  60. if (($settings['featured_banner_enabled'] ?? '0') === '1' && ($settings['featured_banner_hide_duplicates'] ?? '0') === '1') {
  61. if (($settings['featured_banner_mode'] ?? 'static') === 'static') {
  62. for ($i = 1; $i <= 5; $i++) {
  63. $id = (int) ($settings['featured_banner_product_' . $i] ?? 0);
  64. if ($id > 0 && !in_array($id, $excludedIds, true)) {
  65. $excludedIds[] = $id;
  66. }
  67. }
  68. }
  69. }
  70. $excludeSql = empty($excludedIds) ? '' : ' AND p.id NOT IN (' . implode(',', $excludedIds) . ')';
  71. ​
  72. $perPage = max(1, (int) $settings['products_per_page_home']);
  73. $offset = Pagination::offset($page, $perPage);
  74. ​
  75. $totalProducts = (int) $db->query("SELECT COUNT(*) AS c FROM products p WHERE p.status = 'published'" . $excludeSql)->fetch()['c'];
  76. if (($settings['product_loading_mode'] ?? 'scroll') === 'static') {
  77. $perPage = max(1, $totalProducts);
  78. $page = 1;
  79. $offset = 0;
  80. }
  81. ​
  82. $productsStatement = $db->prepare(
  83. "SELECT p.*, c.name AS category_name, c.slug AS category_slug FROM products p LEFT JOIN categories c ON c.id = p.category_id WHERE p.status = 'published'" . $excludeSql . " ORDER BY p.published_at DESC LIMIT :limit OFFSET :offset"
  84. );
  85. $productsStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
  86. $productsStatement->bindValue('offset', $offset, PDO::PARAM_INT);
  87. $productsStatement->execute();
  88. $products = $productsStatement->fetchAll();
  89. ​
  90. $baseUrl = 'index.php';
  91. $extraParams = [];
  92. } elseif ($source === 'search') {
  93. $query = trim((string) ($_GET['q'] ?? ''));
  94. $categoryId = (int) ($_GET['category'] ?? 0);
  95. $tagId = ProductTags::isEnabled() ? (int) ($_GET['tag'] ?? 0) : 0;
  96. $sort = ProductSearch::sanitizeSort((string) ($_GET['sort'] ?? 'newest'));
  97. ​
  98. $perPage = max(1, (int) $settings['products_per_page_home']);
  99. $result = ProductSearch::run(
  100. $settings,
  101. ['q' => $query, 'category' => $categoryId, 'tag' => $tagId, 'sort' => $sort],
  102. $page,
  103. $perPage
  104. );
  105. $products = $result['products'];
  106. $totalProducts = $result['total'];
  107. $page = $result['page'];
  108. $perPage = $result['perPage'];
  109. ​
  110. $baseUrl = 'search.php';
  111. $extraParams = ['q' => $query];
  112. if ($categoryId > 0) {
  113. $extraParams['category'] = $categoryId;
  114. }
  115. if ($tagId > 0) {
  116. $extraParams['tag'] = $tagId;
  117. }
  118. if ($sort !== 'newest') {
  119. $extraParams['sort'] = $sort;
  120. }
  121. } else {
  122. http_response_code(400);
  123. exit;
  124. }
  125. ​
  126. $totalPages = Pagination::totalPages($totalProducts, $perPage);
  127. ​
  128. header('Content-Type: text/html; charset=utf-8');
  129. ​
  130. foreach ($products as $product) {
  131. echo renderProductCard($product);
  132. }
  133. ​
  134. echo Pagination::render($settings['pagination_style'], $baseUrl, $page, $totalPages, $extraParams, 'article-list');
  135. ​