Actualiser api/index.php
CI / PHP Syntax Check (push) Has been cancelled
CI / JavaScript Lint (push) Has been cancelled
CI / Docker Build Test (push) Has been cancelled
CI / Validate Translation Files (push) Has been cancelled
CI / Auto-merge develop → main (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
Security Scan (Trivy) / Trivy — Docker image scan (push) Has been cancelled
Security Scan (Trivy) / Trivy — Filesystem scan (push) Has been cancelled

This commit is contained in:
2026-06-18 08:56:27 +00:00
parent 046355d6b0
commit 1e40da7235
+109
View File
@@ -976,6 +976,18 @@ try {
case 'subcategories_update': case 'subcategories_update':
subcategoriesUpdate($db); subcategoriesUpdate($db);
break; break;
case 'categories_list':
categoriesList($db);
break;
case 'categories_add':
categoriesAdd($db);
break;
case 'categories_remove':
categoriesRemove($db);
break;
case 'categories_update':
categoriesUpdate($db);
break;
case 'recipes_list': case 'recipes_list':
recipesList($db); recipesList($db);
break; break;
@@ -12204,6 +12216,103 @@ function subcategoriesUpdate(PDO $db): void {
echo json_encode(['success' => true]); echo json_encode(['success' => true]);
} }
function categoriesList(PDO $db): void {
$rows = $db->query("SELECT key, label, icon, keywords, sort_order, is_builtin FROM categories ORDER BY sort_order ASC, id ASC")->fetchAll();
echo json_encode(['success' => true, 'categories' => $rows]);
}
function categoriesAdd(PDO $db): void {
$input = json_decode(file_get_contents('php://input'), true) ?? [];
$label = trim($input['label'] ?? '');
$icon = trim($input['icon'] ?? '📦');
$keywords = trim($input['keywords'] ?? '');
if ($label === '') {
echo json_encode(['success' => false, 'error' => 'label required']);
return;
}
$key = mb_strtolower(trim($label));
$key = preg_replace('/[^a-z0-9]+/u', '_', $key);
$key = trim($key, '_');
if ($key === '') {
echo json_encode(['success' => false, 'error' => 'invalid label']);
return;
}
$stmt = $db->prepare("SELECT id FROM categories WHERE key = ?");
$stmt->execute([$key]);
if ($stmt->fetch()) {
echo json_encode(['success' => false, 'error' => 'category already exists']);
return;
}
$maxOrder = (int)$db->query("SELECT COALESCE(MAX(sort_order), 0) FROM categories")->fetchColumn();
$stmt = $db->prepare("INSERT INTO categories (key, label, icon, keywords, sort_order, is_builtin) VALUES (?, ?, ?, ?, ?, 0)");
$stmt->execute([$key, $label, $icon, $keywords, $maxOrder + 1]);
echo json_encode(['success' => true, 'key' => $key]);
}
function categoriesRemove(PDO $db): void {
$input = json_decode(file_get_contents('php://input'), true) ?? [];
$key = trim($input['key'] ?? '');
if ($key === '') {
echo json_encode(['success' => false, 'error' => 'key required']);
return;
}
$stmt = $db->prepare("SELECT is_builtin FROM categories WHERE key = ?");
$stmt->execute([$key]);
$row = $stmt->fetch();
if (!$row) {
echo json_encode(['success' => false, 'error' => 'category not found']);
return;
}
if ((int)$row['is_builtin'] === 1) {
echo json_encode(['success' => false, 'error' => 'cannot delete a builtin category']);
return;
}
$stmt = $db->prepare("SELECT COUNT(*) FROM products WHERE category = ?");
$stmt->execute([$key]);
if ((int)$stmt->fetchColumn() > 0) {
echo json_encode(['success' => false, 'error' => 'category still used by products']);
return;
}
$db->prepare("DELETE FROM categories WHERE key = ?")->execute([$key]);
echo json_encode(['success' => true]);
}
function categoriesUpdate(PDO $db): void {
$input = json_decode(file_get_contents('php://input'), true) ?? [];
$key = trim($input['key'] ?? '');
$label = trim($input['label'] ?? '');
$icon = trim($input['icon'] ?? '');
$keywords = trim($input['keywords'] ?? '');
if ($key === '' || $label === '') {
echo json_encode(['success' => false, 'error' => 'key and label required']);
return;
}
$stmt = $db->prepare("SELECT id FROM categories WHERE key = ?");
$stmt->execute([$key]);
if (!$stmt->fetch()) {
echo json_encode(['success' => false, 'error' => 'category not found']);
return;
}
$stmt = $db->prepare("UPDATE categories SET label = ?, icon = ?, keywords = ? WHERE key = ?");
$stmt->execute([$label, $icon ?: '📦', $keywords, $key]);
echo json_encode(['success' => true]);
}
// ===== SHARED APP DATA FUNCTIONS ===== // ===== SHARED APP DATA FUNCTIONS =====
function appSettingsGet(PDO $db): void { function appSettingsGet(PDO $db): void {