prepare("SELECT before_image, after_image FROM projects WHERE id = :id"); $stmt_files->execute(['id' => $delete_id]); $files = $stmt_files->fetch(); if ($files) { if (!empty($files['before_image']) && file_exists(__DIR__ . '/../' . $files['before_image'])) { @unlink(__DIR__ . '/../' . $files['before_image']); } if (!empty($files['after_image']) && file_exists(__DIR__ . '/../' . $files['after_image'])) { @unlink(__DIR__ . '/../' . $files['after_image']); } } // Fetch and delete sub-gallery images $stmt_sub = $pdo->prepare("SELECT image_path FROM project_images WHERE project_id = :project_id"); $stmt_sub->execute(['project_id' => $delete_id]); $sub_images = $stmt_sub->fetchAll(); foreach ($sub_images as $simg) { if (file_exists(__DIR__ . '/../' . $simg['image_path'])) { @unlink(__DIR__ . '/../' . $simg['image_path']); } } // Delete records $stmt = $pdo->prepare("DELETE FROM projects WHERE id = :id"); $stmt->execute(['id' => $delete_id]); header("Location: " . BASE_URL . "admin/projects.php?success=deleted"); exit(); } catch (PDOException $e) { $error_msg = "Failed to delete project: " . $e->getMessage(); } } // Handle Add/Edit Submissions if (isset($_POST['submit_project'])) { $name = trim($_POST['name']); $category = trim($_POST['category']); $location = trim($_POST['location']); $completion_date = trim($_POST['completion_date']); $description = trim($_POST['description']); $is_featured = isset($_POST['is_featured']) ? 1 : 0; // Fallback date to null if empty if (empty($completion_date)) $completion_date = null; if (empty($name) || empty($category) || empty($description)) { $error_msg = "Please fill in all required inputs (Name, Category, Description)."; } else { try { if ($action === 'add') { $before_path = handle_project_upload($_FILES['before_image']); $after_path = handle_project_upload($_FILES['after_image']); $stmt = $pdo->prepare("INSERT INTO projects (name, description, category, location, completion_date, before_image, after_image, is_featured) VALUES (:name, :description, :category, :location, :completion_date, :before_image, :after_image, :is_featured)"); $stmt->execute([ 'name' => $name, 'description' => $description, 'category' => $category, 'location' => $location, 'completion_date' => $completion_date, 'before_image' => $before_path, 'after_image' => $after_path, 'is_featured' => $is_featured ]); $project_id = $pdo->lastInsertId(); // Handle sub-gallery multiple image uploads if (!empty($_FILES['sub_gallery']['name'][0])) { $files_count = count($_FILES['sub_gallery']['name']); for ($i = 0; $i < $files_count; $i++) { $single_file = [ 'name' => $_FILES['sub_gallery']['name'][$i], 'type' => $_FILES['sub_gallery']['type'][$i], 'tmp_name' => $_FILES['sub_gallery']['tmp_name'][$i], 'error' => $_FILES['sub_gallery']['error'][$i], 'size' => $_FILES['sub_gallery']['size'][$i] ]; $path = handle_project_upload($single_file); if ($path) { $stmt_sub = $pdo->prepare("INSERT INTO project_images (project_id, image_path) VALUES (:project_id, :image_path)"); $stmt_sub->execute(['project_id' => $project_id, 'image_path' => $path]); } } } header("Location: " . BASE_URL . "admin/projects.php?success=added"); exit(); } elseif ($action === 'edit') { // Keep original paths unless replaced $stmt_orig = $pdo->prepare("SELECT before_image, after_image FROM projects WHERE id = :id"); $stmt_orig->execute(['id' => $id]); $orig = $stmt_orig->fetch(); $before_path = $orig['before_image']; $after_path = $orig['after_image']; $new_before = handle_project_upload($_FILES['before_image']); $new_after = handle_project_upload($_FILES['after_image']); if ($new_before) { if (!empty($before_path) && file_exists(__DIR__ . '/../' . $before_path)) { @unlink(__DIR__ . '/../' . $before_path); } $before_path = $new_before; } if ($new_after) { if (!empty($after_path) && file_exists(__DIR__ . '/../' . $after_path)) { @unlink(__DIR__ . '/../' . $after_path); } $after_path = $new_after; } $stmt = $pdo->prepare("UPDATE projects SET name = :name, description = :description, category = :category, location = :location, completion_date = :completion_date, before_image = :before_image, after_image = :after_image, is_featured = :is_featured WHERE id = :id"); $stmt->execute([ 'name' => $name, 'description' => $description, 'category' => $category, 'location' => $location, 'completion_date' => $completion_date, 'before_image' => $before_path, 'after_image' => $after_path, 'is_featured' => $is_featured, 'id' => $id ]); // Add more sub-gallery images if uploaded if (!empty($_FILES['sub_gallery']['name'][0])) { $files_count = count($_FILES['sub_gallery']['name']); for ($i = 0; $i < $files_count; $i++) { $single_file = [ 'name' => $_FILES['sub_gallery']['name'][$i], 'type' => $_FILES['sub_gallery']['type'][$i], 'tmp_name' => $_FILES['sub_gallery']['tmp_name'][$i], 'error' => $_FILES['sub_gallery']['error'][$i], 'size' => $_FILES['sub_gallery']['size'][$i] ]; $path = handle_project_upload($single_file); if ($path) { $stmt_sub = $pdo->prepare("INSERT INTO project_images (project_id, image_path) VALUES (:project_id, :image_path)"); $stmt_sub->execute(['project_id' => $id, 'image_path' => $path]); } } } header("Location: " . BASE_URL . "admin/projects.php?success=updated"); exit(); } } catch (PDOException $e) { $error_msg = "Error updating project database: " . $e->getMessage(); } } } // Edit Mode details loader $edit_data = ['name' => '', 'category' => '', 'location' => '', 'completion_date' => '', 'description' => '', 'before_image' => '', 'after_image' => '', 'is_featured' => 0]; $project_sub_images = []; if ($action === 'edit' && $id > 0) { try { $stmt = $pdo->prepare("SELECT * FROM projects WHERE id = :id"); $stmt->execute(['id' => $id]); $edit_data = $stmt->fetch(); if (!$edit_data) { header("Location: " . BASE_URL . "admin/projects.php"); exit(); } $img_stmt = $pdo->prepare("SELECT * FROM project_images WHERE project_id = :project_id ORDER BY id ASC"); $img_stmt->execute(['project_id' => $id]); $project_sub_images = $img_stmt->fetchAll(); } catch (PDOException $e) { $error_msg = "Error loading database entries: " . $e->getMessage(); } } // Remove single sub-gallery image action if (isset($_GET['delete_sub']) && $id > 0) { $sub_id = intval($_GET['delete_sub']); try { $stmt_sub = $pdo->prepare("SELECT image_path FROM project_images WHERE id = :id AND project_id = :project_id"); $stmt_sub->execute(['id' => $sub_id, 'project_id' => $id]); $sub = $stmt_sub->fetch(); if ($sub) { if (file_exists(__DIR__ . '/../' . $sub['image_path'])) { @unlink(__DIR__ . '/../' . $sub['image_path']); } $stmt_del = $pdo->prepare("DELETE FROM project_images WHERE id = :id"); $stmt_del->execute(['id' => $sub_id]); } header("Location: " . BASE_URL . "admin/projects.php?action=edit&id=" . $id); exit(); } catch (PDOException $e) { $error_msg = "Failed to delete sub-gallery image: " . $e->getMessage(); } } // Load all projects $all_projects = []; try { $stmt = $pdo->query("SELECT * FROM projects ORDER BY id DESC"); $all_projects = $stmt->fetchAll(); } catch (PDOException $e) { // offline } require_once __DIR__ . '/header.php'; require_once __DIR__ . '/navbar.php'; ?>
To enable before/after slider effects in the front pages, please upload both a **Before Design Image** and a **Completed (After) Image**. If the Before image is omitted, only the Completed image will show on detail views.