This commit is contained in:
Alisa
2026-06-07 21:14:30 +03:00
parent 76bf5d8191
commit 66e931e0d6
14 changed files with 1079 additions and 12 deletions
+138 -1
View File
@@ -18,6 +18,7 @@ final class HomeController
$categories = [];
$catalogProducts = [];
$popularProducts = [];
$homeCatalogSections = [];
$settings = [];
$notice = null;
@@ -25,10 +26,65 @@ final class HomeController
Database::pdo()->query('SELECT 1');
$dbStatus = 'connected';
$categories = (new CategoryRepository())->tree();
$categoryRepository = new CategoryRepository();
$categories = $categoryRepository->tree();
$productRepository = new ProductRepository();
$catalogProducts = $productRepository->catalogPreview(24);
$popularProducts = $productRepository->popularPreview(10);
$homeCatalogSections[] = [
'key' => 'promos',
'title' => 'Акции',
'url' => '/catalog',
'count' => 10,
'kind' => 'promo',
'products' => $productRepository->promoPreview(10),
];
$homeCatalogSections[] = [
'key' => 'new',
'title' => 'Новинки',
'url' => '/catalog',
'count' => 50,
'kind' => 'new',
'products' => $productRepository->newPreview(10),
];
foreach ([
['slugs' => ['ikra'], 'title' => 'Икра'],
['slugs' => ['ryba'], 'title' => 'Рыба'],
['slugs' => ['moreprodukty'], 'title' => 'Морепродукты'],
['slugs' => ['myaso'], 'title' => 'Мясо'],
['slugs' => ['polufabrikaty'], 'title' => 'Полуфабрикаты'],
['slugs' => ['syry'], 'title' => 'Сыры'],
['slugs' => ['frukty-ovoschi-yagody-griby'], 'title' => 'Фрукты, овощи, ягоды, грибы'],
['slugs' => ['mramornaya-govyadina'], 'title' => 'Мраморная говядина'],
['slugs' => ['kopchenaya-ryba', 'kopchenaya-riba-1'], 'title' => 'Копченая рыба'],
['slugs' => ['vyalenaya-ryba', 'vylaenaya-riba'], 'title' => 'Вяленая рыба'],
['slugs' => ['malosolnaya-ryba', 'malosolnaya-riba'], 'title' => 'Малосольная рыба'],
] as $sectionConfig) {
$categoryMatch = $this->homeCategoryShowcase(
$categoryRepository,
$productRepository,
$categories,
$sectionConfig['slugs'],
$sectionConfig['title']
);
if ($categoryMatch === null) {
continue;
}
$category = $categoryMatch['category'];
$showcase = $categoryMatch['showcase'];
$slug = (string) ($sectionConfig['slugs'][0] ?? $category['slug']);
$homeCatalogSections[] = [
'key' => $slug,
'title' => $sectionConfig['title'],
'url' => '/catalog/' . $category['slug'],
'count' => $showcase['total'],
'kind' => 'category',
'products' => $showcase['products'],
];
}
$settings = (new SettingRepository())->allKeyed();
} catch (Throwable $exception) {
$dbStatus = app_env('APP_DEBUG', 'false') === 'true'
@@ -49,9 +105,90 @@ final class HomeController
'categories' => $categories,
'catalogProducts' => $catalogProducts,
'popularProducts' => $popularProducts,
'homeCatalogSections' => $homeCatalogSections,
'settings' => $settings,
'notice' => $notice,
'buildVersion' => '2026-06-04-02',
]);
}
/**
* @param array<int, array<string, mixed>> $categoryTree
* @param array<int, string> $slugs
* @return array{category: array<string, mixed>, showcase: array{products: array<int, array<string, mixed>>, total: int}}|null
*/
private function homeCategoryShowcase(
CategoryRepository $categoryRepository,
ProductRepository $productRepository,
array $categoryTree,
array $slugs,
string $title
): ?array {
$candidates = [];
$seenIds = [];
foreach ($slugs as $slug) {
$category = $categoryRepository->findBySlug($slug);
if ($category !== null) {
$categoryId = (int) $category['id'];
$seenIds[$categoryId] = true;
$candidates[] = $category;
}
}
foreach ($this->findCategoriesByName($categoryTree, $title) as $category) {
$categoryId = (int) $category['id'];
if (isset($seenIds[$categoryId])) {
continue;
}
$seenIds[$categoryId] = true;
$candidates[] = $category;
}
$fallback = null;
foreach ($candidates as $category) {
$showcase = $productRepository->categoryRetailShowcase((int) $category['id'], 10);
$match = [
'category' => $category,
'showcase' => $showcase,
];
if ($showcase['products'] !== []) {
return $match;
}
$fallback ??= $match;
}
return $fallback;
}
/**
* @param array<int, array<string, mixed>> $categories
* @return array<int, array<string, mixed>>
*/
private function findCategoriesByName(array $categories, string $name): array
{
$matches = [];
$normalizedName = $this->normalizeCategoryName($name);
foreach ($categories as $category) {
if ($this->normalizeCategoryName((string) ($category['name'] ?? '')) === $normalizedName) {
$matches[] = $category;
}
$matches = array_merge(
$matches,
$this->findCategoriesByName($category['children'] ?? [], $name)
);
}
return $matches;
}
private function normalizeCategoryName(string $name): string
{
return trim(str_replace('ё', 'е', mb_strtolower($name)));
}
}