v1.0.0.1
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';
- require_once __DIR__ . '/includes/product-search.php';
- require_once __DIR__ . '/includes/tags.php';
-
- UserAuth::boot();
-
- $db = Database::site();
- $settings = SiteFront::settings();
-
- $source = (string) ($_GET['source'] ?? '');
- $page = Pagination::currentPage();
-
- if ($source === 'category') {
- $slug = trim((string) ($_GET['slug'] ?? ''));
-
- $categoryStatement = $db->prepare('SELECT id FROM categories WHERE slug = :slug LIMIT 1');
- $categoryStatement->execute(['slug' => $slug]);
- $category = $categoryStatement->fetch();
-
- if (!$category) {
- http_response_code(404);
- exit;
- }
-
- $perPage = max(1, (int) $settings['products_per_page_category']);
- $offset = Pagination::offset($page, $perPage);
-
- $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'];
- if (($settings['product_loading_mode'] ?? 'scroll') === 'static') {
- $perPage = max(1, $totalProducts);
- $page = 1;
- $offset = 0;
- }
-
- $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();
-
- $baseUrl = 'category.php';
- $extraParams = ['slug' => $slug];
- } elseif ($source === 'home') {
- $excludedIds = [];
- if (($settings['featured_banner_enabled'] ?? '0') === '1' && ($settings['featured_banner_hide_duplicates'] ?? '0') === '1') {
- if (($settings['featured_banner_mode'] ?? 'static') === 'static') {
- for ($i = 1; $i <= 5; $i++) {
- $id = (int) ($settings['featured_banner_product_' . $i] ?? 0);
- if ($id > 0 && !in_array($id, $excludedIds, true)) {
- $excludedIds[] = $id;
- }
- }
- }
- }
- $excludeSql = empty($excludedIds) ? '' : ' AND p.id NOT IN (' . implode(',', $excludedIds) . ')';
-
- $perPage = max(1, (int) $settings['products_per_page_home']);
- $offset = Pagination::offset($page, $perPage);
-
- $totalProducts = (int) $db->query("SELECT COUNT(*) AS c FROM products p WHERE p.status = 'published'" . $excludeSql)->fetch()['c'];
- if (($settings['product_loading_mode'] ?? 'scroll') === 'static') {
- $perPage = max(1, $totalProducts);
- $page = 1;
- $offset = 0;
- }
-
- $productsStatement = $db->prepare(
- "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"
- );
- $productsStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
- $productsStatement->bindValue('offset', $offset, PDO::PARAM_INT);
- $productsStatement->execute();
- $products = $productsStatement->fetchAll();
-
- $baseUrl = 'index.php';
- $extraParams = [];
- } elseif ($source === 'search') {
- $query = trim((string) ($_GET['q'] ?? ''));
- $categoryId = (int) ($_GET['category'] ?? 0);
- $tagId = ProductTags::isEnabled() ? (int) ($_GET['tag'] ?? 0) : 0;
- $sort = ProductSearch::sanitizeSort((string) ($_GET['sort'] ?? 'newest'));
-
- $perPage = max(1, (int) $settings['products_per_page_home']);
- $result = ProductSearch::run(
- $settings,
- ['q' => $query, 'category' => $categoryId, 'tag' => $tagId, 'sort' => $sort],
- $page,
- $perPage
- );
- $products = $result['products'];
- $totalProducts = $result['total'];
- $page = $result['page'];
- $perPage = $result['perPage'];
-
- $baseUrl = 'search.php';
- $extraParams = ['q' => $query];
- if ($categoryId > 0) {
- $extraParams['category'] = $categoryId;
- }
- if ($tagId > 0) {
- $extraParams['tag'] = $tagId;
- }
- if ($sort !== 'newest') {
- $extraParams['sort'] = $sort;
- }
- } else {
- http_response_code(400);
- exit;
- }
-
- $totalPages = Pagination::totalPages($totalProducts, $perPage);
-
- header('Content-Type: text/html; charset=utf-8');
-
- foreach ($products as $product) {
- echo renderProductCard($product);
- }
-
- echo Pagination::render($settings['pagination_style'], $baseUrl, $page, $totalPages, $extraParams, 'article-list');
-