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:
@@ -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
|
||||
<button type="submit" class="button" style="width: fit-content;"><?php echo $editing ? 'Save changes' : 'Save recipe'; ?></button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+15
-7
@@ -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++]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user