Feature: Add trip publisher system - create, edit, delete, and publish trips
This commit is contained in:
59
src/processors/toggle_trip_published.php
Normal file
59
src/processors/toggle_trip_published.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
ob_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$rootPath = dirname(dirname(__DIR__));
|
||||
require_once($rootPath . '/src/config/functions.php');
|
||||
require_once($rootPath . '/src/config/connection.php');
|
||||
|
||||
// Check admin status
|
||||
session_start();
|
||||
if (empty($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
|
||||
ob_end_clean();
|
||||
echo json_encode(['status' => 'error', 'message' => 'Unauthorized access']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$trip_id = intval($_POST['trip_id'] ?? 0);
|
||||
|
||||
if ($trip_id <= 0) {
|
||||
throw new Exception('Invalid trip ID');
|
||||
}
|
||||
|
||||
// Fetch current published status
|
||||
$stmt = $conn->prepare("SELECT published FROM trips WHERE trip_id = ?");
|
||||
$stmt->bind_param("i", $trip_id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result->num_rows === 0) {
|
||||
throw new Exception('Trip not found');
|
||||
}
|
||||
|
||||
$row = $result->fetch_assoc();
|
||||
$new_status = $row['published'] == 1 ? 0 : 1;
|
||||
$stmt->close();
|
||||
|
||||
// Update published status
|
||||
$stmt = $conn->prepare("UPDATE trips SET published = ? WHERE trip_id = ?");
|
||||
$stmt->bind_param("ii", $new_status, $trip_id);
|
||||
|
||||
if (!$stmt->execute()) {
|
||||
throw new Exception('Failed to update trip status: ' . $stmt->error);
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
|
||||
ob_end_clean();
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'message' => $new_status == 1 ? 'Trip published successfully' : 'Trip unpublished successfully',
|
||||
'published' => $new_status
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
ob_end_clean();
|
||||
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user