101 lines
5.5 KiB
PHP
101 lines
5.5 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'client') {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$client_id = $_SESSION['user_id'];
|
|
|
|
// Handle new ticket submission
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_ticket'])) {
|
|
$subject = trim($_POST['subject']);
|
|
$message = trim($_POST['message']);
|
|
|
|
if (!empty($subject) && !empty($message)) {
|
|
try {
|
|
$pdo = db();
|
|
$pdo->beginTransaction();
|
|
|
|
// Insert into support_tickets
|
|
$stmt = $pdo->prepare('INSERT INTO support_tickets (client_id, subject) VALUES (?, ?)');
|
|
$stmt->execute([$client_id, $subject]);
|
|
$ticket_id = $pdo->lastInsertId();
|
|
|
|
// Insert into support_ticket_messages
|
|
$stmt = $pdo->prepare('INSERT INTO support_ticket_messages (ticket_id, user_id, is_admin, message) VALUES (?, ?, ?, ?)');
|
|
$stmt->execute([$ticket_id, $client_id, false, $message]);\n\n $pdo->commit();\n\n // Send email notification to admin\n require_once \'mail/MailService.php\';\n $stmt = $pdo->prepare(\'SELECT name, email FROM clients WHERE id = ?\');\n $stmt->execute([$client_id]);\n $client = $stmt->fetch();\n\n $admin_email = getenv(\'MAIL_TO\') ?: getenv(\'MAIL_FROM\');\n if ($admin_email && $client) {\n $email_subject = \"New Support Ticket #{$ticket_id}: {$subject}\";\n $email_html = \"<p>A new support ticket has been created by <strong>{\$client[\'name\']}</strong> ({$client[\'email\']}).</p>\"\n . \"<p><strong>Subject:</strong> {$subject}</p>\"\n . \"<p><strong>Message:</strong></p><div>\" . nl2br(htmlspecialchars($message)) . \"</div>\"\n . \"<p>You can view the ticket here: <a href=\'http://{\$GLOBALS[HTTP_HOST]}/admin/view-ticket.php?id={$ticket_id}\'>View Ticket</a></p>\";\n $email_text = \"A new support ticket has been created by {\$client[\'name\']} ({\$client[\'email\']}).\\n\\n\"\n . \"Subject: {$subject}\\n\\n\"\n . \"Message:\\n\" . htmlspecialchars($message) . \"\\n\\n\"\n . \"You can view the ticket here: http://{\$GLOBALS[HTTP_HOST]}/admin/view-ticket.php?id={$ticket_id}\";\n\n MailService::sendMail($admin_email, $email_subject, $email_html, $email_text);\n }\n\n $_SESSION[\'success_message\'] = \'Support ticket created successfully.\';\n // Redirect to prevent form resubmission\n header(\'Location: support.php\');
|
|
exit;
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
$error_message = 'Failed to create support ticket. Please try again. Error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// Fetch existing tickets for the client
|
|
$stmt = db()->prepare('SELECT * FROM support_tickets WHERE client_id = ? ORDER BY updated_at DESC');
|
|
$stmt->execute([$client_id]);
|
|
$tickets = $stmt->fetchAll();
|
|
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<h2>Support Tickets</h2>
|
|
|
|
<?php if (isset($_SESSION['success_message'])): ?>
|
|
<div class="alert alert-success">
|
|
<?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (isset($error_message)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php echo $error_message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">Create New Ticket</div>
|
|
<div class="card-body">
|
|
<form action="support.php" method="POST">
|
|
<div class="form-group">
|
|
<label for="subject">Subject</label>
|
|
<input type="text" class="form-control" id="subject" name="subject" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="message">Message</label>
|
|
<textarea class="form-control" id="message" name="message" rows="3" required></textarea>
|
|
</div>
|
|
<button type="submit" name="create_ticket" class="btn btn-primary mt-3">Submit</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header">Your Tickets</div>
|
|
<div class="card-body">
|
|
<div class="list-group">
|
|
<?php if (empty($tickets)): ?>
|
|
<p>You have no support tickets.</p>
|
|
<?php else: ?>
|
|
<?php foreach ($tickets as $ticket): ?>
|
|
<a href="view-ticket.php?id=<?php echo $ticket['id']; ?>" class="list-group-item list-group-item-action">
|
|
<div class="d-flex w-100 justify-content-between">
|
|
<h5 class="mb-1"><?php echo htmlspecialchars($ticket['subject']); ?></h5>
|
|
<small>Last updated: <?php echo date('M j, Y, g:i a', strtotime($ticket['updated_at'])); ?></small>
|
|
</div>
|
|
<p class="mb-1">Status: <span class="badge bg-<?php echo $ticket['status'] === 'Closed' ? 'secondary' : 'success'; ?>"><?php echo htmlspecialchars($ticket['status']); ?></span></p>
|
|
</a>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|