conn = $conn; $this->ticketModel = new TicketModel($conn); $this->commentModel = new CommentModel($conn); $this->auditLogModel = new AuditLogModel($conn); $this->userModel = new UserModel($conn); $this->workflowModel = new WorkflowModel($conn); $this->templateModel = new TemplateModel($conn); } public function view($id) { // Get current user $currentUser = $GLOBALS['currentUser'] ?? null; $userId = $currentUser['user_id'] ?? null; // Get ticket data $ticket = $this->ticketModel->getTicketById($id); if (!$ticket || !$this->ticketModel->canUserAccessTicket($ticket, $currentUser)) { http_response_code(404); include dirname(__DIR__) . '/views/error_404.php'; return; } // 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); // Get timeline for this ticket $timeline = $this->auditLogModel->getTicketTimeline($id); // Get all users for assignment dropdown $allUsers = $this->userModel->getAllUsers(); // Get allowed status transitions for this ticket $allowedTransitions = $this->workflowModel->getAllowedTransitions($ticket['status']); // Make $conn available to view for visibility groups $conn = $this->conn; // Load the view include dirname(__DIR__) . '/views/TicketView.php'; } public function create() { // Get current user $currentUser = $GLOBALS['currentUser'] ?? null; $userId = $currentUser['user_id'] ?? null; // Check if form was submitted if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 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; } // 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'])); } $ticketData = [ 'title' => $_POST['title'] ?? '', 'description' => $_POST['description'] ?? '', 'priority' => $_POST['priority'] ?? '4', 'category' => $_POST['category'] ?? 'General', 'type' => $_POST['type'] ?? 'Issue', 'visibility' => $_POST['visibility'] ?? 'public', 'visibility_groups' => $visibilityGroups, 'assigned_to' => !empty($_POST['assigned_to']) ? $_POST['assigned_to'] : null ]; // Validate input if (empty($ticketData['title'])) { $error = "Title is required"; $templates = $this->templateModel->getAllTemplates(); $allUsers = $this->userModel->getAllUsers(); $conn = $this->conn; // Make $conn available to view include dirname(__DIR__) . '/views/CreateTicketView.php'; return; } // Create ticket with user tracking $result = $this->ticketModel->createTicket($ticketData, $userId); if ($result['success']) { // Log ticket creation to audit log if (isset($GLOBALS['auditLog']) && $userId) { $GLOBALS['auditLog']->logTicketCreate($userId, $result['ticket_id'], $ticketData); } // Auto-link as duplicate if requested from create form $linkDupOfRaw = trim($_POST['link_duplicate_of'] ?? ''); if ($linkDupOfRaw !== '' && ctype_digit($linkDupOfRaw)) { $depSql = "INSERT IGNORE INTO ticket_dependencies (ticket_id, depends_on_id, dependency_type, created_by) VALUES (?, ?, 'duplicates', ?)"; $depStmt = $this->conn->prepare($depSql); $depStmt->bind_param("ssi", $result['ticket_id'], $linkDupOfRaw, $userId); $depStmt->execute(); $depStmt->close(); } // Send Matrix notification for new ticket NotificationHelper::sendTicketNotification($result['ticket_id'], $ticketData, 'manual'); // Redirect to the new ticket header("Location: " . $GLOBALS['config']['BASE_URL'] . "/ticket/" . $result['ticket_id']); exit; } else { $error = $result['error']; $templates = $this->templateModel->getAllTemplates(); $allUsers = $this->userModel->getAllUsers(); $conn = $this->conn; // Make $conn available to view include dirname(__DIR__) . '/views/CreateTicketView.php'; return; } } else { // Get all templates for the template selector $templates = $this->templateModel->getAllTemplates(); // Get all users for assignment dropdown $allUsers = $this->userModel->getAllUsers(); $conn = $this->conn; // Make $conn available to view // Display the create ticket form include dirname(__DIR__) . '/views/CreateTicketView.php'; } } } ?>