diff --git a/README.md b/README.md index 26de12e..8da2926 100644 --- a/README.md +++ b/README.md @@ -244,3 +244,4 @@ Das Projekt basiert auf bewährten Web-Standards: * Bugfix ("Headers already sent"): Fehler beim Speichern im Artikel-Editor (`edit_article.php`) behoben, indem Output-Buffering hinzugefügt wurde. * **Features:** * Die Gewichts-Anzeige in der Rucksackübersicht (`backpacks.php`) wurde angepasst und zeigt nun das "Basisgewicht + Zusatzgewicht" in Klammern an anstatt nur dem Zusatzgewicht. + * In der Rucksack-Bearbeitung (`edit_backpack.php`) kann nun der Besitzer des Rucksacks (analog zu Artikeln) innerhalb des Haushalts geändert werden. diff --git a/src/edit_backpack.php b/src/edit_backpack.php index b7be59d..123e84b 100644 --- a/src/edit_backpack.php +++ b/src/edit_backpack.php @@ -110,12 +110,25 @@ if ($backpack_id > 0) { // Load Manufacturers & Categories $hh_ids = [$user_id]; +$all_household_members = []; if ($household_id) { - $stmt_hhm = $conn->prepare("SELECT id FROM users WHERE household_id = ?"); + $stmt_hhm = $conn->prepare("SELECT id, COALESCE(NULLIF(display_name, ''), username) AS username FROM users WHERE household_id = ? ORDER BY username ASC"); $stmt_hhm->bind_param("i", $household_id); $stmt_hhm->execute(); $res_hhm = $stmt_hhm->get_result(); - while($r = $res_hhm->fetch_assoc()) $hh_ids[] = $r['id']; + while($r = $res_hhm->fetch_assoc()) { + if (!in_array($r['id'], $hh_ids)) $hh_ids[] = $r['id']; + $all_household_members[] = $r; + } +} +// Ensure current user is in $all_household_members if not already +$found = false; +foreach ($all_household_members as $m) { if ($m['id'] == $user_id) $found = true; } +if (!$found) { + $stmt_u = $conn->prepare("SELECT id, COALESCE(NULLIF(display_name, ''), username) AS username FROM users WHERE id = ?"); + $stmt_u->bind_param("i", $user_id); + $stmt_u->execute(); + if ($row = $stmt_u->get_result()->fetch_assoc()) $all_household_members[] = $row; } $placeholders = implode(',', array_fill(0, count($hh_ids), '?')); $types_hh = str_repeat('i', count($hh_ids)); @@ -208,14 +221,15 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } $final_household_id = ($share_household && $household_id) ? $household_id : NULL; + $new_owner_id = !empty($_POST['owner_id']) ? intval($_POST['owner_id']) : $user_id; if ($backpack_id > 0) { - $stmt = $conn->prepare("UPDATE backpacks SET name=?, manufacturer=?, model=?, weight_grams=?, volume_liters=?, household_id=?, image_url=?, product_url=?, category_id=? WHERE id=? AND user_id=?"); - $stmt->bind_param("sssiisssiii", $name, $manufacturer, $model, $weight, $volume, $final_household_id, $image_url_for_db, $product_url_input, $category_id, $backpack_id, $user_id); + $stmt = $conn->prepare("UPDATE backpacks SET user_id=?, name=?, manufacturer=?, model=?, weight_grams=?, volume_liters=?, household_id=?, image_url=?, product_url=?, category_id=? WHERE id=? AND user_id=?"); + $stmt->bind_param("isssiisssiii", $new_owner_id, $name, $manufacturer, $model, $weight, $volume, $final_household_id, $image_url_for_db, $product_url_input, $category_id, $backpack_id, $user_id); $stmt->execute(); } else { $stmt = $conn->prepare("INSERT INTO backpacks (user_id, household_id, name, manufacturer, model, weight_grams, volume_liters, image_url, product_url, category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - $stmt->bind_param("iisssiissi", $user_id, $final_household_id, $name, $manufacturer, $model, $weight, $volume, $image_url_for_db, $product_url_input, $category_id); + $stmt->bind_param("iisssiissi", $new_owner_id, $final_household_id, $name, $manufacturer, $model, $weight, $volume, $image_url_for_db, $product_url_input, $category_id); $stmt->execute(); $backpack_id = $stmt->insert_id; } @@ -358,12 +372,25 @@ require_once 'header.php'; -
-
+
+
>
+ +
+ + +
+