getUserDetails($_SESSION['user_id']); $fkisp_id_of = $currentUserDetails['fkisp_id_of']; // Handle form submissions if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action_type = $_POST['action_type'] ?? ''; // 'add' or 'edit' or 'delete' $title_en = trim($_POST['title_en'] ?? ''); $description = trim($_POST['description'] ?? ''); $slide_id = $_POST['slide_id'] ?? null; $current_photo = $_POST['current_photo'] ?? ''; // For editing, keep track of existing photo if ($action_type === 'delete') { if ($slide_id) { try { $slideManager->deleteSlide($slide_id); set_message('Slide deleted successfully!', 'success'); } catch (Exception $e) { set_message('Error deleting slide: ' . $e->getMessage(), 'danger'); } } } else { // Handle photo upload $photoPath = $current_photo; // Default to current photo if not uploading new if (isset($_FILES['photo']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) { try { $photoPath = $slideManager->handlePhotoUpload($_FILES['photo']); // If editing and a new photo is uploaded, delete the old one if ($action_type === 'edit' && !empty($current_photo) && $current_photo !== $photoPath) { // Ensure the old photo path is not empty and different from the new one if (!empty($current_photo) && file_exists('../uploads/slides/' . $current_photo)) { unlink('../uploads/slides/' . $current_photo); // Delete old file } } } catch (Exception $e) { set_message('Photo upload error: ' . $e->getMessage(), 'danger'); header('Location: manage_slides.php'); exit(); } } elseif ($action_type === 'add' && (!isset($_FILES['photo']) || $_FILES['photo']['error'] !== UPLOAD_ERR_OK)) { // For adding, a photo is required. set_message('Please upload a photo for the slide.', 'danger'); header('Location: manage_slides.php'); exit(); } if (empty($title_en) || empty($description)) { set_message('Title and description cannot be empty.', 'danger'); } else { try { if ($action_type === 'add') { $slideManager->addSlide($title_en, $description, $photoPath, $_SESSION['user_id'], $fkisp_id_of); set_message('Slide added successfully!', 'success'); } elseif ($action_type === 'edit' && $slide_id) { $slideManager->updateSlide($slide_id, $title_en, $description, $photoPath, $_SESSION['user_id'], $fkisp_id_of); set_message('Slide updated successfully!', 'success'); } } catch (Exception $e) { set_message('Error: ' . $e->getMessage(), 'danger'); } } } header('Location: manage_slides.php'); exit(); } // Fetch slides for display $slides = $slideManager->getAllSlides(); // Prepare data for editing if action is 'edit' $editSlide = null; if ($action === 'edit' && $id) { $editSlide = $slideManager->getSlideById($id); } ?>