Sindbad~EG File Manager

Current Path : /home/u625735752/domains/snackoncrumbs.com/public_html/demo/1.1/
Upload File :
Current File : /home/u625735752/domains/snackoncrumbs.com/public_html/demo/1.1/manage-services.php

<?php
session_start();

include 'conn.php';
include 'manage-common1.php';

// Define pagination variables
$items_per_page = 10; // Number of services to display per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $items_per_page;

function handle_file_upload($file) {
    // Specify the directory where images will be stored
    $target_dir = "assets/img/services/";
    
    // Create a unique file name based on the current date and time
    $date_prefix = date("d-m-y-H-i-s"); // Format: dd-mm-yy-HH-MM-SS
    $original_name = basename($file["name"]);
    $file_extension = strtolower(pathinfo($original_name, PATHINFO_EXTENSION));
    $new_file_name = $date_prefix . '-' . $original_name;

    // Set the target file path
    $target_file = $target_dir . $new_file_name;

    $uploadOk = 1;

    // Check if file is an image
    $check = getimagesize($file["tmp_name"]);
    if ($check === false) {
        return "File is not an image.";
    }

    // Check file size (limit to 5MB)
    if ($file["size"] > 5000000) {
        return "Sorry, your file is too large. Max size is 5MB.";
    }

    // Allow certain file formats (jpg, jpeg, png, gif)
    if (!in_array($file_extension, ["jpg", "jpeg", "png", "gif"])) {
        return "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    }

    // Attempt to move the uploaded file
    if (move_uploaded_file($file["tmp_name"], $target_file)) {
        return $target_file; // Return the image URL
    } else {
        return "Sorry, there was an error uploading your file. Please try again.";
    }
}

// Add New Service
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_service'])) {
    function sanitize($data) {
        return htmlspecialchars(strip_tags(trim($data)));
    }

    $heading = sanitize($_POST['heading']);
    $heading_span_text = sanitize($_POST['heading_span_text']);
    $para = sanitize($_POST['para']);
    $link = sanitize($_POST['link']);
    $image = ''; // Initialize image variable

    // If a file was uploaded, handle the file upload
    if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
        $uploaded_file = handle_file_upload($_FILES['image']);
        if (strpos($uploaded_file, "Sorry") === 0) { // Check if it's an error string
            echo "<p class='text-danger'>$uploaded_file</p>";
        } else {
            $image = $uploaded_file; // Set the image URL
        }
    }

    // Check if image path is valid (to avoid empty paths being stored)
    if (empty($image)) {
        echo "<p class='text-danger'>Image upload failed or no image uploaded.</p>";
        exit();
    }

    // Insert data into the database
    $sql = "INSERT INTO services (heading, heading_span_text, para, link, image) VALUES (?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sssss", $heading, $heading_span_text, $para, $link, $image);

    if ($stmt->execute()) {
        header("Location: manage-services.php");
        exit();
    } else {
        echo "Error: Unable to add service. " . $stmt->error;
    }
    $stmt->close();
}

// Edit Service
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_service'])) {
    function sanitize($data) {
        return htmlspecialchars(strip_tags(trim($data)));
    }

    $service_id = $_POST['edit_id'];
    $heading = sanitize($_POST['edit_heading']);
    $heading_span_text = sanitize($_POST['edit_heading_span_text']);
    $para = sanitize($_POST['edit_para']);
    $link = sanitize($_POST['edit_link']);
    
    // Retain existing image URL if no new image is uploaded
    $image = htmlspecialchars($_POST['edit_image']);
    
    // If a new file was uploaded, handle the file upload
    if (isset($_FILES['edit_image']) && $_FILES['edit_image']['error'] === UPLOAD_ERR_OK) {
        $uploaded_file = handle_file_upload($_FILES['edit_image']);
        if (strpos($uploaded_file, "Sorry") === 0) { // Check if it's an error string
            echo "<p class='text-danger'>$uploaded_file</p>";
        } else {
            $image = $uploaded_file; // Set the new image URL
        }
    }

    // Check if image path is valid (to avoid empty paths being stored)
    if (empty($image)) {
        echo "<p class='text-danger'>Image upload failed or no image uploaded.</p>";
        exit();
    }

    // Update the service in the database
    $sql = "UPDATE services SET heading=?, heading_span_text=?, para=?, link=?, image=? WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sssssi", $heading, $heading_span_text, $para, $link, $image, $service_id);

    if ($stmt->execute()) {
        header("Location: manage-services.php");
        exit();
    } else {
        echo "Error: Unable to edit service. " . $stmt->error;
    }
    $stmt->close();
}

// Delete Service
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['delete_service_id'])) {
    $service_id = $_GET['delete_service_id'];

    $sql = "DELETE FROM services WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $service_id);
    if ($stmt->execute()) {
        header("Location: manage-services.php");
        exit();
    } else {
        echo "Error: Unable to delete service. " . $stmt->error;
    }
    $stmt->close();
}

// Fetch Services for Display (pagination)
$total_services_result = $conn->query("SELECT COUNT(*) as total FROM services");
$total_services = $total_services_result->fetch_assoc()['total'];
$total_pages = ceil($total_services / $items_per_page);

