false, 'message' => '', 'filename' => '']; // Check if file was uploaded without errors if ($file['error'] !== UPLOAD_ERR_OK) { $result['message'] = 'File upload error: ' . $file['error']; return $result; } // Validate file type (allow only images) $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; $fileType = mime_content_type($file['tmp_name']); if (!in_array($fileType, $allowedTypes)) { $result['message'] = 'Invalid file type. Only JPG, PNG, GIF, and WebP are allowed.'; return $result; } // Validate file size (max 5MB) $maxSize = 5 * 1024 * 1024; // 5MB in bytes if ($file['size'] > $maxSize) { $result['message'] = 'File size exceeds 5MB limit.'; return $result; } // Create target directory if it doesn't exist $targetPath = __DIR__ . '/../' . $targetDir; if (!is_dir($targetPath)) { mkdir($targetPath, 0755, true); } // Generate a unique filename to avoid conflicts $fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION); $uniqueName = uniqid('slider_', true) . '.' . strtolower($fileExtension); $targetFile = $targetPath . '/' . $uniqueName; // Move the uploaded file to the target directory if (move_uploaded_file($file['tmp_name'], $targetFile)) { $result['success'] = true; $result['filename'] = $uniqueName; } else { $result['message'] = 'Failed to move uploaded file.'; } return $result; } // Initialize variables $action = isset($_GET['action']) ? $_GET['action'] : 'list'; $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; $success = isset($_GET['success']) ? sanitizeInput($_GET['success']) : ''; $error = ''; $slider_item = []; $db = getDbConnection(); // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['action'])) { $formAction = $_POST['action']; // Add or Edit Slider Item if ($formAction === 'add' || $formAction === 'edit') { $sort_order = isset($_POST['sort_order']) ? (int)$_POST['sort_order'] : 0; $status = isset($_POST['status']) ? sanitizeInput($_POST['status']) : 'active'; try { // Handle file upload $image = ''; if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $uploadResult = uploadFile($_FILES['image'], 'img/sliders'); if ($uploadResult['success']) { $image = $uploadResult['filename']; } else { $error = $uploadResult['message']; throw new Exception($uploadResult['message']); } } if ($formAction === 'add') { if (empty($image)) { throw new Exception("Image is required for new slider item."); } $stmt = $db->prepare("INSERT INTO sliders (image, sort_order, status, created_at) VALUES (:image, :sort_order, :status, NOW())"); $stmt->bindParam(':image', $image); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->execute(); $success = "Slider item added successfully."; } else { $slider_id = isset($_POST['slider_id']) ? (int)$_POST['slider_id'] : 0; // Get current slider data $stmt = $db->prepare("SELECT image FROM sliders WHERE id = :id"); $stmt->bindParam(':id', $slider_id); $stmt->execute(); $currentItem = $stmt->fetch(); if (!$currentItem) { throw new Exception("Slider item not found."); } // Use current image if no new image uploaded if (empty($image)) { $image = $currentItem['image']; } else { // Delete old image if a new one is uploaded if (!empty($currentItem['image'])) { $oldImagePath = __DIR__ . '/../img/sliders/' . $currentItem['image']; if (file_exists($oldImagePath)) { unlink($oldImagePath); } } } $stmt = $db->prepare("UPDATE sliders SET image = :image, sort_order = :sort_order, status = :status WHERE id = :id"); $stmt->bindParam(':image', $image); $stmt->bindParam(':sort_order', $sort_order); $stmt->bindParam(':status', $status); $stmt->bindParam(':id', $slider_id); $stmt->execute(); $success = "Slider item updated successfully."; } header("Location: sliders.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = $e->getMessage(); } } // Delete Slider Item if ($formAction === 'delete') { $slider_id = isset($_POST['slider_id']) ? (int)$_POST['slider_id'] : 0; try { // Get slider image $stmt = $db->prepare("SELECT image FROM sliders WHERE id = :id"); $stmt->bindParam(':id', $slider_id); $stmt->execute(); $slider_item = $stmt->fetch(); // Delete slider item $stmt = $db->prepare("DELETE FROM sliders WHERE id = :id"); $stmt->bindParam(':id', $slider_id); $stmt->execute(); // Delete image file if exists if (!empty($slider_item['image'])) { $imagePath = __DIR__ . '/../img/sliders/' . $slider_item['image']; if (file_exists($imagePath)) { unlink($imagePath); } } $success = "Slider item deleted successfully."; header("Location: sliders.php?success=" . urlencode($success)); exit; } catch (PDOException $e) { $error = "Database error: " . $e->getMessage(); } } // Toggle Status if ($formAction === 'toggle') { $slider_id = isset($_POST['slider_id']) ? (int)$_POST['slider_id'] : 0; try { $stmt = $db->prepare("SELECT status FROM sliders WHERE id = :id"); $stmt->bindParam(':id', $slider_id); $stmt->execute(); $currentItem = $stmt->fetch(); if (!$currentItem) { throw new Exception("Slider item not found."); } $newStatus = $currentItem['status'] === 'active' ? 'inactive' : 'active'; $stmt = $db->prepare("UPDATE sliders SET status = :status WHERE id = :id"); $stmt->bindParam(':status', $newStatus); $stmt->bindParam(':id', $slider_id); $stmt->execute(); $success = "Slider item status updated successfully."; header("Location: sliders.php?success=" . urlencode($success)); exit; } catch (Exception $e) { $error = $e->getMessage(); } } } } // Get slider item for edit if ($action === 'edit' && $id > 0) { try { $stmt = $db->prepare("SELECT * FROM sliders WHERE id = :id"); $stmt->bindParam(':id', $id); $stmt->execute(); $slider_item = $stmt->fetch(); if (!$slider_item) { $error = "Slider item not found."; $action = 'list'; } } catch (PDOException $e) { $error = "Database error: " . $e->getMessage(); $action = 'list'; } } // Get slider items 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 sliders"); $stmt->execute(); $totalRecords = $stmt->fetchColumn(); $stmt = $db->prepare("SELECT * FROM sliders ORDER BY sort_order ASC, created_at DESC LIMIT :limit OFFSET :offset"); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $slider_items = $stmt->fetchAll(); $totalPages = ceil($totalRecords / $limit); } catch (PDOException $e) { $error = "Database error: " . $e->getMessage(); $slider_items = []; $totalPages = 0; } $pageTitle = ($action === 'add') ? "Add Slider Item" : (($action === 'edit') ? "Edit Slider Item" : "Slider Management"); include 'includes/header.php'; ?>