Reorganize event processors and update routing
- Move process_event.php from src/admin to src/processors - Move toggle_event_published.php from src/admin to src/processors - Move delete_event.php from src/admin to src/processors - Update .htaccess rewrite rules to point event processors to correct location - Keep admin_events.php and manage_events.php in admin (display pages only)
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
include_once($rootPath . '/header.php');
|
||||
checkAdmin();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$event_id = $_POST['event_id'] ?? null;
|
||||
|
||||
if (!$event_id) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Event ID is required']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Get event details to delete associated files
|
||||
$stmt = $conn->prepare("SELECT image, promo FROM events WHERE event_id = ?");
|
||||
$stmt->bind_param("i", $event_id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result->num_rows > 0) {
|
||||
$event = $result->fetch_assoc();
|
||||
|
||||
// Delete image files
|
||||
if ($event['image'] && file_exists($rootPath . '/' . $event['image'])) {
|
||||
unlink($rootPath . '/' . $event['image']);
|
||||
}
|
||||
if ($event['promo'] && file_exists($rootPath . '/' . $event['promo'])) {
|
||||
unlink($rootPath . '/' . $event['promo']);
|
||||
}
|
||||
|
||||
// Delete from database
|
||||
$delete_stmt = $conn->prepare("DELETE FROM events WHERE event_id = ?");
|
||||
$delete_stmt->bind_param("i", $event_id);
|
||||
|
||||
if ($delete_stmt->execute()) {
|
||||
echo json_encode(['status' => 'success', 'message' => 'Event deleted successfully']);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Failed to delete event']);
|
||||
}
|
||||
$delete_stmt->close();
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Event not found']);
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
@@ -1,193 +0,0 @@
|
||||
<?php
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
include_once($rootPath . '/header.php');
|
||||
checkAdmin();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Handle delete action
|
||||
if ($_GET['action'] ?? null === 'delete') {
|
||||
$event_id = $_GET['event_id'] ?? null;
|
||||
|
||||
if (!$event_id) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Event ID is required']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Get event details to delete associated files
|
||||
$stmt = $conn->prepare("SELECT image, promo FROM events WHERE event_id = ?");
|
||||
$stmt->bind_param("i", $event_id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result->num_rows > 0) {
|
||||
$event = $result->fetch_assoc();
|
||||
|
||||
// Delete image files
|
||||
if ($event['image'] && file_exists($rootPath . '/' . $event['image'])) {
|
||||
unlink($rootPath . '/' . $event['image']);
|
||||
}
|
||||
if ($event['promo'] && file_exists($rootPath . '/' . $event['promo'])) {
|
||||
unlink($rootPath . '/' . $event['promo']);
|
||||
}
|
||||
|
||||
// Delete from database
|
||||
$delete_stmt = $conn->prepare("DELETE FROM events WHERE event_id = ?");
|
||||
$delete_stmt->bind_param("i", $event_id);
|
||||
|
||||
if ($delete_stmt->execute()) {
|
||||
echo json_encode(['status' => 'success', 'message' => 'Event deleted successfully']);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Failed to delete event']);
|
||||
}
|
||||
$delete_stmt->close();
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Event not found']);
|
||||
}
|
||||
$stmt->close();
|
||||
exit;
|
||||
}
|
||||
|
||||
// Check CSRF token
|
||||
if (!isset($_POST['csrf_token']) || !verifyCsrfToken($_POST['csrf_token'])) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'CSRF token validation failed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$event_id = $_POST['event_id'] ?? null;
|
||||
$name = $_POST['name'] ?? null;
|
||||
$type = $_POST['type'] ?? null;
|
||||
$location = $_POST['location'] ?? null;
|
||||
$date = $_POST['date'] ?? null;
|
||||
$time = $_POST['time'] ?? null;
|
||||
$feature = $_POST['feature'] ?? null;
|
||||
$description = $_POST['description'] ?? null;
|
||||
|
||||
// Validate required fields
|
||||
if (!$name || !$type || !$location || !$date || !$time || !$feature || !$description) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'All required fields must be filled']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Handle image upload
|
||||
$image_path = null;
|
||||
if (!empty($_FILES['image']['name'])) {
|
||||
$upload_dir = $rootPath . '/assets/images/events/';
|
||||
if (!is_dir($upload_dir)) {
|
||||
mkdir($upload_dir, 0755, true);
|
||||
}
|
||||
|
||||
$file_name = uniqid() . '_' . basename($_FILES['image']['name']);
|
||||
$target_file = $upload_dir . $file_name;
|
||||
$file_type = mime_content_type($_FILES['image']['tmp_name']);
|
||||
|
||||
// Validate image file
|
||||
$allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
||||
if (!in_array($file_type, $allowed_types)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Invalid image file type. Only JPEG, PNG, GIF, and WebP are allowed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
|
||||
$image_path = 'assets/images/events/' . $file_name;
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Failed to upload image']);
|
||||
exit;
|
||||
}
|
||||
} else if (!$event_id) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Image is required for new events']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Handle promo image upload
|
||||
$promo_path = null;
|
||||
if (!empty($_FILES['promo']['name'])) {
|
||||
$upload_dir = $rootPath . '/assets/images/events/';
|
||||
if (!is_dir($upload_dir)) {
|
||||
mkdir($upload_dir, 0755, true);
|
||||
}
|
||||
|
||||
$file_name = uniqid() . '_promo_' . basename($_FILES['promo']['name']);
|
||||
$target_file = $upload_dir . $file_name;
|
||||
$file_type = mime_content_type($_FILES['promo']['tmp_name']);
|
||||
|
||||
// Validate image file
|
||||
$allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
||||
if (!in_array($file_type, $allowed_types)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Invalid promo image file type. Only JPEG, PNG, GIF, and WebP are allowed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (move_uploaded_file($_FILES['promo']['tmp_name'], $target_file)) {
|
||||
$promo_path = 'assets/images/events/' . $file_name;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if ($event_id) {
|
||||
// Update existing event
|
||||
$update_fields = [
|
||||
'name' => $name,
|
||||
'type' => $type,
|
||||
'location' => $location,
|
||||
'date' => $date,
|
||||
'time' => $time,
|
||||
'feature' => $feature,
|
||||
'description' => $description,
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
if ($image_path) {
|
||||
$update_fields['image'] = $image_path;
|
||||
}
|
||||
if ($promo_path) {
|
||||
$update_fields['promo'] = $promo_path;
|
||||
}
|
||||
|
||||
$set_clause = implode(', ', array_map(function($key) {
|
||||
return $key . ' = ?';
|
||||
}, array_keys($update_fields)));
|
||||
|
||||
$values = array_values($update_fields);
|
||||
$values[] = $event_id;
|
||||
|
||||
$stmt = $conn->prepare("UPDATE events SET $set_clause WHERE event_id = ?");
|
||||
|
||||
// Build type string for bind_param
|
||||
$type_str = str_repeat('s', count($update_fields)) . 'i';
|
||||
$stmt->bind_param($type_str, ...$values);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(['status' => 'success', 'message' => 'Event updated successfully']);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Failed to update event: ' . $stmt->error]);
|
||||
}
|
||||
} else {
|
||||
// Create new event
|
||||
if (!$image_path) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Image is required for new events']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$promo_path = $promo_path ?? 'assets/images/events/default-promo.jpg';
|
||||
|
||||
$stmt = $conn->prepare("
|
||||
INSERT INTO events (name, type, location, date, time, feature, description, image, promo, created_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
");
|
||||
|
||||
$created_by = $_SESSION['user_id'] ?? 0;
|
||||
|
||||
$stmt->bind_param('sssssssssi', $name, $type, $location, $date, $time, $feature, $description, $image_path, $promo_path, $created_by);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(['status' => 'success', 'message' => 'Event created successfully']);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Failed to create event: ' . $stmt->error]);
|
||||
}
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'An error occurred: ' . $e->getMessage()]);
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
<?php
|
||||
// Set JSON header FIRST before any includes that might output
|
||||
header('Content-Type: application/json');
|
||||
header('Cache-Control: no-cache, no-store, must-revalidate');
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: 0');
|
||||
|
||||
// Clean any output buffers before including header
|
||||
while (ob_get_level() > 0) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
include_once($rootPath . '/header.php');
|
||||
checkAdmin();
|
||||
|
||||
// Clean output buffer again in case header.php added content
|
||||
ob_clean();
|
||||
|
||||
$event_id = $_POST['event_id'] ?? null;
|
||||
|
||||
if (!$event_id) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Event ID is required']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// Get current published status
|
||||
$stmt = $conn->prepare("SELECT published FROM events WHERE event_id = ?");
|
||||
if (!$stmt) {
|
||||
throw new Exception("Prepare failed: " . $conn->error);
|
||||
}
|
||||
|
||||
$stmt->bind_param("i", $event_id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result->num_rows === 0) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Event not found']);
|
||||
$stmt->close();
|
||||
exit;
|
||||
}
|
||||
|
||||
$event = $result->fetch_assoc();
|
||||
$new_status = $event['published'] == 1 ? 0 : 1;
|
||||
$stmt->close();
|
||||
|
||||
// Update published status
|
||||
$update_stmt = $conn->prepare("UPDATE events SET published = ?, updated_at = NOW() WHERE event_id = ?");
|
||||
if (!$update_stmt) {
|
||||
throw new Exception("Prepare failed: " . $conn->error);
|
||||
}
|
||||
|
||||
$update_stmt->bind_param("ii", $new_status, $event_id);
|
||||
|
||||
if ($update_stmt->execute()) {
|
||||
ob_clean(); // Clean any buffered output before sending JSON
|
||||
http_response_code(200);
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'message' => $new_status == 1 ? 'Event published' : 'Event unpublished',
|
||||
'published' => $new_status
|
||||
]);
|
||||
} else {
|
||||
throw new Exception("Update failed: " . $update_stmt->error);
|
||||
}
|
||||
$update_stmt->close();
|
||||
} catch (Exception $e) {
|
||||
ob_clean(); // Clean any buffered output before sending JSON
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user