diff --git a/admin/edit_product.php b/admin/edit_product.php index 6af4bcc..c9f8f56 100644 --- a/admin/edit_product.php +++ b/admin/edit_product.php @@ -50,6 +50,29 @@ if (isset($_GET['delete_document']) && isset($_GET['id'])) { exit; } +// Handle material deletion +if (isset($_GET['delete_material']) && isset($_GET['id'])) { + $material_id_to_delete = $_GET['delete_material']; + $product_id_for_redirect = $_GET['id']; + + $material_stmt = $pdo->prepare("SELECT path FROM product_materials WHERE id = ? AND product_id = ?"); + $material_stmt->execute([$material_id_to_delete, $product_id_for_redirect]); + $material_to_delete = $material_stmt->fetch(PDO::FETCH_ASSOC); + + if ($material_to_delete && !empty($material_to_delete['path'])) { + $file_path = __DIR__ . '/../uploads/products/' . $material_to_delete['path']; + if (file_exists($file_path)) { + unlink($file_path); + } + } + + $delete_stmt = $pdo->prepare("DELETE FROM product_materials WHERE id = ?"); + $delete_stmt->execute([$material_id_to_delete]); + + header('Location: edit_product.php?id=' . $product_id_for_redirect); + exit; +} + $product = [ 'id' => null, @@ -104,6 +127,11 @@ if (isset($_GET['id'])) { $docs_stmt->execute([$product_id]); $product_documents = $docs_stmt->fetchAll(PDO::FETCH_ASSOC); + // Fetch product materials + $materials_stmt = $pdo->prepare("SELECT * FROM product_materials WHERE product_id = ?"); + $materials_stmt->execute([$product_id]); + $product_materials = $materials_stmt->fetchAll(PDO::FETCH_ASSOC); + } @@ -244,6 +272,60 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } } + // Handle material uploads + if (isset($_POST['material_type']) && !empty($_POST['material_type'])) { + $material_type = $_POST['material_type']; + $material_title = $_POST['material_title'] ?? ''; + $material_errors = []; + + if (empty($material_title)) { + $material_errors[] = 'Tytuł materiału jest wymagany.'; + } + + if ($material_type === 'youtube') { + $material_url = $_POST['material_url'] ?? ''; + if (empty($material_url) || !filter_var($material_url, FILTER_VALIDATE_URL)) { + $material_errors[] = 'Nieprawidłowy adres URL YouTube.'; + } else { + $material_stmt = $pdo->prepare("INSERT INTO product_materials (product_id, material_type, title, url) VALUES (?, ?, ?, ?)"); + $material_stmt->execute([$product_id, $material_type, $material_title, $material_url]); + } + } else { // pdf or image + if (isset($_FILES['material_file']) && $_FILES['material_file']['error'] === UPLOAD_ERR_OK) { + $allowed_types = ['application/pdf', 'image/jpeg', 'image/png']; + $file_type = mime_content_type($_FILES['material_file']['tmp_name']); + + if (in_array($file_type, $allowed_types)) { + $upload_dir = __DIR__ . '/../uploads/products/' . $product_id . '/materials/'; + if (!is_dir($upload_dir)) { + mkdir($upload_dir, 0777, true); + } + $file_ext = pathinfo($_FILES['material_file']['name'], PATHINFO_EXTENSION); + $file_name = uniqid('mat_' . $product_id . '_', true) . '.' . $file_ext; + $destination = $upload_dir . $file_name; + + if (move_uploaded_file($_FILES['material_file']['tmp_name'], $destination)) { + $material_stmt = $pdo->prepare("INSERT INTO product_materials (product_id, material_type, title, path) VALUES (?, ?, ?, ?)"); + $material_stmt->execute([$product_id, $material_type, $material_title, $product_id . '/materials/' . $file_name]); + } else { + $material_errors[] = "Nie udało się przenieść pliku materiału."; + } + } else { + $material_errors[] = "Niedozwolony typ pliku materiału."; + } + } else { + $material_errors[] = "Błąd podczas przesyłania pliku materiału."; + } + } + + if (!empty($material_errors)) { + $errors = array_merge($errors, $material_errors); + if ($pdo->inTransaction()) $pdo->rollBack(); + goto end_of_post_handling; + } + } + + $clear_stmt = $pdo->prepare("DELETE FROM product_attributes WHERE product_id = ?"); @@ -418,12 +500,60 @@ $page_title = $product['id'] ? 'Edytuj produkt' : 'Dodaj produkt'; endif; ?> +