This commit is contained in:
2026-03-10 21:37:24 +01:00
parent 5bce68c56b
commit 9d70ea0547
37 changed files with 1534 additions and 282 deletions

View File

@@ -104,6 +104,9 @@ class ApiRouter
$producerRepo = new \Domain\Producer\ProducerRepository($db);
return new Controllers\DictionariesApiController($statusRepo, $transportRepo, $paymentRepo, $attrRepo, $producerRepo);
},
'categories' => function () use ($db) {
return new Controllers\CategoriesApiController();
},
];
}

View File

@@ -0,0 +1,104 @@
<?php
namespace api\Controllers;
use api\ApiRouter;
class CategoriesApiController
{
public function list(): void
{
if (!ApiRouter::requireMethod('GET')) {
return;
}
$db = $GLOBALS['mdb'] ?? null;
if (!$db) {
ApiRouter::sendError('INTERNAL_ERROR', 'Database not available', 500);
return;
}
// Default shop language
$defaultLang = $db->get('pp_langs', 'id', ['start' => 1]);
if (!$defaultLang) {
$defaultLang = 'pl';
}
$defaultLang = (string)$defaultLang;
// All active categories, ordered by display order
$rows = $db->select(
'pp_shop_categories',
['id', 'parent_id'],
[
'status' => 1,
'ORDER' => ['o' => 'ASC'],
]
);
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);
if ($categoryId <= 0) {
continue;
}
$parentId = $row['parent_id'] !== null ? (int)$row['parent_id'] : null;
$title = $titlesByCategory[$categoryId] ?? ('Kategoria #' . $categoryId);
$categories[] = [
'id' => $categoryId,
'parent_id' => $parentId,
'title' => $title,
];
}
ApiRouter::sendSuccess(['categories' => $categories]);
}
}

View File

@@ -437,7 +437,7 @@ class ProductsApiController
// String fields — direct mapping
$stringFields = [
'sku', 'ean', 'custom_label_0', 'custom_label_1', 'custom_label_2',
'custom_label_3', 'custom_label_4', 'wp',
'custom_label_3', 'custom_label_4', 'wp', 'new_to_date', 'additional_message_text',
];
foreach ($stringFields as $field) {
if (isset($body[$field])) {
@@ -447,6 +447,18 @@ class ProductsApiController
}
}
if (isset($body['additional_message'])) {
$d['additional_message'] = !empty($body['additional_message']) ? 'on' : '';
} elseif ($existing !== null) {
$d['additional_message'] = !empty($existing['additional_message']) ? 'on' : '';
}
if (isset($body['additional_message_required'])) {
$d['additional_message_required'] = !empty($body['additional_message_required']) ? 'on' : '';
} elseif ($existing !== null) {
$d['additional_message_required'] = !empty($existing['additional_message_required']) ? 'on' : '';
}
// Foreign keys
if (isset($body['set_id'])) {
$d['set'] = $body['set_id'];