File "event_crud.php"
Full Path: /home/ovanhxso/public_html/panel/admin/app/event_crud.php
File size: 8.93 KB
MIME-type: text/x-php
Charset: utf-8
<?php
session_start();
include '../../assets/constant/config.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit'])) {
// print_r($_POST);exit;
$uploadDir = '../../assets/images/';
if (!empty($_FILES['photo']['tmp_name'])) {
$originalName = basename($_FILES['photo']['name']);
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
$newName = rand(100, 999) . '.' . $extension;
$newFilePath = $uploadDir . $newName;
if (move_uploaded_file($_FILES['photo']['tmp_name'], $newFilePath)) {
$img = $newName;
// Compression Logic
// compressImage($newFilePath, $newFilePath, 75); // 75 is the compression quality
} else {
echo 'There was an error uploading the file.';
exit;
}
}
$stmt = $conn->prepare("INSERT INTO `events`(`name1`,`description`, `photo`,`event_date`,`address`,`time`) VALUES ( ?,?,?,?,?,?)");
$stmt->execute([
htmlspecialchars($_POST['name1'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($_POST['description'], ENT_QUOTES, 'UTF-8'),
$img,
htmlspecialchars($_POST['event_date'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($_POST['address'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($_POST['time'], ENT_QUOTES, 'UTF-8'),
]);
$last_inserted_id = $conn->lastInsertId();
$service = count($_FILES['img']['name']);
// extract($_POST);
for($i=0;$i<$service;$i++) {
$uploadDir = '../../assets/images/';
if (!empty($_FILES['img']['tmp_name'][$i])) {
$originalName = basename($_FILES['img']['name'][$i]);
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
$newName = rand(100, 999) . '.' . $extension;
$newFilePath = $uploadDir . $newName;
if (move_uploaded_file($_FILES['img']['tmp_name'][$i], $newFilePath)) {
$img1 = $newName;
$stmtq = $conn->prepare("INSERT INTO `images`(`event_id`,`photo` ) VALUES (?,?)");
$stmtq->execute([$last_inserted_id,$img1]);
} else {
echo 'There was an error uploading the file.';
// exit;
}
}
}
$stmtq1 = $conn->prepare("Delete from `images` WHERE photo=''");
$stmtq1->execute();
$st = $conn->prepare("SELECT * FROM subscribe ");
$st->execute();
$rw=$st->fetchALL();
foreach($rw as $tg){
$rt= $tg['email'];
}
// email start
$stmt = $conn->prepare("SELECT * FROM emailsetting");
$stmt->execute();
$result = $stmt->fetchAll();
//print_r($result);
foreach ($result as $row) {
$smtp_server = $row['smtp_server'];
$smtp_password = $row['smtp_password'];
$smtp_enc = $row['smtp_type'];
$smtp_username = $row['smtp_username'];
$smtp_port = $row['stmp_port'];
// $email = $row['email'];
$email = $rt;
}
$dt = date('Y-m-d H:i:s');
$msg1 = " Dear Subscriber,<br><br>
Thank you for subscribing to Bharatiya Open Volunteer Association (OVA)! We're excited to keep you informed with the latest Events updates.<br><br>
You can view our latest Events and updates by clicking the link below:<br>
<a href='https://system365.cloud/ova/event.php'>Bharatiya Open Volunteer Association (OVA)</a><br><br>
Stay tuned for latest Events, in-depth analysis, and exclusive coverage delivered right to you.<br><br>
Best regards,<br>
The Bharatiya Open Volunteer Association (OVA) Team
";
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = $smtp_server;
$mail->SMTPAuth = true;
$mail->Username = $smtp_username;
$mail->Password = $smtp_password;
$mail->SMTPSecure = $smtp_enc;
$mail->Port = $smtp_port;
$mail->setFrom($smtp_username);
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Bharatiya Open Volunteer Association (OVA)' . $dt;
$mail->Body = $msg1;
$mail->AltBody = $msg1;
$mail->send();
$_SESSION['success'] = "event Added";
?>
<script>
// Redirect to ../about_seo.php
window.location.href = "../manage_event.php";
</script>
<?php
}
if (isset($_POST['update'])) {
$uploadDir = "../../assets/images/";
$img = $_POST['old_photo_img']; // Default to old photo if no new file is uploaded
// Handling the main event image
if (!empty($_FILES['photo']['tmp_name'])) {
$file_extension = pathinfo($_FILES["photo"]["name"], PATHINFO_EXTENSION);
$new_filename = uniqid() . '.' . $file_extension;
$filepath = $uploadDir . $new_filename;
if (move_uploaded_file($_FILES["photo"]["tmp_name"], $filepath)) {
$img = $new_filename;
// Delete the old photo if it exists
$oldFilePath = $uploadDir . $_POST['old_photo_img'];
if (file_exists($oldFilePath) && $_POST['old_photo_img'] !== '') {
unlink($oldFilePath);
}
}
}
// Update event data
$stmt = $conn->prepare("UPDATE `events` SET `name1`=?, `description`=?, `event_date`=?, `address`=?, `time`=?, `photo`=? WHERE id=?");
$stmt->execute([$_POST['name1'], $_POST['description'], $_POST['event_date'], $_POST['address'], $_POST['time'], $img, $_POST['id']]);
// Update or add additional images for the event
$product_id = $_POST['id'];
$existingImages = $_POST['old_img'] ?? []; // Array of existing images (hidden input fields)
$newImages = $_FILES['img'] ?? []; // Array of new images
// Loop through each existing image for replacement
foreach ($existingImages as $index => $oldImage) {
if (!empty($newImages['tmp_name'][$index])) {
$extension = pathinfo($newImages["name"][$index], PATHINFO_EXTENSION);
$new_filename = uniqid() . '.' . $extension;
$filepath = $uploadDir . $new_filename;
// Move uploaded new image
if (move_uploaded_file($newImages["tmp_name"][$index], $filepath)) {
// Delete old image file
$oldFilePath = $uploadDir . $oldImage;
if (file_exists($oldFilePath)) {
unlink($oldFilePath);
}
// Update image in database
$stmtUpdate = $conn->prepare("UPDATE `images` SET `photo`=? WHERE `event_id`=? AND `photo`=?");
$stmtUpdate->execute([$new_filename, $product_id, $oldImage]);
}
}
}
// Handle additional new images
$additionalImages = array_slice($newImages['tmp_name'], count($existingImages));
foreach ($additionalImages as $i => $tempName) {
if (!empty($tempName)) {
$extension = pathinfo($newImages["name"][count($existingImages) + $i], PATHINFO_EXTENSION);
$new_filename = uniqid() . '.' . $extension;
$filepath = $uploadDir . $new_filename;
if (move_uploaded_file($tempName, $filepath)) {
$stmtInsert = $conn->prepare("INSERT INTO `images` (`event_id`, `photo`) VALUES (?, ?)");
$stmtInsert->execute([$product_id, $new_filename]);
}
}
}
$_SESSION['success'] = "Event Updated Successfully!";
?>
<script>
window.location.href = "../manage_event.php";
</script>
<?php
}
if (isset($_POST['del_id'])) {
$stmt = $conn->prepare("UPDATE `events` SET delete_status='1' WHERE id=?");
$stmt->bindParam(1, htmlspecialchars($_POST['del_id'], ENT_QUOTES, 'UTF-8'));
$stmt->execute();
$_SESSION['success'] = "event Deleted";
header("location:../manage_event.php");
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
function compressImage($source, $destination, $quality)
{
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($source);
imagejpeg($image, $destination, $quality);
} elseif ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($source);
imagepng($image, $destination, round(9 - ($quality / 10))); // PNG quality ranges from 0 to 9
}
}