From 2461087d9b1ac86d40ca7651effbf6bf09796be1 Mon Sep 17 00:00:00 2001 From: Jacek Pyziak Date: Fri, 27 Feb 2026 18:51:26 +0100 Subject: [PATCH] feat: add categories/list API endpoint Co-Authored-By: Claude Sonnet 4.6 --- autoload/api/ApiRouter.php | 4 + .../Controllers/CategoriesApiController.php | 84 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 autoload/api/Controllers/CategoriesApiController.php diff --git a/autoload/api/ApiRouter.php b/autoload/api/ApiRouter.php index 3bc8d11..6e38b16 100644 --- a/autoload/api/ApiRouter.php +++ b/autoload/api/ApiRouter.php @@ -104,6 +104,10 @@ class ApiRouter $producerRepo = new \Domain\Producer\ProducerRepository($db); return new Controllers\DictionariesApiController($statusRepo, $transportRepo, $paymentRepo, $attrRepo, $producerRepo); }, + 'categories' => function () use ($db) { + $categoryRepo = new \Domain\Category\CategoryRepository($db); + return new Controllers\CategoriesApiController($categoryRepo); + }, ]; } diff --git a/autoload/api/Controllers/CategoriesApiController.php b/autoload/api/Controllers/CategoriesApiController.php new file mode 100644 index 0000000..e5c7017 --- /dev/null +++ b/autoload/api/Controllers/CategoriesApiController.php @@ -0,0 +1,84 @@ +categoryRepo = $categoryRepo; + } + + public function list(): void + { + if (!ApiRouter::requireMethod('GET')) { + return; + } + + $db = $GLOBALS['mdb'] ?? null; + if (!$db) { + ApiRouter::sendError('INTERNAL_ERROR', 'Database not available', 500); + return; + } + + // Pobierz domyślny język sklepu + $defaultLang = $db->get('pp_langs', 'id', ['start' => 1]); + if (!$defaultLang) { + $defaultLang = 'pl'; + } + $defaultLang = (string)$defaultLang; + + // Pobierz wszystkie aktywne kategorie (płaska lista) + $rows = $db->select( + 'pp_shop_categories', + ['id', 'parent_id'], + [ + 'status' => 1, + 'ORDER' => ['o' => 'ASC'], + ] + ); + + if (!is_array($rows)) { + ApiRouter::sendSuccess(['categories' => []]); + return; + } + + $categories = []; + foreach ($rows as $row) { + $categoryId = (int)($row['id'] ?? 0); + if ($categoryId <= 0) { + 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; + + $categories[] = [ + 'id' => $categoryId, + 'parent_id' => $parentId, + 'title' => (string)($title ?? 'Kategoria #' . $categoryId), + ]; + } + + ApiRouter::sendSuccess(['categories' => $categories]); + } +}