diff --git a/src/add_article.php b/src/add_article.php index f74714e..d76cc76 100644 --- a/src/add_article.php +++ b/src/add_article.php @@ -1,7 +1,6 @@ prepare("SELECT id, name FROM categories WHERE user_id IN ($placeholders) ORDER BY name ASC"); $stmt_cat_load->bind_param($types, ...$household_member_ids); $stmt_cat_load->execute(); $categories = $stmt_cat_load->get_result()->fetch_all(MYSQLI_ASSOC); $stmt_cat_load->close(); +// Lade Hersteller $stmt_man_load = $conn->prepare("SELECT id, name FROM manufacturers WHERE user_id IN ($placeholders) ORDER BY name ASC"); $stmt_man_load->bind_param($types, ...$household_member_ids); $stmt_man_load->execute(); $manufacturers = $stmt_man_load->get_result()->fetch_all(MYSQLI_ASSOC); $stmt_man_load->close(); +// Lade Lagerorte $stmt_loc_load = $conn->prepare("SELECT id, name, parent_id FROM storage_locations WHERE user_id IN ($placeholders) ORDER BY parent_id, name"); $stmt_loc_load->bind_param($types, ...$household_member_ids); $stmt_loc_load->execute(); $all_locations = $stmt_loc_load->get_result()->fetch_all(MYSQLI_ASSOC); $stmt_loc_load->close(); +// Lade Eltern-Artikel $all_parent_params = array_merge($household_member_ids, [$household_id_for_user]); $all_parent_types = $types . 'i'; $stmt_parent_articles = $conn->prepare("SELECT a.id, a.name, m.name as manufacturer_name, a.product_designation FROM articles a LEFT JOIN manufacturers m ON a.manufacturer_id = m.id WHERE (a.user_id IN ($placeholders) OR a.household_id = ?) AND a.parent_article_id IS NULL ORDER BY a.name ASC"); @@ -72,6 +74,14 @@ $stmt_parent_articles->execute(); $parent_articles = $stmt_parent_articles->get_result()->fetch_all(MYSQLI_ASSOC); $stmt_parent_articles->close(); +// Lade letzte Bilder (für Auswahl) +$stmt_imgs = $conn->prepare("SELECT DISTINCT image_url FROM articles WHERE user_id IN ($placeholders) AND image_url IS NOT NULL AND image_url != '' AND image_url != '0' ORDER BY created_at DESC LIMIT 24"); +$stmt_imgs->bind_param($types, ...$household_member_ids); +$stmt_imgs->execute(); +$recent_images = $stmt_imgs->get_result()->fetch_all(MYSQLI_ASSOC); +$stmt_imgs->close(); + +// Strukturierung Lagerorte $storage_locations_structured = []; foreach ($all_locations as $loc) { if ($loc['parent_id'] === NULL) { if (!isset($storage_locations_structured[$loc['id']])) { $storage_locations_structured[$loc['id']] = ['name' => $loc['name'], 'children' => []]; } } } foreach ($all_locations as $loc) { if ($loc['parent_id'] !== NULL && isset($storage_locations_structured[$loc['parent_id']])) { $storage_locations_structured[$loc['parent_id']]['children'][] = $loc; } } @@ -80,50 +90,31 @@ $upload_dir = 'uploads/images/'; if (!is_dir($upload_dir)) { @mkdir($upload_dir, 0777, true); } function save_image_from_url($url, $upload_dir) { - // KORREKTUR: Unsichere SSL-Optionen entfernt, @-Operator entfernt $context_options = ["http" => ["header" => "User-Agent: Mozilla/5.0\r\n", "timeout" => 10]]; $context = stream_context_create($context_options); - - // Fehlerbehandlung für file_get_contents - $image_data = file_get_contents($url, false, $context); - if ($image_data === false) { - return [false, "Bild konnte von der URL nicht heruntergeladen werden. Überprüfen Sie den Link und die Erreichbarkeit."]; - } - - // Fehlerbehandlung für getimagesizefromstring - $image_info = getimagesizefromstring($image_data); - if ($image_info === false) { - return [false, "Die angegebene URL führt nicht zu einem gültigen Bild."]; - } - - $allowed_mime_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; - if (!in_array($image_info['mime'], $allowed_mime_types)) { - return [false, "Nicht unterstützter Bildtyp: " . htmlspecialchars($image_info['mime'])]; - } - - $extension = image_type_to_extension($image_info[2], false); - $unique_file_name = uniqid('img_url_', true) . '.' . $extension; - $destination = $upload_dir . $unique_file_name; - - if (file_put_contents($destination, $image_data)) { - return [true, $destination]; - } - - return [false, "Bild konnte nicht auf dem Server gespeichert werden."]; + $image_data = @file_get_contents($url, false, $context); + if ($image_data === false) return [false, "Bild-Download fehlgeschlagen."]; + $image_info = @getimagesizefromstring($image_data); + if ($image_info === false) return [false, "Ungültiges Bild."]; + $allowed = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; + if (!in_array($image_info['mime'], $allowed)) return [false, "Format nicht unterstützt."]; + $ext = image_type_to_extension($image_info[2], false); + $name = uniqid('img_url_', true) . '.' . $ext; + if (file_put_contents($upload_dir . $name, $image_data)) return [true, $upload_dir . $name]; + return [false, "Speicherfehler."]; } function save_image_from_base64($base64_string, $upload_dir) { if (preg_match('/^data:image\/(\w+);base64,/', $base64_string, $type)) { $data = substr($base64_string, strpos($base64_string, ',') + 1); $type = strtolower($type[1]); - if (!in_array($type, ['jpg', 'jpeg', 'png', 'gif'])) { return [false, "Nicht unterstützter Bildtyp aus der Zwischenablage."]; } + if (!in_array($type, ['jpg', 'jpeg', 'png', 'gif'])) return [false, "Format nicht unterstützt."]; $data = base64_decode($data); - if ($data === false) { return [false, "Base64-Dekodierung fehlgeschlagen."]; } - } else { return [false, "Ungültiger Base64-String."]; } - $unique_file_name = uniqid('img_paste_', true) . '.' . $type; - $destination = $upload_dir . $unique_file_name; - if (file_put_contents($destination, $data)) { return [true, $destination]; } - return [false, "Bild aus der Zwischenablage konnte nicht gespeichert werden."]; + if ($data === false) return [false, "Dekodierfehler."]; + $name = uniqid('img_paste_', true) . '.' . $type; + if (file_put_contents($upload_dir . $name, $data)) return [true, $upload_dir . $name]; + } + return [false, "Ungültiges Base64."]; } if ($_SERVER["REQUEST_METHOD"] == "POST") { @@ -138,6 +129,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { $product_url = trim($_POST['product_url']); $product_designation = trim($_POST['product_designation']); $pasted_image_data = $_POST['pasted_image_data'] ?? ''; + $selected_existing_image = trim($_POST['selected_existing_image'] ?? ''); if (isset($_POST['manufacturer_id']) && $_POST['manufacturer_id'] === 'new') { $new_manufacturer_name = trim($_POST['new_manufacturer_name']); @@ -173,6 +165,10 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { $image_url_for_db = $destination; } else { $image_error = "Fehler beim Verschieben der hochgeladenen Datei."; } } + elseif (!empty($selected_existing_image)) { + // User chose an existing image + $image_url_for_db = $selected_existing_image; + } elseif (!empty($image_url_from_input)) { list($success, $result) = save_image_from_url($image_url_from_input, $upload_dir); if ($success) { $image_url_for_db = $result; } else { $image_error = $result; } @@ -213,10 +209,6 @@ $conn->close(); - - - -