?
| Path : /home/u949514618/domains/drpktiwari.com/public_html/superadmin/ |
| Current File : /home/u949514618/domains/drpktiwari.com/public_html/superadmin/banners.php |
<?php
// Start the session to check if the user is logged in
session_start();
// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
// If not logged in, redirect to login page
header("Location: login.php");
exit();
}
// If logged in, proceed with the rest of the page
include_once('db.php');
?>
<!DOCTYPE html>
<html
lang="en"
class="light-style layout-menu-fixed"
dir="ltr"
data-theme="theme-default"
data-assets-path="../backend/assets/"
data-template="vertical-menu-template-free"
>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
/>
<title>Dashboard - Made By Ajay Singh Rathore (+91 9031351333)</title>
<meta name="description" content="" />
<!-- Favicon -->
<link rel="icon" type="image/x-icon" href="../backend/assets/img/brand/favicon.png" />
<!-- Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&display=swap"
rel="stylesheet"
/>
<!-- Icons. Uncomment required icon fonts -->
<link rel="stylesheet" href="../backend/assets/vendor/fonts/boxicons.css" />
<!-- Core CSS -->
<link rel="stylesheet" href="../backend/assets/vendor/css/core.css" class="template-customizer-core-css" />
<link rel="stylesheet" href="../backend/assets/vendor/css/theme-default.css" class="template-customizer-theme-css" />
<link rel="stylesheet" href="../backend/assets/css/demo.css" />
<!-- Vendors CSS -->
<link rel="stylesheet" href="../backend/assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.css" />
<link rel="stylesheet" href="../backend/assets/vendor/libs/apex-charts/apex-charts.css" />
<!-- Page CSS -->
<!-- Helpers -->
<script src="../backend/assets/vendor/js/helpers.js"></script>
<!--! Template customizer & Theme config files MUST be included after core stylesheets and helpers.js in the <head> section -->
<!--? Config: Mandatory theme config file contain global vars & default theme options, Set your preferred theme option in this file. -->
<script src="../backend/assets/js/config.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
<body>
<!-- Layout wrapper -->
<div class="layout-wrapper layout-content-navbar">
<div class="layout-container">
<!-- Menu -->
<?php include_once('include/dash_sidebar.php'); ?>
<!-- / Menu -->
<!-- Layout container -->
<div class="layout-page">
<!-- Navbar -->
<?php include_once('include/nav.php'); ?>
<?php
if (isset($_POST['insert_banner'])) {
$title = $_POST['title'];
$image = $_FILES['image'];
// Validate Image
$allowed_types = ['image/jpeg', 'image/png', 'image/jpg'];
if (!in_array($image['type'], $allowed_types)) {
echo "<script>Swal.fire('Error', 'Invalid image format!', 'error');</script>";
exit;
}
// Rename Image with Current Date and Time
$image_name = date('YmdHis') . '.' . pathinfo($image['name'], PATHINFO_EXTENSION);
$image_path = "../front/img/$image_name";
// Resize and Save Image
list($width, $height) = getimagesize($image['tmp_name']);
$src = imagecreatefromstring(file_get_contents($image['tmp_name']));
$dst = imagecreatetruecolor(1600, 500);
imagecopyresampled($dst, $src, 0, 0, 0, 0, 1600, 500, $width, $height);
if (imagejpeg($dst, $image_path)) {
// Insert into Database
$query = "INSERT INTO banners (title, image) VALUES ('$title', '$image_name')";
if ($conn->query($query)) {
echo "<script>
Swal.fire('Success', 'Banner added successfully!', 'success')
.then(() => { window.location.href = 'banners.php'; });
</script>";
} else {
echo "<script>Swal.fire('Error', 'Database insertion failed!', 'error');</script>";
}
} else {
echo "<script>Swal.fire('Error', 'Image upload failed!', 'error');</script>";
}
}
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
// Fetch and Delete Image
$result = $conn->query("SELECT image FROM banners WHERE id = $id");
$row = $result->fetch_assoc();
unlink("../front/img/" . $row['image']);
// Delete Record
if ($conn->query("DELETE FROM banners WHERE id = $id")) {
echo "<script>
Swal.fire('Success', 'Banner deleted successfully!', 'success')
.then(() => { window.location.href = 'banners.php'; });
</script>";
} else {
echo "<script>Swal.fire('Error', 'Failed to delete banner!', 'error');</script>";
}
}
if (isset($_POST['update_banner'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$query = "UPDATE banners SET title = '$title'";
// Handle Image Upload
if (!empty($_FILES['image']['name'])) {
$image = $_FILES['image'];
$allowed_types = ['image/jpeg', 'image/png', 'image/jpg'];
if (!in_array($image['type'], $allowed_types)) {
echo "<script>Swal.fire('Error', 'Invalid image format!', 'error');</script>";
exit;
}
// Rename Image
$image_name = date('YmdHis') . '.' . pathinfo($image['name'], PATHINFO_EXTENSION);
$image_path = "../front/img/$image_name";
// Resize and Save Image
list($width, $height) = getimagesize($image['tmp_name']);
$src = imagecreatefromstring(file_get_contents($image['tmp_name']));
$dst = imagecreatetruecolor(1600, 500);
imagecopyresampled($dst, $src, 0, 0, 0, 0, 1600, 500, $width, $height);
if (imagejpeg($dst, $image_path)) {
// Fetch Current Image and Delete
$result = $conn->query("SELECT image FROM banners WHERE id = $id");
$row = $result->fetch_assoc();
unlink("../front/img/" . $row['image']);
$query .= ", image = '$image_name'";
}
}
$query .= " WHERE id = $id";
if ($conn->query($query)) {
echo "<script>
Swal.fire('Success', 'Banner updated successfully!', 'success')
.then(() => { window.location.href = 'banners.php'; });
</script>";
} else {
echo "<script>Swal.fire('Error', 'Failed to update banner!', 'error');</script>";
}
}
?>
<!-- Content wrapper -->
<div class="content-wrapper">
<!-- Content -->
<div class="container-xxl flex-grow-1 container-p-y">
<h4 class="fw-bold py-3 mb-4"><span class="text-muted fw-light">Banner /</span> Section</h4>
<form id="bannerForm" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="title" class="form-label">Banner Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="image" class="form-label">Upload Banner Image</label>
<input type="file" class="form-control" id="image" name="image" accept="image/*" required>
</div>
<button type="submit" name="insert_banner" class="btn btn-primary">Add Banner</button>
</form>
<hr>
<h4 class="mb-3">Banner List</h4>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Image</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$result = $conn->query("SELECT * FROM banners");
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['title']}</td>
<td><img src='../front/img/{$row['image']}' alt='{$row['title']}' width='150'></td>
<td>
<button class='btn btn-warning btn-sm edit-btn'
data-id='{$row['id']}'
data-title='{$row['title']}'
data-image='{$row['image']}'>
Edit
</button>
<a href='?delete={$row['id']}' class='btn btn-danger btn-sm'>Delete</a>
</td>
</tr>";
}
?>
</tbody>
</table>
</div>
</div>
<!-- / Content -->
<!-- Edit Modal -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form id="editForm" method="POST" enctype="multipart/form-data">
<div class="modal-header">
<h5 class="modal-title" id="editModalLabel">Edit Banner</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="hidden" id="edit-id" name="id">
<div class="mb-3">
<label for="edit-title" class="form-label">Banner Title</label>
<input type="text" class="form-control" id="edit-title" name="title" required>
</div>
<div class="mb-3">
<label for="edit-image" class="form-label">Upload Banner Image</label>
<input type="file" class="form-control" id="edit-image" name="image" accept="image/*">
<small class="text-muted">Leave blank to keep current image.</small>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="update_banner" class="btn btn-primary">Update Banner</button>
</div>
</form>
</div>
</div>
</div>
<!-- Footer -->
<footer class="content-footer footer bg-footer-theme">
<div class="container-xxl d-flex flex-wrap justify-content-between py-2 flex-md-row flex-column">
<div class="mb-2 mb-md-0">
©
<script>
document.write(new Date().getFullYear());
</script>
, made with ❤️ by
<a href="#" target="_blank" class="footer-link fw-bolder">Made By Ajay Singh Rathore (+91 9031351333)</a>
</div>
<div>
<a href="#" class="footer-link me-4" target="_blank">Privacy Policy</a>
<a href="#" target="_blank" class="footer-link me-4">Terms & Condition</a>
</div>
</div>
</footer>
<!-- / Footer -->
<div class="content-backdrop fade"></div>
</div>
<!-- Content wrapper -->
</div>
<!-- / Layout page -->
</div>
<!-- Overlay -->
<div class="layout-overlay layout-menu-toggle"></div>
</div>
<!-- / Layout wrapper -->
<!-- Core JS -->
<!-- build:js assets/vendor/js/core.js -->
<script src="../backend/assets/vendor/libs/jquery/jquery.js"></script>
<script src="../backend/assets/vendor/libs/popper/popper.js"></script>
<script src="../backend/assets/vendor/js/bootstrap.js"></script>
<script src="../backend/assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.js"></script>
<script src="../backend/assets/vendor/js/menu.js"></script>
<!-- endbuild -->
<!-- Vendors JS -->
<script src="../backend/assets/vendor/libs/apex-charts/apexcharts.js"></script>
<!-- Main JS -->
<script src="../backend/assets/js/main.js"></script>
<!-- Page JS -->
<script src="../backend/assets/js/dashboards-analytics.js"></script>
<!-- Place this tag in your head or just before your close body tag. -->
<script async defer src="https://buttons.github.io/buttons.js"></script>
<script>
$(document).ready(function() {
// Open the profile modal and fetch data
$('#myProfileLink').on('click', function() {
$.ajax({
url: 'fetch_profile_data.php', // PHP file to fetch user data
type: 'GET',
success: function(response) {
let data = JSON.parse(response);
if (data) {
// Populate modal fields with data
$('#username').val(data.username);
$('#email').val(data.email);
$('#org_name').val(data.org_name);
$('#phone').val(data.phone);
$('#address').val(data.address);
$('#state').val(data.state);
$('#zipcode').val(data.zipcode);
$('#country').val(data.country);
}
$('#profileModal').modal('show'); // Show the modal
}
});
});
});
</script>
<script>
$('#updateProfile').on('click', function() {
let orgName = $('#org_name').val();
let phone = $('#phone').val();
let address = $('#address').val();
let state = $('#state').val();
let zipcode = $('#zipcode').val();
$.ajax({
url: 'update_profile.php', // PHP file to update user data
type: 'POST',
data: {
org_name: orgName,
phone: phone,
address: address,
state: state,
zipcode: zipcode
},
success: function(response) {
if (response == 'success') {
Swal.fire({
title: 'Success',
text: 'Your profile has been updated.',
icon: 'success',
confirmButtonText: 'OK'
});
$('#exLargeModal').modal('hide'); // Close the modal
} else {
Swal.fire({
title: 'Error',
text: 'There was an issue updating your profile.',
icon: 'error',
confirmButtonText: 'OK'
});
}
}
});
});
</script>
<script>
// Handle Edit Button Click
document.querySelectorAll('.edit-btn').forEach(button => {
button.addEventListener('click', function () {
const id = this.dataset.id;
const title = this.dataset.title;
const image = this.dataset.image;
document.getElementById('edit-id').value = id;
document.getElementById('edit-title').value = title;
// Show the modal
new bootstrap.Modal(document.getElementById('editModal')).show();
});
});
</script>
</body>
</html>