баги с превью

This commit is contained in:
Alisa
2026-06-05 19:57:23 +03:00
parent 98987b5355
commit 8f9bc92271
6 changed files with 338 additions and 45 deletions
+64 -2
View File
@@ -13,6 +13,9 @@ final class CatalogController
{
$categoryRepository = new CategoryRepository();
$productRepository = new ProductRepository();
$pagination = $this->paginationInput();
$totalProducts = $productRepository->catalogTotal(true);
$products = $productRepository->catalogPreview($pagination['limit'], $pagination['offset'], true);
view('catalog/index', [
'title' => 'Каталог Рыбсток - рыба, морепродукты, мясо, сыры и продукты',
@@ -24,7 +27,8 @@ final class CatalogController
],
'categories' => $categoryRepository->tree(),
'currentCategory' => null,
'products' => $productRepository->catalogPreview(48),
'products' => $products,
'pagination' => $this->paginationData('/catalog', $totalProducts, count($products), $pagination),
]);
}
@@ -48,6 +52,9 @@ final class CatalogController
}
$categoryName = (string) $category['name'];
$pagination = $this->paginationInput();
$totalProducts = $productRepository->categoryTotal((int) $category['id'], true);
$products = $productRepository->forCategory((int) $category['id'], $pagination['limit'], $pagination['offset'], true);
view('catalog/index', [
'title' => $category['seo_title'] ?: $categoryName . ' - купить в Рыбсток с доставкой по Москве',
@@ -56,7 +63,8 @@ final class CatalogController
'breadcrumbs' => $categoryRepository->breadcrumbs((int) $category['id']),
'categories' => $categoryRepository->tree(),
'currentCategory' => $category,
'products' => $productRepository->forCategory((int) $category['id'], 48),
'products' => $products,
'pagination' => $this->paginationData('/catalog/' . (string) $category['slug'], $totalProducts, count($products), $pagination),
]);
}
@@ -100,4 +108,58 @@ final class CatalogController
'images' => $productRepository->imagesForProduct((int) $product['id']),
]);
}
/**
* @return array{page:int, limit:int, offset:int, per_page:int, cumulative:bool}
*/
private function paginationInput(): array
{
$perPage = 48;
$cumulativeLimit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
if ($cumulativeLimit > 0) {
$limit = max($perPage, min($cumulativeLimit, 5000));
return [
'page' => 1,
'limit' => $limit,
'offset' => 0,
'per_page' => $perPage,
'cumulative' => true,
];
}
$page = max(1, (int) ($_GET['page'] ?? 1));
return [
'page' => $page,
'limit' => $perPage,
'offset' => ($page - 1) * $perPage,
'per_page' => $perPage,
'cumulative' => false,
];
}
/**
* @param array{page:int, limit:int, offset:int, per_page:int, cumulative:bool} $input
* @return array<string, mixed>
*/
private function paginationData(string $basePath, int $total, int $currentCount, array $input): array
{
$shown = min($total, $input['offset'] + $currentCount);
$nextLimit = min($total, max($shown, $input['limit']) + $input['per_page']);
return [
'base_path' => $basePath,
'total' => $total,
'shown' => $shown,
'page' => $input['page'],
'pages' => max(1, (int) ceil($total / $input['per_page'])),
'per_page' => $input['per_page'],
'limit' => $input['limit'],
'has_more' => $shown < $total,
'next_limit' => $nextLimit,
'cumulative' => $input['cumulative'],
];
}
}