prepare("INSERT INTO projects (title, description, image, status, created_at) VALUES (:title, :description, :image, :status, NOW())"); $stmt->bindParam(':title', $title); $stmt->bindParam(':description', $description); $stmt->bindParam(':image', $image); $stmt->bindParam(':status', $status); $stmt->execute(); $success = "प्रोजेक्ट सफलतापूर्वक जोड़ा गया।"; } else { $project_id = isset($_POST['project_id']) ? (int)$_POST['project_id'] : 0; // Get current project data $stmt = $db->prepare("SELECT image FROM projects WHERE id = :id"); $stmt->bindParam(':id', $project_id); $stmt->execute(); $currentProject = $stmt->fetch(); if (!$currentProject) { throw new Exception("प्रोजेक्ट नहीं मिला।"); } // Use current image if no new image uploaded if (empty($image)) { $image = $currentProject['image']; } else { // Delete old image if a new one is uploaded if (!empty($currentProject['image'])) { $oldImagePath = __DIR__ . '/../img/projects/' . $currentProject['image']; if (file_exists($oldImagePath)) { unlink($oldImagePath); } } } $stmt = $db->prepare("UPDATE projects SET title = :title, description = :description, image = :image, status = :status WHERE id = :id"); $stmt->bindParam(':title', $title); $stmt->bindParam(':description', $description); $stmt->bindParam(':image', $image); $stmt->bindParam(':status', $status); $stmt->bindParam(':id', $project_id); $stmt->execute(); $success = "प्रोजेक्ट सफलतापूर्वक अपडेट किया गया।"; } header("Location: projects.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "त्रुटि: " . $e->getMessage(); } } } // Delete Project if ($formAction === 'delete') { $project_id = isset($_POST['project_id']) ? (int)$_POST['project_id'] : 0; try { // Get project image $stmt = $db->prepare("SELECT image FROM projects WHERE id = :id"); $stmt->bindParam(':id', $project_id); $stmt->execute(); $project = $stmt->fetch(); // Delete project $stmt = $db->prepare("DELETE FROM projects WHERE id = :id"); $stmt->bindParam(':id', $project_id); $stmt->execute(); // Delete image file if exists if (!empty($project['image'])) { $imagePath = __DIR__ . '/../img/projects/' . $project['image']; if (file_exists($imagePath)) { unlink($imagePath); } } $success = "प्रोजेक्ट सफलतापूर्वक हटाया गया।"; header("Location: projects.php?success=" . urlencode($success)); exit; } catch (PDOException $e) { $error = "डेटाबेस त्रुटि: " . $e->getMessage(); } } // Toggle Status if ($formAction === 'toggle') { $project_id = isset($_POST['project_id']) ? (int)$_POST['project_id'] : 0; try { $stmt = $db->prepare("SELECT status FROM projects WHERE id = :id"); $stmt->bindParam(':id', $project_id); $stmt->execute(); $currentProject = $stmt->fetch(); if (!$currentProject) { throw new Exception("प्रोजेक्ट नहीं मिला।"); } $newStatus = $currentProject['status'] === 'active' ? 'inactive' : 'active'; $stmt = $db->prepare("UPDATE projects SET status = :status WHERE id = :id"); $stmt->bindParam(':status', $newStatus); $stmt->bindParam(':id', $project_id); $stmt->execute(); $success = "प्रोजेक्ट की स्थिति सफलतापूर्वक अपडेट की गई।"; header("Location: projects.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "त्रुटि: " . $e->getMessage(); } } } } // Get project for edit if ($action === 'edit' && $id > 0) { try { $stmt = $db->prepare("SELECT * FROM projects WHERE id = :id"); $stmt->bindParam(':id', $id); $stmt->execute(); $project = $stmt->fetch(PDO::FETCH_ASSOC); if (!$project) { $error = "प्रोजेक्ट नहीं मिला।"; $action = 'list'; } } catch (PDOException $e) { $error = "डेटाबेस त्रुटि: " . $e->getMessage(); $action = 'list'; } } // Get projects for list with pagination $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $limit = 12; $offset = ($page - 1) * $limit; try { $stmt = $db->prepare("SELECT COUNT(*) FROM projects"); $stmt->execute(); $totalRecords = $stmt->fetchColumn(); $stmt = $db->prepare("SELECT * FROM projects ORDER BY created_at DESC LIMIT :limit OFFSET :offset"); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $projects = $stmt->fetchAll(PDO::FETCH_ASSOC); $totalPages = ceil($totalRecords / $limit); } catch (PDOException $e) { $error = "डेटाबेस त्रुटि: " . $e->getMessage(); $projects = []; $totalPages = 0; } $pageTitle = ($action === 'add') ? "नया प्रोजेक्ट जोड़ें" : (($action === 'edit') ? "प्रोजेक्ट संपादित करें" : "प्रोजेक्ट प्रबंधन"); include 'includes/header.php'; ?>