Fix: Resolve ticket assignment JSON error
Fixed critical bug in assign_ticket.php that was causing JSON parsing errors: - Fixed authentication check to use correct session variable ($_SESSION['user']['user_id']) - Added missing database connection initialization - Added proper connection cleanup (close) - Updated all references to use correct session variable - Changed require paths to use dirname(__DIR__) for consistency This resolves the "Failed to execute 'json' on 'Response': Unexpected end of JSON input" error that occurred when assigning tickets. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,17 +1,19 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
require_once '../config/db.php';
|
require_once dirname(__DIR__) . '/config/db.php';
|
||||||
require_once '../models/TicketModel.php';
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
||||||
require_once '../models/AuditLogModel.php';
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
// Check authentication
|
// Check authentication
|
||||||
if (!isset($_SESSION['user_id'])) {
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
||||||
echo json_encode(['success' => false, 'error' => 'Not authenticated']);
|
echo json_encode(['success' => false, 'error' => 'Not authenticated']);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$userId = $_SESSION['user']['user_id'];
|
||||||
|
|
||||||
// Get request data
|
// Get request data
|
||||||
$data = json_decode(file_get_contents('php://input'), true);
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
$ticketId = $data['ticket_id'] ?? null;
|
$ticketId = $data['ticket_id'] ?? null;
|
||||||
@@ -22,21 +24,36 @@ if (!$ticketId) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create database connection
|
||||||
|
$conn = new mysqli(
|
||||||
|
$GLOBALS['config']['DB_HOST'],
|
||||||
|
$GLOBALS['config']['DB_USER'],
|
||||||
|
$GLOBALS['config']['DB_PASS'],
|
||||||
|
$GLOBALS['config']['DB_NAME']
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($conn->connect_error) {
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Database connection failed']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
$ticketModel = new TicketModel($conn);
|
$ticketModel = new TicketModel($conn);
|
||||||
$auditLogModel = new AuditLogModel($conn);
|
$auditLogModel = new AuditLogModel($conn);
|
||||||
|
|
||||||
if ($assignedTo === null || $assignedTo === '') {
|
if ($assignedTo === null || $assignedTo === '') {
|
||||||
// Unassign ticket
|
// Unassign ticket
|
||||||
$success = $ticketModel->unassignTicket($ticketId, $_SESSION['user_id']);
|
$success = $ticketModel->unassignTicket($ticketId, $userId);
|
||||||
if ($success) {
|
if ($success) {
|
||||||
$auditLogModel->log($_SESSION['user_id'], 'unassign', 'ticket', $ticketId);
|
$auditLogModel->log($userId, 'unassign', 'ticket', $ticketId);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Assign ticket
|
// Assign ticket
|
||||||
$success = $ticketModel->assignTicket($ticketId, $assignedTo, $_SESSION['user_id']);
|
$success = $ticketModel->assignTicket($ticketId, $assignedTo, $userId);
|
||||||
if ($success) {
|
if ($success) {
|
||||||
$auditLogModel->log($_SESSION['user_id'], 'assign', 'ticket', $ticketId, ['assigned_to' => $assignedTo]);
|
$auditLogModel->log($userId, 'assign', 'ticket', $ticketId, ['assigned_to' => $assignedTo]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$conn->close();
|
||||||
|
|
||||||
echo json_encode(['success' => $success]);
|
echo json_encode(['success' => $success]);
|
||||||
|
|||||||
Reference in New Issue
Block a user