diff --git a/packliste.sql b/packliste.sql index f94c626..1903ab7 100644 --- a/packliste.sql +++ b/packliste.sql @@ -402,6 +402,7 @@ CREATE TABLE `packing_lists` ( `name` varchar(255) NOT NULL, `description` text DEFAULT NULL, `share_token` varchar(255) DEFAULT NULL, + `is_template` tinyint(1) NOT NULL DEFAULT 0, `created_at` timestamp NOT NULL DEFAULT current_timestamp(), PRIMARY KEY (`id`), UNIQUE KEY `share_token` (`share_token`), diff --git a/src/add_packing_list.php b/src/add_packing_list.php index a116ef0..ea5cbde 100644 --- a/src/add_packing_list.php +++ b/src/add_packing_list.php @@ -13,6 +13,7 @@ if (!isset($_SESSION['user_id'])) { require_once 'db_connect.php'; require_once 'household_actions.php'; require_once 'backpack_utils.php'; +require_once 'packing_list_utils.php'; $current_user_id = $_SESSION['user_id']; $message = ''; @@ -26,6 +27,23 @@ $stmt_household->execute(); $household_id_for_user = $stmt_household->get_result()->fetch_assoc()['household_id']; $stmt_household->close(); +// Fetch available templates +$templates = []; +$sql_templates = "SELECT id, name FROM packing_lists WHERE is_template = 1 AND (user_id = ? OR household_id = ?)"; +$stmt_templates = $conn->prepare($sql_templates); +// Falls household_id NULL ist, binden wir trotzdem User ID als Dummy oder handeln es anders. +// Einfacher: Wir nutzen current_user_id zweimal, wenn household NULL, oder Logik im SQL. +// Da household_id INT ist, kann es NULL sein. +// Besser: +$h_id = $household_id_for_user ?: 0; // 0 matches nothing usually, safe +$stmt_templates->bind_param("ii", $current_user_id, $h_id); +$stmt_templates->execute(); +$res_templates = $stmt_templates->get_result(); +while ($row = $res_templates->fetch_assoc()) { + $templates[] = $row; +} +$stmt_templates->close(); + // Fetch Users for Backpack Assignment UI $available_users = []; if ($household_id_for_user) { @@ -45,21 +63,25 @@ $stmt_u->close(); if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim($_POST['name']); $description = trim($_POST['description']); + $template_id = isset($_POST['template_id']) ? intval($_POST['template_id']) : 0; $household_id = isset($_POST['is_household_list']) && $household_id_for_user ? $household_id_for_user : NULL; - // Server-Side Validation for duplicate backpacks - $selected_backpacks = []; + // Server-Side Validation for duplicate backpacks (ONLY if no template is selected) $has_duplicate_backpacks = false; - if (isset($_POST['participate']) && is_array($_POST['participate'])) { - foreach ($_POST['participate'] as $uid => $val) { - if ($val) { - $bid = isset($_POST['backpacks'][$uid]) ? intval($_POST['backpacks'][$uid]) : 0; - if ($bid > 0) { - if (in_array($bid, $selected_backpacks)) { - $has_duplicate_backpacks = true; - break; + + if ($template_id == 0) { + $selected_backpacks = []; + if (isset($_POST['participate']) && is_array($_POST['participate'])) { + foreach ($_POST['participate'] as $uid => $val) { + if ($val) { + $bid = isset($_POST['backpacks'][$uid]) ? intval($_POST['backpacks'][$uid]) : 0; + if ($bid > 0) { + if (in_array($bid, $selected_backpacks)) { + $has_duplicate_backpacks = true; + break; + } + $selected_backpacks[] = $bid; } - $selected_backpacks[] = $bid; } } } @@ -70,7 +92,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { } elseif ($has_duplicate_backpacks) { $message = '
Wähle aus, wer mitkommt und wer welchen Rucksack trägt.
-Wähle aus, wer mitkommt und wer welchen Rucksack trägt.
+