From 1e40da7235f35c62f8494a9e431ec502abb181b3 Mon Sep 17 00:00:00 2001 From: morgane Date: Thu, 18 Jun 2026 08:56:27 +0000 Subject: [PATCH] Actualiser api/index.php --- api/index.php | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/api/index.php b/api/index.php index 6dfa575..8cae7d4 100644 --- a/api/index.php +++ b/api/index.php @@ -976,6 +976,18 @@ try { case 'subcategories_update': subcategoriesUpdate($db); 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': recipesList($db); break; @@ -12204,6 +12216,103 @@ function subcategoriesUpdate(PDO $db): void { 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 ===== function appSettingsGet(PDO $db): void {