diff --git a/autoload/api/ApiRouter.php b/autoload/api/ApiRouter.php index 6e38b16..f7d88e5 100644 --- a/autoload/api/ApiRouter.php +++ b/autoload/api/ApiRouter.php @@ -105,8 +105,7 @@ class ApiRouter return new Controllers\DictionariesApiController($statusRepo, $transportRepo, $paymentRepo, $attrRepo, $producerRepo); }, 'categories' => function () use ($db) { - $categoryRepo = new \Domain\Category\CategoryRepository($db); - return new Controllers\CategoriesApiController($categoryRepo); + return new Controllers\CategoriesApiController(); }, ]; } diff --git a/autoload/api/Controllers/CategoriesApiController.php b/autoload/api/Controllers/CategoriesApiController.php index e5c7017..66f6918 100644 --- a/autoload/api/Controllers/CategoriesApiController.php +++ b/autoload/api/Controllers/CategoriesApiController.php @@ -2,17 +2,9 @@ namespace api\Controllers; use api\ApiRouter; -use Domain\Category\CategoryRepository; class CategoriesApiController { - private $categoryRepo; - - public function __construct(CategoryRepository $categoryRepo) - { - $this->categoryRepo = $categoryRepo; - } - public function list(): void { if (!ApiRouter::requireMethod('GET')) { @@ -25,14 +17,14 @@ class CategoriesApiController return; } - // Pobierz domyślny język sklepu + // Default shop language $defaultLang = $db->get('pp_langs', 'id', ['start' => 1]); if (!$defaultLang) { $defaultLang = 'pl'; } $defaultLang = (string)$defaultLang; - // Pobierz wszystkie aktywne kategorie (płaska lista) + // All active categories, ordered by display order $rows = $db->select( 'pp_shop_categories', ['id', 'parent_id'], @@ -42,11 +34,54 @@ class CategoriesApiController ] ); - if (!is_array($rows)) { + if (!is_array($rows) || empty($rows)) { ApiRouter::sendSuccess(['categories' => []]); return; } + $categoryIds = array_values(array_filter( + array_map(fn($row) => (int)($row['id'] ?? 0), $rows), + fn($id) => $id > 0 + )); + + // Bulk fetch titles for default language + $titlesByCategory = []; + $titleRows = $db->select('pp_shop_categories_langs', ['category_id', 'title'], [ + 'AND' => [ + 'category_id' => $categoryIds, + 'lang_id' => $defaultLang, + 'title[!]' => '', + ], + ]); + if (is_array($titleRows)) { + foreach ($titleRows as $tr) { + $tid = (int)($tr['category_id'] ?? 0); + if ($tid > 0 && !isset($titlesByCategory[$tid])) { + $titlesByCategory[$tid] = (string)($tr['title'] ?? ''); + } + } + } + + // Bulk fetch fallback titles for categories without a title in default language + $missingIds = array_values(array_filter($categoryIds, fn($id) => !isset($titlesByCategory[$id]))); + if (!empty($missingIds)) { + $fallbackRows = $db->select('pp_shop_categories_langs', ['category_id', 'title'], [ + 'AND' => [ + 'category_id' => $missingIds, + 'title[!]' => '', + ], + ]); + if (is_array($fallbackRows)) { + foreach ($fallbackRows as $fr) { + $fid = (int)($fr['category_id'] ?? 0); + if ($fid > 0 && !isset($titlesByCategory[$fid])) { + $titlesByCategory[$fid] = (string)($fr['title'] ?? ''); + } + } + } + } + + // Build flat category list $categories = []; foreach ($rows as $row) { $categoryId = (int)($row['id'] ?? 0); @@ -54,28 +89,13 @@ class CategoriesApiController continue; } - $title = $db->get('pp_shop_categories_langs', 'title', [ - 'AND' => [ - 'category_id' => $categoryId, - 'lang_id' => $defaultLang, - ], - ]); - - // Fallback: jeśli brak tłumaczenia w domyślnym języku, weź pierwsze dostępne - if (!$title) { - $title = $db->get('pp_shop_categories_langs', 'title', [ - 'category_id' => $categoryId, - 'title[!]' => '', - 'LIMIT' => 1, - ]); - } - $parentId = $row['parent_id'] !== null ? (int)$row['parent_id'] : null; + $title = $titlesByCategory[$categoryId] ?? ('Kategoria #' . $categoryId); $categories[] = [ 'id' => $categoryId, 'parent_id' => $parentId, - 'title' => (string)($title ?? 'Kategoria #' . $categoryId), + 'title' => $title, ]; }