HOME


5h-3LL 1.0
DIR: /home/aissorg/public_html/fonts/qzfcvsedcv/werwcwsfs
/home/aissorg/public_html/fonts/qzfcvsedcv/werwcwsfs/
Upload File:
Current File : /home/aissorg/public_html/fonts/qzfcvsedcv/werwcwsfs/home-aarti.php.tar
home/aissorg/public_html/admin/php/home-aarti.php000064400000021414151162647740016102 0ustar00<?php
header("Content-Type: application/json");

// Database connection
include('../include/config.php');

$action = $_GET['action'] ?? '';

switch ($action) {
    case 'read':
        $id = $_GET['id'] ?? null;

        if ($id) {
            $query = "SELECT * FROM home_aarti WHERE id = ? AND is_active = 1 AND deleted_at IS NULL";
            $stmt = $con->prepare($query);
            $stmt->bind_param("i", $id);
        } else {
            $query = "SELECT * FROM home_aarti WHERE is_active = 1 AND deleted_at IS NULL";
            $stmt = $con->prepare($query);
        }

        $stmt->execute();
        $result = $stmt->get_result();
        $about_entries = [];

        while ($row = $result->fetch_assoc()) {
            // Fetch related images from about_images table
            $images_query = "SELECT image_path FROM about_images WHERE about_id = ?";
            $images_stmt = $con->prepare($images_query);
            $images_stmt->bind_param("i", $row['id']);
            $images_stmt->execute();
            $images_result = $images_stmt->get_result();
            $images = [];

            while ($image_row = $images_result->fetch_assoc()) {
                $images[] = $image_row['image_path'];
            }

            $row['images'] = $images;
            $about_entries[] = $row;
        }

        echo json_encode(['success' => true, 'data' => $id ? ($about_entries[0] ?? null) : $about_entries]);
        break;

    case 'create':
        $title = $_POST['title'] ?? '';
        $description = $_POST['description'] ?? '';
        $description2 = $_POST['description2'] ?? '';
        $single_image = null;

        $target_dir = "../assets/img/about/";

        // Handle single image upload
        if (!empty($_FILES['single_image']['name'])) {
            $tmp_name = $_FILES['single_image']['tmp_name'];
            if (is_uploaded_file($tmp_name)) {
                $single_image = uniqid() . '_' . time() . '_' . basename($_FILES['single_image']['name']);
                $target_file = $target_dir . $single_image;
                move_uploaded_file($tmp_name, $target_file);
            }
        }

        // Insert main about data
        $query = "INSERT INTO home_aarti (title, description, description2, single_image, is_active) VALUES (?, ?, ?, ?, 1)";
        $stmt = $con->prepare($query);
        $stmt->bind_param("ssss", $title, $description, $description2, $single_image);

        if ($stmt->execute()) {
            $about_id = $stmt->insert_id;

            // Handle multiple images upload
            if (!empty($_FILES['multiple_images']['name']) && is_array($_FILES['multiple_images']['name'])) {
                foreach ($_FILES['multiple_images']['name'] as $key => $name) {
                    $tmp_name = $_FILES['multiple_images']['tmp_name'][$key];
                    if (!empty($name) && is_uploaded_file($tmp_name)) {
                        $new_name = uniqid() . '_' . time() . '_' . basename($name);
                        $target_file = $target_dir . $new_name;

                        if (move_uploaded_file($tmp_name, $target_file)) {
                            $image_query = "INSERT INTO about_images (about_id, image_path) VALUES (?, ?)";
                            $image_stmt = $con->prepare($image_query);
                            $image_stmt->bind_param("is", $about_id, $new_name);
                            $image_stmt->execute();
                        } else {
                            error_log("Failed to move uploaded file: " . $name);
                        }
                    }
                }
            }

            echo json_encode(['success' => true, 'message' => 'Aarti entry added successfully']);
        } else {
            echo json_encode(['success' => false, 'error' => $stmt->error]);
        }
        break;

    case 'update':
        $id = $_POST['id'] ?? 0;
        $title = $_POST['title'] ?? '';
        $description = $_POST['description'] ?? '';
        $description2 = $_POST['description2'] ?? '';
        $single_image = $_POST['existing_image'] ?? null;

        $target_dir = "../assets/img/about/";

        // Handle single image update
        if (!empty($_FILES['single_image']['name'])) {
            $tmp_name = $_FILES['single_image']['tmp_name'];
            if (is_uploaded_file($tmp_name)) {
                $single_image = uniqid() . '_' . time() . '_' . basename($_FILES['single_image']['name']);
                $target_file = $target_dir . $single_image;
                move_uploaded_file($tmp_name, $target_file);
            }
        }

        // Update main aarti data
        $query = "UPDATE home_aarti SET title=?, description=?, description2=?, single_image=?, updated_at=NOW() WHERE id=?";
        $stmt = $con->prepare($query);
        $stmt->bind_param("ssssi", $title, $description, $description2, $single_image, $id);

        if ($stmt->execute()) {
            // Fetch existing images
            $existing_images_query = "SELECT image_path FROM about_images WHERE about_id = ?";
            $existing_images_stmt = $con->prepare($existing_images_query);
            $existing_images_stmt->bind_param("i", $id);
            $existing_images_stmt->execute();
            $existing_images_result = $existing_images_stmt->get_result();
            $existing_images = [];

            while ($row = $existing_images_result->fetch_assoc()) {
                $existing_images[] = $row['image_path'];
            }

            // Handle new multiple images upload
            $new_images = [];
            if (!empty($_FILES['multiple_images']['name']) && is_array($_FILES['multiple_images']['name'])) {
                foreach ($_FILES['multiple_images']['name'] as $key => $name) {
                    $tmp_name = $_FILES['multiple_images']['tmp_name'][$key];
                    if (!empty($name) && is_uploaded_file($tmp_name)) {
                        $new_name = uniqid() . '_' . time() . '_' . basename($name);
                        $target_file = $target_dir . $new_name;

                        if (move_uploaded_file($tmp_name, $target_file)) {
                            $new_images[] = $new_name;
                        }
                    }
                }
            }

            // Combine and check limit
            $all_images = array_merge($existing_images, $new_images);
            if (count($all_images) > 3) {
                echo json_encode(['success' => false, 'error' => 'You can only have up to 3 images.']);
                return;
            }

            // Insert new images into DB
            foreach ($new_images as $image_name) {
                $image_query = "INSERT INTO about_images (about_id, image_path) VALUES (?, ?)";
                $image_stmt = $con->prepare($image_query);
                $image_stmt->bind_param("is", $id, $image_name);
                $image_stmt->execute();
            }

            echo json_encode(['success' => true, 'message' => 'Aarti entry updated successfully']);
        } else {
            echo json_encode(['success' => false, 'error' => $stmt->error]);
        }
        break;

    case 'delete_image':
        $imageName = $_GET['image'] ?? '';
        if (!empty($imageName)) {
            $query = "DELETE FROM about_images WHERE image_path = ?";
            $stmt = $con->prepare($query);
            $stmt->bind_param("s", $imageName);

            if ($stmt->execute()) {
                $imagePath = "../assets/img/about/" . basename($imageName);
                if (file_exists($imagePath)) unlink($imagePath);

                echo json_encode(['success' => true, 'message' => 'Image deleted successfully.']);
            } else {
                echo json_encode(['success' => false, 'error' => 'Error deleting image from database.']);
            }
        } else {
            echo json_encode(['success' => false, 'error' => 'Invalid request.']);
        }
        break;

    case 'delete':
        $id = $_GET['id'] ?? 0;
        $query = "UPDATE home_aarti SET is_active = 0, deleted_at = NOW() WHERE id=?";
        $stmt = $con->prepare($query);
        $stmt->bind_param("i", $id);

        if ($stmt->execute()) {
            $image_delete_query = "DELETE FROM about_images WHERE about_id = ?";
            $image_stmt = $con->prepare($image_delete_query);
            $image_stmt->bind_param("i", $id);
            $image_stmt->execute();

            echo json_encode(['success' => true, 'message' => 'Aarti entry deleted successfully']);
        } else {
            echo json_encode(['success' => false, 'error' => $stmt->error]);
        }
        break;

    default:
        echo json_encode(['success' => false, 'error' => 'Invalid action']);
        break;
}
?>