баги с превью

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
+43 -17
View File
@@ -27,12 +27,15 @@ final class ProductRepository extends BaseRepository
/**
* @return array<int, array<string, mixed>>
*/
public function catalogPreview(int $limit = 24): array
public function catalogPreview(int $limit = 24, int $offset = 0, bool $includeHidden = false): array
{
$limit = max(1, min($limit, 60));
$limit = max(1, min($limit, 5000));
$offset = max(0, $offset);
$sql = $this->previewSelectSql(
'ORDER BY p.sort_order, p.name',
$limit
'ORDER BY (p.is_published = 0 OR p.is_available = 0), p.sort_order, p.name',
$limit,
$offset,
$includeHidden
);
$statement = $this->pdo()->query($sql);
@@ -40,12 +43,21 @@ final class ProductRepository extends BaseRepository
return $this->preparePreviewProducts($statement->fetchAll());
}
public function catalogTotal(bool $includeHidden = false): int
{
$statusSql = $includeHidden ? '' : ' AND p.is_published = 1 AND p.is_available = 1';
$statement = $this->pdo()->query('SELECT COUNT(*) FROM products p WHERE 1=1' . $statusSql);
return (int) $statement->fetchColumn();
}
/**
* @return array<int, array<string, mixed>>
*/
public function forCategory(int $categoryId, int $limit = 48): array
public function forCategory(int $categoryId, int $limit = 48, int $offset = 0, bool $includeHidden = false): array
{
$limit = max(1, min($limit, 96));
$limit = max(1, min($limit, 5000));
$offset = max(0, $offset);
$categoryIds = (new CategoryRepository())->descendantIds($categoryId);
$categoryParents = $this->categoryParentMap();
$requestedRootId = $this->rootCategoryId($categoryId, $categoryParents);
@@ -69,8 +81,10 @@ final class ProductRepository extends BaseRepository
)
)
)
ORDER BY p.sort_order, p.name',
5000
ORDER BY (p.is_published = 0 OR p.is_available = 0), p.sort_order, p.name',
5000,
0,
$includeHidden
);
$statement = $this->pdo()->prepare($sql);
@@ -94,13 +108,22 @@ final class ProductRepository extends BaseRepository
) {
$filtered[] = $product;
}
if (count($filtered) >= $limit) {
break;
}
}
return $filtered;
usort($filtered, static function (array $left, array $right): int {
$leftUnavailable = ((int) ($left['is_published'] ?? 1) === 0 || (int) ($left['is_available'] ?? 1) === 0) ? 1 : 0;
$rightUnavailable = ((int) ($right['is_published'] ?? 1) === 0 || (int) ($right['is_available'] ?? 1) === 0) ? 1 : 0;
return [$leftUnavailable, (int) ($left['sort_order'] ?? 0), (string) ($left['name'] ?? '')]
<=> [$rightUnavailable, (int) ($right['sort_order'] ?? 0), (string) ($right['name'] ?? '')];
});
return array_slice($filtered, $offset, $limit);
}
public function categoryTotal(int $categoryId, bool $includeHidden = false): int
{
return count($this->forCategory($categoryId, 5000, 0, $includeHidden));
}
/**
@@ -252,7 +275,7 @@ final class ProductRepository extends BaseRepository
}
}
private function previewSelectSql(string $tailSql, int $limit): string
private function previewSelectSql(string $tailSql, int $limit, int $offset = 0, bool $includeHidden = false): string
{
return 'SELECT
p.id,
@@ -264,12 +287,15 @@ final class ProductRepository extends BaseRepository
p.seo_description,
p.seo_keywords,
p.category_id,
p.is_published,
p.is_available,
c.name AS category_name,
c.slug AS category_slug,
p.main_image_path,
p.base_unit,
p.package_display_mode,
p.sales_count,
p.sort_order,
v.id AS variant_id,
v.name AS variant_name,
v.unit,
@@ -291,10 +317,10 @@ final class ProductRepository extends BaseRepository
ORDER BY pv.is_default DESC, pv.sort_order, pv.id
LIMIT 1
)
WHERE p.is_published = 1
AND p.is_available = 1
WHERE 1=1
' . ($includeHidden ? '' : 'AND p.is_published = 1 AND p.is_available = 1') . '
' . $tailSql . '
LIMIT ' . $limit;
LIMIT ' . $limit . ' OFFSET ' . $offset;
}
/**