$sql = "SELECT * FROM services LIMIT ? OFFSET ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $items_per_page, $offset);
$stmt->execute();
$services_result = $stmt->get_result();
?>

<!-- Your HTML for displaying the form and services goes here -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>Service Management</title>
</head>
<body>

<?php include 'manage-nav.php';?>

<!-- Add Service Modal -->
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addModalLabel">Add Service</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form method="post" action="manage-services.php" enctype="multipart/form-data">
                    <div class="mb-3">
                        <label for="heading" class="form-label">Heading</label>
                        <input type="text" id="heading" name="heading" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="heading_span_text" class="form-label">Heading Span Text</label>
                        <input type="text" id="heading_span_text" name="heading_span_text" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="para" class="form-label">Paragraph</label>
                        <textarea id="para" name="para" class="form-control" required></textarea>
                    </div>
                    <div class="mb-3">
                        <label for="link" class="form-label">Link</label>
                        <input type="text" id="link" name="link" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="image" class="form-label">Image</label>
                        <input type="file" id="image" name="image" class="form-control" accept="image/*">
                    </div>
                    <button type="submit" name="add_service" class="btn btn-primary">Add Service</button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- Edit Service Modal -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editModalLabel">Edit Service</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form method="post" action="manage-services.php" enctype="multipart/form-data">
                    <input type="hidden" id="edit_id" name="edit_id">
                    <div class="mb-3">
                        <label for="edit_heading" class="form-label">Heading</label>
                        <input type="text" id="edit_heading" name="edit_heading" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="edit_heading_span_text" class="form-label">Heading Span Text</label>
                        <input type="text" id="edit_heading_span_text" name="edit_heading_span_text" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="edit_para" class="form-label">Paragraph</label>
                        <textarea id="edit_para" name="edit_para" class="form-control" required></textarea>
                    </div>
                    <div class="mb-3">
                        <label for="edit_link" class="form-label">Link</label>
                        <input type="text" id="edit_link" name="edit_link" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label for="edit_image" class="form-label">Image</label>
                        <input type="file" id="edit_image" name="edit_image" class="form-control" accept="image/*">
                    </div>
                    <button type="submit" name="edit_service" class="btn btn-primary">Save Changes</button>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="container mt-5">
    <h1 class="mb-4">Service Management</h1>

    <!-- Add New Service Button, only visible if logged in -->
    <?php if ($logged_in) { ?>
        <button type="button" class="btn btn-primary mb-4" data-bs-toggle="modal" data-bs-target="#addModal">
            Add New Service
        </button>
    <?php } ?>

    <table class="table">
        <thead>
            <tr>
                <th>Heading</th>
                <th>Heading Span Text</th>
                <th>Paragraph</th>
                <th>Link</th>
                <th>Image</th>
                <th>Actions</th>
            </tr>
        </thead>

        <tbody>
            <?php while ($service = $services_result->fetch_assoc()) { ?>
                <tr>
                    <td><?= htmlspecialchars($service['heading']) ?></td>
                    <td><?= htmlspecialchars($service['heading_span_text']) ?></td>
                    <td><?= htmlspecialchars($service['para']) ?></td>
                    <td><?= htmlspecialchars($service['link']) ?></td>
                    <td>
                        <?php if (!empty($service['image'])) { ?>
                            <img src="<?= htmlspecialchars($service['image']) ?>" alt="<?= htmlspecialchars($service['heading']) ?>" width="100">
                        <?php } ?>
                    </td>
                    <td>
                        <?php if ($logged_in) { ?>
                            <button type="button" class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#editModal" onclick="populateEditModal(<?= $service['id'] ?>, '<?= addslashes($service['heading']) ?>', '<?= addslashes($service['heading_span_text']) ?>', '<?= addslashes($service['para']) ?>', '<?= addslashes($service['link']) ?>', '<?= addslashes($service['image']) ?>')">
                                Edit
                            </button>
                            <a href="?delete_service_id=<?= $service['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this service?')">Delete</a>
                        <?php } ?>
                    </td>
                </tr>
            <?php } ?>
        </tbody>
    </table>

    <nav>
        <ul class="pagination">
            <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                <a class="page-link" href="?page=<?= $page - 1 ?>">Previous</a>
            </li>
            <?php for ($i = 1; $i <= $total_pages; $i++) { ?>
                <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                    <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                </li>
            <?php } ?>
            <li class="page-item <?= ($page >= $total_pages) ? 'disabled' : '' ?>">
                <a class="page-link" href="?page=<?= $page + 1 ?>">Next</a>
            </li>
        </ul>
    </nav>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    window.populateEditModal = function(id, heading, heading_span_text, para, link, image) {
        document.getElementById('edit_id').value = id;
        document.getElementById('edit_heading').value = heading;
        document.getElementById('edit_heading_span_text').value = heading_span_text;
        document.getElementById('edit_para').value = para;
        document.getElementById('edit_link').value = link;
        document.getElementById('edit_image').value = image;
    }
});
</script>

<?php include 'footer-manage.php'; ?>

</body>
</html>

<?php $conn->close(); ?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists