From b338e2bc30d18e986b3633c6d79674adf5de06ff Mon Sep 17 00:00:00 2001 From: LordSchmackes Date: Sat, 23 May 2026 23:18:19 +0200 Subject: [PATCH] Refactor recipe saving logic in helpers.php to include featured recipe handling and update execution order for tags, ingredients, utensils, and steps. Remove redundant featured recipe clearing from admin.php. --- admin.php | 4 ---- helpers.php | 22 +++++++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/admin.php b/admin.php index 85db7d0..b07411a 100644 --- a/admin.php +++ b/admin.php @@ -332,9 +332,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delet ], ]; - if ($featured) { - clear_featured_recipes(); - } if ($slugOriginal && $slugOriginal !== $slug) { delete_recipe($slugOriginal); } @@ -663,6 +660,5 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delet - diff --git a/helpers.php b/helpers.php index d9cb7cf..1774209 100644 --- a/helpers.php +++ b/helpers.php @@ -258,6 +258,10 @@ function save_recipe(array $recipe, ?PDO $pdo = null): bool { try { $pdo->beginTransaction(); + if (!empty($recipe['featured'])) { + clear_featured_recipes($pdo); + } + $stmt = $pdo->prepare( 'INSERT INTO recipes ( slug, hero, prep_time, cook_time, total_time, servings, @@ -329,30 +333,34 @@ function save_recipe(array $recipe, ?PDO $pdo = null): bool { $block['difficulty'] ?? '', ]); - foreach (array_values($block['tags'] ?? []) as $i => $tag) { + $tagOrder = 0; + foreach (array_values($block['tags'] ?? []) as $tag) { $tag = trim((string) $tag); if ($tag !== '') { - $tagStmt->execute([$slug, $lang, $tag, $i]); + $tagStmt->execute([$slug, $lang, $tag, $tagOrder++]); } } - foreach (array_values($block['ingredients'] ?? []) as $i => $line) { + $ingredientOrder = 0; + foreach (array_values($block['ingredients'] ?? []) as $line) { $line = trim((string) $line); if ($line !== '') { - $ingredientStmt->execute([$slug, $lang, $line, $i]); + $ingredientStmt->execute([$slug, $lang, $line, $ingredientOrder++]); } } - foreach (array_values($block['utensils'] ?? []) as $i => $line) { + $utensilOrder = 0; + foreach (array_values($block['utensils'] ?? []) as $line) { $line = trim((string) $line); if ($line !== '') { - $utensilStmt->execute([$slug, $lang, $line, $i]); + $utensilStmt->execute([$slug, $lang, $line, $utensilOrder++]); } } $steps = array_values($block['steps'] ?? []); $videos = array_values($block['step_videos'] ?? []); $timers = array_values($block['step_timers'] ?? []); + $stepOrder = 0; foreach ($steps as $i => $step) { $step = trim((string) $step); if ($step === '') { @@ -361,7 +369,7 @@ function save_recipe(array $recipe, ?PDO $pdo = null): bool { $video = trim((string) ($videos[$i] ?? '')); $timerRaw = $timers[$i] ?? ''; $timerMinutes = ($timerRaw !== '' && is_numeric($timerRaw)) ? (int) $timerRaw : null; - $stepStmt->execute([$slug, $lang, $step, $video, $timerMinutes, $i]); + $stepStmt->execute([$slug, $lang, $step, $video, $timerMinutes, $stepOrder++]); } }