diff --git a/src/edit_packing_list_details.php b/src/edit_packing_list_details.php index 00719f4..e69de29 100644 --- a/src/edit_packing_list_details.php +++ b/src/edit_packing_list_details.php @@ -1,365 +0,0 @@ - 0) { - $stmt_household_check = $conn->prepare("SELECT household_id FROM users WHERE id = ?"); - $stmt_household_check->bind_param("i", $current_user_id); - $stmt_household_check->execute(); - $current_user_household_id = $stmt_household_check->get_result()->fetch_assoc()['household_id']; - $stmt_household_check->close(); - - $stmt_list_check = $conn->prepare("SELECT id, name, description, user_id, household_id, is_template, todo_list_id FROM packing_lists WHERE id = ?"); - $stmt_list_check->bind_param("i", $packing_list_id); - $stmt_list_check->execute(); - $result = $stmt_list_check->get_result(); - if ($result->num_rows == 1) { - $packing_list = $result->fetch_assoc(); - $is_owner = ($packing_list['user_id'] == $current_user_id); - $is_household_list = !empty($packing_list['household_id']); - $is_in_same_household = ($is_household_list && $packing_list['household_id'] == $current_user_household_id); - - if ($is_owner || $is_in_same_household) { - $can_edit = true; - } else { - $message = '
Keine Berechtigung.
'; - $packing_list = null; - } - } else { - $message = '
Packliste nicht gefunden.
'; - } -} else { - $message = '
Keine ID.
'; -} - -// --- 2. Fetch Data for Dropdowns --- -$available_users = []; -$available_todo_lists = []; -if ($can_edit) { - // Owners: Creator + Household Members (if shared) - if ($packing_list['household_id']) { - $stmt = $conn->prepare("SELECT id, COALESCE(NULLIF(display_name, ''), username) AS username FROM users WHERE household_id = ?"); - $stmt->bind_param("i", $packing_list['household_id']); - } else { - $stmt = $conn->prepare("SELECT id, COALESCE(NULLIF(display_name, ''), username) AS username FROM users WHERE id = ?"); - $stmt->bind_param("i", $packing_list['user_id']); - } - $stmt->execute(); - $res = $stmt->get_result(); - while ($row = $res->fetch_assoc()) { - $available_users[] = $row; - } - - // Fetch Todo Lists - $stmt_tl = $conn->prepare("SELECT id, name FROM todo_lists WHERE user_id = ? OR (household_id IS NOT NULL AND household_id = ?)"); - $stmt_tl->bind_param("ii", $current_user_id, $current_user_household_id); - $stmt_tl->execute(); - $res_tl = $stmt_tl->get_result(); - while ($row = $res_tl->fetch_assoc()) { - $available_todo_lists[] = $row; - } - $stmt_tl->close(); - - // Current Assignments - $current_assignments = []; - $stmt_ca = $conn->prepare("SELECT user_id, backpack_id FROM packing_list_carriers WHERE packing_list_id = ?"); - $stmt_ca->bind_param("i", $packing_list_id); - $stmt_ca->execute(); - $res_ca = $stmt_ca->get_result(); - while ($row = $res_ca->fetch_assoc()) { - $current_assignments[$row['user_id']] = $row['backpack_id']; - } -} - -// --- 3. Handle Form Submission --- -if ($_SERVER["REQUEST_METHOD"] == "POST" && $can_edit) { - // Update Basic Details - $name = trim($_POST['name']); - $description = trim($_POST['description']); - - // Household sharing logic - $new_household_id = NULL; - if (isset($_POST['is_household_list']) && $_POST['is_household_list'] == '1' && $current_user_household_id) { - $new_household_id = $current_user_household_id; - } - - $todo_list_id = !empty($_POST['todo_list_id']) ? intval($_POST['todo_list_id']) : NULL; - - $stmt_update = $conn->prepare("UPDATE packing_lists SET name = ?, description = ?, household_id = ?, todo_list_id = ? WHERE id = ?"); - $stmt_update->bind_param("sssii", $name, $description, $new_household_id, $todo_list_id, $packing_list_id); - $stmt_update->execute(); - $packing_list['name'] = $name; - $packing_list['description'] = $description; - $packing_list['household_id'] = $new_household_id; - $packing_list['todo_list_id'] = $todo_list_id; - - // Handle Participation & Backpacks - // Get all potential users to check if they were unchecked - $participate_map = $_POST['participate'] ?? []; // Array of UserID => "1" if checked - - foreach ($available_users as $user) { - $uid = $user['id']; - $is_checked = isset($participate_map[$uid]); - $was_participating = array_key_exists($uid, $current_assignments); - - if ($is_checked) { - // User participates -> Update/Insert Backpack - $bid = isset($_POST['backpacks'][$uid]) ? intval($_POST['backpacks'][$uid]) : 0; - $bid = ($bid > 0) ? $bid : NULL; - - if ($was_participating) { - $old_bid = $current_assignments[$uid]; - // Update if changed - if ($old_bid != $bid) { - $stmt_up = $conn->prepare("UPDATE packing_list_carriers SET backpack_id = ? WHERE packing_list_id = ? AND user_id = ?"); - $stmt_up->bind_param("iii", $bid, $packing_list_id, $uid); - $stmt_up->execute(); - - // Cleanup Old Containers - cleanup_old_backpack_containers($conn, $packing_list_id, $uid); - - // Sync New - if ($bid) { - sync_backpack_items($conn, $packing_list_id, $uid, $bid); - } - } - } else { - // New Participant - $stmt_in = $conn->prepare("INSERT INTO packing_list_carriers (packing_list_id, user_id, backpack_id) VALUES (?, ?, ?)"); - $stmt_in->bind_param("iii", $packing_list_id, $uid, $bid); - $stmt_in->execute(); - - if ($bid) { - sync_backpack_items($conn, $packing_list_id, $uid, $bid); - } - } - } else { - // User NOT checked -> Remove if existed - if ($was_participating) { - $conn->query("DELETE FROM packing_list_carriers WHERE packing_list_id = $packing_list_id AND user_id = $uid"); - // Cleanup Items: Delete all items carried by this user? - // Or move to unassigned? Let's move to unassigned (NULL) to be safe against data loss. - // But Containers (Backpacks) should be deleted. - - // 1. Delete Containers - cleanup_old_backpack_containers($conn, $packing_list_id, $uid); - - // 2. Move remaining items (loose items) to unassigned? - $conn->query("UPDATE packing_list_items SET carrier_user_id = NULL WHERE packing_list_id = $packing_list_id AND carrier_user_id = $uid"); - } - } - } - - $message = '
Änderungen gespeichert!
'; - // Refresh assignments - $stmt_ca->execute(); - $res_ca = $stmt_ca->get_result(); - $current_assignments = []; - while ($row = $res_ca->fetch_assoc()) { - $current_assignments[$row['user_id']] = $row['backpack_id']; - } -} - -function cleanup_old_backpack_containers($conn, $list_id, $user_id) { - // Find all container items for this user (backpacks or compartments) - $stmt = $conn->prepare("SELECT id FROM packing_list_items WHERE packing_list_id = ? AND carrier_user_id = ? AND (backpack_id IS NOT NULL OR backpack_compartment_id IS NOT NULL)"); - $stmt->bind_param("ii", $list_id, $user_id); - $stmt->execute(); - $res = $stmt->get_result(); - $container_ids = []; - while ($r = $res->fetch_assoc()) $container_ids[] = $r['id']; - - if (!empty($container_ids)) { - $ids_str = implode(',', $container_ids); - // Unparent children so they don't get deleted (or keep them and they get deleted? No, save content) - // Set parent to NULL for children of these containers - $conn->query("UPDATE packing_list_items SET parent_packing_list_item_id = NULL WHERE packing_list_id = $list_id AND parent_packing_list_item_id IN ($ids_str)"); - - // Delete containers - $conn->query("DELETE FROM packing_list_items WHERE id IN ($ids_str)"); - } -} - -$back_link = 'packing_lists.php' . (!empty($packing_list['is_template']) ? '?view=templates' : ''); -$page_headline = !empty($packing_list['is_template']) ? 'Vorlage bearbeiten' : 'Details bearbeiten'; - -?> - -
-
-

:

- Zurück -
-
- - - -
-
-
-
Basisdaten
-
- - -
-
- - -
- -
- - -
Verknüpfe eine ToDo-Liste mit dieser Packliste.
-
- - -
- > - -
- -
- -
-
Teilnehmer & Rucksäcke (Standard)
-
-
-

Wähle hier, wer mitkommt und welchen Rucksack er trägt.

- -
- Ein Rucksack wurde mehrfach ausgewählt! -
- - $my_current_bp_id, - 'backpacks' => $user_backpacks - ]; - ?> -
-
- id="part_"> - -
-
- -
-
- -
-
-
-
- -
- -
- Inhalt bearbeiten - -
-
- -
-
- - - - - - - \ No newline at end of file