|
|
<?php |
|
|
session_start(); |
|
|
header('Content-Type: application/json'); |
|
|
|
|
|
|
|
|
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) { |
|
|
http_response_code(401); |
|
|
echo json_encode(['success' => false, 'message' => 'Unauthorized']); |
|
|
exit; |
|
|
} |
|
|
|
|
|
|
|
|
include_once '../../db.php'; |
|
|
include_once '../models/User.php'; |
|
|
include_once '../models/SupportTicket.php'; |
|
|
|
|
|
|
|
|
class TicketHandler { |
|
|
public static function notifyCustomerCare($ticket_number, $data) { |
|
|
|
|
|
$message = "New Support Ticket Created:\n"; |
|
|
$message .= "Ticket Number: $ticket_number\n"; |
|
|
$message .= "Issue Type: " . $data['issue_type'] . "\n"; |
|
|
$message .= "Subject: " . $data['subject'] . "\n"; |
|
|
$message .= "Priority: " . $data['priority'] . "\n"; |
|
|
|
|
|
|
|
|
$to = "customercare@jmotors.com"; |
|
|
$subject = "New Support Ticket: $ticket_number"; |
|
|
$headers = "From: support@jmotors.com\r\n"; |
|
|
|
|
|
@mail($to, $subject, $message, $headers); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true); |
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
|
|
try { |
|
|
|
|
|
$required = ['issue_type', 'subject', 'description', 'priority']; |
|
|
foreach ($required as $field) { |
|
|
if (empty($data[$field])) { |
|
|
throw new Exception("Missing required field: $field"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$database = new Database(); |
|
|
$db = $database->getConnection(); |
|
|
|
|
|
|
|
|
$user = new User($db); |
|
|
$user_id = $user->syncUser( |
|
|
$_SESSION['username'], |
|
|
$_SESSION['email'], |
|
|
$_SESSION['tier'], |
|
|
$_SESSION['package'] |
|
|
); |
|
|
|
|
|
if (!$user_id) { |
|
|
throw new Exception("Failed to sync user data"); |
|
|
} |
|
|
|
|
|
|
|
|
$attachments = []; |
|
|
if (!empty($_FILES['attachments'])) { |
|
|
$upload_dir = '../uploads/support/'; |
|
|
if (!is_dir($upload_dir)) { |
|
|
mkdir($upload_dir, 0755, true); |
|
|
} |
|
|
|
|
|
foreach ($_FILES['attachments']['tmp_name'] as $key => $tmp_name) { |
|
|
if ($_FILES['attachments']['error'][$key] === UPLOAD_ERR_OK) { |
|
|
$file_name = time() . '_' . basename($_FILES['attachments']['name'][$key]); |
|
|
$file_path = $upload_dir . $file_name; |
|
|
|
|
|
if (move_uploaded_file($tmp_name, $file_path)) { |
|
|
$attachments[] = $file_path; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$ticket = new SupportTicket($db); |
|
|
$ticket->user_id = $user_id; |
|
|
$ticket->issue_type = $data['issue_type']; |
|
|
$ticket->subject = $data['subject']; |
|
|
$ticket->description = $data['description']; |
|
|
$ticket->priority = $data['priority']; |
|
|
$ticket->attachments = json_encode($attachments); |
|
|
|
|
|
$ticket_number = $ticket->create(); |
|
|
|
|
|
if ($ticket_number) { |
|
|
|
|
|
TicketHandler::notifyCustomerCare($ticket_number, $data); |
|
|
|
|
|
echo json_encode([ |
|
|
'success' => true, |
|
|
'message' => 'Support ticket submitted successfully', |
|
|
'ticket_number' => $ticket_number |
|
|
]); |
|
|
} else { |
|
|
throw new Exception("Failed to create support ticket"); |
|
|
} |
|
|
|
|
|
|
|
|
} catch (Exception $e) { |
|
|
http_response_code(400); |
|
|
echo json_encode([ |
|
|
'success' => false, |
|
|
'message' => $e->getMessage() |
|
|
]); |
|
|
} |
|
|
} else { |
|
|
http_response_code(405); |
|
|
echo json_encode(['success' => false, 'message' => 'Method not allowed']); |
|
|
} |
|
|
?> |
|
|
|