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.

This commit is contained in:
2026-05-23 23:18:19 +02:00
parent c87d28f7d0
commit b338e2bc30
2 changed files with 15 additions and 11 deletions
-4
View File
@@ -332,9 +332,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delet
], ],
]; ];
if ($featured) {
clear_featured_recipes();
}
if ($slugOriginal && $slugOriginal !== $slug) { if ($slugOriginal && $slugOriginal !== $slug) {
delete_recipe($slugOriginal); delete_recipe($slugOriginal);
} }
@@ -663,6 +660,5 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delet
<button type="submit" class="button" style="width: fit-content;"><?php echo $editing ? 'Save changes' : 'Save recipe'; ?></button> <button type="submit" class="button" style="width: fit-content;"><?php echo $editing ? 'Save changes' : 'Save recipe'; ?></button>
</form> </form>
<?php endif; ?> <?php endif; ?>
</body> </body>
</html> </html>
+15 -7
View File
@@ -258,6 +258,10 @@ function save_recipe(array $recipe, ?PDO $pdo = null): bool {
try { try {
$pdo->beginTransaction(); $pdo->beginTransaction();
if (!empty($recipe['featured'])) {
clear_featured_recipes($pdo);
}
$stmt = $pdo->prepare( $stmt = $pdo->prepare(
'INSERT INTO recipes ( 'INSERT INTO recipes (
slug, hero, prep_time, cook_time, total_time, servings, slug, hero, prep_time, cook_time, total_time, servings,
@@ -329,30 +333,34 @@ function save_recipe(array $recipe, ?PDO $pdo = null): bool {
$block['difficulty'] ?? '', $block['difficulty'] ?? '',
]); ]);
foreach (array_values($block['tags'] ?? []) as $i => $tag) { $tagOrder = 0;
foreach (array_values($block['tags'] ?? []) as $tag) {
$tag = trim((string) $tag); $tag = trim((string) $tag);
if ($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); $line = trim((string) $line);
if ($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); $line = trim((string) $line);
if ($line !== '') { if ($line !== '') {
$utensilStmt->execute([$slug, $lang, $line, $i]); $utensilStmt->execute([$slug, $lang, $line, $utensilOrder++]);
} }
} }
$steps = array_values($block['steps'] ?? []); $steps = array_values($block['steps'] ?? []);
$videos = array_values($block['step_videos'] ?? []); $videos = array_values($block['step_videos'] ?? []);
$timers = array_values($block['step_timers'] ?? []); $timers = array_values($block['step_timers'] ?? []);
$stepOrder = 0;
foreach ($steps as $i => $step) { foreach ($steps as $i => $step) {
$step = trim((string) $step); $step = trim((string) $step);
if ($step === '') { if ($step === '') {
@@ -361,7 +369,7 @@ function save_recipe(array $recipe, ?PDO $pdo = null): bool {
$video = trim((string) ($videos[$i] ?? '')); $video = trim((string) ($videos[$i] ?? ''));
$timerRaw = $timers[$i] ?? ''; $timerRaw = $timers[$i] ?? '';
$timerMinutes = ($timerRaw !== '' && is_numeric($timerRaw)) ? (int) $timerRaw : null; $timerMinutes = ($timerRaw !== '' && is_numeric($timerRaw)) ? (int) $timerRaw : null;
$stepStmt->execute([$slug, $lang, $step, $video, $timerMinutes, $i]); $stepStmt->execute([$slug, $lang, $step, $video, $timerMinutes, $stepOrder++]);
} }
} }