Dev #9
@@ -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) {
|
||||
|
Hardcoding languages Hardcoding languages `en` and `de` might become problematic if more languages are added. It would be more maintainable to fetch the list of supported languages from a configuration or a dedicated table if the application grows.
|
||||
$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
This function handles saving a recipe to the database, including its translations and related items. It uses transactions for atomicity, which is excellent. However, the repeated calls to
DELETEfor all related tables before inserting new data can be inefficient for updates. If a recipe is updated frequently, consider anUPSERTapproach or more targeted updates for related data rather than full deletes and re-inserts.