false, 'message' => '', 'filename' => '']; // Create target directory if it doesn't exist $targetPath = __DIR__ . '/../' . $targetDir; if (!is_dir($targetPath)) { mkdir($targetPath, 0755, true); } // Check if file was uploaded if ($file['error'] !== UPLOAD_ERR_OK) { $result['message'] = 'फ़ाइल अपलोड में त्रुटि।'; return $result; } $fileName = basename($file['name']); $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); $newFileName = uniqid() . '.' . $fileExt; $targetFile = $targetPath . '/' . $newFileName; // Validate file type if (!in_array($fileExt, $allowedTypes)) { $result['message'] = 'अनुमति नही है फ़ाइल प्रकार। केवल ' . implode(', ', $allowedTypes) . ' अनुमत हैं।'; return $result; } // Validate file size if ($file['size'] > $maxFileSize) { $result['message'] = 'फ़ाइल का आकार 5MB से अधिक नहीं होना चाहिए।'; return $result; } // Move uploaded file if (move_uploaded_file($file['tmp_name'], $targetFile)) { $result['success'] = true; $result['filename'] = $newFileName; } else { $result['message'] = 'फ़ाइल अपलोड करने में विफल।'; } return $result; } // Initialize variables $action = isset($_GET['action']) ? sanitizeInput($_GET['action']) : 'list'; $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; $success = isset($_GET['success']) ? sanitizeInput($_GET['success']) : ''; $error = ''; try { $db = getDbConnection(); // Verify table existence $stmt = $db->query("SHOW TABLES LIKE 'about_us_content'"); if ($stmt->rowCount() == 0) { error_log("Table 'about_us_content' does not exist in database."); $error = "डेटाबेस में 'about_us_content' तालिका नहीं मिली।"; $sectionItems = []; $totalPages = 0; } } catch (PDOException $e) { error_log("Database connection error: " . $e->getMessage()); $error = "डेटाबेस त्रुटि: " . $e->getMessage(); $sectionItems = []; $totalPages = 0; } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['action'])) { $formAction = $_POST['action']; if ($formAction === 'add' || $formAction === 'edit') { $section_title = isset($_POST['section_title']) ? sanitizeInput($_POST['section_title']) : ''; $content = isset($_POST['content']) ? $_POST['content'] : ''; $sort_order = isset($_POST['sort_order']) ? (int)$_POST['sort_order'] : 0; $status = isset($_POST['status']) ? sanitizeInput($_POST['status']) : 'active'; if (empty($section_title) || empty($content)) { $error = "सेक्शन शीर्षक और सामग्री आवश्यक हैं।"; } else { try { $image = null; if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $uploadResult = uploadFile($_FILES['image'], 'img/about'); if ($uploadResult['success']) { $image = $uploadResult['filename']; } else { throw new Exception($uploadResult['message']); } } if ($formAction === 'add') { $stmt = $db->prepare("INSERT INTO about_us_content (section_title, content, image, sort_order, status, created_at) VALUES (:section_title, :content, :image, :sort_order, :status, NOW())"); $stmt->bindParam(':section_title', $section_title); $stmt->bindParam(':content', $content); $stmt->bindParam(':image', $image); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->execute(); $success = "हमारे बारे में सेक्शन सफलतापूर्वक जोड़ा गया।"; } else { $section_id = isset($_POST['section_id']) ? (int)$_POST['section_id'] : 0; $stmt = $db->prepare("SELECT image FROM about_us_content WHERE id = :id"); $stmt->bindParam(':id', $section_id); $stmt->execute(); $currentData = $stmt->fetch(); if (!$currentData) { throw new Exception("सेक्शन नहीं मिला।"); } if (empty($image)) { $image = $currentData['image']; } else { if (!empty($currentData['image'])) { $oldImagePath = __DIR__ . '/../img/about/' . $currentData['image']; if (file_exists($oldImagePath)) { unlink($oldImagePath); } } } $stmt = $db->prepare("UPDATE about_us_content SET section_title = :section_title, content = :content, image = :image, sort_order = :sort_order, status = :status, updated_at = NOW() WHERE id = :id"); $stmt->bindParam(':section_title', $section_title); $stmt->bindParam(':content', $content); $stmt->bindParam(':image', $image); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->bindParam(':id', $section_id); $stmt->execute(); $success = "हमारे बारे में सेक्शन सफलतापूर्वक अपडेट किया गया।"; } header("Location: about_us_content.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "त्रुटि: " . $e->getMessage(); } } } if ($formAction === 'delete') { $section_id = isset($_POST['section_id']) ? (int)$_POST['section_id'] : 0; try { $stmt = $db->prepare("SELECT image FROM about_us_content WHERE id = :id"); $stmt->bindParam(':id', $section_id); $stmt->execute(); $data = $stmt->fetch(); $stmt = $db->prepare("DELETE FROM about_us_content WHERE id = :id"); $stmt->bindParam(':id', $section_id); $stmt->execute(); if (!empty($data['image'])) { $imagePath = __DIR__ . '/../img/about/' . $data['image']; if (file_exists($imagePath)) { unlink($imagePath); } } $success = "हमारे बारे में सेक्शन सफलतापूर्वक हटाया गया।"; header("Location: about_us_content.php?success=" . urlencode($success)); exit; } catch (PDOException $e) { $error = "डेटाबेस त्रुटि: " . $e->getMessage(); } } if ($formAction === 'toggle') { $section_id = isset($_POST['section_id']) ? (int)$_POST['section_id'] : 0; try { $stmt = $db->prepare("SELECT status FROM about_us_content WHERE id = :id"); $stmt->bindParam(':id', $section_id); $stmt->execute(); $currentData = $stmt->fetch(); if (!$currentData) { throw new Exception("सेक्शन नहीं मिला।"); } $newStatus = $currentData['status'] === 'active' ? 'inactive' : 'active'; $stmt = $db->prepare("UPDATE about_us_content SET status = :status, updated_at = NOW() WHERE id = :id"); $stmt->bindParam(':status', $newStatus); $stmt->bindParam(':id', $section_id); $stmt->execute(); $success = "हमारे बारे में सेक्शन की स्थिति सफलतापूर्वक अपडेट की गई।"; header("Location: about_us_content.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = "त्रुटि: " . $e->getMessage(); } } } } // Get data for edit if ($action === 'edit' && $id > 0) { try { $stmt = $db->prepare("SELECT * FROM about_us_content WHERE id = :id"); $stmt->bindParam(':id', $id); $stmt->execute(); $section = $stmt->fetch(PDO::FETCH_ASSOC); if (!$section) { $error = "सेक्शन नहीं मिला।"; $action = 'list'; } } catch (PDOException $e) { error_log("Edit query error: " . $e->getMessage()); $error = "डेटाबेस त्रुटि: " . $e->getMessage(); $action = 'list'; } } // Get data 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 about_us_content"); $stmt->execute(); $totalRecords = $stmt->fetchColumn(); if ($totalRecords == 0) { error_log("No records found in about_us_content table."); } $stmt = $db->prepare("SELECT * FROM about_us_content ORDER BY sort_order ASC LIMIT :limit OFFSET :offset"); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $sectionItems = $stmt->fetchAll(PDO::FETCH_ASSOC); error_log("Fetched " . count($sectionItems) . " records from about_us_content."); $totalPages = ceil($totalRecords / $limit); } catch (PDOException $e) { error_log("Database query error for about_us_content: " . $e->getMessage()); $error = "डेटाबेस त्रुटि: " . $e->getMessage(); $sectionItems = []; $totalPages = 0; } // Set page title $pageTitle = ($action === 'add') ? "नया सेक्शन जोड़ें" : (($action === 'edit') ? "सेक्शन संपादित करें" : "हमारे बारे में सेक्शन प्रबंधन"); include 'includes/header.php'; ?>