diff --git a/api/index.php b/api/index.php index 63fdd73..d757ca7 100644 --- a/api/index.php +++ b/api/index.php @@ -1012,6 +1012,18 @@ try { case 'recipe_tags_update': recipeTagsUpdate($db); break; + case 'custom_units_list': + customUnitsList($db); + break; + case 'custom_units_add': + customUnitsAdd($db); + break; + case 'custom_units_remove': + customUnitsRemove($db); + break; + case 'custom_units_update': + customUnitsUpdate($db); + break; case 'recipes_list': recipesList($db); break; @@ -12494,12 +12506,99 @@ function recipeTagsUpdate(PDO $db): void { return; } - $stmt = $db->prepare("UPDATE recipe_tags SET label = ?, icon = ? WHERE key = ?"); + $stmt = $db->prepare("UPDATE recipe_tags SET label = ?, icon = ? WHERE key = ?"); $stmt->execute([$label, $icon ?: '🏷️', $key]); echo json_encode(['success' => true]); } +function customUnitsList(PDO $db): void { + $rows = $db->query("SELECT id, key, label, icon, base_unit, factor, sort_order FROM custom_units ORDER BY sort_order ASC, id ASC")->fetchAll(); + echo json_encode(['success' => true, 'units' => $rows]); +} + +function customUnitsAdd(PDO $db): void { + $input = json_decode(file_get_contents('php://input'), true) ?? []; + $key = trim($input['key'] ?? ''); + $label = trim($input['label'] ?? ''); + $icon = trim($input['icon'] ?? '📏'); + $baseUnit = trim($input['base_unit'] ?? ''); + $factor = (float)($input['factor'] ?? 1); + + if ($label === '' || $key === '' || !in_array($baseUnit, ['pz', 'g', 'ml'], true) || $factor <= 0) { + echo json_encode(['success' => false, 'error' => 'key, label, base_unit (pz/g/ml) et factor (>0) requis']); + return; + } + + $key = mb_strtolower($key); + $key = preg_replace('/[^a-z0-9]+/u', '_', $key); + $key = trim($key, '_'); + if ($key === '' || in_array($key, ['pz', 'g', 'ml', 'conf'], true)) { + echo json_encode(['success' => false, 'error' => 'clé invalide ou réservée']); + return; + } + + $stmt = $db->prepare("SELECT id FROM custom_units WHERE key = ?"); + $stmt->execute([$key]); + if ($stmt->fetch()) { + echo json_encode(['success' => false, 'error' => 'unit already exists']); + return; + } + + $maxOrder = (int)$db->query("SELECT COALESCE(MAX(sort_order), 0) FROM custom_units")->fetchColumn(); + + $stmt = $db->prepare("INSERT INTO custom_units (key, label, icon, base_unit, factor, sort_order) VALUES (?, ?, ?, ?, ?, ?)"); + $stmt->execute([$key, $label, $icon, $baseUnit, $factor, $maxOrder + 1]); + + echo json_encode(['success' => true, 'key' => $key]); +} + +function customUnitsRemove(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 COUNT(*) FROM products WHERE display_unit_key = ?"); + $stmt->execute([$key]); + if ((int)$stmt->fetchColumn() > 0) { + echo json_encode(['success' => false, 'error' => 'unit still used by a product']); + return; + } + + $db->prepare("DELETE FROM custom_units WHERE key = ?")->execute([$key]); + echo json_encode(['success' => true]); +} + +function customUnitsUpdate(PDO $db): void { + $input = json_decode(file_get_contents('php://input'), true) ?? []; + $key = trim($input['key'] ?? ''); + $label = trim($input['label'] ?? ''); + $icon = trim($input['icon'] ?? ''); + $baseUnit = trim($input['base_unit'] ?? ''); + $factor = (float)($input['factor'] ?? 0); + + if ($key === '' || $label === '' || !in_array($baseUnit, ['pz', 'g', 'ml'], true) || $factor <= 0) { + echo json_encode(['success' => false, 'error' => 'champs invalides']); + return; + } + + $stmt = $db->prepare("SELECT id FROM custom_units WHERE key = ?"); + $stmt->execute([$key]); + if (!$stmt->fetch()) { + echo json_encode(['success' => false, 'error' => 'unit not found']); + return; + } + + $stmt = $db->prepare("UPDATE custom_units SET label = ?, icon = ?, base_unit = ?, factor = ? WHERE key = ?"); + $stmt->execute([$label, $icon ?: '📏', $baseUnit, $factor, $key]); + + echo json_encode(['success' => true]); +} + // ===== SHARED APP DATA FUNCTIONS ===== function appSettingsGet(PDO $db): void {