2025-05-16 20:02:49 -04:00
|
|
|
<?php
|
|
|
|
|
// Use absolute paths for model includes
|
|
|
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
|
|
|
require_once dirname(__DIR__) . '/models/CommentModel.php';
|
2026-01-01 18:25:19 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
2026-01-01 18:36:34 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/UserModel.php';
|
2026-01-01 18:57:23 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/WorkflowModel.php';
|
2026-01-01 19:00:42 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/TemplateModel.php';
|
2026-01-30 18:51:16 -05:00
|
|
|
require_once dirname(__DIR__) . '/helpers/UrlHelper.php';
|
2026-02-21 19:17:46 -05:00
|
|
|
require_once dirname(__DIR__) . '/helpers/NotificationHelper.php';
|
2025-05-16 20:02:49 -04:00
|
|
|
|
|
|
|
|
class TicketController {
|
|
|
|
|
private $ticketModel;
|
|
|
|
|
private $commentModel;
|
2026-01-01 18:25:19 -05:00
|
|
|
private $auditLogModel;
|
2026-01-01 18:36:34 -05:00
|
|
|
private $userModel;
|
2026-01-01 18:57:23 -05:00
|
|
|
private $workflowModel;
|
2026-01-01 19:00:42 -05:00
|
|
|
private $templateModel;
|
2026-01-23 10:01:50 -05:00
|
|
|
private $conn;
|
2026-01-01 18:57:23 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
public function __construct($conn) {
|
2026-01-23 10:01:50 -05:00
|
|
|
$this->conn = $conn;
|
2025-05-16 20:02:49 -04:00
|
|
|
$this->ticketModel = new TicketModel($conn);
|
|
|
|
|
$this->commentModel = new CommentModel($conn);
|
2026-01-01 18:25:19 -05:00
|
|
|
$this->auditLogModel = new AuditLogModel($conn);
|
2026-01-01 18:36:34 -05:00
|
|
|
$this->userModel = new UserModel($conn);
|
2026-01-01 18:57:23 -05:00
|
|
|
$this->workflowModel = new WorkflowModel($conn);
|
2026-01-01 19:00:42 -05:00
|
|
|
$this->templateModel = new TemplateModel($conn);
|
2025-05-16 20:02:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function view($id) {
|
2026-01-01 15:40:32 -05:00
|
|
|
// Get current user
|
|
|
|
|
$currentUser = $GLOBALS['currentUser'] ?? null;
|
|
|
|
|
$userId = $currentUser['user_id'] ?? null;
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Get ticket data
|
|
|
|
|
$ticket = $this->ticketModel->getTicketById($id);
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
if (!$ticket) {
|
|
|
|
|
header("HTTP/1.0 404 Not Found");
|
|
|
|
|
echo "Ticket not found";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2026-03-20 21:47:28 -04:00
|
|
|
// Check visibility access — return 404 rather than 403 to avoid leaking ticket existence
|
2026-01-23 10:01:50 -05:00
|
|
|
if (!$this->ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
2026-03-20 21:47:28 -04:00
|
|
|
header("HTTP/1.0 404 Not Found");
|
|
|
|
|
echo "Ticket not found";
|
2026-01-23 10:01:50 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 21:34:16 -04:00
|
|
|
// Load first page of comments; show "load more" if ticket has many
|
|
|
|
|
$commentPageSize = 50;
|
|
|
|
|
$totalComments = $this->commentModel->getCommentCount((int)$id);
|
|
|
|
|
$comments = $this->commentModel->getCommentsByTicketId($id, true, $commentPageSize, 0);
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2026-01-01 18:25:19 -05:00
|
|
|
// Get timeline for this ticket
|
|
|
|
|
$timeline = $this->auditLogModel->getTicketTimeline($id);
|
|
|
|
|
|
2026-01-01 18:36:34 -05:00
|
|
|
// Get all users for assignment dropdown
|
|
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
|
|
|
|
|
2026-01-01 18:57:23 -05:00
|
|
|
// Get allowed status transitions for this ticket
|
|
|
|
|
$allowedTransitions = $this->workflowModel->getAllowedTransitions($ticket['status']);
|
|
|
|
|
|
2026-01-23 10:23:19 -05:00
|
|
|
// Make $conn available to view for visibility groups
|
|
|
|
|
$conn = $this->conn;
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Load the view
|
|
|
|
|
include dirname(__DIR__) . '/views/TicketView.php';
|
|
|
|
|
}
|
2026-01-23 10:23:19 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
public function create() {
|
2026-01-01 15:40:32 -05:00
|
|
|
// Get current user
|
|
|
|
|
$currentUser = $GLOBALS['currentUser'] ?? null;
|
|
|
|
|
$userId = $currentUser['user_id'] ?? null;
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Check if form was submitted
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
2026-03-28 21:26:52 -04:00
|
|
|
// Validate CSRF token
|
|
|
|
|
require_once dirname(__DIR__) . '/middleware/CsrfMiddleware.php';
|
|
|
|
|
$csrfToken = $_POST['csrf_token'] ?? '';
|
|
|
|
|
if (!CsrfMiddleware::validateToken($csrfToken)) {
|
|
|
|
|
$error = "Invalid or expired security token. Please try again.";
|
|
|
|
|
$templates = $this->templateModel->getAllTemplates();
|
|
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
|
|
|
|
$conn = $this->conn;
|
|
|
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 10:01:50 -05:00
|
|
|
// Handle visibility groups (comes as array from checkboxes)
|
|
|
|
|
$visibilityGroups = null;
|
|
|
|
|
if (isset($_POST['visibility_groups']) && is_array($_POST['visibility_groups'])) {
|
|
|
|
|
$visibilityGroups = implode(',', array_map('trim', $_POST['visibility_groups']));
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
$ticketData = [
|
|
|
|
|
'title' => $_POST['title'] ?? '',
|
|
|
|
|
'description' => $_POST['description'] ?? '',
|
|
|
|
|
'priority' => $_POST['priority'] ?? '4',
|
|
|
|
|
'category' => $_POST['category'] ?? 'General',
|
2026-01-23 10:01:50 -05:00
|
|
|
'type' => $_POST['type'] ?? 'Issue',
|
|
|
|
|
'visibility' => $_POST['visibility'] ?? 'public',
|
2026-02-05 10:24:00 -05:00
|
|
|
'visibility_groups' => $visibilityGroups,
|
|
|
|
|
'assigned_to' => !empty($_POST['assigned_to']) ? $_POST['assigned_to'] : null
|
2025-05-16 20:02:49 -04:00
|
|
|
];
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Validate input
|
|
|
|
|
if (empty($ticketData['title'])) {
|
|
|
|
|
$error = "Title is required";
|
2026-01-01 19:00:42 -05:00
|
|
|
$templates = $this->templateModel->getAllTemplates();
|
2026-02-05 10:24:00 -05:00
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
2026-01-23 10:01:50 -05:00
|
|
|
$conn = $this->conn; // Make $conn available to view
|
2025-05-16 20:02:49 -04:00
|
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
|
|
|
|
|
// Create ticket with user tracking
|
|
|
|
|
$result = $this->ticketModel->createTicket($ticketData, $userId);
|
|
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
if ($result['success']) {
|
2026-01-01 15:40:32 -05:00
|
|
|
// Log ticket creation to audit log
|
|
|
|
|
if (isset($GLOBALS['auditLog']) && $userId) {
|
|
|
|
|
$GLOBALS['auditLog']->logTicketCreate($userId, $result['ticket_id'], $ticketData);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 19:17:46 -05:00
|
|
|
// Send Matrix notification for new ticket
|
|
|
|
|
NotificationHelper::sendTicketNotification($result['ticket_id'], $ticketData, 'manual');
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Redirect to the new ticket
|
2025-09-05 11:08:56 -04:00
|
|
|
header("Location: " . $GLOBALS['config']['BASE_URL'] . "/ticket/" . $result['ticket_id']);
|
2025-05-16 20:02:49 -04:00
|
|
|
exit;
|
|
|
|
|
} else {
|
|
|
|
|
$error = $result['error'];
|
2026-01-01 19:00:42 -05:00
|
|
|
$templates = $this->templateModel->getAllTemplates();
|
2026-02-05 10:24:00 -05:00
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
2026-01-23 10:01:50 -05:00
|
|
|
$conn = $this->conn; // Make $conn available to view
|
2025-05-16 20:02:49 -04:00
|
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2026-01-01 19:00:42 -05:00
|
|
|
// Get all templates for the template selector
|
|
|
|
|
$templates = $this->templateModel->getAllTemplates();
|
2026-02-05 10:24:00 -05:00
|
|
|
// Get all users for assignment dropdown
|
|
|
|
|
$allUsers = $this->userModel->getAllUsers();
|
2026-01-23 10:01:50 -05:00
|
|
|
$conn = $this->conn; // Make $conn available to view
|
2026-01-01 19:00:42 -05:00
|
|
|
|
2025-05-16 20:02:49 -04:00
|
|
|
// Display the create ticket form
|
|
|
|
|
include dirname(__DIR__) . '/views/CreateTicketView.php';
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-01 15:40:32 -05:00
|
|
|
|
2025-09-05 11:08:56 -04:00
|
|
|
}
|
|
|
|
|
?>
|